0ef05aacb265482ba65d6cbd8f09f010a8bd14ac
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 */
35 #include "util/mesa-sha1.h"
36 #include "util/disk_cache.h"
40 #ifdef ENABLE_SHADER_CACHE
43 expect_equal(uint64_t actual
, uint64_t expected
, const char *test
)
45 if (actual
!= expected
) {
46 fprintf(stderr
, "Error: Test '%s' failed: Expected=%" PRIu64
47 ", Actual=%" PRIu64
"\n",
48 test
, expected
, actual
);
54 expect_null(void *ptr
, const char *test
)
57 fprintf(stderr
, "Error: Test '%s' failed: Result=%p, but expected NULL.\n",
64 expect_non_null(void *ptr
, const char *test
)
67 fprintf(stderr
, "Error: Test '%s' failed: Result=NULL, but expected something else.\n",
74 expect_equal_str(const char *actual
, const char *expected
, const char *test
)
76 if (strcmp(actual
, expected
)) {
77 fprintf(stderr
, "Error: Test '%s' failed:\n\t"
78 "Expected=\"%s\", Actual=\"%s\"\n",
79 test
, expected
, actual
);
84 /* Callback for nftw used in rmrf_local below.
87 remove_entry(const char *path
,
88 const struct stat
*sb
,
92 int err
= remove(path
);
95 fprintf(stderr
, "Error removing %s: %s\n", path
, strerror(errno
));
100 /* Recursively remove a directory.
102 * This is equivalent to "rm -rf <dir>" with one bit of protection
103 * that the directory name must begin with "." to ensure we don't
104 * wander around deleting more than intended.
106 * Returns 0 on success, -1 on any error.
109 rmrf_local(const char *path
)
111 if (path
== NULL
|| *path
== '\0' || *path
!= '.')
114 return nftw(path
, remove_entry
, 64, FTW_DEPTH
| FTW_PHYS
| FTW_MOUNT
);
117 #define CACHE_TEST_TMP "./cache-test-tmp"
120 test_disk_cache_create(void)
122 struct disk_cache
*cache
;
125 /* Before doing anything else, ensure that with
126 * MESA_GLSL_CACHE_DISABLE set, that disk_cache_create returns NULL.
128 setenv("MESA_GLSL_CACHE_DISABLE", "1", 1);
129 cache
= disk_cache_create();
130 expect_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DISABLE set");
132 unsetenv("MESA_GLSL_CACHE_DISABLE");
134 /* For the first real disk_cache_create() clear these environment
135 * variables to test creation of cache in home directory.
137 unsetenv("MESA_GLSL_CACHE_DIR");
138 unsetenv("XDG_CACHE_HOME");
140 cache
= disk_cache_create();
141 expect_non_null(cache
, "disk_cache_create with no environment variables");
143 disk_cache_destroy(cache
);
145 /* Test with XDG_CACHE_HOME set */
146 setenv("XDG_CACHE_HOME", CACHE_TEST_TMP
"/xdg-cache-home", 1);
147 cache
= disk_cache_create();
148 expect_null(cache
, "disk_cache_create with XDG_CACHE_HOME set with"
149 "a non-existing parent directory");
151 mkdir(CACHE_TEST_TMP
, 0755);
152 cache
= disk_cache_create();
153 expect_non_null(cache
, "disk_cache_create with XDG_CACHE_HOME set");
155 disk_cache_destroy(cache
);
157 /* Test with MESA_GLSL_CACHE_DIR set */
158 err
= rmrf_local(CACHE_TEST_TMP
);
159 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
);
161 setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP
"/mesa-glsl-cache-dir", 1);
162 cache
= disk_cache_create();
163 expect_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DIR set with"
164 "a non-existing parent directory");
166 mkdir(CACHE_TEST_TMP
, 0755);
167 cache
= disk_cache_create();
168 expect_non_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DIR set");
170 disk_cache_destroy(cache
);
174 does_cache_contain(struct disk_cache
*cache
, cache_key key
)
178 result
= disk_cache_get(cache
, key
, NULL
);
189 test_put_and_get(void)
191 struct disk_cache
*cache
;
192 /* If the text of this blob is changed, then blob_key_byte_zero
193 * also needs to be updated.
195 char blob
[] = "This is a blob of thirty-seven bytes";
196 uint8_t blob_key
[20];
197 uint8_t blob_key_byte_zero
= 0xca;
198 char string
[] = "While this string has thirty-four";
199 uint8_t string_key
[20];
202 uint8_t *one_KB
, *one_MB
;
203 uint8_t one_KB_key
[20], one_MB_key
[20];
206 cache
= disk_cache_create();
208 _mesa_sha1_compute(blob
, sizeof(blob
), blob_key
);
210 /* Ensure that disk_cache_get returns nothing before anything is added. */
211 result
= disk_cache_get(cache
, blob_key
, &size
);
212 expect_null(result
, "disk_cache_get with non-existent item (pointer)");
213 expect_equal(size
, 0, "disk_cache_get with non-existent item (size)");
215 /* Simple test of put and get. */
216 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
));
218 result
= disk_cache_get(cache
, blob_key
, &size
);
219 expect_equal_str(blob
, result
, "disk_cache_get of existing item (pointer)");
220 expect_equal(size
, sizeof(blob
), "disk_cache_get of existing item (size)");
224 /* Test put and get of a second item. */
225 _mesa_sha1_compute(string
, sizeof(string
), string_key
);
226 disk_cache_put(cache
, string_key
, string
, sizeof(string
));
228 result
= disk_cache_get(cache
, string_key
, &size
);
229 expect_equal_str(result
, string
, "2nd disk_cache_get of existing item (pointer)");
230 expect_equal(size
, sizeof(string
), "2nd disk_cache_get of existing item (size)");
234 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
235 disk_cache_destroy(cache
);
237 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
238 cache
= disk_cache_create();
240 one_KB
= calloc(1, 1024);
242 /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
243 * interesting. But we do have want to take some special care with
244 * the hash we use here. The issue is that in this artificial case,
245 * (with only three files in the cache), the probability is good
246 * that each of the three files will end up in their own
247 * directory. Then, if the directory containing the .tmp file for
248 * the new item being added for disk_cache_put() is the chosen victim
249 * directory for eviction, then no suitable file will be found and
250 * nothing will be evicted.
252 * That's actually expected given how the eviction code is
253 * implemented, (which expects to only evict once things are more
254 * interestingly full than that).
256 * For this test, we force this signature to land in the same
257 * directory as the original blob first written to the cache.
259 _mesa_sha1_compute(one_KB
, 1024, one_KB_key
);
260 one_KB_key
[0] = blob_key_byte_zero
;
262 disk_cache_put(cache
, one_KB_key
, one_KB
, 1024);
266 result
= disk_cache_get(cache
, one_KB_key
, &size
);
267 expect_non_null(result
, "3rd disk_cache_get of existing item (pointer)");
268 expect_equal(size
, 1024, "3rd disk_cache_get of existing item (size)");
272 /* Ensure eviction happened by checking that only one of the two
273 * previously-added items can still be fetched.
276 if (does_cache_contain(cache
, blob_key
))
279 if (does_cache_contain(cache
, string_key
))
282 expect_equal(count
, 1, "disk_cache_put eviction with MAX_SIZE=1K");
284 /* Now increase the size to 1M, add back both items, and ensure all
285 * three that have been added are available via disk_cache_get.
287 disk_cache_destroy(cache
);
289 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
290 cache
= disk_cache_create();
292 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
));
293 disk_cache_put(cache
, string_key
, string
, sizeof(string
));
296 if (does_cache_contain(cache
, blob_key
))
299 if (does_cache_contain(cache
, string_key
))
302 if (does_cache_contain(cache
, one_KB_key
))
305 expect_equal(count
, 3, "no eviction before overflow with MAX_SIZE=1M");
307 /* Finally, check eviction again after adding an object of size 1M. */
308 one_MB
= calloc(1024, 1024);
310 _mesa_sha1_compute(one_MB
, 1024 * 1024, one_MB_key
);
311 one_MB_key
[0] = blob_key_byte_zero
;;
313 disk_cache_put(cache
, one_MB_key
, one_MB
, 1024 * 1024);
318 if (does_cache_contain(cache
, blob_key
))
321 if (does_cache_contain(cache
, string_key
))
324 if (does_cache_contain(cache
, one_KB_key
))
327 expect_equal(count
, 2, "eviction after overflow with MAX_SIZE=1M");
329 disk_cache_destroy(cache
);
333 test_put_key_and_get_key(void)
335 struct disk_cache
*cache
;
338 uint8_t key_a
[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
339 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
340 uint8_t key_b
[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
341 30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
342 uint8_t key_a_collide
[20] =
343 { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
344 50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
346 cache
= disk_cache_create();
348 /* First test that disk_cache_has_key returns false before disk_cache_put_key */
349 result
= disk_cache_has_key(cache
, key_a
);
350 expect_equal(result
, 0, "disk_cache_has_key before key added");
352 /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
353 disk_cache_put_key(cache
, key_a
);
354 result
= disk_cache_has_key(cache
, key_a
);
355 expect_equal(result
, 1, "disk_cache_has_key after key added");
357 disk_cache_put_key(cache
, key_b
);
358 result
= disk_cache_has_key(cache
, key_b
);
359 expect_equal(result
, 1, "2nd disk_cache_has_key after key added");
361 /* Test that a key with the same two bytes as an existing key
362 * forces an eviction.
364 disk_cache_put_key(cache
, key_a_collide
);
365 result
= disk_cache_has_key(cache
, key_a_collide
);
366 expect_equal(result
, 1, "put_key of a colliding key lands in the cache");
368 result
= disk_cache_has_key(cache
, key_a
);
369 expect_equal(result
, 0, "put_key of a colliding key evicts from the cache");
371 /* And finally test that we can re-add the original key to re-evict
374 disk_cache_put_key(cache
, key_a
);
375 result
= disk_cache_has_key(cache
, key_a
);
376 expect_equal(result
, 1, "put_key of original key lands again");
378 result
= disk_cache_has_key(cache
, key_a_collide
);
379 expect_equal(result
, 0, "put_key of orginal key evicts the colliding key");
381 disk_cache_destroy(cache
);
383 #endif /* ENABLE_SHADER_CACHE */
388 #ifdef ENABLE_SHADER_CACHE
391 test_disk_cache_create();
395 test_put_key_and_get_key();
397 err
= rmrf_local(CACHE_TEST_TMP
);
398 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
" again");
399 #endif /* ENABLE_SHADER_CACHE */
401 return error
? 1 : 0;