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 */
37 #include "util/mesa-sha1.h"
38 #include "util/disk_cache.h"
42 #ifdef ENABLE_SHADER_CACHE
45 expect_true(bool result
, const char *test
)
48 fprintf(stderr
, "Error: Test '%s' failed: Expected=true"
49 ", Actual=false\n", test
);
55 expect_equal(uint64_t actual
, uint64_t expected
, const char *test
)
57 if (actual
!= expected
) {
58 fprintf(stderr
, "Error: Test '%s' failed: Expected=%" PRIu64
59 ", Actual=%" PRIu64
"\n",
60 test
, expected
, actual
);
66 expect_null(void *ptr
, const char *test
)
69 fprintf(stderr
, "Error: Test '%s' failed: Result=%p, but expected NULL.\n",
76 expect_non_null(void *ptr
, const char *test
)
79 fprintf(stderr
, "Error: Test '%s' failed: Result=NULL, but expected something else.\n",
86 expect_equal_str(const char *actual
, const char *expected
, const char *test
)
88 if (strcmp(actual
, expected
)) {
89 fprintf(stderr
, "Error: Test '%s' failed:\n\t"
90 "Expected=\"%s\", Actual=\"%s\"\n",
91 test
, expected
, actual
);
96 /* Callback for nftw used in rmrf_local below.
99 remove_entry(const char *path
,
100 const struct stat
*sb
,
104 int err
= remove(path
);
107 fprintf(stderr
, "Error removing %s: %s\n", path
, strerror(errno
));
112 /* Recursively remove a directory.
114 * This is equivalent to "rm -rf <dir>" with one bit of protection
115 * that the directory name must begin with "." to ensure we don't
116 * wander around deleting more than intended.
118 * Returns 0 on success, -1 on any error.
121 rmrf_local(const char *path
)
123 if (path
== NULL
|| *path
== '\0' || *path
!= '.')
126 return nftw(path
, remove_entry
, 64, FTW_DEPTH
| FTW_PHYS
| FTW_MOUNT
);
130 check_timestamp_and_gpu_id_directories_created(char *cache_dir
)
132 bool sub_dirs_created
= false;
135 if (getcwd(buf
, PATH_MAX
)) {
136 char *full_path
= NULL
;
137 if (asprintf(&full_path
, "%s%s", buf
, ++cache_dir
) != -1 ) {
139 if (stat(full_path
, &sb
) != -1 && S_ISDIR(sb
.st_mode
))
140 sub_dirs_created
= true;
146 expect_true(sub_dirs_created
, "create timestamp and gpu ip sub dirs");
149 #define CACHE_TEST_TMP "./cache-test-tmp"
152 test_disk_cache_create(void)
154 struct disk_cache
*cache
;
157 /* Before doing anything else, ensure that with
158 * MESA_GLSL_CACHE_DISABLE set, that disk_cache_create returns NULL.
160 setenv("MESA_GLSL_CACHE_DISABLE", "1", 1);
161 cache
= disk_cache_create("test", "make_check");
162 expect_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DISABLE set");
164 unsetenv("MESA_GLSL_CACHE_DISABLE");
166 /* For the first real disk_cache_create() clear these environment
167 * variables to test creation of cache in home directory.
169 unsetenv("MESA_GLSL_CACHE_DIR");
170 unsetenv("XDG_CACHE_HOME");
172 cache
= disk_cache_create("test", "make_check");
173 expect_non_null(cache
, "disk_cache_create with no environment variables");
175 disk_cache_destroy(cache
);
177 /* Test with XDG_CACHE_HOME set */
178 setenv("XDG_CACHE_HOME", CACHE_TEST_TMP
"/xdg-cache-home", 1);
179 cache
= disk_cache_create("test", "make_check");
180 expect_null(cache
, "disk_cache_create with XDG_CACHE_HOME set with"
181 "a non-existing parent directory");
183 /* Create string with expected directory hierarchy */
184 char expected_dir_h
[255];
185 sprintf(expected_dir_h
, "%s%s%s", CACHE_TEST_TMP
"/xdg-cache-home/mesa/",
186 get_arch_bitness_str(), "/make_check/test");
188 mkdir(CACHE_TEST_TMP
, 0755);
189 cache
= disk_cache_create("test", "make_check");
190 expect_non_null(cache
, "disk_cache_create with XDG_CACHE_HOME set");
192 check_timestamp_and_gpu_id_directories_created(expected_dir_h
);
194 disk_cache_destroy(cache
);
196 /* Test with MESA_GLSL_CACHE_DIR set */
197 err
= rmrf_local(CACHE_TEST_TMP
);
198 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
);
200 setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP
"/mesa-glsl-cache-dir", 1);
201 cache
= disk_cache_create("test", "make_check");
202 expect_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DIR set with"
203 "a non-existing parent directory");
205 sprintf(expected_dir_h
, "%s%s%s", CACHE_TEST_TMP
206 "/mesa-glsl-cache-dir/mesa/", get_arch_bitness_str(),
209 mkdir(CACHE_TEST_TMP
, 0755);
210 cache
= disk_cache_create("test", "make_check");
211 expect_non_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DIR set");
213 check_timestamp_and_gpu_id_directories_created(expected_dir_h
);
215 disk_cache_destroy(cache
);
219 does_cache_contain(struct disk_cache
*cache
, cache_key key
)
223 result
= disk_cache_get(cache
, key
, NULL
);
234 test_put_and_get(void)
236 struct disk_cache
*cache
;
237 /* If the text of this blob is changed, then blob_key_byte_zero
238 * also needs to be updated.
240 char blob
[] = "This is a blob of thirty-seven bytes";
241 uint8_t blob_key
[20];
242 uint8_t blob_key_byte_zero
= 0xca;
243 char string
[] = "While this string has thirty-four";
244 uint8_t string_key
[20];
247 uint8_t *one_KB
, *one_MB
;
248 uint8_t one_KB_key
[20], one_MB_key
[20];
251 cache
= disk_cache_create("test", "make_check");
253 _mesa_sha1_compute(blob
, sizeof(blob
), blob_key
);
255 /* Ensure that disk_cache_get returns nothing before anything is added. */
256 result
= disk_cache_get(cache
, blob_key
, &size
);
257 expect_null(result
, "disk_cache_get with non-existent item (pointer)");
258 expect_equal(size
, 0, "disk_cache_get with non-existent item (size)");
260 /* Simple test of put and get. */
261 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
));
263 result
= disk_cache_get(cache
, blob_key
, &size
);
264 expect_equal_str(blob
, result
, "disk_cache_get of existing item (pointer)");
265 expect_equal(size
, sizeof(blob
), "disk_cache_get of existing item (size)");
269 /* Test put and get of a second item. */
270 _mesa_sha1_compute(string
, sizeof(string
), string_key
);
271 disk_cache_put(cache
, string_key
, string
, sizeof(string
));
273 result
= disk_cache_get(cache
, string_key
, &size
);
274 expect_equal_str(result
, string
, "2nd disk_cache_get of existing item (pointer)");
275 expect_equal(size
, sizeof(string
), "2nd disk_cache_get of existing item (size)");
279 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
280 disk_cache_destroy(cache
);
282 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
283 cache
= disk_cache_create("test", "make_check");
285 one_KB
= calloc(1, 1024);
287 /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
288 * interesting. But we do have want to take some special care with
289 * the hash we use here. The issue is that in this artificial case,
290 * (with only three files in the cache), the probability is good
291 * that each of the three files will end up in their own
292 * directory. Then, if the directory containing the .tmp file for
293 * the new item being added for disk_cache_put() is the chosen victim
294 * directory for eviction, then no suitable file will be found and
295 * nothing will be evicted.
297 * That's actually expected given how the eviction code is
298 * implemented, (which expects to only evict once things are more
299 * interestingly full than that).
301 * For this test, we force this signature to land in the same
302 * directory as the original blob first written to the cache.
304 _mesa_sha1_compute(one_KB
, 1024, one_KB_key
);
305 one_KB_key
[0] = blob_key_byte_zero
;
307 disk_cache_put(cache
, one_KB_key
, one_KB
, 1024);
311 result
= disk_cache_get(cache
, one_KB_key
, &size
);
312 expect_non_null(result
, "3rd disk_cache_get of existing item (pointer)");
313 expect_equal(size
, 1024, "3rd disk_cache_get of existing item (size)");
317 /* Ensure eviction happened by checking that only one of the two
318 * previously-added items can still be fetched.
321 if (does_cache_contain(cache
, blob_key
))
324 if (does_cache_contain(cache
, string_key
))
327 expect_equal(count
, 1, "disk_cache_put eviction with MAX_SIZE=1K");
329 /* Now increase the size to 1M, add back both items, and ensure all
330 * three that have been added are available via disk_cache_get.
332 disk_cache_destroy(cache
);
334 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
335 cache
= disk_cache_create("test", "make_check");
337 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
));
338 disk_cache_put(cache
, string_key
, string
, sizeof(string
));
341 if (does_cache_contain(cache
, blob_key
))
344 if (does_cache_contain(cache
, string_key
))
347 if (does_cache_contain(cache
, one_KB_key
))
350 expect_equal(count
, 3, "no eviction before overflow with MAX_SIZE=1M");
352 /* Finally, check eviction again after adding an object of size 1M. */
353 one_MB
= calloc(1024, 1024);
355 _mesa_sha1_compute(one_MB
, 1024 * 1024, one_MB_key
);
356 one_MB_key
[0] = blob_key_byte_zero
;;
358 disk_cache_put(cache
, one_MB_key
, one_MB
, 1024 * 1024);
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
))
372 expect_equal(count
, 2, "eviction after overflow with MAX_SIZE=1M");
374 disk_cache_destroy(cache
);
378 test_put_key_and_get_key(void)
380 struct disk_cache
*cache
;
383 uint8_t key_a
[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
384 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
385 uint8_t key_b
[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
386 30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
387 uint8_t key_a_collide
[20] =
388 { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
389 50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
391 cache
= disk_cache_create("test", "make_check");
393 /* First test that disk_cache_has_key returns false before disk_cache_put_key */
394 result
= disk_cache_has_key(cache
, key_a
);
395 expect_equal(result
, 0, "disk_cache_has_key before key added");
397 /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
398 disk_cache_put_key(cache
, key_a
);
399 result
= disk_cache_has_key(cache
, key_a
);
400 expect_equal(result
, 1, "disk_cache_has_key after key added");
402 disk_cache_put_key(cache
, key_b
);
403 result
= disk_cache_has_key(cache
, key_b
);
404 expect_equal(result
, 1, "2nd disk_cache_has_key after key added");
406 /* Test that a key with the same two bytes as an existing key
407 * forces an eviction.
409 disk_cache_put_key(cache
, key_a_collide
);
410 result
= disk_cache_has_key(cache
, key_a_collide
);
411 expect_equal(result
, 1, "put_key of a colliding key lands in the cache");
413 result
= disk_cache_has_key(cache
, key_a
);
414 expect_equal(result
, 0, "put_key of a colliding key evicts from the cache");
416 /* And finally test that we can re-add the original key to re-evict
419 disk_cache_put_key(cache
, key_a
);
420 result
= disk_cache_has_key(cache
, key_a
);
421 expect_equal(result
, 1, "put_key of original key lands again");
423 result
= disk_cache_has_key(cache
, key_a_collide
);
424 expect_equal(result
, 0, "put_key of orginal key evicts the colliding key");
426 disk_cache_destroy(cache
);
428 #endif /* ENABLE_SHADER_CACHE */
433 #ifdef ENABLE_SHADER_CACHE
436 test_disk_cache_create();
440 test_put_key_and_get_key();
442 err
= rmrf_local(CACHE_TEST_TMP
);
443 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
" again");
444 #endif /* ENABLE_SHADER_CACHE */
446 return error
? 1 : 0;