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"
41 expect_equal(uint64_t actual
, uint64_t expected
, const char *test
)
43 if (actual
!= expected
) {
44 fprintf(stderr
, "Error: Test '%s' failed: Expected=%" PRIu64
45 ", Actual=%" PRIu64
"\n",
46 test
, expected
, actual
);
52 expect_null(void *ptr
, const char *test
)
55 fprintf(stderr
, "Error: Test '%s' failed: Result=%p, but expected NULL.\n",
62 expect_non_null(void *ptr
, const char *test
)
65 fprintf(stderr
, "Error: Test '%s' failed: Result=NULL, but expected something else.\n",
72 expect_equal_str(const char *actual
, const char *expected
, const char *test
)
74 if (strcmp(actual
, expected
)) {
75 fprintf(stderr
, "Error: Test '%s' failed:\n\t"
76 "Expected=\"%s\", Actual=\"%s\"\n",
77 test
, expected
, actual
);
82 /* Callback for nftw used in rmrf_local below.
85 remove_entry(const char *path
,
86 const struct stat
*sb
,
90 int err
= remove(path
);
93 fprintf(stderr
, "Error removing %s: %s\n", path
, strerror(errno
));
98 /* Recursively remove a directory.
100 * This is equivalent to "rm -rf <dir>" with one bit of protection
101 * that the directory name must begin with "." to ensure we don't
102 * wander around deleting more than intended.
104 * Returns 0 on success, -1 on any error.
107 rmrf_local(const char *path
)
109 if (path
== NULL
|| *path
== '\0' || *path
!= '.')
112 return nftw(path
, remove_entry
, 64, FTW_DEPTH
| FTW_PHYS
| FTW_MOUNT
);
115 #define CACHE_TEST_TMP "./cache-test-tmp"
118 test_disk_cache_create(void)
120 struct disk_cache
*cache
;
123 /* Before doing anything else, ensure that with
124 * MESA_GLSL_CACHE_DISABLE set, that disk_cache_create returns NULL.
126 setenv("MESA_GLSL_CACHE_DISABLE", "1", 1);
127 cache
= disk_cache_create();
128 expect_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DISABLE set");
130 unsetenv("MESA_GLSL_CACHE_DISABLE");
132 /* For the first real disk_cache_create() clear these environment
133 * variables to test creation of cache in home directory.
135 unsetenv("MESA_GLSL_CACHE_DIR");
136 unsetenv("XDG_CACHE_HOME");
138 cache
= disk_cache_create();
139 expect_non_null(cache
, "disk_cache_create with no environment variables");
141 disk_cache_destroy(cache
);
143 /* Test with XDG_CACHE_HOME set */
144 setenv("XDG_CACHE_HOME", CACHE_TEST_TMP
"/xdg-cache-home", 1);
145 cache
= disk_cache_create();
146 expect_null(cache
, "disk_cache_create with XDG_CACHE_HOME set with"
147 "a non-existing parent directory");
149 mkdir(CACHE_TEST_TMP
, 0755);
150 cache
= disk_cache_create();
151 expect_non_null(cache
, "disk_cache_create with XDG_CACHE_HOME set");
153 disk_cache_destroy(cache
);
155 /* Test with MESA_GLSL_CACHE_DIR set */
156 err
= rmrf_local(CACHE_TEST_TMP
);
157 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
);
159 setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP
"/mesa-glsl-cache-dir", 1);
160 cache
= disk_cache_create();
161 expect_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DIR set with"
162 "a non-existing parent directory");
164 mkdir(CACHE_TEST_TMP
, 0755);
165 cache
= disk_cache_create();
166 expect_non_null(cache
, "disk_cache_create with MESA_GLSL_CACHE_DIR set");
168 disk_cache_destroy(cache
);
172 does_cache_contain(struct disk_cache
*cache
, cache_key key
)
176 result
= disk_cache_get(cache
, key
, NULL
);
187 test_put_and_get(void)
189 struct disk_cache
*cache
;
190 /* If the text of this blob is changed, then blob_key_byte_zero
191 * also needs to be updated.
193 char blob
[] = "This is a blob of thirty-seven bytes";
194 uint8_t blob_key
[20];
195 uint8_t blob_key_byte_zero
= 0xca;
196 char string
[] = "While this string has thirty-four";
197 uint8_t string_key
[20];
200 uint8_t *one_KB
, *one_MB
;
201 uint8_t one_KB_key
[20], one_MB_key
[20];
204 cache
= disk_cache_create();
206 _mesa_sha1_compute(blob
, sizeof(blob
), blob_key
);
208 /* Ensure that disk_cache_get returns nothing before anything is added. */
209 result
= disk_cache_get(cache
, blob_key
, &size
);
210 expect_null(result
, "disk_cache_get with non-existent item (pointer)");
211 expect_equal(size
, 0, "disk_cache_get with non-existent item (size)");
213 /* Simple test of put and get. */
214 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
));
216 result
= disk_cache_get(cache
, blob_key
, &size
);
217 expect_equal_str(blob
, result
, "disk_cache_get of existing item (pointer)");
218 expect_equal(size
, sizeof(blob
), "disk_cache_get of existing item (size)");
222 /* Test put and get of a second item. */
223 _mesa_sha1_compute(string
, sizeof(string
), string_key
);
224 disk_cache_put(cache
, string_key
, string
, sizeof(string
));
226 result
= disk_cache_get(cache
, string_key
, &size
);
227 expect_equal_str(result
, string
, "2nd disk_cache_get of existing item (pointer)");
228 expect_equal(size
, sizeof(string
), "2nd disk_cache_get of existing item (size)");
232 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
233 disk_cache_destroy(cache
);
235 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
236 cache
= disk_cache_create();
238 one_KB
= calloc(1, 1024);
240 /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
241 * interesting. But we do have want to take some special care with
242 * the hash we use here. The issue is that in this artificial case,
243 * (with only three files in the cache), the probability is good
244 * that each of the three files will end up in their own
245 * directory. Then, if the directory containing the .tmp file for
246 * the new item being added for disk_cache_put() is the chosen victim
247 * directory for eviction, then no suitable file will be found and
248 * nothing will be evicted.
250 * That's actually expected given how the eviction code is
251 * implemented, (which expects to only evict once things are more
252 * interestingly full than that).
254 * For this test, we force this signature to land in the same
255 * directory as the original blob first written to the cache.
257 _mesa_sha1_compute(one_KB
, 1024, one_KB_key
);
258 one_KB_key
[0] = blob_key_byte_zero
;
260 disk_cache_put(cache
, one_KB_key
, one_KB
, 1024);
264 result
= disk_cache_get(cache
, one_KB_key
, &size
);
265 expect_non_null(result
, "3rd disk_cache_get of existing item (pointer)");
266 expect_equal(size
, 1024, "3rd disk_cache_get of existing item (size)");
270 /* Ensure eviction happened by checking that only one of the two
271 * previously-added items can still be fetched.
274 if (does_cache_contain(cache
, blob_key
))
277 if (does_cache_contain(cache
, string_key
))
280 expect_equal(count
, 1, "disk_cache_put eviction with MAX_SIZE=1K");
282 /* Now increase the size to 1M, add back both items, and ensure all
283 * three that have been added are available via disk_cache_get.
285 disk_cache_destroy(cache
);
287 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
288 cache
= disk_cache_create();
290 disk_cache_put(cache
, blob_key
, blob
, sizeof(blob
));
291 disk_cache_put(cache
, string_key
, string
, sizeof(string
));
294 if (does_cache_contain(cache
, blob_key
))
297 if (does_cache_contain(cache
, string_key
))
300 if (does_cache_contain(cache
, one_KB_key
))
303 expect_equal(count
, 3, "no eviction before overflow with MAX_SIZE=1M");
305 /* Finally, check eviction again after adding an object of size 1M. */
306 one_MB
= calloc(1024, 1024);
308 _mesa_sha1_compute(one_MB
, 1024 * 1024, one_MB_key
);
309 one_MB_key
[0] = blob_key_byte_zero
;;
311 disk_cache_put(cache
, one_MB_key
, one_MB
, 1024 * 1024);
316 if (does_cache_contain(cache
, blob_key
))
319 if (does_cache_contain(cache
, string_key
))
322 if (does_cache_contain(cache
, one_KB_key
))
325 expect_equal(count
, 2, "eviction after overflow with MAX_SIZE=1M");
327 disk_cache_destroy(cache
);
331 test_put_key_and_get_key(void)
333 struct disk_cache
*cache
;
336 uint8_t key_a
[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
337 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
338 uint8_t key_b
[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
339 30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
340 uint8_t key_a_collide
[20] =
341 { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
342 50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
344 cache
= disk_cache_create();
346 /* First test that disk_cache_has_key returns false before disk_cache_put_key */
347 result
= disk_cache_has_key(cache
, key_a
);
348 expect_equal(result
, 0, "disk_cache_has_key before key added");
350 /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
351 disk_cache_put_key(cache
, key_a
);
352 result
= disk_cache_has_key(cache
, key_a
);
353 expect_equal(result
, 1, "disk_cache_has_key after key added");
355 disk_cache_put_key(cache
, key_b
);
356 result
= disk_cache_has_key(cache
, key_b
);
357 expect_equal(result
, 1, "2nd disk_cache_has_key after key added");
359 /* Test that a key with the same two bytes as an existing key
360 * forces an eviction.
362 disk_cache_put_key(cache
, key_a_collide
);
363 result
= disk_cache_has_key(cache
, key_a_collide
);
364 expect_equal(result
, 1, "put_key of a colliding key lands in the cache");
366 result
= disk_cache_has_key(cache
, key_a
);
367 expect_equal(result
, 0, "put_key of a colliding key evicts from the cache");
369 /* And finally test that we can re-add the original key to re-evict
372 disk_cache_put_key(cache
, key_a
);
373 result
= disk_cache_has_key(cache
, key_a
);
374 expect_equal(result
, 1, "put_key of original key lands again");
376 result
= disk_cache_has_key(cache
, key_a_collide
);
377 expect_equal(result
, 0, "put_key of orginal key evicts the colliding key");
379 disk_cache_destroy(cache
);
387 test_disk_cache_create();
391 test_put_key_and_get_key();
393 err
= rmrf_local(CACHE_TEST_TMP
);
394 expect_equal(err
, 0, "Removing " CACHE_TEST_TMP
" again");
396 return error
? 1 : 0;