util/disk_cache: add support for detecting corrupt cache entries
[mesa.git] / src / util / disk_cache.c
1 /*
2 * Copyright © 2014 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 #ifdef ENABLE_SHADER_CACHE
25
26 #include <ctype.h>
27 #include <ftw.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <sys/file.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <pwd.h>
38 #include <errno.h>
39 #include <dirent.h>
40
41 #include "util/crc32.h"
42 #include "util/u_atomic.h"
43 #include "util/mesa-sha1.h"
44 #include "util/ralloc.h"
45 #include "main/errors.h"
46
47 #include "disk_cache.h"
48
49 /* Number of bits to mask off from a cache key to get an index. */
50 #define CACHE_INDEX_KEY_BITS 16
51
52 /* Mask for computing an index from a key. */
53 #define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
54
55 /* The number of keys that can be stored in the index. */
56 #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
57
58 struct disk_cache {
59 /* The path to the cache directory. */
60 char *path;
61
62 /* A pointer to the mmapped index file within the cache directory. */
63 uint8_t *index_mmap;
64 size_t index_mmap_size;
65
66 /* Pointer to total size of all objects in cache (within index_mmap) */
67 uint64_t *size;
68
69 /* Pointer to stored keys, (within index_mmap). */
70 uint8_t *stored_keys;
71
72 /* Maximum size of all cached objects (in bytes). */
73 uint64_t max_size;
74 };
75
76 /* Create a directory named 'path' if it does not already exist.
77 *
78 * Returns: 0 if path already exists as a directory or if created.
79 * -1 in all other cases.
80 */
81 static int
82 mkdir_if_needed(char *path)
83 {
84 struct stat sb;
85
86 /* If the path exists already, then our work is done if it's a
87 * directory, but it's an error if it is not.
88 */
89 if (stat(path, &sb) == 0) {
90 if (S_ISDIR(sb.st_mode)) {
91 return 0;
92 } else {
93 fprintf(stderr, "Cannot use %s for shader cache (not a directory)"
94 "---disabling.\n", path);
95 return -1;
96 }
97 }
98
99 int ret = mkdir(path, 0755);
100 if (ret == 0 || (ret == -1 && errno == EEXIST))
101 return 0;
102
103 fprintf(stderr, "Failed to create %s for shader cache (%s)---disabling.\n",
104 path, strerror(errno));
105
106 return -1;
107 }
108
109 /* Concatenate an existing path and a new name to form a new path. If the new
110 * path does not exist as a directory, create it then return the resulting
111 * name of the new path (ralloc'ed off of 'ctx').
112 *
113 * Returns NULL on any error, such as:
114 *
115 * <path> does not exist or is not a directory
116 * <path>/<name> exists but is not a directory
117 * <path>/<name> cannot be created as a directory
118 */
119 static char *
120 concatenate_and_mkdir(void *ctx, char *path, const char *name)
121 {
122 char *new_path;
123 struct stat sb;
124
125 if (stat(path, &sb) != 0 || ! S_ISDIR(sb.st_mode))
126 return NULL;
127
128 new_path = ralloc_asprintf(ctx, "%s/%s", path, name);
129
130 if (mkdir_if_needed(new_path) == 0)
131 return new_path;
132 else
133 return NULL;
134 }
135
136 static int
137 remove_dir(const char *fpath, const struct stat *sb,
138 int typeflag, struct FTW *ftwbuf)
139 {
140 if (S_ISREG(sb->st_mode))
141 unlink(fpath);
142 else if (S_ISDIR(sb->st_mode))
143 rmdir(fpath);
144
145 return 0;
146 }
147
148 static void
149 remove_old_cache_directories(void *mem_ctx, char *path, const char *timestamp)
150 {
151 DIR *dir = opendir(path);
152
153 struct dirent* d_entry;
154 while((d_entry = readdir(dir)) != NULL)
155 {
156 char *full_path =
157 ralloc_asprintf(mem_ctx, "%s/%s", path, d_entry->d_name);
158
159 struct stat sb;
160 if (stat(full_path, &sb) == 0 && S_ISDIR(sb.st_mode) &&
161 strcmp(d_entry->d_name, timestamp) != 0 &&
162 strcmp(d_entry->d_name, "..") != 0 &&
163 strcmp(d_entry->d_name, ".") != 0) {
164 nftw(full_path, remove_dir, 20, FTW_DEPTH);
165 }
166 }
167
168 closedir(dir);
169 }
170
171 static char *
172 create_mesa_cache_dir(void *mem_ctx, char *path, const char *timestamp,
173 const char *gpu_name)
174 {
175 char *new_path = concatenate_and_mkdir(mem_ctx, path, "mesa");
176 if (new_path == NULL)
177 return NULL;
178
179 /* Remove cache directories for old Mesa versions */
180 remove_old_cache_directories(mem_ctx, new_path, timestamp);
181
182 new_path = concatenate_and_mkdir(mem_ctx, new_path, timestamp);
183 if (new_path == NULL)
184 return NULL;
185
186 new_path = concatenate_and_mkdir(mem_ctx, new_path, gpu_name);
187 if (new_path == NULL)
188 return NULL;
189
190 return new_path;
191 }
192
193 struct disk_cache *
194 disk_cache_create(const char *gpu_name, const char *timestamp)
195 {
196 void *local;
197 struct disk_cache *cache = NULL;
198 char *path, *max_size_str;
199 uint64_t max_size;
200 int fd = -1;
201 struct stat sb;
202 size_t size;
203
204 /* If running as a users other than the real user disable cache */
205 if (geteuid() != getuid())
206 return NULL;
207
208 /* A ralloc context for transient data during this invocation. */
209 local = ralloc_context(NULL);
210 if (local == NULL)
211 goto fail;
212
213 /* At user request, disable shader cache entirely. */
214 if (getenv("MESA_GLSL_CACHE_DISABLE"))
215 goto fail;
216
217 /* As a temporary measure, (while the shader cache is under
218 * development, and known to not be fully functional), also require
219 * the MESA_GLSL_CACHE_ENABLE variable to be set.
220 */
221 if (!getenv("MESA_GLSL_CACHE_ENABLE"))
222 goto fail;
223
224 /* Determine path for cache based on the first defined name as follows:
225 *
226 * $MESA_GLSL_CACHE_DIR
227 * $XDG_CACHE_HOME/mesa
228 * <pwd.pw_dir>/.cache/mesa
229 */
230 path = getenv("MESA_GLSL_CACHE_DIR");
231 if (path) {
232 if (mkdir_if_needed(path) == -1)
233 goto fail;
234
235 path = create_mesa_cache_dir(local, path, timestamp,
236 gpu_name);
237 if (path == NULL)
238 goto fail;
239 }
240
241 if (path == NULL) {
242 char *xdg_cache_home = getenv("XDG_CACHE_HOME");
243
244 if (xdg_cache_home) {
245 if (mkdir_if_needed(xdg_cache_home) == -1)
246 goto fail;
247
248 path = create_mesa_cache_dir(local, xdg_cache_home, timestamp,
249 gpu_name);
250 if (path == NULL)
251 goto fail;
252 }
253 }
254
255 if (path == NULL) {
256 char *buf;
257 size_t buf_size;
258 struct passwd pwd, *result;
259
260 buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
261 if (buf_size == -1)
262 buf_size = 512;
263
264 /* Loop until buf_size is large enough to query the directory */
265 while (1) {
266 buf = ralloc_size(local, buf_size);
267
268 getpwuid_r(getuid(), &pwd, buf, buf_size, &result);
269 if (result)
270 break;
271
272 if (errno == ERANGE) {
273 ralloc_free(buf);
274 buf = NULL;
275 buf_size *= 2;
276 } else {
277 goto fail;
278 }
279 }
280
281 path = concatenate_and_mkdir(local, pwd.pw_dir, ".cache");
282 if (path == NULL)
283 goto fail;
284
285 path = create_mesa_cache_dir(local, path, timestamp, gpu_name);
286 if (path == NULL)
287 goto fail;
288 }
289
290 cache = ralloc(NULL, struct disk_cache);
291 if (cache == NULL)
292 goto fail;
293
294 cache->path = ralloc_strdup(cache, path);
295 if (cache->path == NULL)
296 goto fail;
297
298 path = ralloc_asprintf(local, "%s/index", cache->path);
299 if (path == NULL)
300 goto fail;
301
302 fd = open(path, O_RDWR | O_CREAT | O_CLOEXEC, 0644);
303 if (fd == -1)
304 goto fail;
305
306 if (fstat(fd, &sb) == -1)
307 goto fail;
308
309 /* Force the index file to be the expected size. */
310 size = sizeof(*cache->size) + CACHE_INDEX_MAX_KEYS * CACHE_KEY_SIZE;
311 if (sb.st_size != size) {
312 if (ftruncate(fd, size) == -1)
313 goto fail;
314 }
315
316 /* We map this shared so that other processes see updates that we
317 * make.
318 *
319 * Note: We do use atomic addition to ensure that multiple
320 * processes don't scramble the cache size recorded in the
321 * index. But we don't use any locking to prevent multiple
322 * processes from updating the same entry simultaneously. The idea
323 * is that if either result lands entirely in the index, then
324 * that's equivalent to a well-ordered write followed by an
325 * eviction and a write. On the other hand, if the simultaneous
326 * writes result in a corrupt entry, that's not really any
327 * different than both entries being evicted, (since within the
328 * guarantees of the cryptographic hash, a corrupt entry is
329 * unlikely to ever match a real cache key).
330 */
331 cache->index_mmap = mmap(NULL, size, PROT_READ | PROT_WRITE,
332 MAP_SHARED, fd, 0);
333 if (cache->index_mmap == MAP_FAILED)
334 goto fail;
335 cache->index_mmap_size = size;
336
337 close(fd);
338
339 cache->size = (uint64_t *) cache->index_mmap;
340 cache->stored_keys = cache->index_mmap + sizeof(uint64_t);
341
342 max_size = 0;
343
344 max_size_str = getenv("MESA_GLSL_CACHE_MAX_SIZE");
345 if (max_size_str) {
346 char *end;
347 max_size = strtoul(max_size_str, &end, 10);
348 if (end == max_size_str) {
349 max_size = 0;
350 } else {
351 switch (*end) {
352 case 'K':
353 case 'k':
354 max_size *= 1024;
355 break;
356 case 'M':
357 case 'm':
358 max_size *= 1024*1024;
359 break;
360 case '\0':
361 case 'G':
362 case 'g':
363 default:
364 max_size *= 1024*1024*1024;
365 break;
366 }
367 }
368 }
369
370 /* Default to 1GB for maximum cache size. */
371 if (max_size == 0)
372 max_size = 1024*1024*1024;
373
374 cache->max_size = max_size;
375
376 ralloc_free(local);
377
378 return cache;
379
380 fail:
381 if (fd != -1)
382 close(fd);
383 if (cache)
384 ralloc_free(cache);
385 ralloc_free(local);
386
387 return NULL;
388 }
389
390 void
391 disk_cache_destroy(struct disk_cache *cache)
392 {
393 if (cache)
394 munmap(cache->index_mmap, cache->index_mmap_size);
395
396 ralloc_free(cache);
397 }
398
399 /* Return a filename within the cache's directory corresponding to 'key'. The
400 * returned filename is ralloced with 'cache' as the parent context.
401 *
402 * Returns NULL if out of memory.
403 */
404 static char *
405 get_cache_file(struct disk_cache *cache, cache_key key)
406 {
407 char buf[41];
408 char *filename;
409
410 _mesa_sha1_format(buf, key);
411 if (asprintf(&filename, "%s/%c%c/%s", cache->path, buf[0],
412 buf[1], buf + 2) == -1)
413 return NULL;
414
415 return filename;
416 }
417
418 /* Create the directory that will be needed for the cache file for \key.
419 *
420 * Obviously, the implementation here must closely match
421 * _get_cache_file above.
422 */
423 static void
424 make_cache_file_directory(struct disk_cache *cache, cache_key key)
425 {
426 char *dir;
427 char buf[41];
428
429 _mesa_sha1_format(buf, key);
430 if (asprintf(&dir, "%s/%c%c", cache->path, buf[0], buf[1]) == -1)
431 return;
432
433 mkdir_if_needed(dir);
434 free(dir);
435 }
436
437 /* Given a directory path and predicate function, count all entries in
438 * that directory for which the predicate returns true. Then choose a
439 * random entry from among those counted.
440 *
441 * Returns: A malloc'ed string for the path to the chosen file, (or
442 * NULL on any error). The caller should free the string when
443 * finished.
444 */
445 static char *
446 choose_random_file_matching(const char *dir_path,
447 bool (*predicate)(struct dirent *,
448 const char *dir_path))
449 {
450 DIR *dir;
451 struct dirent *entry;
452 unsigned int count, victim;
453 char *filename;
454
455 dir = opendir(dir_path);
456 if (dir == NULL)
457 return NULL;
458
459 count = 0;
460
461 while (1) {
462 entry = readdir(dir);
463 if (entry == NULL)
464 break;
465 if (!predicate(entry, dir_path))
466 continue;
467
468 count++;
469 }
470
471 if (count == 0) {
472 closedir(dir);
473 return NULL;
474 }
475
476 victim = rand() % count;
477
478 rewinddir(dir);
479 count = 0;
480
481 while (1) {
482 entry = readdir(dir);
483 if (entry == NULL)
484 break;
485 if (!predicate(entry, dir_path))
486 continue;
487 if (count == victim)
488 break;
489
490 count++;
491 }
492
493 if (entry == NULL) {
494 closedir(dir);
495 return NULL;
496 }
497
498 if (asprintf(&filename, "%s/%s", dir_path, entry->d_name) < 0)
499 filename = NULL;
500
501 closedir(dir);
502
503 return filename;
504 }
505
506 /* Is entry a regular file, and not having a name with a trailing
507 * ".tmp"
508 */
509 static bool
510 is_regular_non_tmp_file(struct dirent *entry, const char *path)
511 {
512 char *filename;
513 if (asprintf(&filename, "%s/%s", path, entry->d_name) == -1)
514 return false;
515
516 struct stat sb;
517 int res = stat(filename, &sb);
518 free(filename);
519
520 if (res == -1 || !S_ISREG(sb.st_mode))
521 return false;
522
523 size_t len = strlen (entry->d_name);
524 if (len >= 4 && strcmp(&entry->d_name[len-4], ".tmp") == 0)
525 return false;
526
527 return true;
528 }
529
530 /* Returns the size of the deleted file, (or 0 on any error). */
531 static size_t
532 unlink_random_file_from_directory(const char *path)
533 {
534 struct stat sb;
535 char *filename;
536
537 filename = choose_random_file_matching(path, is_regular_non_tmp_file);
538 if (filename == NULL)
539 return 0;
540
541 if (stat(filename, &sb) == -1) {
542 free (filename);
543 return 0;
544 }
545
546 unlink(filename);
547
548 free (filename);
549
550 return sb.st_size;
551 }
552
553 /* Is entry a directory with a two-character name, (and not the
554 * special name of "..")
555 */
556 static bool
557 is_two_character_sub_directory(struct dirent *entry, const char *path)
558 {
559 char *subdir;
560 if (asprintf(&subdir, "%s/%s", path, entry->d_name) == -1)
561 return false;
562
563 struct stat sb;
564 int res = stat(subdir, &sb);
565 free(subdir);
566
567 if (res == -1 || !S_ISDIR(sb.st_mode))
568 return false;
569
570 if (strlen(entry->d_name) != 2)
571 return false;
572
573 if (strcmp(entry->d_name, "..") == 0)
574 return false;
575
576 return true;
577 }
578
579 static void
580 evict_random_item(struct disk_cache *cache)
581 {
582 const char hex[] = "0123456789abcde";
583 char *dir_path;
584 int a, b;
585 size_t size;
586
587 /* With a reasonably-sized, full cache, (and with keys generated
588 * from a cryptographic hash), we can choose two random hex digits
589 * and reasonably expect the directory to exist with a file in it.
590 */
591 a = rand() % 16;
592 b = rand() % 16;
593
594 if (asprintf(&dir_path, "%s/%c%c", cache->path, hex[a], hex[b]) < 0)
595 return;
596
597 size = unlink_random_file_from_directory(dir_path);
598
599 free(dir_path);
600
601 if (size) {
602 p_atomic_add(cache->size, - size);
603 return;
604 }
605
606 /* In the case where the random choice of directory didn't find
607 * something, we choose randomly from the existing directories.
608 *
609 * Really, the only reason this code exists is to allow the unit
610 * tests to work, (which use an artificially-small cache to be able
611 * to force a single cached item to be evicted).
612 */
613 dir_path = choose_random_file_matching(cache->path,
614 is_two_character_sub_directory);
615 if (dir_path == NULL)
616 return;
617
618 size = unlink_random_file_from_directory(dir_path);
619
620 free(dir_path);
621
622 if (size)
623 p_atomic_add(cache->size, - size);
624 }
625
626 void
627 disk_cache_remove(struct disk_cache *cache, cache_key key)
628 {
629 struct stat sb;
630
631 char *filename = get_cache_file(cache, key);
632 if (filename == NULL) {
633 return;
634 }
635
636 if (stat(filename, &sb) == -1) {
637 free(filename);
638 return;
639 }
640
641 unlink(filename);
642 free(filename);
643
644 if (sb.st_size)
645 p_atomic_add(cache->size, - sb.st_size);
646 }
647
648 void
649 disk_cache_put(struct disk_cache *cache,
650 cache_key key,
651 const void *data,
652 size_t size)
653 {
654 int fd = -1, fd_final = -1, err, ret;
655 size_t len;
656 char *filename = NULL, *filename_tmp = NULL;
657 const char *p = data;
658
659 filename = get_cache_file(cache, key);
660 if (filename == NULL)
661 goto done;
662
663 /* Write to a temporary file to allow for an atomic rename to the
664 * final destination filename, (to prevent any readers from seeing
665 * a partially written file).
666 */
667 if (asprintf(&filename_tmp, "%s.tmp", filename) == -1)
668 goto done;
669
670 fd = open(filename_tmp, O_WRONLY | O_CLOEXEC | O_CREAT, 0644);
671
672 /* Make the two-character subdirectory within the cache as needed. */
673 if (fd == -1) {
674 if (errno != ENOENT)
675 goto done;
676
677 make_cache_file_directory(cache, key);
678
679 fd = open(filename_tmp, O_WRONLY | O_CLOEXEC | O_CREAT, 0644);
680 if (fd == -1)
681 goto done;
682 }
683
684 /* With the temporary file open, we take an exclusive flock on
685 * it. If the flock fails, then another process still has the file
686 * open with the flock held. So just let that file be responsible
687 * for writing the file.
688 */
689 err = flock(fd, LOCK_EX | LOCK_NB);
690 if (err == -1)
691 goto done;
692
693 /* Now that we have the lock on the open temporary file, we can
694 * check to see if the destination file already exists. If so,
695 * another process won the race between when we saw that the file
696 * didn't exist and now. In this case, we don't do anything more,
697 * (to ensure the size accounting of the cache doesn't get off).
698 */
699 fd_final = open(filename, O_RDONLY | O_CLOEXEC);
700 if (fd_final != -1)
701 goto done;
702
703 /* OK, we're now on the hook to write out a file that we know is
704 * not in the cache, and is also not being written out to the cache
705 * by some other process.
706 *
707 * Before we do that, if the cache is too large, evict something
708 * else first.
709 */
710 if (*cache->size + size > cache->max_size)
711 evict_random_item(cache);
712
713 /* Create CRC of the data and store at the start of the file. We will
714 * read this when restoring the cache and use it to check for corruption.
715 */
716 uint32_t crc32 = util_hash_crc32(data, size);
717 size_t crc_size = sizeof(crc32);
718 for (len = 0; len < crc_size; len += ret) {
719 ret = write(fd, ((uint8_t *) &crc32) + len, crc_size - len);
720 if (ret == -1) {
721 unlink(filename_tmp);
722 goto done;
723 }
724 }
725
726 /* Now, finally, write out the contents to the temporary file, then
727 * rename them atomically to the destination filename, and also
728 * perform an atomic increment of the total cache size.
729 */
730 for (len = 0; len < size; len += ret) {
731 ret = write(fd, p + len, size - len);
732 if (ret == -1) {
733 unlink(filename_tmp);
734 goto done;
735 }
736 }
737
738 rename(filename_tmp, filename);
739
740 size += crc_size;
741 p_atomic_add(cache->size, size);
742
743 done:
744 if (fd_final != -1)
745 close(fd_final);
746 /* This close finally releases the flock, (now that the final dile
747 * has been renamed into place and the size has been added).
748 */
749 if (fd != -1)
750 close(fd);
751 if (filename_tmp)
752 free(filename_tmp);
753 if (filename)
754 free(filename);
755 }
756
757 void *
758 disk_cache_get(struct disk_cache *cache, cache_key key, size_t *size)
759 {
760 int fd = -1, ret, len;
761 struct stat sb;
762 char *filename = NULL;
763 uint8_t *data = NULL;
764
765 if (size)
766 *size = 0;
767
768 filename = get_cache_file(cache, key);
769 if (filename == NULL)
770 goto fail;
771
772 fd = open(filename, O_RDONLY | O_CLOEXEC);
773 if (fd == -1)
774 goto fail;
775
776 if (fstat(fd, &sb) == -1)
777 goto fail;
778
779 data = malloc(sb.st_size);
780 if (data == NULL)
781 goto fail;
782
783 /* Load the CRC that was created when the file was written. */
784 uint32_t crc32;
785 size_t crc_size = sizeof(crc32);
786 assert(sb.st_size > crc_size);
787 for (len = 0; len < crc_size; len += ret) {
788 ret = read(fd, ((uint8_t *) &crc32) + len, crc_size - len);
789 if (ret == -1)
790 goto fail;
791 }
792
793 /* Load the actual cache data. */
794 size_t cache_data_size = sb.st_size - crc_size;
795 for (len = 0; len < cache_data_size; len += ret) {
796 ret = read(fd, data + len, cache_data_size - len);
797 if (ret == -1)
798 goto fail;
799 }
800
801 /* Check the data for corruption */
802 if (crc32 != util_hash_crc32(data, cache_data_size))
803 goto fail;
804
805 free(filename);
806 close(fd);
807
808 if (size)
809 *size = cache_data_size;
810
811 return data;
812
813 fail:
814 if (data)
815 free(data);
816 if (filename)
817 free(filename);
818 if (fd != -1)
819 close(fd);
820
821 return NULL;
822 }
823
824 void
825 disk_cache_put_key(struct disk_cache *cache, cache_key key)
826 {
827 uint32_t *key_chunk = (uint32_t *) key;
828 int i = *key_chunk & CACHE_INDEX_KEY_MASK;
829 unsigned char *entry;
830
831 entry = &cache->stored_keys[i + CACHE_KEY_SIZE];
832
833 memcpy(entry, key, CACHE_KEY_SIZE);
834 }
835
836 /* This function lets us test whether a given key was previously
837 * stored in the cache with disk_cache_put_key(). The implement is
838 * efficient by not using syscalls or hitting the disk. It's not
839 * race-free, but the races are benign. If we race with someone else
840 * calling disk_cache_put_key, then that's just an extra cache miss and an
841 * extra recompile.
842 */
843 bool
844 disk_cache_has_key(struct disk_cache *cache, cache_key key)
845 {
846 uint32_t *key_chunk = (uint32_t *) key;
847 int i = *key_chunk & CACHE_INDEX_KEY_MASK;
848 unsigned char *entry;
849
850 entry = &cache->stored_keys[i + CACHE_KEY_SIZE];
851
852 return memcmp(entry, key, CACHE_KEY_SIZE) == 0;
853 }
854
855 #endif /* ENABLE_SHADER_CACHE */