b604943feeb01ad80cfd2afa16767e3b0237477d
[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 <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
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
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
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
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
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
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
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 | FTW_MOUNT);
128 }
129
130 static void
131 check_directories_created(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 timestamp and gpu ip sub dirs");
148 }
149
150 #define CACHE_TEST_TMP "./cache-test-tmp"
151
152 static 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, that disk_cache_create returns NULL.
160 */
161 setenv("MESA_GLSL_CACHE_DISABLE", "1", 1);
162 cache = disk_cache_create("test", "make_check");
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");
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");
181 expect_null(cache, "disk_cache_create with XDG_CACHE_HOME set with"
182 "a non-existing parent directory");
183
184 /* Create string with expected directory hierarchy */
185 char expected_dir_h[255];
186 sprintf(expected_dir_h, "%s%s%s", CACHE_TEST_TMP "/xdg-cache-home/mesa/",
187 get_arch_bitness_str(), "/test");
188
189 mkdir(CACHE_TEST_TMP, 0755);
190 cache = disk_cache_create("test", "make_check");
191 expect_non_null(cache, "disk_cache_create with XDG_CACHE_HOME set");
192
193 check_directories_created(expected_dir_h);
194
195 disk_cache_destroy(cache);
196
197 /* Test with MESA_GLSL_CACHE_DIR set */
198 err = rmrf_local(CACHE_TEST_TMP);
199 expect_equal(err, 0, "Removing " CACHE_TEST_TMP);
200
201 setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP "/mesa-glsl-cache-dir", 1);
202 cache = disk_cache_create("test", "make_check");
203 expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set with"
204 "a non-existing parent directory");
205
206 sprintf(expected_dir_h, "%s%s%s", CACHE_TEST_TMP
207 "/mesa-glsl-cache-dir/mesa/", get_arch_bitness_str(),
208 "/test");
209
210 mkdir(CACHE_TEST_TMP, 0755);
211 cache = disk_cache_create("test", "make_check");
212 expect_non_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set");
213
214 check_directories_created(expected_dir_h);
215
216 disk_cache_destroy(cache);
217 }
218
219 static bool
220 does_cache_contain(struct disk_cache *cache, cache_key key)
221 {
222 void *result;
223
224 result = disk_cache_get(cache, key, NULL);
225
226 if (result) {
227 free(result);
228 return true;
229 }
230
231 return false;
232 }
233
234 static void
235 wait_until_file_written(struct disk_cache *cache, cache_key key)
236 {
237 struct timespec req;
238 struct timespec rem;
239
240 /* Set 100ms delay */
241 req.tv_sec = 0;
242 req.tv_nsec = 100000000;
243
244 unsigned retries = 0;
245 while (retries++ < 20) {
246 if (does_cache_contain(cache, key)) {
247 break;
248 }
249
250 nanosleep(&req, &rem);
251 }
252 }
253
254 static void
255 test_put_and_get(void)
256 {
257 struct disk_cache *cache;
258 char blob[] = "This is a blob of thirty-seven bytes";
259 uint8_t blob_key[20];
260 char string[] = "While this string has thirty-four";
261 uint8_t string_key[20];
262 char *result;
263 size_t size;
264 uint8_t *one_KB, *one_MB;
265 uint8_t one_KB_key[20], one_MB_key[20];
266 int count;
267
268 cache = disk_cache_create("test", "make_check");
269
270 disk_cache_compute_key(cache, blob, sizeof(blob), blob_key);
271
272 /* Ensure that disk_cache_get returns nothing before anything is added. */
273 result = disk_cache_get(cache, blob_key, &size);
274 expect_null(result, "disk_cache_get with non-existent item (pointer)");
275 expect_equal(size, 0, "disk_cache_get with non-existent item (size)");
276
277 /* Simple test of put and get. */
278 disk_cache_put(cache, blob_key, blob, sizeof(blob));
279
280 /* disk_cache_put() hands things off to a thread give it some time to
281 * finish.
282 */
283 wait_until_file_written(cache, blob_key);
284
285 result = disk_cache_get(cache, blob_key, &size);
286 expect_equal_str(blob, result, "disk_cache_get of existing item (pointer)");
287 expect_equal(size, sizeof(blob), "disk_cache_get of existing item (size)");
288
289 free(result);
290
291 /* Test put and get of a second item. */
292 disk_cache_compute_key(cache, string, sizeof(string), string_key);
293 disk_cache_put(cache, string_key, string, sizeof(string));
294
295 /* disk_cache_put() hands things off to a thread give it some time to
296 * finish.
297 */
298 wait_until_file_written(cache, string_key);
299
300 result = disk_cache_get(cache, string_key, &size);
301 expect_equal_str(result, string, "2nd disk_cache_get of existing item (pointer)");
302 expect_equal(size, sizeof(string), "2nd disk_cache_get of existing item (size)");
303
304 free(result);
305
306 /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
307 disk_cache_destroy(cache);
308
309 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
310 cache = disk_cache_create("test", "make_check");
311
312 one_KB = calloc(1, 1024);
313
314 /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
315 * interesting. But we do have want to take some special care with
316 * the hash we use here. The issue is that in this artificial case,
317 * (with only three files in the cache), the probability is good
318 * that each of the three files will end up in their own
319 * directory. Then, if the directory containing the .tmp file for
320 * the new item being added for disk_cache_put() is the chosen victim
321 * directory for eviction, then no suitable file will be found and
322 * nothing will be evicted.
323 *
324 * That's actually expected given how the eviction code is
325 * implemented, (which expects to only evict once things are more
326 * interestingly full than that).
327 *
328 * For this test, we force this signature to land in the same
329 * directory as the original blob first written to the cache.
330 */
331 disk_cache_compute_key(cache, one_KB, 1024, one_KB_key);
332 one_KB_key[0] = blob_key[0];
333
334 disk_cache_put(cache, one_KB_key, one_KB, 1024);
335
336 free(one_KB);
337
338 /* disk_cache_put() hands things off to a thread give it some time to
339 * finish.
340 */
341 wait_until_file_written(cache, one_KB_key);
342
343 result = disk_cache_get(cache, one_KB_key, &size);
344 expect_non_null(result, "3rd disk_cache_get of existing item (pointer)");
345 expect_equal(size, 1024, "3rd disk_cache_get of existing item (size)");
346
347 free(result);
348
349 /* Ensure eviction happened by checking that both of the previous
350 * cache itesm were evicted.
351 */
352 bool contains_1KB_file = false;
353 count = 0;
354 if (does_cache_contain(cache, blob_key))
355 count++;
356
357 if (does_cache_contain(cache, string_key))
358 count++;
359
360 if (does_cache_contain(cache, one_KB_key)) {
361 count++;
362 contains_1KB_file = true;
363 }
364
365 expect_true(contains_1KB_file,
366 "disk_cache_put eviction last file == MAX_SIZE (1KB)");
367 expect_equal(count, 1, "disk_cache_put eviction with MAX_SIZE=1K");
368
369 /* Now increase the size to 1M, add back both items, and ensure all
370 * three that have been added are available via disk_cache_get.
371 */
372 disk_cache_destroy(cache);
373
374 setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
375 cache = disk_cache_create("test", "make_check");
376
377 disk_cache_put(cache, blob_key, blob, sizeof(blob));
378 disk_cache_put(cache, string_key, string, sizeof(string));
379
380 /* disk_cache_put() hands things off to a thread give it some time to
381 * finish.
382 */
383 wait_until_file_written(cache, blob_key);
384 wait_until_file_written(cache, string_key);
385
386 count = 0;
387 if (does_cache_contain(cache, blob_key))
388 count++;
389
390 if (does_cache_contain(cache, string_key))
391 count++;
392
393 if (does_cache_contain(cache, one_KB_key))
394 count++;
395
396 expect_equal(count, 3, "no eviction before overflow with MAX_SIZE=1M");
397
398 /* Finally, check eviction again after adding an object of size 1M. */
399 one_MB = calloc(1024, 1024);
400
401 disk_cache_compute_key(cache, one_MB, 1024 * 1024, one_MB_key);
402 one_MB_key[0] = blob_key[0];
403
404 disk_cache_put(cache, one_MB_key, one_MB, 1024 * 1024);
405
406 free(one_MB);
407
408 /* disk_cache_put() hands things off to a thread give it some time to
409 * finish.
410 */
411 wait_until_file_written(cache, one_MB_key);
412
413 bool contains_1MB_file = false;
414 count = 0;
415 if (does_cache_contain(cache, blob_key))
416 count++;
417
418 if (does_cache_contain(cache, string_key))
419 count++;
420
421 if (does_cache_contain(cache, one_KB_key))
422 count++;
423
424 if (does_cache_contain(cache, one_MB_key)) {
425 count++;
426 contains_1MB_file = true;
427 }
428
429 expect_true(contains_1MB_file,
430 "disk_cache_put eviction last file == MAX_SIZE (1MB)");
431 expect_equal(count, 1, "eviction after overflow with MAX_SIZE=1M");
432
433 disk_cache_destroy(cache);
434 }
435
436 static void
437 test_put_key_and_get_key(void)
438 {
439 struct disk_cache *cache;
440 bool result;
441
442 uint8_t key_a[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
443 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
444 uint8_t key_b[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
445 30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
446 uint8_t key_a_collide[20] =
447 { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49,
448 50, 55, 52, 53, 54, 55, 56, 57, 58, 59};
449
450 cache = disk_cache_create("test", "make_check");
451
452 /* First test that disk_cache_has_key returns false before disk_cache_put_key */
453 result = disk_cache_has_key(cache, key_a);
454 expect_equal(result, 0, "disk_cache_has_key before key added");
455
456 /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
457 disk_cache_put_key(cache, key_a);
458 result = disk_cache_has_key(cache, key_a);
459 expect_equal(result, 1, "disk_cache_has_key after key added");
460
461 disk_cache_put_key(cache, key_b);
462 result = disk_cache_has_key(cache, key_b);
463 expect_equal(result, 1, "2nd disk_cache_has_key after key added");
464
465 /* Test that a key with the same two bytes as an existing key
466 * forces an eviction.
467 */
468 disk_cache_put_key(cache, key_a_collide);
469 result = disk_cache_has_key(cache, key_a_collide);
470 expect_equal(result, 1, "put_key of a colliding key lands in the cache");
471
472 result = disk_cache_has_key(cache, key_a);
473 expect_equal(result, 0, "put_key of a colliding key evicts from the cache");
474
475 /* And finally test that we can re-add the original key to re-evict
476 * the colliding key.
477 */
478 disk_cache_put_key(cache, key_a);
479 result = disk_cache_has_key(cache, key_a);
480 expect_equal(result, 1, "put_key of original key lands again");
481
482 result = disk_cache_has_key(cache, key_a_collide);
483 expect_equal(result, 0, "put_key of orginal key evicts the colliding key");
484
485 disk_cache_destroy(cache);
486 }
487 #endif /* ENABLE_SHADER_CACHE */
488
489 int
490 main(void)
491 {
492 #ifdef ENABLE_SHADER_CACHE
493 int err;
494
495 test_disk_cache_create();
496
497 test_put_and_get();
498
499 test_put_key_and_get_key();
500
501 err = rmrf_local(CACHE_TEST_TMP);
502 expect_equal(err, 0, "Removing " CACHE_TEST_TMP " again");
503 #endif /* ENABLE_SHADER_CACHE */
504
505 return error ? 1 : 0;
506 }