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
expect_true(bool result,const char * test)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
expect_equal(uint64_t actual,uint64_t expected,const char * test)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
expect_null(void * ptr,const char * test)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
expect_non_null(void * ptr,const char * test)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
expect_equal_str(const char * actual,const char * expected,const char * test)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
remove_entry(const char * path,const struct stat * sb,int typeflag,struct FTW * ftwbuf)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
rmrf_local(const char * path)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
check_directories_created(const char * cache_dir)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
does_cache_contain(struct disk_cache * cache,const cache_key key)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 *
cache_exists(struct disk_cache * cache)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
test_disk_cache_create(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 #ifdef SHADER_CACHE_DISABLE_BY_DEFAULT
197 /* With SHADER_CACHE_DISABLE_BY_DEFAULT, ensure that with
198 * MESA_GLSL_CACHE_DISABLE set to nothing, disk_cache_create returns NULL.
199 */
200 unsetenv("MESA_GLSL_CACHE_DISABLE");
201 cache = disk_cache_create("test", "make_check", 0);
202 expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DISABLE unset "
203 " and SHADER_CACHE_DISABLE_BY_DEFAULT build option");
204
205 /* For remaining tests, ensure that the cache is enabled. */
206 setenv("MESA_GLSL_CACHE_DISABLE", "false", 1);
207 #endif /* SHADER_CACHE_DISABLE_BY_DEFAULT */
208
209 /* For the first real disk_cache_create() clear these environment
210 * variables to test creation of cache in home directory.
211 */
212 unsetenv("MESA_GLSL_CACHE_DIR");
213 unsetenv("XDG_CACHE_HOME");
214
215 cache = disk_cache_create("test", "make_check", 0);
216 expect_non_null(cache, "disk_cache_create with no environment variables");
217
218 disk_cache_destroy(cache);
219
220 #ifdef ANDROID
221 /* Android doesn't try writing to disk (just calls the cache callbacks), so
222 * the directory tests below don't apply.
223 */
224 exit(error ? 1 : 0);
225 #endif
226
227 /* Test with XDG_CACHE_HOME set */
228 setenv("XDG_CACHE_HOME", CACHE_TEST_TMP "/xdg-cache-home", 1);
229 cache = disk_cache_create("test", "make_check", 0);
230 expect_null(cache_exists(cache), "disk_cache_create with XDG_CACHE_HOME set "
231 "with a non-existing parent directory");
232
233 err = mkdir(CACHE_TEST_TMP, 0755);
234 if (err != 0) {
235 fprintf(stderr, "Error creating %s: %s\n", CACHE_TEST_TMP, strerror(errno));
236 error = true;
237 return;
238 }
239
240 cache = disk_cache_create("test", "make_check", 0);
241 expect_non_null(cache_exists(cache), "disk_cache_create with XDG_CACHE_HOME "
242 "set");
243
244 check_directories_created(CACHE_TEST_TMP "/xdg-cache-home/"
245 CACHE_DIR_NAME);
246
247 disk_cache_destroy(cache);
248
249 /* Test with MESA_GLSL_CACHE_DIR set */
250 err = rmrf_local(CACHE_TEST_TMP);
251 expect_equal(err, 0, "Removing " CACHE_TEST_TMP);
252
253 setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP "/mesa-glsl-cache-dir", 1);
254 cache = disk_cache_create("test", "make_check", 0);
255 expect_null(cache_exists(cache), "disk_cache_create with MESA_GLSL_CACHE_DIR"
256 " set with a non-existing parent directory");
257
258 err = mkdir(CACHE_TEST_TMP, 0755);
259 if (err != 0) {
260 fprintf(stderr, "Error creating %s: %s\n", CACHE_TEST_TMP, strerror(errno));
261 error = true;
262 return;
263 }
264
265 cache = disk_cache_create("test", "make_check", 0);
266 expect_non_null(cache_exists(cache), "disk_cache_create with "
267 "MESA_GLSL_CACHE_DIR set");
268
269 check_directories_created(CACHE_TEST_TMP "/mesa-glsl-cache-dir/"
270 CACHE_DIR_NAME);
271
272 disk_cache_destroy(cache);
273 }
274
275 static void
test_put_and_get(void)276 test_put_and_get(void)
277 {
278 struct disk_cache *cache;
279 char blob[] = "This is a blob of thirty-seven bytes";
280 uint8_t blob_key[20];
281 char string[] = "While this string has thirty-four";
282 uint8_t string_key[20];
283 char *result;
284 size_t size;
285 uint8_t *one_KB, *one_MB;
286 uint8_t one_KB_key[20], one_MB_key[20];
287 int count;
288
289 #ifdef SHADER_CACHE_DISABLE_BY_DEFAULT
290 setenv("MESA_GLSL_CACHE_DISABLE", "false", 1);
291 #endif /* SHADER_CACHE_DISABLE_BY_DEFAULT */
292
293 cache = disk_cache_create("test", "make_check", 0);
294
295 disk_cache_compute_key(cache, blob, sizeof(blob), blob_key);
296
297 /* Ensure that disk_cache_get returns nothing before anything is added. */
298 result = disk_cache_get(cache, blob_key, &size);
299 expect_null(result, "disk_cache_get with non-existent item (pointer)");
300 expect_equal(size, 0, "disk_cache_get with non-existent item (size)");
301
302 /* Simple test of put and get. */
303 disk_cache_put(cache, blob_key, blob, sizeof(blob), NULL);
304
305 /* disk_cache_put() hands things off to a thread so wait for it. */
306 disk_cache_wait_for_idle(cache);
307
308 result = disk_cache_get(cache, blob_key, &size);
309 expect_equal_str(blob, result, "disk_cache_get of existing item (pointer)");
310 expect_equal(size, sizeof(blob), "disk_cache_get of existing item (size)");
311
312 free(result);
313
314 /* Test put and get of a second item. */
315 disk_cache_compute_key(cache, string, sizeof(string), string_key);
316 disk_cache_put(cache, string_key, string, sizeof(string), NULL);
317
318 /* disk_cache_put() hands things off to a thread so wait for it. */
319 disk_cache_wait_for_idle(cache);
320
321 result = disk_cache_get(cache, string_key, &size);
322 expect_equal_str(result, string, "2nd disk_cache_get of existing item (pointer)");
323 expect_equal(size, sizeof(string), "2nd disk_cache_get of existing item (size)");
324
325 free(result);
326
327 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
328 disk_cache_destroy(cache);
329
330 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
331 cache = disk_cache_create("test", "make_check", 0);
332
333 one_KB = calloc(1, 1024);
334
335 /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
336 * interesting. But we do have want to take some special care with
337 * the hash we use here. The issue is that in this artificial case,
338 * (with only three files in the cache), the probability is good
339 * that each of the three files will end up in their own
340 * directory. Then, if the directory containing the .tmp file for
341 * the new item being added for disk_cache_put() is the chosen victim
342 * directory for eviction, then no suitable file will be found and
343 * nothing will be evicted.
344 *
345 * That's actually expected given how the eviction code is
346 * implemented, (which expects to only evict once things are more
347 * interestingly full than that).
348 *
349 * For this test, we force this signature to land in the same
350 * directory as the original blob first written to the cache.
351 */
352 disk_cache_compute_key(cache, one_KB, 1024, one_KB_key);
353 one_KB_key[0] = blob_key[0];
354
355 disk_cache_put(cache, one_KB_key, one_KB, 1024, NULL);
356
357 free(one_KB);
358
359 /* disk_cache_put() hands things off to a thread so wait for it. */
360 disk_cache_wait_for_idle(cache);
361
362 result = disk_cache_get(cache, one_KB_key, &size);
363 expect_non_null(result, "3rd disk_cache_get of existing item (pointer)");
364 expect_equal(size, 1024, "3rd disk_cache_get of existing item (size)");
365
366 free(result);
367
368 /* Ensure eviction happened by checking that both of the previous
369 * cache itesm were evicted.
370 */
371 bool contains_1KB_file = false;
372 count = 0;
373 if (does_cache_contain(cache, blob_key))
374 count++;
375
376 if (does_cache_contain(cache, string_key))
377 count++;
378
379 if (does_cache_contain(cache, one_KB_key)) {
380 count++;
381 contains_1KB_file = true;
382 }
383
384 expect_true(contains_1KB_file,
385 "disk_cache_put eviction last file == MAX_SIZE (1KB)");
386 expect_equal(count, 1, "disk_cache_put eviction with MAX_SIZE=1K");
387
388 /* Now increase the size to 1M, add back both items, and ensure all
389 * three that have been added are available via disk_cache_get.
390 */
391 disk_cache_destroy(cache);
392
393 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
394 cache = disk_cache_create("test", "make_check", 0);
395
396 disk_cache_put(cache, blob_key, blob, sizeof(blob), NULL);
397 disk_cache_put(cache, string_key, string, sizeof(string), NULL);
398
399 /* disk_cache_put() hands things off to a thread so wait for it. */
400 disk_cache_wait_for_idle(cache);
401
402 count = 0;
403 if (does_cache_contain(cache, blob_key))
404 count++;
405
406 if (does_cache_contain(cache, string_key))
407 count++;
408
409 if (does_cache_contain(cache, one_KB_key))
410 count++;
411
412 expect_equal(count, 3, "no eviction before overflow with MAX_SIZE=1M");
413
414 /* Finally, check eviction again after adding an object of size 1M. */
415 one_MB = calloc(1024, 1024);
416
417 disk_cache_compute_key(cache, one_MB, 1024 * 1024, one_MB_key);
418 one_MB_key[0] = blob_key[0];
419
420 disk_cache_put(cache, one_MB_key, one_MB, 1024 * 1024, NULL);
421
422 free(one_MB);
423
424 /* disk_cache_put() hands things off to a thread so wait for it. */
425 disk_cache_wait_for_idle(cache);
426
427 bool contains_1MB_file = false;
428 count = 0;
429 if (does_cache_contain(cache, blob_key))
430 count++;
431
432 if (does_cache_contain(cache, string_key))
433 count++;
434
435 if (does_cache_contain(cache, one_KB_key))
436 count++;
437
438 if (does_cache_contain(cache, one_MB_key)) {
439 count++;
440 contains_1MB_file = true;
441 }
442
443 expect_true(contains_1MB_file,
444 "disk_cache_put eviction last file == MAX_SIZE (1MB)");
445 expect_equal(count, 1, "eviction after overflow with MAX_SIZE=1M");
446
447 disk_cache_destroy(cache);
448 }
449
450 static void
test_put_key_and_get_key(void)451 test_put_key_and_get_key(void)
452 {
453 struct disk_cache *cache;
454 bool result;
455
456 uint8_t key_a[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
457 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
458 uint8_t key_b[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
459 30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
460 uint8_t key_a_collide[20] =
461 { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
462 50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
463
464 #ifdef SHADER_CACHE_DISABLE_BY_DEFAULT
465 setenv("MESA_GLSL_CACHE_DISABLE", "false", 1);
466 #endif /* SHADER_CACHE_DISABLE_BY_DEFAULT */
467
468 cache = disk_cache_create("test", "make_check", 0);
469
470 /* First test that disk_cache_has_key returns false before disk_cache_put_key */
471 result = disk_cache_has_key(cache, key_a);
472 expect_equal(result, 0, "disk_cache_has_key before key added");
473
474 /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
475 disk_cache_put_key(cache, key_a);
476 result = disk_cache_has_key(cache, key_a);
477 expect_equal(result, 1, "disk_cache_has_key after key added");
478
479 disk_cache_put_key(cache, key_b);
480 result = disk_cache_has_key(cache, key_b);
481 expect_equal(result, 1, "2nd disk_cache_has_key after key added");
482
483 /* Test that a key with the same two bytes as an existing key
484 * forces an eviction.
485 */
486 disk_cache_put_key(cache, key_a_collide);
487 result = disk_cache_has_key(cache, key_a_collide);
488 expect_equal(result, 1, "put_key of a colliding key lands in the cache");
489
490 result = disk_cache_has_key(cache, key_a);
491 expect_equal(result, 0, "put_key of a colliding key evicts from the cache");
492
493 /* And finally test that we can re-add the original key to re-evict
494 * the colliding key.
495 */
496 disk_cache_put_key(cache, key_a);
497 result = disk_cache_has_key(cache, key_a);
498 expect_equal(result, 1, "put_key of original key lands again");
499
500 result = disk_cache_has_key(cache, key_a_collide);
501 expect_equal(result, 0, "put_key of orginal key evicts the colliding key");
502
503 disk_cache_destroy(cache);
504 }
505 #endif /* ENABLE_SHADER_CACHE */
506
507 int
main(void)508 main(void)
509 {
510 #ifdef ENABLE_SHADER_CACHE
511 int err;
512
513 test_disk_cache_create();
514
515 test_put_and_get();
516
517 test_put_key_and_get_key();
518
519 err = rmrf_local(CACHE_TEST_TMP);
520 expect_equal(err, 0, "Removing " CACHE_TEST_TMP " again");
521 #endif /* ENABLE_SHADER_CACHE */
522
523 return error ? 1 : 0;
524 }
525