Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / compiler / glsl / tests / cache_test.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /* A collection of unit tests for cache.c */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <ftw.h>
31 #include <errno.h>
32 #include <stdarg.h>
33 #include <inttypes.h>
34 #include <limits.h>
35 #include <time.h>
36 #include <unistd.h>
37
38 #include "util/mesa-sha1.h"
39 #include "util/disk_cache.h"
40
41 bool error = false;
42
43 #ifdef ENABLE_SHADER_CACHE
44
45 static void
46 expect_true(bool result, const char *test)
47 {
48 if (!result) {
49 fprintf(stderr, "Error: Test '%s' failed: Expected=true"
50 ", Actual=false\n", test);
51 error = true;
52 }
53 }
54
55 static void
56 expect_equal(uint64_t actual, uint64_t expected, const char *test)
57 {
58 if (actual != expected) {
59 fprintf(stderr, "Error: Test '%s' failed: Expected=%" PRIu64
60 ", Actual=%" PRIu64 "\n",
61 test, expected, actual);
62 error = true;
63 }
64 }
65
66 static void
67 expect_null(void *ptr, const char *test)
68 {
69 if (ptr != NULL) {
70 fprintf(stderr, "Error: Test '%s' failed: Result=%p, but expected NULL.\n",
71 test, ptr);
72 error = true;
73 }
74 }
75
76 static void
77 expect_non_null(void *ptr, const char *test)
78 {
79 if (ptr == NULL) {
80 fprintf(stderr, "Error: Test '%s' failed: Result=NULL, but expected something else.\n",
81 test);
82 error = true;
83 }
84 }
85
86 static void
87 expect_equal_str(const char *actual, const char *expected, const char *test)
88 {
89 if (strcmp(actual, expected)) {
90 fprintf(stderr, "Error: Test '%s' failed:\n\t"
91 "Expected=\"%s\", Actual=\"%s\"\n",
92 test, expected, actual);
93 error = true;
94 }
95 }
96
97 /* Callback for nftw used in rmrf_local below.
98 */
99 static int
100 remove_entry(const char *path,
101 const struct stat *sb,
102 int typeflag,
103 struct FTW *ftwbuf)
104 {
105 int err = remove(path);
106
107 if (err)
108 fprintf(stderr, "Error removing %s: %s\n", path, strerror(errno));
109
110 return err;
111 }
112
113 /* Recursively remove a directory.
114 *
115 * This is equivalent to "rm -rf <dir>" with one bit of protection
116 * that the directory name must begin with "." to ensure we don't
117 * wander around deleting more than intended.
118 *
119 * Returns 0 on success, -1 on any error.
120 */
121 static int
122 rmrf_local(const char *path)
123 {
124 if (path == NULL || *path == '\0' || *path != '.')
125 return -1;
126
127 return nftw(path, remove_entry, 64, FTW_DEPTH | FTW_PHYS);
128 }
129
130 static void
131 check_directories_created(const char *cache_dir)
132 {
133 bool sub_dirs_created = false;
134
135 char buf[PATH_MAX];
136 if (getcwd(buf, PATH_MAX)) {
137 char *full_path = NULL;
138 if (asprintf(&full_path, "%s%s", buf, ++cache_dir) != -1 ) {
139 struct stat sb;
140 if (stat(full_path, &sb) != -1 && S_ISDIR(sb.st_mode))
141 sub_dirs_created = true;
142
143 free(full_path);
144 }
145 }
146
147 expect_true(sub_dirs_created, "create sub dirs");
148 }
149
150 static bool
151 does_cache_contain(struct disk_cache *cache, const cache_key key)
152 {
153 void *result;
154
155 result = disk_cache_get(cache, key, NULL);
156
157 if (result) {
158 free(result);
159 return true;
160 }
161
162 return false;
163 }
164
165 static void *
166 cache_exists(struct disk_cache *cache)
167 {
168 uint8_t dummy_key[20];
169 char data[] = "some test data";
170
171 if (!cache)
172 return NULL;
173
174 disk_cache_put(cache, dummy_key, data, sizeof(data), NULL);
175 disk_cache_wait_for_idle(cache);
176 return disk_cache_get(cache, dummy_key, NULL);
177 }
178
179 #define CACHE_TEST_TMP "./cache-test-tmp"
180
181 static void
182 test_disk_cache_create(void)
183 {
184 struct disk_cache *cache;
185 int err;
186
187 /* Before doing anything else, ensure that with
188 * MESA_GLSL_CACHE_DISABLE set to true, that disk_cache_create returns NULL.
189 */
190 setenv("MESA_GLSL_CACHE_DISABLE", "true", 1);
191 cache = disk_cache_create("test", "make_check", 0);
192 expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DISABLE set");
193
194 unsetenv("MESA_GLSL_CACHE_DISABLE");
195
196 /* For the first real disk_cache_create() clear these environment
197 * variables to test creation of cache in home directory.
198 */
199 unsetenv("MESA_GLSL_CACHE_DIR");
200 unsetenv("XDG_CACHE_HOME");
201
202 cache = disk_cache_create("test", "make_check", 0);
203 expect_non_null(cache, "disk_cache_create with no environment variables");
204
205 disk_cache_destroy(cache);
206
207 /* Test with XDG_CACHE_HOME set */
208 setenv("XDG_CACHE_HOME", CACHE_TEST_TMP "/xdg-cache-home", 1);
209 cache = disk_cache_create("test", "make_check", 0);
210 expect_null(cache_exists(cache), "disk_cache_create with XDG_CACHE_HOME set "
211 "with a non-existing parent directory");
212
213 err = mkdir(CACHE_TEST_TMP, 0755);
214 if (err != 0) {
215 fprintf(stderr, "Error creating %s: %s\n", CACHE_TEST_TMP, strerror(errno));
216 error = true;
217 return;
218 }
219
220 cache = disk_cache_create("test", "make_check", 0);
221 expect_non_null(cache_exists(cache), "disk_cache_create with XDG_CACHE_HOME "
222 "set");
223
224 check_directories_created(CACHE_TEST_TMP "/xdg-cache-home/"
225 CACHE_DIR_NAME);
226
227 disk_cache_destroy(cache);
228
229 /* Test with MESA_GLSL_CACHE_DIR set */
230 err = rmrf_local(CACHE_TEST_TMP);
231 expect_equal(err, 0, "Removing " CACHE_TEST_TMP);
232
233 setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP "/mesa-glsl-cache-dir", 1);
234 cache = disk_cache_create("test", "make_check", 0);
235 expect_null(cache_exists(cache), "disk_cache_create with MESA_GLSL_CACHE_DIR"
236 " set with a non-existing parent directory");
237
238 err = mkdir(CACHE_TEST_TMP, 0755);
239 if (err != 0) {
240 fprintf(stderr, "Error creating %s: %s\n", CACHE_TEST_TMP, strerror(errno));
241 error = true;
242 return;
243 }
244
245 cache = disk_cache_create("test", "make_check", 0);
246 expect_non_null(cache_exists(cache), "disk_cache_create with "
247 "MESA_GLSL_CACHE_DIR set");
248
249 check_directories_created(CACHE_TEST_TMP "/mesa-glsl-cache-dir/"
250 CACHE_DIR_NAME);
251
252 disk_cache_destroy(cache);
253 }
254
255 static void
256 test_put_and_get(void)
257 {
258 struct disk_cache *cache;
259 char blob[] = "This is a blob of thirty-seven bytes";
260 uint8_t blob_key[20];
261 char string[] = "While this string has thirty-four";
262 uint8_t string_key[20];
263 char *result;
264 size_t size;
265 uint8_t *one_KB, *one_MB;
266 uint8_t one_KB_key[20], one_MB_key[20];
267 int count;
268
269 cache = disk_cache_create("test", "make_check", 0);
270
271 disk_cache_compute_key(cache, blob, sizeof(blob), blob_key);
272
273 /* Ensure that disk_cache_get returns nothing before anything is added. */
274 result = disk_cache_get(cache, blob_key, &size);
275 expect_null(result, "disk_cache_get with non-existent item (pointer)");
276 expect_equal(size, 0, "disk_cache_get with non-existent item (size)");
277
278 /* Simple test of put and get. */
279 disk_cache_put(cache, blob_key, blob, sizeof(blob), NULL);
280
281 /* disk_cache_put() hands things off to a thread so wait for it. */
282 disk_cache_wait_for_idle(cache);
283
284 result = disk_cache_get(cache, blob_key, &size);
285 expect_equal_str(blob, result, "disk_cache_get of existing item (pointer)");
286 expect_equal(size, sizeof(blob), "disk_cache_get of existing item (size)");
287
288 free(result);
289
290 /* Test put and get of a second item. */
291 disk_cache_compute_key(cache, string, sizeof(string), string_key);
292 disk_cache_put(cache, string_key, string, sizeof(string), NULL);
293
294 /* disk_cache_put() hands things off to a thread so wait for it. */
295 disk_cache_wait_for_idle(cache);
296
297 result = disk_cache_get(cache, string_key, &size);
298 expect_equal_str(result, string, "2nd disk_cache_get of existing item (pointer)");
299 expect_equal(size, sizeof(string), "2nd disk_cache_get of existing item (size)");
300
301 free(result);
302
303 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
304 disk_cache_destroy(cache);
305
306 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
307 cache = disk_cache_create("test", "make_check", 0);
308
309 one_KB = calloc(1, 1024);
310
311 /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
312 * interesting. But we do have want to take some special care with
313 * the hash we use here. The issue is that in this artificial case,
314 * (with only three files in the cache), the probability is good
315 * that each of the three files will end up in their own
316 * directory. Then, if the directory containing the .tmp file for
317 * the new item being added for disk_cache_put() is the chosen victim
318 * directory for eviction, then no suitable file will be found and
319 * nothing will be evicted.
320 *
321 * That's actually expected given how the eviction code is
322 * implemented, (which expects to only evict once things are more
323 * interestingly full than that).
324 *
325 * For this test, we force this signature to land in the same
326 * directory as the original blob first written to the cache.
327 */
328 disk_cache_compute_key(cache, one_KB, 1024, one_KB_key);
329 one_KB_key[0] = blob_key[0];
330
331 disk_cache_put(cache, one_KB_key, one_KB, 1024, NULL);
332
333 free(one_KB);
334
335 /* disk_cache_put() hands things off to a thread so wait for it. */
336 disk_cache_wait_for_idle(cache);
337
338 result = disk_cache_get(cache, one_KB_key, &size);
339 expect_non_null(result, "3rd disk_cache_get of existing item (pointer)");
340 expect_equal(size, 1024, "3rd disk_cache_get of existing item (size)");
341
342 free(result);
343
344 /* Ensure eviction happened by checking that both of the previous
345 * cache itesm were evicted.
346 */
347 bool contains_1KB_file = false;
348 count = 0;
349 if (does_cache_contain(cache, blob_key))
350 count++;
351
352 if (does_cache_contain(cache, string_key))
353 count++;
354
355 if (does_cache_contain(cache, one_KB_key)) {
356 count++;
357 contains_1KB_file = true;
358 }
359
360 expect_true(contains_1KB_file,
361 "disk_cache_put eviction last file == MAX_SIZE (1KB)");
362 expect_equal(count, 1, "disk_cache_put eviction with MAX_SIZE=1K");
363
364 /* Now increase the size to 1M, add back both items, and ensure all
365 * three that have been added are available via disk_cache_get.
366 */
367 disk_cache_destroy(cache);
368
369 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
370 cache = disk_cache_create("test", "make_check", 0);
371
372 disk_cache_put(cache, blob_key, blob, sizeof(blob), NULL);
373 disk_cache_put(cache, string_key, string, sizeof(string), NULL);
374
375 /* disk_cache_put() hands things off to a thread so wait for it. */
376 disk_cache_wait_for_idle(cache);
377
378 count = 0;
379 if (does_cache_contain(cache, blob_key))
380 count++;
381
382 if (does_cache_contain(cache, string_key))
383 count++;
384
385 if (does_cache_contain(cache, one_KB_key))
386 count++;
387
388 expect_equal(count, 3, "no eviction before overflow with MAX_SIZE=1M");
389
390 /* Finally, check eviction again after adding an object of size 1M. */
391 one_MB = calloc(1024, 1024);
392
393 disk_cache_compute_key(cache, one_MB, 1024 * 1024, one_MB_key);
394 one_MB_key[0] = blob_key[0];
395
396 disk_cache_put(cache, one_MB_key, one_MB, 1024 * 1024, NULL);
397
398 free(one_MB);
399
400 /* disk_cache_put() hands things off to a thread so wait for it. */
401 disk_cache_wait_for_idle(cache);
402
403 bool contains_1MB_file = false;
404 count = 0;
405 if (does_cache_contain(cache, blob_key))
406 count++;
407
408 if (does_cache_contain(cache, string_key))
409 count++;
410
411 if (does_cache_contain(cache, one_KB_key))
412 count++;
413
414 if (does_cache_contain(cache, one_MB_key)) {
415 count++;
416 contains_1MB_file = true;
417 }
418
419 expect_true(contains_1MB_file,
420 "disk_cache_put eviction last file == MAX_SIZE (1MB)");
421 expect_equal(count, 1, "eviction after overflow with MAX_SIZE=1M");
422
423 disk_cache_destroy(cache);
424 }
425
426 static void
427 test_put_key_and_get_key(void)
428 {
429 struct disk_cache *cache;
430 bool result;
431
432 uint8_t key_a[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
433 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
434 uint8_t key_b[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
435 30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
436 uint8_t key_a_collide[20] =
437 { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
438 50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
439
440 cache = disk_cache_create("test", "make_check", 0);
441
442 /* First test that disk_cache_has_key returns false before disk_cache_put_key */
443 result = disk_cache_has_key(cache, key_a);
444 expect_equal(result, 0, "disk_cache_has_key before key added");
445
446 /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
447 disk_cache_put_key(cache, key_a);
448 result = disk_cache_has_key(cache, key_a);
449 expect_equal(result, 1, "disk_cache_has_key after key added");
450
451 disk_cache_put_key(cache, key_b);
452 result = disk_cache_has_key(cache, key_b);
453 expect_equal(result, 1, "2nd disk_cache_has_key after key added");
454
455 /* Test that a key with the same two bytes as an existing key
456 * forces an eviction.
457 */
458 disk_cache_put_key(cache, key_a_collide);
459 result = disk_cache_has_key(cache, key_a_collide);
460 expect_equal(result, 1, "put_key of a colliding key lands in the cache");
461
462 result = disk_cache_has_key(cache, key_a);
463 expect_equal(result, 0, "put_key of a colliding key evicts from the cache");
464
465 /* And finally test that we can re-add the original key to re-evict
466 * the colliding key.
467 */
468 disk_cache_put_key(cache, key_a);
469 result = disk_cache_has_key(cache, key_a);
470 expect_equal(result, 1, "put_key of original key lands again");
471
472 result = disk_cache_has_key(cache, key_a_collide);
473 expect_equal(result, 0, "put_key of orginal key evicts the colliding key");
474
475 disk_cache_destroy(cache);
476 }
477 #endif /* ENABLE_SHADER_CACHE */
478
479 int
480 main(void)
481 {
482 #ifdef ENABLE_SHADER_CACHE
483 int err;
484
485 test_disk_cache_create();
486
487 test_put_and_get();
488
489 test_put_key_and_get_key();
490
491 err = rmrf_local(CACHE_TEST_TMP);
492 expect_equal(err, 0, "Removing " CACHE_TEST_TMP " again");
493 #endif /* ENABLE_SHADER_CACHE */
494
495 return error ? 1 : 0;
496 }