b604943feeb01ad80cfd2afa16767e3b0237477d
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
| FTW_MOUNT
);
131 check_directories_created(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 timestamp and gpu ip 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, that disk_cache_create returns NULL.
161 setenv("MESA_GLSL_CACHE_DISABLE", "1", 1);
162 cache
= disk_cache_create("test", "make_check");
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");
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");
181 expect_null(cache
, "disk_cache_create with XDG_CACHE_HOME set with"
182 "a non-existing parent directory");
184 /* Create string with expected directory hierarchy */
185 char expected_dir_h
[255];
186 sprintf(expected_dir_h
, "%s%s%s", CACHE_TEST_TMP
"/xdg-cache-home/mesa/",
187 get_arch_bitness_str(), "/test");
189 mkdir(CACHE_TEST_TMP
, 0755);
190 cache
= disk_cache_create("test", "make_check");
191 expect_non_null(cache
, "disk_cache_create with XDG_CACHE_HOME set");
193 check_directories_created(expected_dir_h
);
195 disk_cache_destroy(cache
);
197 /* Test with MESA_GLSL_CACHE_DIR set */
198 err
= rmrf_local(CACHE_TEST_TMP
);
199 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
);
201 setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP
"/mesa-glsl-cache-dir", 1);
202 cache
= disk_cache_create("test", "make_check");
203 expect_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DIR set with"
204 "a non-existing parent directory");
206 sprintf(expected_dir_h
, "%s%s%s", CACHE_TEST_TMP
207 "/mesa-glsl-cache-dir/mesa/", get_arch_bitness_str(),
210 mkdir(CACHE_TEST_TMP
, 0755);
211 cache
= disk_cache_create("test", "make_check");
212 expect_non_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DIR set");
214 check_directories_created(expected_dir_h
);
216 disk_cache_destroy(cache
);
220 does_cache_contain(struct disk_cache
*cache
, cache_key key
)
224 result
= disk_cache_get(cache
, key
, NULL
);
235 wait_until_file_written(struct disk_cache
*cache
, cache_key key
)
240 /* Set 100ms delay */
242 req
.tv_nsec
= 100000000;
244 unsigned retries
= 0;
245 while (retries
++ < 20) {
246 if (does_cache_contain(cache
, key
)) {
250 nanosleep(&req
, &rem
);
255 test_put_and_get(void)
257 struct disk_cache
*cache
;
258 char blob
[] = "This is a blob of thirty-seven bytes";
259 uint8_t blob_key
[20];
260 char string
[] = "While this string has thirty-four";
261 uint8_t string_key
[20];
264 uint8_t *one_KB
, *one_MB
;
265 uint8_t one_KB_key
[20], one_MB_key
[20];
268 cache
= disk_cache_create("test", "make_check");
270 disk_cache_compute_key(cache
, blob
, sizeof(blob
), blob_key
);
272 /* Ensure that disk_cache_get returns nothing before anything is added. */
273 result
= disk_cache_get(cache
, blob_key
, &size
);
274 expect_null(result
, "disk_cache_get with non-existent item (pointer)");
275 expect_equal(size
, 0, "disk_cache_get with non-existent item (size)");
277 /* Simple test of put and get. */
278 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
));
280 /* disk_cache_put() hands things off to a thread give it some time to
283 wait_until_file_written(cache
, blob_key
);
285 result
= disk_cache_get(cache
, blob_key
, &size
);
286 expect_equal_str(blob
, result
, "disk_cache_get of existing item (pointer)");
287 expect_equal(size
, sizeof(blob
), "disk_cache_get of existing item (size)");
291 /* Test put and get of a second item. */
292 disk_cache_compute_key(cache
, string
, sizeof(string
), string_key
);
293 disk_cache_put(cache
, string_key
, string
, sizeof(string
));
295 /* disk_cache_put() hands things off to a thread give it some time to
298 wait_until_file_written(cache
, string_key
);
300 result
= disk_cache_get(cache
, string_key
, &size
);
301 expect_equal_str(result
, string
, "2nd disk_cache_get of existing item (pointer)");
302 expect_equal(size
, sizeof(string
), "2nd disk_cache_get of existing item (size)");
306 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
307 disk_cache_destroy(cache
);
309 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
310 cache
= disk_cache_create("test", "make_check");
312 one_KB
= calloc(1, 1024);
314 /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
315 * interesting. But we do have want to take some special care with
316 * the hash we use here. The issue is that in this artificial case,
317 * (with only three files in the cache), the probability is good
318 * that each of the three files will end up in their own
319 * directory. Then, if the directory containing the .tmp file for
320 * the new item being added for disk_cache_put() is the chosen victim
321 * directory for eviction, then no suitable file will be found and
322 * nothing will be evicted.
324 * That's actually expected given how the eviction code is
325 * implemented, (which expects to only evict once things are more
326 * interestingly full than that).
328 * For this test, we force this signature to land in the same
329 * directory as the original blob first written to the cache.
331 disk_cache_compute_key(cache
, one_KB
, 1024, one_KB_key
);
332 one_KB_key
[0] = blob_key
[0];
334 disk_cache_put(cache
, one_KB_key
, one_KB
, 1024);
338 /* disk_cache_put() hands things off to a thread give it some time to
341 wait_until_file_written(cache
, one_KB_key
);
343 result
= disk_cache_get(cache
, one_KB_key
, &size
);
344 expect_non_null(result
, "3rd disk_cache_get of existing item (pointer)");
345 expect_equal(size
, 1024, "3rd disk_cache_get of existing item (size)");
349 /* Ensure eviction happened by checking that both of the previous
350 * cache itesm were evicted.
352 bool contains_1KB_file
= false;
354 if (does_cache_contain(cache
, blob_key
))
357 if (does_cache_contain(cache
, string_key
))
360 if (does_cache_contain(cache
, one_KB_key
)) {
362 contains_1KB_file
= true;
365 expect_true(contains_1KB_file
,
366 "disk_cache_put eviction last file == MAX_SIZE (1KB)");
367 expect_equal(count
, 1, "disk_cache_put eviction with MAX_SIZE=1K");
369 /* Now increase the size to 1M, add back both items, and ensure all
370 * three that have been added are available via disk_cache_get.
372 disk_cache_destroy(cache
);
374 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
375 cache
= disk_cache_create("test", "make_check");
377 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
));
378 disk_cache_put(cache
, string_key
, string
, sizeof(string
));
380 /* disk_cache_put() hands things off to a thread give it some time to
383 wait_until_file_written(cache
, blob_key
);
384 wait_until_file_written(cache
, string_key
);
387 if (does_cache_contain(cache
, blob_key
))
390 if (does_cache_contain(cache
, string_key
))
393 if (does_cache_contain(cache
, one_KB_key
))
396 expect_equal(count
, 3, "no eviction before overflow with MAX_SIZE=1M");
398 /* Finally, check eviction again after adding an object of size 1M. */
399 one_MB
= calloc(1024, 1024);
401 disk_cache_compute_key(cache
, one_MB
, 1024 * 1024, one_MB_key
);
402 one_MB_key
[0] = blob_key
[0];
404 disk_cache_put(cache
, one_MB_key
, one_MB
, 1024 * 1024);
408 /* disk_cache_put() hands things off to a thread give it some time to
411 wait_until_file_written(cache
, one_MB_key
);
413 bool contains_1MB_file
= false;
415 if (does_cache_contain(cache
, blob_key
))
418 if (does_cache_contain(cache
, string_key
))
421 if (does_cache_contain(cache
, one_KB_key
))
424 if (does_cache_contain(cache
, one_MB_key
)) {
426 contains_1MB_file
= true;
429 expect_true(contains_1MB_file
,
430 "disk_cache_put eviction last file == MAX_SIZE (1MB)");
431 expect_equal(count
, 1, "eviction after overflow with MAX_SIZE=1M");
433 disk_cache_destroy(cache
);
437 test_put_key_and_get_key(void)
439 struct disk_cache
*cache
;
442 uint8_t key_a
[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
443 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
444 uint8_t key_b
[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
445 30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
446 uint8_t key_a_collide
[20] =
447 { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
448 50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
450 cache
= disk_cache_create("test", "make_check");
452 /* First test that disk_cache_has_key returns false before disk_cache_put_key */
453 result
= disk_cache_has_key(cache
, key_a
);
454 expect_equal(result
, 0, "disk_cache_has_key before key added");
456 /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
457 disk_cache_put_key(cache
, key_a
);
458 result
= disk_cache_has_key(cache
, key_a
);
459 expect_equal(result
, 1, "disk_cache_has_key after key added");
461 disk_cache_put_key(cache
, key_b
);
462 result
= disk_cache_has_key(cache
, key_b
);
463 expect_equal(result
, 1, "2nd disk_cache_has_key after key added");
465 /* Test that a key with the same two bytes as an existing key
466 * forces an eviction.
468 disk_cache_put_key(cache
, key_a_collide
);
469 result
= disk_cache_has_key(cache
, key_a_collide
);
470 expect_equal(result
, 1, "put_key of a colliding key lands in the cache");
472 result
= disk_cache_has_key(cache
, key_a
);
473 expect_equal(result
, 0, "put_key of a colliding key evicts from the cache");
475 /* And finally test that we can re-add the original key to re-evict
478 disk_cache_put_key(cache
, key_a
);
479 result
= disk_cache_has_key(cache
, key_a
);
480 expect_equal(result
, 1, "put_key of original key lands again");
482 result
= disk_cache_has_key(cache
, key_a_collide
);
483 expect_equal(result
, 0, "put_key of orginal key evicts the colliding key");
485 disk_cache_destroy(cache
);
487 #endif /* ENABLE_SHADER_CACHE */
492 #ifdef ENABLE_SHADER_CACHE
495 test_disk_cache_create();
499 test_put_key_and_get_key();
501 err
= rmrf_local(CACHE_TEST_TMP
);
502 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
" again");
503 #endif /* ENABLE_SHADER_CACHE */
505 return error
? 1 : 0;