summaryrefslogtreecommitdiffstats
path: root/src/compiler/glsl/tests/cache_test.c
blob: b1b3c33535e750474861e38d138c3bcced9331e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
 * Copyright © 2015 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/* A collection of unit tests for cache.c */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ftw.h>
#include <errno.h>
#include <stdarg.h>
#include <inttypes.h>
#include <limits.h>
#include <time.h>
#include <unistd.h>

#include "util/mesa-sha1.h"
#include "util/disk_cache.h"

bool error = false;

#ifdef ENABLE_SHADER_CACHE

static void
expect_true(bool result, const char *test)
{
   if (!result) {
      fprintf(stderr, "Error: Test '%s' failed: Expected=true"
              ", Actual=false\n", test);
      error = true;
   }
}

static void
expect_equal(uint64_t actual, uint64_t expected, const char *test)
{
   if (actual != expected) {
      fprintf(stderr, "Error: Test '%s' failed: Expected=%" PRIu64
              ", Actual=%" PRIu64 "\n",
              test, expected, actual);
      error = true;
   }
}

static void
expect_null(void *ptr, const char *test)
{
   if (ptr != NULL) {
      fprintf(stderr, "Error: Test '%s' failed: Result=%p, but expected NULL.\n",
              test, ptr);
      error = true;
   }
}

static void
expect_non_null(void *ptr, const char *test)
{
   if (ptr == NULL) {
      fprintf(stderr, "Error: Test '%s' failed: Result=NULL, but expected something else.\n",
              test);
      error = true;
   }
}

static void
expect_equal_str(const char *actual, const char *expected, const char *test)
{
   if (strcmp(actual, expected)) {
      fprintf(stderr, "Error: Test '%s' failed:\n\t"
              "Expected=\"%s\", Actual=\"%s\"\n",
              test, expected, actual);
      error = true;
   }
}

/* Callback for nftw used in rmrf_local below.
 */
static int
remove_entry(const char *path,
             const struct stat *sb,
             int typeflag,
             struct FTW *ftwbuf)
{
   int err = remove(path);

   if (err)
      fprintf(stderr, "Error removing %s: %s\n", path, strerror(errno));

   return err;
}

/* Recursively remove a directory.
 *
 * This is equivalent to "rm -rf <dir>" with one bit of protection
 * that the directory name must begin with "." to ensure we don't
 * wander around deleting more than intended.
 *
 * Returns 0 on success, -1 on any error.
 */
static int
rmrf_local(const char *path)
{
   if (path == NULL || *path == '\0' || *path != '.')
      return -1;

   return nftw(path, remove_entry, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
}

static void
check_timestamp_and_gpu_id_directories_created(char *cache_dir)
{
   bool sub_dirs_created = false;

   char buf[PATH_MAX];
   if (getcwd(buf, PATH_MAX)) {
      char *full_path = NULL;
      if (asprintf(&full_path, "%s%s", buf, ++cache_dir) != -1 ) {
         struct stat sb;
         if (stat(full_path, &sb) != -1 && S_ISDIR(sb.st_mode))
            sub_dirs_created = true;

         free(full_path);
      }
   }

   expect_true(sub_dirs_created, "create timestamp and gpu ip sub dirs");
}

#define CACHE_TEST_TMP "./cache-test-tmp"

static void
test_disk_cache_create(void)
{
   struct disk_cache *cache;
   int err;

   /* Before doing anything else, ensure that with
    * MESA_GLSL_CACHE_DISABLE set, that disk_cache_create returns NULL.
    */
   setenv("MESA_GLSL_CACHE_DISABLE", "1", 1);
   cache = disk_cache_create("test", "make_check");
   expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DISABLE set");

   unsetenv("MESA_GLSL_CACHE_DISABLE");

   /* For the first real disk_cache_create() clear these environment
    * variables to test creation of cache in home directory.
    */
   unsetenv("MESA_GLSL_CACHE_DIR");
   unsetenv("XDG_CACHE_HOME");

   cache = disk_cache_create("test", "make_check");
   expect_non_null(cache, "disk_cache_create with no environment variables");

   disk_cache_destroy(cache);

   /* Test with XDG_CACHE_HOME set */
   setenv("XDG_CACHE_HOME", CACHE_TEST_TMP "/xdg-cache-home", 1);
   cache = disk_cache_create("test", "make_check");
   expect_null(cache, "disk_cache_create with XDG_CACHE_HOME set with"
               "a non-existing parent directory");

   /* Create string with expected directory hierarchy */
   char expected_dir_h[255];
   sprintf(expected_dir_h, "%s%s%s", CACHE_TEST_TMP "/xdg-cache-home/mesa/",
           get_arch_bitness_str(), "/make_check/test");

   mkdir(CACHE_TEST_TMP, 0755);
   cache = disk_cache_create("test", "make_check");
   expect_non_null(cache, "disk_cache_create with XDG_CACHE_HOME set");

   check_timestamp_and_gpu_id_directories_created(expected_dir_h);

   disk_cache_destroy(cache);

   /* Test with MESA_GLSL_CACHE_DIR set */
   err = rmrf_local(CACHE_TEST_TMP);
   expect_equal(err, 0, "Removing " CACHE_TEST_TMP);

   setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP "/mesa-glsl-cache-dir", 1);
   cache = disk_cache_create("test", "make_check");
   expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set with"
               "a non-existing parent directory");

   sprintf(expected_dir_h, "%s%s%s", CACHE_TEST_TMP
           "/mesa-glsl-cache-dir/mesa/", get_arch_bitness_str(),
           "/make_check/test");

   mkdir(CACHE_TEST_TMP, 0755);
   cache = disk_cache_create("test", "make_check");
   expect_non_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set");

   check_timestamp_and_gpu_id_directories_created(expected_dir_h);

   disk_cache_destroy(cache);
}

static bool
does_cache_contain(struct disk_cache *cache, cache_key key)
{
   void *result;

   result = disk_cache_get(cache, key, NULL);

   if (result) {
      free(result);
      return true;
   }

   return false;
}

static void
wait_until_file_written(struct disk_cache *cache, cache_key key)
{
   struct timespec req;
   struct timespec rem;

   /* Set 100ms delay */
   req.tv_sec = 0;
   req.tv_nsec = 100000000;

   unsigned retries = 0;
   while (retries++ < 20) {
      if (does_cache_contain(cache, key)) {
         break;
      }

      nanosleep(&req, &rem);
   }
}

static void
test_put_and_get(void)
{
   struct disk_cache *cache;
   char blob[] = "This is a blob of thirty-seven bytes";
   uint8_t blob_key[20];
   char string[] = "While this string has thirty-four";
   uint8_t string_key[20];
   char *result;
   size_t size;
   uint8_t *one_KB, *one_MB;
   uint8_t one_KB_key[20], one_MB_key[20];
   int count;

   cache = disk_cache_create("test", "make_check");

   disk_cache_compute_key(cache, blob, sizeof(blob), blob_key);

   /* Ensure that disk_cache_get returns nothing before anything is added. */
   result = disk_cache_get(cache, blob_key, &size);
   expect_null(result, "disk_cache_get with non-existent item (pointer)");
   expect_equal(size, 0, "disk_cache_get with non-existent item (size)");

   /* Simple test of put and get. */
   disk_cache_put(cache, blob_key, blob, sizeof(blob));

   /* disk_cache_put() hands things off to a thread give it some time to
    * finish.
    */
   wait_until_file_written(cache, blob_key);

   result = disk_cache_get(cache, blob_key, &size);
   expect_equal_str(blob, result, "disk_cache_get of existing item (pointer)");
   expect_equal(size, sizeof(blob), "disk_cache_get of existing item (size)");

   free(result);

   /* Test put and get of a second item. */
   disk_cache_compute_key(cache, string, sizeof(string), string_key);
   disk_cache_put(cache, string_key, string, sizeof(string));

   /* disk_cache_put() hands things off to a thread give it some time to
    * finish.
    */
   wait_until_file_written(cache, string_key);

   result = disk_cache_get(cache, string_key, &size);
   expect_equal_str(result, string, "2nd disk_cache_get of existing item (pointer)");
   expect_equal(size, sizeof(string), "2nd disk_cache_get of existing item (size)");

   free(result);

   /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
   disk_cache_destroy(cache);

   setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
   cache = disk_cache_create("test", "make_check");

   one_KB = calloc(1, 1024);

   /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly
    * interesting. But we do have want to take some special care with
    * the hash we use here. The issue is that in this artificial case,
    * (with only three files in the cache), the probability is good
    * that each of the three files will end up in their own
    * directory. Then, if the directory containing the .tmp file for
    * the new item being added for disk_cache_put() is the chosen victim
    * directory for eviction, then no suitable file will be found and
    * nothing will be evicted.
    *
    * That's actually expected given how the eviction code is
    * implemented, (which expects to only evict once things are more
    * interestingly full than that).
    *
    * For this test, we force this signature to land in the same
    * directory as the original blob first written to the cache.
    */
   disk_cache_compute_key(cache, one_KB, 1024, one_KB_key);
   one_KB_key[0] = blob_key[0];

   disk_cache_put(cache, one_KB_key, one_KB, 1024);

   free(one_KB);

   /* disk_cache_put() hands things off to a thread give it some time to
    * finish.
    */
   wait_until_file_written(cache, one_KB_key);

   result = disk_cache_get(cache, one_KB_key, &size);
   expect_non_null(result, "3rd disk_cache_get of existing item (pointer)");
   expect_equal(size, 1024, "3rd disk_cache_get of existing item (size)");

   free(result);

   /* Ensure eviction happened by checking that both of the previous
    * cache itesm were evicted.
    */
   bool contains_1KB_file = false;
   count = 0;
   if (does_cache_contain(cache, blob_key))
       count++;

   if (does_cache_contain(cache, string_key))
       count++;

   if (does_cache_contain(cache, one_KB_key)) {
      count++;
      contains_1KB_file = true;
   }

   expect_true(contains_1KB_file,
               "disk_cache_put eviction last file == MAX_SIZE (1KB)");
   expect_equal(count, 1, "disk_cache_put eviction with MAX_SIZE=1K");

   /* Now increase the size to 1M, add back both items, and ensure all
    * three that have been added are available via disk_cache_get.
    */
   disk_cache_destroy(cache);

   setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1);
   cache = disk_cache_create("test", "make_check");

   disk_cache_put(cache, blob_key, blob, sizeof(blob));
   disk_cache_put(cache, string_key, string, sizeof(string));

   /* disk_cache_put() hands things off to a thread give it some time to
    * finish.
    */
   wait_until_file_written(cache, blob_key);
   wait_until_file_written(cache, string_key);

   count = 0;
   if (does_cache_contain(cache, blob_key))
       count++;

   if (does_cache_contain(cache, string_key))
       count++;

   if (does_cache_contain(cache, one_KB_key))
       count++;

   expect_equal(count, 3, "no eviction before overflow with MAX_SIZE=1M");

   /* Finally, check eviction again after adding an object of size 1M. */
   one_MB = calloc(1024, 1024);

   disk_cache_compute_key(cache, one_MB, 1024 * 1024, one_MB_key);
   one_MB_key[0] = blob_key[0];

   disk_cache_put(cache, one_MB_key, one_MB, 1024 * 1024);

   free(one_MB);

   /* disk_cache_put() hands things off to a thread give it some time to
    * finish.
    */
   wait_until_file_written(cache, one_MB_key);

   bool contains_1MB_file = false;
   count = 0;
   if (does_cache_contain(cache, blob_key))
       count++;

   if (does_cache_contain(cache, string_key))
       count++;

   if (does_cache_contain(cache, one_KB_key))
       count++;

   if (does_cache_contain(cache, one_MB_key)) {
      count++;
      contains_1MB_file = true;
   }

   expect_true(contains_1MB_file,
               "disk_cache_put eviction last file == MAX_SIZE (1MB)");
   expect_equal(count, 1, "eviction after overflow with MAX_SIZE=1M");

   disk_cache_destroy(cache);
}

static void
test_put_key_and_get_key(void)
{
   struct disk_cache *cache;
   bool result;

   uint8_t key_a[20] = {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
                         10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
   uint8_t key_b[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
                         30, 33, 32, 33, 34, 35, 36, 37, 38, 39};
   uint8_t key_a_collide[20] =
                        { 0,  1, 42, 43, 44, 45, 46, 47, 48, 49,
                         50, 55, 52, 53, 54, 55, 56, 57, 58, 59};

   cache = disk_cache_create("test", "make_check");

   /* First test that disk_cache_has_key returns false before disk_cache_put_key */
   result = disk_cache_has_key(cache, key_a);
   expect_equal(result, 0, "disk_cache_has_key before key added");

   /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */
   disk_cache_put_key(cache, key_a);
   result = disk_cache_has_key(cache, key_a);
   expect_equal(result, 1, "disk_cache_has_key after key added");

   disk_cache_put_key(cache, key_b);
   result = disk_cache_has_key(cache, key_b);
   expect_equal(result, 1, "2nd disk_cache_has_key after key added");

   /* Test that a key with the same two bytes as an existing key
    * forces an eviction.
    */
   disk_cache_put_key(cache, key_a_collide);
   result = disk_cache_has_key(cache, key_a_collide);
   expect_equal(result, 1, "put_key of a colliding key lands in the cache");

   result = disk_cache_has_key(cache, key_a);
   expect_equal(result, 0, "put_key of a colliding key evicts from the cache");

   /* And finally test that we can re-add the original key to re-evict
    * the colliding key.
    */
   disk_cache_put_key(cache, key_a);
   result = disk_cache_has_key(cache, key_a);
   expect_equal(result, 1, "put_key of original key lands again");

   result = disk_cache_has_key(cache, key_a_collide);
   expect_equal(result, 0, "put_key of orginal key evicts the colliding key");

   disk_cache_destroy(cache);
}
#endif /* ENABLE_SHADER_CACHE */

int
main(void)
{
#ifdef ENABLE_SHADER_CACHE
   int err;

   test_disk_cache_create();

   test_put_and_get();

   test_put_key_and_get_key();

   err = rmrf_local(CACHE_TEST_TMP);
   expect_equal(err, 0, "Removing " CACHE_TEST_TMP " again");
#endif /* ENABLE_SHADER_CACHE */

   return error ? 1 : 0;
}