2 * Copyright © 2015 Intel Corporation
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:
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
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
24 /* A collection of unit tests for cache.c */
38 #include "util/mesa-sha1.h"
39 #include "util/disk_cache.h"
43 #ifdef ENABLE_SHADER_CACHE
46 expect_true(bool result
, const char *test
)
49 fprintf(stderr
, "Error: Test '%s' failed: Expected=true"
50 ", Actual=false\n", test
);
56 expect_equal(uint64_t actual
, uint64_t expected
, const char *test
)
58 if (actual
!= expected
) {
59 fprintf(stderr
, "Error: Test '%s' failed: Expected=%" PRIu64
60 ", Actual=%" PRIu64
"\n",
61 test
, expected
, actual
);
67 expect_null(void *ptr
, const char *test
)
70 fprintf(stderr
, "Error: Test '%s' failed: Result=%p, but expected NULL.\n",
77 expect_non_null(void *ptr
, const char *test
)
80 fprintf(stderr
, "Error: Test '%s' failed: Result=NULL, but expected something else.\n",
87 expect_equal_str(const char *actual
, const char *expected
, const char *test
)
89 if (strcmp(actual
, expected
)) {
90 fprintf(stderr
, "Error: Test '%s' failed:\n\t"
91 "Expected=\"%s\", Actual=\"%s\"\n",
92 test
, expected
, actual
);
97 /* Callback for nftw used in rmrf_local below.
100 remove_entry(const char *path
,
101 const struct stat
*sb
,
105 int err
= remove(path
);
108 fprintf(stderr
, "Error removing %s: %s\n", path
, strerror(errno
));
113 /* Recursively remove a directory.
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.
119 * Returns 0 on success, -1 on any error.
122 rmrf_local(const char *path
)
124 if (path
== NULL
|| *path
== '\0' || *path
!= '.')
127 return nftw(path
, remove_entry
, 64, FTW_DEPTH
| FTW_PHYS
);
131 check_directories_created(const char *cache_dir
)
133 bool sub_dirs_created
= false;
136 if (getcwd(buf
, PATH_MAX
)) {
137 char *full_path
= NULL
;
138 if (asprintf(&full_path
, "%s%s", buf
, ++cache_dir
) != -1 ) {
140 if (stat(full_path
, &sb
) != -1 && S_ISDIR(sb
.st_mode
))
141 sub_dirs_created
= true;
147 expect_true(sub_dirs_created
, "create sub dirs");
150 #define CACHE_TEST_TMP "./cache-test-tmp"
153 test_disk_cache_create(void)
155 struct disk_cache
*cache
;
158 /* Before doing anything else, ensure that with
159 * MESA_GLSL_CACHE_DISABLE set to true, that disk_cache_create returns NULL.
161 setenv("MESA_GLSL_CACHE_DISABLE", "true", 1);
162 cache
= disk_cache_create("test", "make_check", 0);
163 expect_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DISABLE set");
165 unsetenv("MESA_GLSL_CACHE_DISABLE");
167 /* For the first real disk_cache_create() clear these environment
168 * variables to test creation of cache in home directory.
170 unsetenv("MESA_GLSL_CACHE_DIR");
171 unsetenv("XDG_CACHE_HOME");
173 cache
= disk_cache_create("test", "make_check", 0);
174 expect_non_null(cache
, "disk_cache_create with no environment variables");
176 disk_cache_destroy(cache
);
178 /* Test with XDG_CACHE_HOME set */
179 setenv("XDG_CACHE_HOME", CACHE_TEST_TMP
"/xdg-cache-home", 1);
180 cache
= disk_cache_create("test", "make_check", 0);
181 expect_null(cache
, "disk_cache_create with XDG_CACHE_HOME set with"
182 "a non-existing parent directory");
184 mkdir(CACHE_TEST_TMP
, 0755);
185 cache
= disk_cache_create("test", "make_check", 0);
186 expect_non_null(cache
, "disk_cache_create with XDG_CACHE_HOME set");
188 check_directories_created(CACHE_TEST_TMP
"/xdg-cache-home/"
191 disk_cache_destroy(cache
);
193 /* Test with MESA_GLSL_CACHE_DIR set */
194 err
= rmrf_local(CACHE_TEST_TMP
);
195 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
);
197 setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP
"/mesa-glsl-cache-dir", 1);
198 cache
= disk_cache_create("test", "make_check", 0);
199 expect_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DIR set with"
200 "a non-existing parent directory");
202 mkdir(CACHE_TEST_TMP
, 0755);
203 cache
= disk_cache_create("test", "make_check", 0);
204 expect_non_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DIR set");
206 check_directories_created(CACHE_TEST_TMP
"/mesa-glsl-cache-dir/"
209 disk_cache_destroy(cache
);
213 does_cache_contain(struct disk_cache
*cache
, const cache_key key
)
217 result
= disk_cache_get(cache
, key
, NULL
);
228 wait_until_file_written(struct disk_cache
*cache
, const cache_key key
)
233 /* Set 100ms delay */
235 req
.tv_nsec
= 100000000;
237 unsigned retries
= 0;
238 while (retries
++ < 20) {
239 if (does_cache_contain(cache
, key
)) {
243 nanosleep(&req
, &rem
);
248 test_put_and_get(void)
250 struct disk_cache
*cache
;
251 char blob
[] = "This is a blob of thirty-seven bytes";
252 uint8_t blob_key
[20];
253 char string
[] = "While this string has thirty-four";
254 uint8_t string_key
[20];
257 uint8_t *one_KB
, *one_MB
;
258 uint8_t one_KB_key
[20], one_MB_key
[20];
261 cache
= disk_cache_create("test", "make_check", 0);
263 disk_cache_compute_key(cache
, blob
, sizeof(blob
), blob_key
);
265 /* Ensure that disk_cache_get returns nothing before anything is added. */
266 result
= disk_cache_get(cache
, blob_key
, &size
);
267 expect_null(result
, "disk_cache_get with non-existent item (pointer)");
268 expect_equal(size
, 0, "disk_cache_get with non-existent item (size)");
270 /* Simple test of put and get. */
271 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
), NULL
);
273 /* disk_cache_put() hands things off to a thread give it some time to
276 wait_until_file_written(cache
, blob_key
);
278 result
= disk_cache_get(cache
, blob_key
, &size
);
279 expect_equal_str(blob
, result
, "disk_cache_get of existing item (pointer)");
280 expect_equal(size
, sizeof(blob
), "disk_cache_get of existing item (size)");
284 /* Test put and get of a second item. */
285 disk_cache_compute_key(cache
, string
, sizeof(string
), string_key
);
286 disk_cache_put(cache
, string_key
, string
, sizeof(string
), NULL
);
288 /* disk_cache_put() hands things off to a thread give it some time to
291 wait_until_file_written(cache
, string_key
);
293 result
= disk_cache_get(cache
, string_key
, &size
);
294 expect_equal_str(result
, string
, "2nd disk_cache_get of existing item (pointer)");
295 expect_equal(size
, sizeof(string
), "2nd disk_cache_get of existing item (size)");
299 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
300 disk_cache_destroy(cache
);
302 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
303 cache
= disk_cache_create("test", "make_check", 0);
305 one_KB
= calloc(1, 1024);
307 /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
308 * interesting. But we do have want to take some special care with
309 * the hash we use here. The issue is that in this artificial case,
310 * (with only three files in the cache), the probability is good
311 * that each of the three files will end up in their own
312 * directory. Then, if the directory containing the .tmp file for
313 * the new item being added for disk_cache_put() is the chosen victim
314 * directory for eviction, then no suitable file will be found and
315 * nothing will be evicted.
317 * That's actually expected given how the eviction code is
318 * implemented, (which expects to only evict once things are more
319 * interestingly full than that).
321 * For this test, we force this signature to land in the same
322 * directory as the original blob first written to the cache.
324 disk_cache_compute_key(cache
, one_KB
, 1024, one_KB_key
);
325 one_KB_key
[0] = blob_key
[0];
327 disk_cache_put(cache
, one_KB_key
, one_KB
, 1024, NULL
);
331 /* disk_cache_put() hands things off to a thread give it some time to
334 wait_until_file_written(cache
, one_KB_key
);
336 result
= disk_cache_get(cache
, one_KB_key
, &size
);
337 expect_non_null(result
, "3rd disk_cache_get of existing item (pointer)");
338 expect_equal(size
, 1024, "3rd disk_cache_get of existing item (size)");
342 /* Ensure eviction happened by checking that both of the previous
343 * cache itesm were evicted.
345 bool contains_1KB_file
= false;
347 if (does_cache_contain(cache
, blob_key
))
350 if (does_cache_contain(cache
, string_key
))
353 if (does_cache_contain(cache
, one_KB_key
)) {
355 contains_1KB_file
= true;
358 expect_true(contains_1KB_file
,
359 "disk_cache_put eviction last file == MAX_SIZE (1KB)");
360 expect_equal(count
, 1, "disk_cache_put eviction with MAX_SIZE=1K");
362 /* Now increase the size to 1M, add back both items, and ensure all
363 * three that have been added are available via disk_cache_get.
365 disk_cache_destroy(cache
);
367 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
368 cache
= disk_cache_create("test", "make_check", 0);
370 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
), NULL
);
371 disk_cache_put(cache
, string_key
, string
, sizeof(string
), NULL
);
373 /* disk_cache_put() hands things off to a thread give it some time to
376 wait_until_file_written(cache
, blob_key
);
377 wait_until_file_written(cache
, string_key
);
380 if (does_cache_contain(cache
, blob_key
))
383 if (does_cache_contain(cache
, string_key
))
386 if (does_cache_contain(cache
, one_KB_key
))
389 expect_equal(count
, 3, "no eviction before overflow with MAX_SIZE=1M");
391 /* Finally, check eviction again after adding an object of size 1M. */
392 one_MB
= calloc(1024, 1024);
394 disk_cache_compute_key(cache
, one_MB
, 1024 * 1024, one_MB_key
);
395 one_MB_key
[0] = blob_key
[0];
397 disk_cache_put(cache
, one_MB_key
, one_MB
, 1024 * 1024, NULL
);
401 /* disk_cache_put() hands things off to a thread give it some time to
404 wait_until_file_written(cache
, one_MB_key
);
406 bool contains_1MB_file
= false;
408 if (does_cache_contain(cache
, blob_key
))
411 if (does_cache_contain(cache
, string_key
))
414 if (does_cache_contain(cache
, one_KB_key
))
417 if (does_cache_contain(cache
, one_MB_key
)) {
419 contains_1MB_file
= true;
422 expect_true(contains_1MB_file
,
423 "disk_cache_put eviction last file == MAX_SIZE (1MB)");
424 expect_equal(count
, 1, "eviction after overflow with MAX_SIZE=1M");
426 disk_cache_destroy(cache
);
430 test_put_key_and_get_key(void)
432 struct disk_cache
*cache
;
435 uint8_t key_a
[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
436 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
437 uint8_t key_b
[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
438 30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
439 uint8_t key_a_collide
[20] =
440 { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
441 50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
443 cache
= disk_cache_create("test", "make_check", 0);
445 /* First test that disk_cache_has_key returns false before disk_cache_put_key */
446 result
= disk_cache_has_key(cache
, key_a
);
447 expect_equal(result
, 0, "disk_cache_has_key before key added");
449 /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
450 disk_cache_put_key(cache
, key_a
);
451 result
= disk_cache_has_key(cache
, key_a
);
452 expect_equal(result
, 1, "disk_cache_has_key after key added");
454 disk_cache_put_key(cache
, key_b
);
455 result
= disk_cache_has_key(cache
, key_b
);
456 expect_equal(result
, 1, "2nd disk_cache_has_key after key added");
458 /* Test that a key with the same two bytes as an existing key
459 * forces an eviction.
461 disk_cache_put_key(cache
, key_a_collide
);
462 result
= disk_cache_has_key(cache
, key_a_collide
);
463 expect_equal(result
, 1, "put_key of a colliding key lands in the cache");
465 result
= disk_cache_has_key(cache
, key_a
);
466 expect_equal(result
, 0, "put_key of a colliding key evicts from the cache");
468 /* And finally test that we can re-add the original key to re-evict
471 disk_cache_put_key(cache
, key_a
);
472 result
= disk_cache_has_key(cache
, key_a
);
473 expect_equal(result
, 1, "put_key of original key lands again");
475 result
= disk_cache_has_key(cache
, key_a_collide
);
476 expect_equal(result
, 0, "put_key of orginal key evicts the colliding key");
478 disk_cache_destroy(cache
);
480 #endif /* ENABLE_SHADER_CACHE */
485 #ifdef ENABLE_SHADER_CACHE
488 test_disk_cache_create();
492 test_put_key_and_get_key();
494 err
= rmrf_local(CACHE_TEST_TMP
);
495 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
" again");
496 #endif /* ENABLE_SHADER_CACHE */
498 return error
? 1 : 0;