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