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");
151 does_cache_contain(struct disk_cache
*cache
, const cache_key key
)
155 result
= disk_cache_get(cache
, key
, NULL
);
166 wait_until_file_written(struct disk_cache
*cache
, const cache_key key
)
171 /* Set 100ms delay */
173 req
.tv_nsec
= 100000000;
175 unsigned retries
= 0;
176 while (retries
++ < 20) {
177 if (does_cache_contain(cache
, key
)) {
181 nanosleep(&req
, &rem
);
186 cache_exists(struct disk_cache
*cache
)
188 uint8_t dummy_key
[20];
189 char data
[] = "some test data";
194 disk_cache_put(cache
, dummy_key
, data
, sizeof(data
), NULL
);
195 wait_until_file_written(cache
, dummy_key
);
196 return disk_cache_get(cache
, dummy_key
, NULL
);
199 #define CACHE_TEST_TMP "./cache-test-tmp"
202 test_disk_cache_create(void)
204 struct disk_cache
*cache
;
207 /* Before doing anything else, ensure that with
208 * MESA_GLSL_CACHE_DISABLE set to true, that disk_cache_create returns NULL.
210 setenv("MESA_GLSL_CACHE_DISABLE", "true", 1);
211 cache
= disk_cache_create("test", "make_check", 0);
212 expect_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DISABLE set");
214 unsetenv("MESA_GLSL_CACHE_DISABLE");
216 /* For the first real disk_cache_create() clear these environment
217 * variables to test creation of cache in home directory.
219 unsetenv("MESA_GLSL_CACHE_DIR");
220 unsetenv("XDG_CACHE_HOME");
222 cache
= disk_cache_create("test", "make_check", 0);
223 expect_non_null(cache
, "disk_cache_create with no environment variables");
225 disk_cache_destroy(cache
);
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");
233 mkdir(CACHE_TEST_TMP
, 0755);
234 cache
= disk_cache_create("test", "make_check", 0);
235 expect_non_null(cache_exists(cache
), "disk_cache_create with XDG_CACHE_HOME "
238 check_directories_created(CACHE_TEST_TMP
"/xdg-cache-home/"
241 disk_cache_destroy(cache
);
243 /* Test with MESA_GLSL_CACHE_DIR set */
244 err
= rmrf_local(CACHE_TEST_TMP
);
245 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
);
247 setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP
"/mesa-glsl-cache-dir", 1);
248 cache
= disk_cache_create("test", "make_check", 0);
249 expect_null(cache_exists(cache
), "disk_cache_create with MESA_GLSL_CACHE_DIR"
250 " set with a non-existing parent directory");
252 mkdir(CACHE_TEST_TMP
, 0755);
253 cache
= disk_cache_create("test", "make_check", 0);
254 expect_non_null(cache_exists(cache
), "disk_cache_create with "
255 "MESA_GLSL_CACHE_DIR set");
257 check_directories_created(CACHE_TEST_TMP
"/mesa-glsl-cache-dir/"
260 disk_cache_destroy(cache
);
264 test_put_and_get(void)
266 struct disk_cache
*cache
;
267 char blob
[] = "This is a blob of thirty-seven bytes";
268 uint8_t blob_key
[20];
269 char string
[] = "While this string has thirty-four";
270 uint8_t string_key
[20];
273 uint8_t *one_KB
, *one_MB
;
274 uint8_t one_KB_key
[20], one_MB_key
[20];
277 cache
= disk_cache_create("test", "make_check", 0);
279 disk_cache_compute_key(cache
, blob
, sizeof(blob
), blob_key
);
281 /* Ensure that disk_cache_get returns nothing before anything is added. */
282 result
= disk_cache_get(cache
, blob_key
, &size
);
283 expect_null(result
, "disk_cache_get with non-existent item (pointer)");
284 expect_equal(size
, 0, "disk_cache_get with non-existent item (size)");
286 /* Simple test of put and get. */
287 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
), NULL
);
289 /* disk_cache_put() hands things off to a thread give it some time to
292 wait_until_file_written(cache
, blob_key
);
294 result
= disk_cache_get(cache
, blob_key
, &size
);
295 expect_equal_str(blob
, result
, "disk_cache_get of existing item (pointer)");
296 expect_equal(size
, sizeof(blob
), "disk_cache_get of existing item (size)");
300 /* Test put and get of a second item. */
301 disk_cache_compute_key(cache
, string
, sizeof(string
), string_key
);
302 disk_cache_put(cache
, string_key
, string
, sizeof(string
), NULL
);
304 /* disk_cache_put() hands things off to a thread give it some time to
307 wait_until_file_written(cache
, string_key
);
309 result
= disk_cache_get(cache
, string_key
, &size
);
310 expect_equal_str(result
, string
, "2nd disk_cache_get of existing item (pointer)");
311 expect_equal(size
, sizeof(string
), "2nd disk_cache_get of existing item (size)");
315 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
316 disk_cache_destroy(cache
);
318 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
319 cache
= disk_cache_create("test", "make_check", 0);
321 one_KB
= calloc(1, 1024);
323 /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
324 * interesting. But we do have want to take some special care with
325 * the hash we use here. The issue is that in this artificial case,
326 * (with only three files in the cache), the probability is good
327 * that each of the three files will end up in their own
328 * directory. Then, if the directory containing the .tmp file for
329 * the new item being added for disk_cache_put() is the chosen victim
330 * directory for eviction, then no suitable file will be found and
331 * nothing will be evicted.
333 * That's actually expected given how the eviction code is
334 * implemented, (which expects to only evict once things are more
335 * interestingly full than that).
337 * For this test, we force this signature to land in the same
338 * directory as the original blob first written to the cache.
340 disk_cache_compute_key(cache
, one_KB
, 1024, one_KB_key
);
341 one_KB_key
[0] = blob_key
[0];
343 disk_cache_put(cache
, one_KB_key
, one_KB
, 1024, NULL
);
347 /* disk_cache_put() hands things off to a thread give it some time to
350 wait_until_file_written(cache
, one_KB_key
);
352 result
= disk_cache_get(cache
, one_KB_key
, &size
);
353 expect_non_null(result
, "3rd disk_cache_get of existing item (pointer)");
354 expect_equal(size
, 1024, "3rd disk_cache_get of existing item (size)");
358 /* Ensure eviction happened by checking that both of the previous
359 * cache itesm were evicted.
361 bool contains_1KB_file
= false;
363 if (does_cache_contain(cache
, blob_key
))
366 if (does_cache_contain(cache
, string_key
))
369 if (does_cache_contain(cache
, one_KB_key
)) {
371 contains_1KB_file
= true;
374 expect_true(contains_1KB_file
,
375 "disk_cache_put eviction last file == MAX_SIZE (1KB)");
376 expect_equal(count
, 1, "disk_cache_put eviction with MAX_SIZE=1K");
378 /* Now increase the size to 1M, add back both items, and ensure all
379 * three that have been added are available via disk_cache_get.
381 disk_cache_destroy(cache
);
383 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
384 cache
= disk_cache_create("test", "make_check", 0);
386 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
), NULL
);
387 disk_cache_put(cache
, string_key
, string
, sizeof(string
), NULL
);
389 /* disk_cache_put() hands things off to a thread give it some time to
392 wait_until_file_written(cache
, blob_key
);
393 wait_until_file_written(cache
, string_key
);
396 if (does_cache_contain(cache
, blob_key
))
399 if (does_cache_contain(cache
, string_key
))
402 if (does_cache_contain(cache
, one_KB_key
))
405 expect_equal(count
, 3, "no eviction before overflow with MAX_SIZE=1M");
407 /* Finally, check eviction again after adding an object of size 1M. */
408 one_MB
= calloc(1024, 1024);
410 disk_cache_compute_key(cache
, one_MB
, 1024 * 1024, one_MB_key
);
411 one_MB_key
[0] = blob_key
[0];
413 disk_cache_put(cache
, one_MB_key
, one_MB
, 1024 * 1024, NULL
);
417 /* disk_cache_put() hands things off to a thread give it some time to
420 wait_until_file_written(cache
, one_MB_key
);
422 bool contains_1MB_file
= false;
424 if (does_cache_contain(cache
, blob_key
))
427 if (does_cache_contain(cache
, string_key
))
430 if (does_cache_contain(cache
, one_KB_key
))
433 if (does_cache_contain(cache
, one_MB_key
)) {
435 contains_1MB_file
= true;
438 expect_true(contains_1MB_file
,
439 "disk_cache_put eviction last file == MAX_SIZE (1MB)");
440 expect_equal(count
, 1, "eviction after overflow with MAX_SIZE=1M");
442 disk_cache_destroy(cache
);
446 test_put_key_and_get_key(void)
448 struct disk_cache
*cache
;
451 uint8_t key_a
[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
452 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
453 uint8_t key_b
[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
454 30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
455 uint8_t key_a_collide
[20] =
456 { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
457 50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
459 cache
= disk_cache_create("test", "make_check", 0);
461 /* First test that disk_cache_has_key returns false before disk_cache_put_key */
462 result
= disk_cache_has_key(cache
, key_a
);
463 expect_equal(result
, 0, "disk_cache_has_key before key added");
465 /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
466 disk_cache_put_key(cache
, key_a
);
467 result
= disk_cache_has_key(cache
, key_a
);
468 expect_equal(result
, 1, "disk_cache_has_key after key added");
470 disk_cache_put_key(cache
, key_b
);
471 result
= disk_cache_has_key(cache
, key_b
);
472 expect_equal(result
, 1, "2nd disk_cache_has_key after key added");
474 /* Test that a key with the same two bytes as an existing key
475 * forces an eviction.
477 disk_cache_put_key(cache
, key_a_collide
);
478 result
= disk_cache_has_key(cache
, key_a_collide
);
479 expect_equal(result
, 1, "put_key of a colliding key lands in the cache");
481 result
= disk_cache_has_key(cache
, key_a
);
482 expect_equal(result
, 0, "put_key of a colliding key evicts from the cache");
484 /* And finally test that we can re-add the original key to re-evict
487 disk_cache_put_key(cache
, key_a
);
488 result
= disk_cache_has_key(cache
, key_a
);
489 expect_equal(result
, 1, "put_key of original key lands again");
491 result
= disk_cache_has_key(cache
, key_a_collide
);
492 expect_equal(result
, 0, "put_key of orginal key evicts the colliding key");
494 disk_cache_destroy(cache
);
496 #endif /* ENABLE_SHADER_CACHE */
501 #ifdef ENABLE_SHADER_CACHE
504 test_disk_cache_create();
508 test_put_key_and_get_key();
510 err
= rmrf_local(CACHE_TEST_TMP
);
511 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
" again");
512 #endif /* ENABLE_SHADER_CACHE */
514 return error
? 1 : 0;