• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  */
23 
24 /* A collection of unit tests for cache.c */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <ftw.h>
31 #include <errno.h>
32 #include <stdarg.h>
33 #include <inttypes.h>
34 #include <limits.h>
35 #include <time.h>
36 #include <unistd.h>
37 
38 #include "util/mesa-sha1.h"
39 #include "util/disk_cache.h"
40 
41 bool error = false;
42 
43 #ifdef ENABLE_SHADER_CACHE
44 
45 static void
expect_true(bool result,const char * test)46 expect_true(bool result, const char *test)
47 {
48    if (!result) {
49       fprintf(stderr, "Error: Test '%s' failed: Expected=true"
50               ", Actual=false\n", test);
51       error = true;
52    }
53 }
54 
55 static void
expect_equal(uint64_t actual,uint64_t expected,const char * test)56 expect_equal(uint64_t actual, uint64_t expected, const char *test)
57 {
58    if (actual != expected) {
59       fprintf(stderr, "Error: Test '%s' failed: Expected=%" PRIu64
60               ", Actual=%" PRIu64 "\n",
61               test, expected, actual);
62       error = true;
63    }
64 }
65 
66 static void
expect_null(void * ptr,const char * test)67 expect_null(void *ptr, const char *test)
68 {
69    if (ptr != NULL) {
70       fprintf(stderr, "Error: Test '%s' failed: Result=%p, but expected NULL.\n",
71               test, ptr);
72       error = true;
73    }
74 }
75 
76 static void
expect_non_null(void * ptr,const char * test)77 expect_non_null(void *ptr, const char *test)
78 {
79    if (ptr == NULL) {
80       fprintf(stderr, "Error: Test '%s' failed: Result=NULL, but expected something else.\n",
81               test);
82       error = true;
83    }
84 }
85 
86 static void
expect_equal_str(const char * actual,const char * expected,const char * test)87 expect_equal_str(const char *actual, const char *expected, const char *test)
88 {
89    if (strcmp(actual, expected)) {
90       fprintf(stderr, "Error: Test '%s' failed:\n\t"
91               "Expected=\"%s\", Actual=\"%s\"\n",
92               test, expected, actual);
93       error = true;
94    }
95 }
96 
97 /* Callback for nftw used in rmrf_local below.
98  */
99 static int
remove_entry(const char * path,const struct stat * sb,int typeflag,struct FTW * ftwbuf)100 remove_entry(const char *path,
101              const struct stat *sb,
102              int typeflag,
103              struct FTW *ftwbuf)
104 {
105    int err = remove(path);
106 
107    if (err)
108       fprintf(stderr, "Error removing %s: %s\n", path, strerror(errno));
109 
110    return err;
111 }
112 
113 /* Recursively remove a directory.
114  *
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.
118  *
119  * Returns 0 on success, -1 on any error.
120  */
121 static int
rmrf_local(const char * path)122 rmrf_local(const char *path)
123 {
124    if (path == NULL || *path == '\0' || *path != '.')
125       return -1;
126 
127    return nftw(path, remove_entry, 64, FTW_DEPTH | FTW_PHYS);
128 }
129 
130 static void
check_directories_created(const char * cache_dir)131 check_directories_created(const char *cache_dir)
132 {
133    bool sub_dirs_created = false;
134 
135    char buf[PATH_MAX];
136    if (getcwd(buf, PATH_MAX)) {
137       char *full_path = NULL;
138       if (asprintf(&full_path, "%s%s", buf, ++cache_dir) != -1 ) {
139          struct stat sb;
140          if (stat(full_path, &sb) != -1 && S_ISDIR(sb.st_mode))
141             sub_dirs_created = true;
142 
143          free(full_path);
144       }
145    }
146 
147    expect_true(sub_dirs_created, "create sub dirs");
148 }
149 
150 #define CACHE_TEST_TMP "./cache-test-tmp"
151 
152 static void
test_disk_cache_create(void)153 test_disk_cache_create(void)
154 {
155    struct disk_cache *cache;
156    int err;
157 
158    /* Before doing anything else, ensure that with
159     * MESA_GLSL_CACHE_DISABLE set to true, that disk_cache_create returns NULL.
160     */
161    setenv("MESA_GLSL_CACHE_DISABLE", "true", 1);
162    cache = disk_cache_create("test", "make_check", 0);
163    expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DISABLE set");
164 
165    unsetenv("MESA_GLSL_CACHE_DISABLE");
166 
167    /* For the first real disk_cache_create() clear these environment
168     * variables to test creation of cache in home directory.
169     */
170    unsetenv("MESA_GLSL_CACHE_DIR");
171    unsetenv("XDG_CACHE_HOME");
172 
173    cache = disk_cache_create("test", "make_check", 0);
174    expect_non_null(cache, "disk_cache_create with no environment variables");
175 
176    disk_cache_destroy(cache);
177 
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", 0);
181    expect_null(cache, "disk_cache_create with XDG_CACHE_HOME set with"
182                "a non-existing parent directory");
183 
184    mkdir(CACHE_TEST_TMP, 0755);
185    cache = disk_cache_create("test", "make_check", 0);
186    expect_non_null(cache, "disk_cache_create with XDG_CACHE_HOME set");
187 
188    check_directories_created(CACHE_TEST_TMP "/xdg-cache-home/"
189                              CACHE_DIR_NAME);
190 
191    disk_cache_destroy(cache);
192 
193    /* Test with MESA_GLSL_CACHE_DIR set */
194    err = rmrf_local(CACHE_TEST_TMP);
195    expect_equal(err, 0, "Removing " CACHE_TEST_TMP);
196 
197    setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP "/mesa-glsl-cache-dir", 1);
198    cache = disk_cache_create("test", "make_check", 0);
199    expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set with"
200                "a non-existing parent directory");
201 
202    mkdir(CACHE_TEST_TMP, 0755);
203    cache = disk_cache_create("test", "make_check", 0);
204    expect_non_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set");
205 
206    check_directories_created(CACHE_TEST_TMP "/mesa-glsl-cache-dir/"
207                              CACHE_DIR_NAME);
208 
209    disk_cache_destroy(cache);
210 }
211 
212 static bool
does_cache_contain(struct disk_cache * cache,const cache_key key)213 does_cache_contain(struct disk_cache *cache, const cache_key key)
214 {
215    void *result;
216 
217    result = disk_cache_get(cache, key, NULL);
218 
219    if (result) {
220       free(result);
221       return true;
222    }
223 
224    return false;
225 }
226 
227 static void
wait_until_file_written(struct disk_cache * cache,const cache_key key)228 wait_until_file_written(struct disk_cache *cache, const cache_key key)
229 {
230    struct timespec req;
231    struct timespec rem;
232 
233    /* Set 100ms delay */
234    req.tv_sec = 0;
235    req.tv_nsec = 100000000;
236 
237    unsigned retries = 0;
238    while (retries++ < 20) {
239       if (does_cache_contain(cache, key)) {
240          break;
241       }
242 
243       nanosleep(&req, &rem);
244    }
245 }
246 
247 static void
test_put_and_get(void)248 test_put_and_get(void)
249 {
250    struct disk_cache *cache;
251    char blob[] = "This is a blob of thirty-seven bytes";
252    uint8_t blob_key[20];
253    char string[] = "While this string has thirty-four";
254    uint8_t string_key[20];
255    char *result;
256    size_t size;
257    uint8_t *one_KB, *one_MB;
258    uint8_t one_KB_key[20], one_MB_key[20];
259    int count;
260 
261    cache = disk_cache_create("test", "make_check", 0);
262 
263    disk_cache_compute_key(cache, blob, sizeof(blob), blob_key);
264 
265    /* Ensure that disk_cache_get returns nothing before anything is added. */
266    result = disk_cache_get(cache, blob_key, &size);
267    expect_null(result, "disk_cache_get with non-existent item (pointer)");
268    expect_equal(size, 0, "disk_cache_get with non-existent item (size)");
269 
270    /* Simple test of put and get. */
271    disk_cache_put(cache, blob_key, blob, sizeof(blob), NULL);
272 
273    /* disk_cache_put() hands things off to a thread give it some time to
274     * finish.
275     */
276    wait_until_file_written(cache, blob_key);
277 
278    result = disk_cache_get(cache, blob_key, &size);
279    expect_equal_str(blob, result, "disk_cache_get of existing item (pointer)");
280    expect_equal(size, sizeof(blob), "disk_cache_get of existing item (size)");
281 
282    free(result);
283 
284    /* Test put and get of a second item. */
285    disk_cache_compute_key(cache, string, sizeof(string), string_key);
286    disk_cache_put(cache, string_key, string, sizeof(string), NULL);
287 
288    /* disk_cache_put() hands things off to a thread give it some time to
289     * finish.
290     */
291    wait_until_file_written(cache, string_key);
292 
293    result = disk_cache_get(cache, string_key, &size);
294    expect_equal_str(result, string, "2nd disk_cache_get of existing item (pointer)");
295    expect_equal(size, sizeof(string), "2nd disk_cache_get of existing item (size)");
296 
297    free(result);
298 
299    /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
300    disk_cache_destroy(cache);
301 
302    setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
303    cache = disk_cache_create("test", "make_check", 0);
304 
305    one_KB = calloc(1, 1024);
306 
307    /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
308     * interesting. But we do have want to take some special care with
309     * the hash we use here. The issue is that in this artificial case,
310     * (with only three files in the cache), the probability is good
311     * that each of the three files will end up in their own
312     * directory. Then, if the directory containing the .tmp file for
313     * the new item being added for disk_cache_put() is the chosen victim
314     * directory for eviction, then no suitable file will be found and
315     * nothing will be evicted.
316     *
317     * That's actually expected given how the eviction code is
318     * implemented, (which expects to only evict once things are more
319     * interestingly full than that).
320     *
321     * For this test, we force this signature to land in the same
322     * directory as the original blob first written to the cache.
323     */
324    disk_cache_compute_key(cache, one_KB, 1024, one_KB_key);
325    one_KB_key[0] = blob_key[0];
326 
327    disk_cache_put(cache, one_KB_key, one_KB, 1024, NULL);
328 
329    free(one_KB);
330 
331    /* disk_cache_put() hands things off to a thread give it some time to
332     * finish.
333     */
334    wait_until_file_written(cache, one_KB_key);
335 
336    result = disk_cache_get(cache, one_KB_key, &size);
337    expect_non_null(result, "3rd disk_cache_get of existing item (pointer)");
338    expect_equal(size, 1024, "3rd disk_cache_get of existing item (size)");
339 
340    free(result);
341 
342    /* Ensure eviction happened by checking that both of the previous
343     * cache itesm were evicted.
344     */
345    bool contains_1KB_file = false;
346    count = 0;
347    if (does_cache_contain(cache, blob_key))
348        count++;
349 
350    if (does_cache_contain(cache, string_key))
351        count++;
352 
353    if (does_cache_contain(cache, one_KB_key)) {
354       count++;
355       contains_1KB_file = true;
356    }
357 
358    expect_true(contains_1KB_file,
359                "disk_cache_put eviction last file == MAX_SIZE (1KB)");
360    expect_equal(count, 1, "disk_cache_put eviction with MAX_SIZE=1K");
361 
362    /* Now increase the size to 1M, add back both items, and ensure all
363     * three that have been added are available via disk_cache_get.
364     */
365    disk_cache_destroy(cache);
366 
367    setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
368    cache = disk_cache_create("test", "make_check", 0);
369 
370    disk_cache_put(cache, blob_key, blob, sizeof(blob), NULL);
371    disk_cache_put(cache, string_key, string, sizeof(string), NULL);
372 
373    /* disk_cache_put() hands things off to a thread give it some time to
374     * finish.
375     */
376    wait_until_file_written(cache, blob_key);
377    wait_until_file_written(cache, string_key);
378 
379    count = 0;
380    if (does_cache_contain(cache, blob_key))
381        count++;
382 
383    if (does_cache_contain(cache, string_key))
384        count++;
385 
386    if (does_cache_contain(cache, one_KB_key))
387        count++;
388 
389    expect_equal(count, 3, "no eviction before overflow with MAX_SIZE=1M");
390 
391    /* Finally, check eviction again after adding an object of size 1M. */
392    one_MB = calloc(1024, 1024);
393 
394    disk_cache_compute_key(cache, one_MB, 1024 * 1024, one_MB_key);
395    one_MB_key[0] = blob_key[0];
396 
397    disk_cache_put(cache, one_MB_key, one_MB, 1024 * 1024, NULL);
398 
399    free(one_MB);
400 
401    /* disk_cache_put() hands things off to a thread give it some time to
402     * finish.
403     */
404    wait_until_file_written(cache, one_MB_key);
405 
406    bool contains_1MB_file = false;
407    count = 0;
408    if (does_cache_contain(cache, blob_key))
409        count++;
410 
411    if (does_cache_contain(cache, string_key))
412        count++;
413 
414    if (does_cache_contain(cache, one_KB_key))
415        count++;
416 
417    if (does_cache_contain(cache, one_MB_key)) {
418       count++;
419       contains_1MB_file = true;
420    }
421 
422    expect_true(contains_1MB_file,
423                "disk_cache_put eviction last file == MAX_SIZE (1MB)");
424    expect_equal(count, 1, "eviction after overflow with MAX_SIZE=1M");
425 
426    disk_cache_destroy(cache);
427 }
428 
429 static void
test_put_key_and_get_key(void)430 test_put_key_and_get_key(void)
431 {
432    struct disk_cache *cache;
433    bool result;
434 
435    uint8_t key_a[20] = {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
436                          10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
437    uint8_t key_b[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
438                          30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
439    uint8_t key_a_collide[20] =
440                         { 0,  1, 42, 43, 44, 45, 46, 47, 48, 49,
441                          50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
442 
443    cache = disk_cache_create("test", "make_check", 0);
444 
445    /* First test that disk_cache_has_key returns false before disk_cache_put_key */
446    result = disk_cache_has_key(cache, key_a);
447    expect_equal(result, 0, "disk_cache_has_key before key added");
448 
449    /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
450    disk_cache_put_key(cache, key_a);
451    result = disk_cache_has_key(cache, key_a);
452    expect_equal(result, 1, "disk_cache_has_key after key added");
453 
454    disk_cache_put_key(cache, key_b);
455    result = disk_cache_has_key(cache, key_b);
456    expect_equal(result, 1, "2nd disk_cache_has_key after key added");
457 
458    /* Test that a key with the same two bytes as an existing key
459     * forces an eviction.
460     */
461    disk_cache_put_key(cache, key_a_collide);
462    result = disk_cache_has_key(cache, key_a_collide);
463    expect_equal(result, 1, "put_key of a colliding key lands in the cache");
464 
465    result = disk_cache_has_key(cache, key_a);
466    expect_equal(result, 0, "put_key of a colliding key evicts from the cache");
467 
468    /* And finally test that we can re-add the original key to re-evict
469     * the colliding key.
470     */
471    disk_cache_put_key(cache, key_a);
472    result = disk_cache_has_key(cache, key_a);
473    expect_equal(result, 1, "put_key of original key lands again");
474 
475    result = disk_cache_has_key(cache, key_a_collide);
476    expect_equal(result, 0, "put_key of orginal key evicts the colliding key");
477 
478    disk_cache_destroy(cache);
479 }
480 #endif /* ENABLE_SHADER_CACHE */
481 
482 int
main(void)483 main(void)
484 {
485 #ifdef ENABLE_SHADER_CACHE
486    int err;
487 
488    test_disk_cache_create();
489 
490    test_put_and_get();
491 
492    test_put_key_and_get_key();
493 
494    err = rmrf_local(CACHE_TEST_TMP);
495    expect_equal(err, 0, "Removing " CACHE_TEST_TMP " again");
496 #endif /* ENABLE_SHADER_CACHE */
497 
498    return error ? 1 : 0;
499 }
500