util/disk_cache: fix make check
[mesa.git] / src / compiler / glsl / tests / cache_test.c
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 <unistd.h>
36
37 #include "util/mesa-sha1.h"
38 #include "util/disk_cache.h"
39
40 bool error = false;
41
42 #ifdef ENABLE_SHADER_CACHE
43
44 static void
45 expect_true(bool result, const char *test)
46 {
47 if (!result) {
48 fprintf(stderr, "Error: Test '%s' failed: Expected=true"
49 ", Actual=false\n", test);
50 error = true;
51 }
52 }
53
54 static void
55 expect_equal(uint64_t actual, uint64_t expected, const char *test)
56 {
57 if (actual != expected) {
58 fprintf(stderr, "Error: Test '%s' failed: Expected=%" PRIu64
59 ", Actual=%" PRIu64 "\n",
60 test, expected, actual);
61 error = true;
62 }
63 }
64
65 static void
66 expect_null(void *ptr, const char *test)
67 {
68 if (ptr != NULL) {
69 fprintf(stderr, "Error: Test '%s' failed: Result=%p, but expected NULL.\n",
70 test, ptr);
71 error = true;
72 }
73 }
74
75 static void
76 expect_non_null(void *ptr, const char *test)
77 {
78 if (ptr == NULL) {
79 fprintf(stderr, "Error: Test '%s' failed: Result=NULL, but expected something else.\n",
80 test);
81 error = true;
82 }
83 }
84
85 static void
86 expect_equal_str(const char *actual, const char *expected, const char *test)
87 {
88 if (strcmp(actual, expected)) {
89 fprintf(stderr, "Error: Test '%s' failed:\n\t"
90 "Expected=\"%s\", Actual=\"%s\"\n",
91 test, expected, actual);
92 error = true;
93 }
94 }
95
96 /* Callback for nftw used in rmrf_local below.
97 */
98 static int
99 remove_entry(const char *path,
100 const struct stat *sb,
101 int typeflag,
102 struct FTW *ftwbuf)
103 {
104 int err = remove(path);
105
106 if (err)
107 fprintf(stderr, "Error removing %s: %s\n", path, strerror(errno));
108
109 return err;
110 }
111
112 /* Recursively remove a directory.
113 *
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.
117 *
118 * Returns 0 on success, -1 on any error.
119 */
120 static int
121 rmrf_local(const char *path)
122 {
123 if (path == NULL || *path == '\0' || *path != '.')
124 return -1;
125
126 return nftw(path, remove_entry, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
127 }
128
129 static void
130 check_timestamp_and_gpu_id_directories_created(char *cache_dir)
131 {
132 bool sub_dirs_created = false;
133
134 char buf[PATH_MAX];
135 if (getcwd(buf, PATH_MAX)) {
136 char *full_path = NULL;
137 if (asprintf(&full_path, "%s%s", buf, ++cache_dir) != -1 ) {
138 struct stat sb;
139 if (stat(full_path, &sb) != -1 && S_ISDIR(sb.st_mode))
140 sub_dirs_created = true;
141
142 free(full_path);
143 }
144 }
145
146 expect_true(sub_dirs_created, "create timestamp and gpu ip sub dirs");
147 }
148
149 #define CACHE_TEST_TMP "./cache-test-tmp"
150
151 static void
152 test_disk_cache_create(void)
153 {
154 struct disk_cache *cache;
155 int err;
156
157 /* Before doing anything else, ensure that with
158 * MESA_GLSL_CACHE_DISABLE set, that disk_cache_create returns NULL.
159 */
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");
163
164 unsetenv("MESA_GLSL_CACHE_DISABLE");
165
166 /* For the first real disk_cache_create() clear these environment
167 * variables to test creation of cache in home directory.
168 */
169 unsetenv("MESA_GLSL_CACHE_DIR");
170 unsetenv("XDG_CACHE_HOME");
171
172 cache = disk_cache_create("test", "make_check");
173 expect_non_null(cache, "disk_cache_create with no environment variables");
174
175 disk_cache_destroy(cache);
176
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");
182
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");
187
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");
191
192 check_timestamp_and_gpu_id_directories_created(expected_dir_h);
193
194 disk_cache_destroy(cache);
195
196 /* Test with MESA_GLSL_CACHE_DIR set */
197 err = rmrf_local(CACHE_TEST_TMP);
198 expect_equal(err, 0, "Removing " CACHE_TEST_TMP);
199
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");
204
205 sprintf(expected_dir_h, "%s%s%s", CACHE_TEST_TMP
206 "/mesa-glsl-cache-dir/mesa/", get_arch_bitness_str(),
207 "/make_check/test");
208
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");
212
213 check_timestamp_and_gpu_id_directories_created(expected_dir_h);
214
215 disk_cache_destroy(cache);
216 }
217
218 static bool
219 does_cache_contain(struct disk_cache *cache, cache_key key)
220 {
221 void *result;
222
223 result = disk_cache_get(cache, key, NULL);
224
225 if (result) {
226 free(result);
227 return true;
228 }
229
230 return false;
231 }
232
233 static void
234 test_put_and_get(void)
235 {
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.
239 */
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];
245 char *result;
246 size_t size;
247 uint8_t *one_KB, *one_MB;
248 uint8_t one_KB_key[20], one_MB_key[20];
249 int count;
250
251 cache = disk_cache_create("test", "make_check");
252
253 _mesa_sha1_compute(blob, sizeof(blob), blob_key);
254
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)");
259
260 /* Simple test of put and get. */
261 disk_cache_put(cache, blob_key, blob, sizeof(blob));
262
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)");
266
267 free(result);
268
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));
272
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)");
276
277 free(result);
278
279 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
280 disk_cache_destroy(cache);
281
282 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
283 cache = disk_cache_create("test", "make_check");
284
285 one_KB = calloc(1, 1024);
286
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.
296 *
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).
300 *
301 * For this test, we force this signature to land in the same
302 * directory as the original blob first written to the cache.
303 */
304 _mesa_sha1_compute(one_KB, 1024, one_KB_key);
305 one_KB_key[0] = blob_key_byte_zero;
306
307 disk_cache_put(cache, one_KB_key, one_KB, 1024);
308
309 free(one_KB);
310
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)");
314
315 free(result);
316
317 /* Ensure eviction happened by checking that only one of the two
318 * previously-added items can still be fetched.
319 */
320 count = 0;
321 if (does_cache_contain(cache, blob_key))
322 count++;
323
324 if (does_cache_contain(cache, string_key))
325 count++;
326
327 expect_equal(count, 1, "disk_cache_put eviction with MAX_SIZE=1K");
328
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.
331 */
332 disk_cache_destroy(cache);
333
334 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
335 cache = disk_cache_create("test", "make_check");
336
337 disk_cache_put(cache, blob_key, blob, sizeof(blob));
338 disk_cache_put(cache, string_key, string, sizeof(string));
339
340 count = 0;
341 if (does_cache_contain(cache, blob_key))
342 count++;
343
344 if (does_cache_contain(cache, string_key))
345 count++;
346
347 if (does_cache_contain(cache, one_KB_key))
348 count++;
349
350 expect_equal(count, 3, "no eviction before overflow with MAX_SIZE=1M");
351
352 /* Finally, check eviction again after adding an object of size 1M. */
353 one_MB = calloc(1024, 1024);
354
355 _mesa_sha1_compute(one_MB, 1024 * 1024, one_MB_key);
356 one_MB_key[0] = blob_key_byte_zero;;
357
358 disk_cache_put(cache, one_MB_key, one_MB, 1024 * 1024);
359
360 free(one_MB);
361
362 count = 0;
363 if (does_cache_contain(cache, blob_key))
364 count++;
365
366 if (does_cache_contain(cache, string_key))
367 count++;
368
369 if (does_cache_contain(cache, one_KB_key))
370 count++;
371
372 expect_equal(count, 2, "eviction after overflow with MAX_SIZE=1M");
373
374 disk_cache_destroy(cache);
375 }
376
377 static void
378 test_put_key_and_get_key(void)
379 {
380 struct disk_cache *cache;
381 bool result;
382
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};
390
391 cache = disk_cache_create("test", "make_check");
392
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");
396
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");
401
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");
405
406 /* Test that a key with the same two bytes as an existing key
407 * forces an eviction.
408 */
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");
412
413 result = disk_cache_has_key(cache, key_a);
414 expect_equal(result, 0, "put_key of a colliding key evicts from the cache");
415
416 /* And finally test that we can re-add the original key to re-evict
417 * the colliding key.
418 */
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");
422
423 result = disk_cache_has_key(cache, key_a_collide);
424 expect_equal(result, 0, "put_key of orginal key evicts the colliding key");
425
426 disk_cache_destroy(cache);
427 }
428 #endif /* ENABLE_SHADER_CACHE */
429
430 int
431 main(void)
432 {
433 #ifdef ENABLE_SHADER_CACHE
434 int err;
435
436 test_disk_cache_create();
437
438 test_put_and_get();
439
440 test_put_key_and_get_key();
441
442 err = rmrf_local(CACHE_TEST_TMP);
443 expect_equal(err, 0, "Removing " CACHE_TEST_TMP " again");
444 #endif /* ENABLE_SHADER_CACHE */
445
446 return error ? 1 : 0;
447 }