2 * Copyright © 2014 Intel Corporation
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:
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
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
24 #ifdef ENABLE_SHADER_CACHE
32 #include <sys/types.h>
34 #include <sys/statvfs.h>
43 #include "util/crc32.h"
44 #include "util/rand_xor.h"
45 #include "util/u_atomic.h"
46 #include "util/u_queue.h"
47 #include "util/mesa-sha1.h"
48 #include "util/ralloc.h"
49 #include "main/errors.h"
50 #include "util/macros.h"
52 #include "disk_cache.h"
54 /* Number of bits to mask off from a cache key to get an index. */
55 #define CACHE_INDEX_KEY_BITS 16
57 /* Mask for computing an index from a key. */
58 #define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
60 /* The number of keys that can be stored in the index. */
61 #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
64 /* The path to the cache directory. */
67 /* Thread queue for compressing and writing cache entries to disk */
68 struct util_queue cache_queue
;
70 /* Seed for rand, which is used to pick a random directory */
71 uint64_t seed_xorshift128plus
[2];
73 /* A pointer to the mmapped index file within the cache directory. */
75 size_t index_mmap_size
;
77 /* Pointer to total size of all objects in cache (within index_mmap) */
80 /* Pointer to stored keys, (within index_mmap). */
83 /* Maximum size of all cached objects (in bytes). */
86 /* Driver cache keys. */
87 uint8_t *driver_keys_blob
;
88 size_t driver_keys_blob_size
;
91 struct disk_cache_put_job
{
92 struct util_queue_fence fence
;
94 struct disk_cache
*cache
;
98 /* Copy of cache data to be compressed and written. */
101 /* Size of data to be compressed and written. */
105 /* Create a directory named 'path' if it does not already exist.
107 * Returns: 0 if path already exists as a directory or if created.
108 * -1 in all other cases.
111 mkdir_if_needed(const char *path
)
115 /* If the path exists already, then our work is done if it's a
116 * directory, but it's an error if it is not.
118 if (stat(path
, &sb
) == 0) {
119 if (S_ISDIR(sb
.st_mode
)) {
122 fprintf(stderr
, "Cannot use %s for shader cache (not a directory)"
123 "---disabling.\n", path
);
128 int ret
= mkdir(path
, 0755);
129 if (ret
== 0 || (ret
== -1 && errno
== EEXIST
))
132 fprintf(stderr
, "Failed to create %s for shader cache (%s)---disabling.\n",
133 path
, strerror(errno
));
138 /* Concatenate an existing path and a new name to form a new path. If the new
139 * path does not exist as a directory, create it then return the resulting
140 * name of the new path (ralloc'ed off of 'ctx').
142 * Returns NULL on any error, such as:
144 * <path> does not exist or is not a directory
145 * <path>/<name> exists but is not a directory
146 * <path>/<name> cannot be created as a directory
149 concatenate_and_mkdir(void *ctx
, const char *path
, const char *name
)
154 if (stat(path
, &sb
) != 0 || ! S_ISDIR(sb
.st_mode
))
157 new_path
= ralloc_asprintf(ctx
, "%s/%s", path
, name
);
159 if (mkdir_if_needed(new_path
) == 0)
166 disk_cache_create(const char *gpu_name
, const char *timestamp
)
169 struct disk_cache
*cache
= NULL
;
170 char *path
, *max_size_str
;
174 struct statvfs vfs
= { 0 };
177 /* If running as a users other than the real user disable cache */
178 if (geteuid() != getuid())
181 /* A ralloc context for transient data during this invocation. */
182 local
= ralloc_context(NULL
);
186 /* At user request, disable shader cache entirely. */
187 if (getenv("MESA_GLSL_CACHE_DISABLE"))
190 /* Determine path for cache based on the first defined name as follows:
192 * $MESA_GLSL_CACHE_DIR
193 * $XDG_CACHE_HOME/mesa
194 * <pwd.pw_dir>/.cache/mesa
196 path
= getenv("MESA_GLSL_CACHE_DIR");
198 if (mkdir_if_needed(path
) == -1)
201 path
= concatenate_and_mkdir(local
, path
, "mesa");
207 char *xdg_cache_home
= getenv("XDG_CACHE_HOME");
209 if (xdg_cache_home
) {
210 if (mkdir_if_needed(xdg_cache_home
) == -1)
213 path
= concatenate_and_mkdir(local
, xdg_cache_home
, "mesa");
222 struct passwd pwd
, *result
;
224 buf_size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
228 /* Loop until buf_size is large enough to query the directory */
230 buf
= ralloc_size(local
, buf_size
);
232 getpwuid_r(getuid(), &pwd
, buf
, buf_size
, &result
);
236 if (errno
== ERANGE
) {
245 path
= concatenate_and_mkdir(local
, pwd
.pw_dir
, ".cache");
249 path
= concatenate_and_mkdir(local
, path
, "mesa");
254 cache
= ralloc(NULL
, struct disk_cache
);
258 cache
->path
= ralloc_strdup(cache
, path
);
259 if (cache
->path
== NULL
)
262 path
= ralloc_asprintf(local
, "%s/index", cache
->path
);
266 fd
= open(path
, O_RDWR
| O_CREAT
| O_CLOEXEC
, 0644);
270 if (fstat(fd
, &sb
) == -1)
273 /* Force the index file to be the expected size. */
274 size
= sizeof(*cache
->size
) + CACHE_INDEX_MAX_KEYS
* CACHE_KEY_SIZE
;
275 if (sb
.st_size
!= size
) {
276 if (ftruncate(fd
, size
) == -1)
280 /* We map this shared so that other processes see updates that we
283 * Note: We do use atomic addition to ensure that multiple
284 * processes don't scramble the cache size recorded in the
285 * index. But we don't use any locking to prevent multiple
286 * processes from updating the same entry simultaneously. The idea
287 * is that if either result lands entirely in the index, then
288 * that's equivalent to a well-ordered write followed by an
289 * eviction and a write. On the other hand, if the simultaneous
290 * writes result in a corrupt entry, that's not really any
291 * different than both entries being evicted, (since within the
292 * guarantees of the cryptographic hash, a corrupt entry is
293 * unlikely to ever match a real cache key).
295 cache
->index_mmap
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
297 if (cache
->index_mmap
== MAP_FAILED
)
299 cache
->index_mmap_size
= size
;
303 cache
->size
= (uint64_t *) cache
->index_mmap
;
304 cache
->stored_keys
= cache
->index_mmap
+ sizeof(uint64_t);
308 max_size_str
= getenv("MESA_GLSL_CACHE_MAX_SIZE");
311 max_size
= strtoul(max_size_str
, &end
, 10);
312 if (end
== max_size_str
) {
322 max_size
*= 1024*1024;
328 max_size
*= 1024*1024*1024;
334 /* Default to 1GB or 10% of filesystem for maximum cache size. */
337 max_size
= MAX2(1024*1024*1024, vfs
.f_blocks
* vfs
.f_bsize
/ 10);
340 cache
->max_size
= max_size
;
342 /* A limit of 32 jobs was choosen as observations of Deus Ex start-up times
343 * showed that we reached at most 11 jobs on an Intel i5-6400 CPU@2.70GHz
344 * (a fairly modest desktop CPU). 1 thread was chosen because we don't
345 * really care about getting things to disk quickly just that it's not
346 * blocking other tasks.
348 util_queue_init(&cache
->cache_queue
, "disk_cache", 32, 1);
350 /* Create driver id keys */
351 size_t ts_size
= strlen(timestamp
) + 1;
352 size_t gpu_name_size
= strlen(gpu_name
) + 1;
353 cache
->driver_keys_blob_size
= ts_size
;
354 cache
->driver_keys_blob_size
+= gpu_name_size
;
356 /* We sometimes store entire structs that contains a pointers in the cache,
357 * use pointer size as a key to avoid hard to debug issues.
359 uint8_t ptr_size
= sizeof(void *);
360 size_t ptr_size_size
= sizeof(ptr_size
);
361 cache
->driver_keys_blob_size
+= ptr_size_size
;
363 cache
->driver_keys_blob
=
364 ralloc_size(cache
, cache
->driver_keys_blob_size
);
365 if (!cache
->driver_keys_blob
)
368 memcpy(cache
->driver_keys_blob
, timestamp
, ts_size
);
369 memcpy(cache
->driver_keys_blob
+ ts_size
, gpu_name
, gpu_name_size
);
370 memcpy(cache
->driver_keys_blob
+ ts_size
+ gpu_name_size
, &ptr_size
,
373 /* Seed our rand function */
374 s_rand_xorshift128plus(cache
->seed_xorshift128plus
, true);
391 disk_cache_destroy(struct disk_cache
*cache
)
394 util_queue_destroy(&cache
->cache_queue
);
395 munmap(cache
->index_mmap
, cache
->index_mmap_size
);
401 /* Return a filename within the cache's directory corresponding to 'key'. The
402 * returned filename is ralloced with 'cache' as the parent context.
404 * Returns NULL if out of memory.
407 get_cache_file(struct disk_cache
*cache
, const cache_key key
)
412 _mesa_sha1_format(buf
, key
);
413 if (asprintf(&filename
, "%s/%c%c/%s", cache
->path
, buf
[0],
414 buf
[1], buf
+ 2) == -1)
420 /* Create the directory that will be needed for the cache file for \key.
422 * Obviously, the implementation here must closely match
423 * _get_cache_file above.
426 make_cache_file_directory(struct disk_cache
*cache
, const cache_key key
)
431 _mesa_sha1_format(buf
, key
);
432 if (asprintf(&dir
, "%s/%c%c", cache
->path
, buf
[0], buf
[1]) == -1)
435 mkdir_if_needed(dir
);
439 /* Given a directory path and predicate function, find the entry with
440 * the oldest access time in that directory for which the predicate
443 * Returns: A malloc'ed string for the path to the chosen file, (or
444 * NULL on any error). The caller should free the string when
448 choose_lru_file_matching(const char *dir_path
,
449 bool (*predicate
)(const char *dir_path
,
451 const char *, const size_t))
454 struct dirent
*entry
;
456 char *lru_name
= NULL
;
457 time_t lru_atime
= 0;
459 dir
= opendir(dir_path
);
464 entry
= readdir(dir
);
469 if (fstatat(dirfd(dir
), entry
->d_name
, &sb
, 0) == 0) {
470 if (!lru_atime
|| (sb
.st_atime
< lru_atime
)) {
471 size_t len
= strlen(entry
->d_name
);
473 if (!predicate(dir_path
, &sb
, entry
->d_name
, len
))
476 char *tmp
= realloc(lru_name
, len
+ 1);
479 memcpy(lru_name
, entry
->d_name
, len
+ 1);
480 lru_atime
= sb
.st_atime
;
486 if (lru_name
== NULL
) {
491 if (asprintf(&filename
, "%s/%s", dir_path
, lru_name
) < 0)
500 /* Is entry a regular file, and not having a name with a trailing
504 is_regular_non_tmp_file(const char *path
, const struct stat
*sb
,
505 const char *d_name
, const size_t len
)
507 if (!S_ISREG(sb
->st_mode
))
510 if (len
>= 4 && strcmp(&d_name
[len
-4], ".tmp") == 0)
516 /* Returns the size of the deleted file, (or 0 on any error). */
518 unlink_lru_file_from_directory(const char *path
)
523 filename
= choose_lru_file_matching(path
, is_regular_non_tmp_file
);
524 if (filename
== NULL
)
527 if (stat(filename
, &sb
) == -1) {
538 /* Is entry a directory with a two-character name, (and not the
539 * special name of ".."). We also return false if the dir is empty.
542 is_two_character_sub_directory(const char *path
, const struct stat
*sb
,
543 const char *d_name
, const size_t len
)
545 if (!S_ISDIR(sb
->st_mode
))
551 if (strcmp(d_name
, "..") == 0)
555 if (asprintf(&subdir
, "%s/%s", path
, d_name
) == -1)
557 DIR *dir
= opendir(subdir
);
563 unsigned subdir_entries
= 0;
565 while ((d
= readdir(dir
)) != NULL
) {
566 if(++subdir_entries
> 2)
571 /* If dir only contains '.' and '..' it must be empty */
572 if (subdir_entries
<= 2)
579 evict_lru_item(struct disk_cache
*cache
)
583 /* With a reasonably-sized, full cache, (and with keys generated
584 * from a cryptographic hash), we can choose two random hex digits
585 * and reasonably expect the directory to exist with a file in it.
586 * Provides pseudo-LRU eviction to reduce checking all cache files.
588 uint64_t rand64
= rand_xorshift128plus(cache
->seed_xorshift128plus
);
589 if (asprintf(&dir_path
, "%s/%02" PRIx64
, cache
->path
, rand64
& 0xff) < 0)
592 size_t size
= unlink_lru_file_from_directory(dir_path
);
597 p_atomic_add(cache
->size
, - (uint64_t)size
);
601 /* In the case where the random choice of directory didn't find
602 * something, we choose the least recently accessed from the
603 * existing directories.
605 * Really, the only reason this code exists is to allow the unit
606 * tests to work, (which use an artificially-small cache to be able
607 * to force a single cached item to be evicted).
609 dir_path
= choose_lru_file_matching(cache
->path
,
610 is_two_character_sub_directory
);
611 if (dir_path
== NULL
)
614 size
= unlink_lru_file_from_directory(dir_path
);
619 p_atomic_add(cache
->size
, - (uint64_t)size
);
623 disk_cache_remove(struct disk_cache
*cache
, const cache_key key
)
627 char *filename
= get_cache_file(cache
, key
);
628 if (filename
== NULL
) {
632 if (stat(filename
, &sb
) == -1) {
641 p_atomic_add(cache
->size
, - (uint64_t)sb
.st_size
);
645 write_all(int fd
, const void *buf
, size_t count
)
647 const char *out
= buf
;
651 for (done
= 0; done
< count
; done
+= written
) {
652 written
= write(fd
, out
+ done
, count
- done
);
659 /* From the zlib docs:
660 * "If the memory is available, buffers sizes on the order of 128K or 256K
661 * bytes should be used."
663 #define BUFSIZE 256 * 1024
666 * Compresses cache entry in memory and writes it to disk. Returns the size
667 * of the data written to disk.
670 deflate_and_write_to_disk(const void *in_data
, size_t in_data_size
, int dest
,
671 const char *filename
)
673 unsigned char out
[BUFSIZE
];
675 /* allocate deflate state */
677 strm
.zalloc
= Z_NULL
;
679 strm
.opaque
= Z_NULL
;
680 strm
.next_in
= (uint8_t *) in_data
;
681 strm
.avail_in
= in_data_size
;
683 int ret
= deflateInit(&strm
, Z_BEST_COMPRESSION
);
687 /* compress until end of in_data */
688 size_t compressed_size
= 0;
691 int remaining
= in_data_size
- BUFSIZE
;
692 flush
= remaining
> 0 ? Z_NO_FLUSH
: Z_FINISH
;
693 in_data_size
-= BUFSIZE
;
695 /* Run deflate() on input until the output buffer is not full (which
696 * means there is no more data to deflate).
699 strm
.avail_out
= BUFSIZE
;
702 ret
= deflate(&strm
, flush
); /* no bad return value */
703 assert(ret
!= Z_STREAM_ERROR
); /* state not clobbered */
705 size_t have
= BUFSIZE
- strm
.avail_out
;
706 compressed_size
+= have
;
708 ssize_t written
= write_all(dest
, out
, have
);
710 (void)deflateEnd(&strm
);
713 } while (strm
.avail_out
== 0);
715 /* all input should be used */
716 assert(strm
.avail_in
== 0);
718 } while (flush
!= Z_FINISH
);
720 /* stream should be complete */
721 assert(ret
== Z_STREAM_END
);
723 /* clean up and return */
724 (void)deflateEnd(&strm
);
725 return compressed_size
;
728 static struct disk_cache_put_job
*
729 create_put_job(struct disk_cache
*cache
, const cache_key key
,
730 const void *data
, size_t size
)
732 struct disk_cache_put_job
*dc_job
= (struct disk_cache_put_job
*)
733 malloc(sizeof(struct disk_cache_put_job
) + size
);
736 dc_job
->cache
= cache
;
737 memcpy(dc_job
->key
, key
, sizeof(cache_key
));
738 dc_job
->data
= dc_job
+ 1;
739 memcpy(dc_job
->data
, data
, size
);
747 destroy_put_job(void *job
, int thread_index
)
754 struct cache_entry_file_data
{
756 uint32_t uncompressed_size
;
760 cache_put(void *job
, int thread_index
)
764 int fd
= -1, fd_final
= -1, err
, ret
;
766 char *filename
= NULL
, *filename_tmp
= NULL
;
767 struct disk_cache_put_job
*dc_job
= (struct disk_cache_put_job
*) job
;
769 filename
= get_cache_file(dc_job
->cache
, dc_job
->key
);
770 if (filename
== NULL
)
773 /* If the cache is too large, evict something else first. */
774 while (*dc_job
->cache
->size
+ dc_job
->size
> dc_job
->cache
->max_size
&&
776 evict_lru_item(dc_job
->cache
);
780 /* Write to a temporary file to allow for an atomic rename to the
781 * final destination filename, (to prevent any readers from seeing
782 * a partially written file).
784 if (asprintf(&filename_tmp
, "%s.tmp", filename
) == -1)
787 fd
= open(filename_tmp
, O_WRONLY
| O_CLOEXEC
| O_CREAT
, 0644);
789 /* Make the two-character subdirectory within the cache as needed. */
794 make_cache_file_directory(dc_job
->cache
, dc_job
->key
);
796 fd
= open(filename_tmp
, O_WRONLY
| O_CLOEXEC
| O_CREAT
, 0644);
801 /* With the temporary file open, we take an exclusive flock on
802 * it. If the flock fails, then another process still has the file
803 * open with the flock held. So just let that file be responsible
804 * for writing the file.
806 err
= flock(fd
, LOCK_EX
| LOCK_NB
);
810 /* Now that we have the lock on the open temporary file, we can
811 * check to see if the destination file already exists. If so,
812 * another process won the race between when we saw that the file
813 * didn't exist and now. In this case, we don't do anything more,
814 * (to ensure the size accounting of the cache doesn't get off).
816 fd_final
= open(filename
, O_RDONLY
| O_CLOEXEC
);
817 if (fd_final
!= -1) {
818 unlink(filename_tmp
);
822 /* OK, we're now on the hook to write out a file that we know is
823 * not in the cache, and is also not being written out to the cache
824 * by some other process.
826 * Create CRC of the data and store at the start of the file. We will
827 * read this when restoring the cache and use it to check for corruption.
829 struct cache_entry_file_data cf_data
;
830 cf_data
.crc32
= util_hash_crc32(dc_job
->data
, dc_job
->size
);
831 cf_data
.uncompressed_size
= dc_job
->size
;
833 size_t cf_data_size
= sizeof(cf_data
);
834 ret
= write_all(fd
, &cf_data
, cf_data_size
);
836 unlink(filename_tmp
);
840 /* Now, finally, write out the contents to the temporary file, then
841 * rename them atomically to the destination filename, and also
842 * perform an atomic increment of the total cache size.
844 size_t file_size
= deflate_and_write_to_disk(dc_job
->data
, dc_job
->size
,
846 if (file_size
== 0) {
847 unlink(filename_tmp
);
850 ret
= rename(filename_tmp
, filename
);
852 unlink(filename_tmp
);
856 file_size
+= cf_data_size
;
857 p_atomic_add(dc_job
->cache
->size
, file_size
);
862 /* This close finally releases the flock, (now that the final file
863 * has been renamed into place and the size has been added).
874 disk_cache_put(struct disk_cache
*cache
, const cache_key key
,
875 const void *data
, size_t size
)
877 struct disk_cache_put_job
*dc_job
=
878 create_put_job(cache
, key
, data
, size
);
881 util_queue_fence_init(&dc_job
->fence
);
882 util_queue_add_job(&cache
->cache_queue
, dc_job
, &dc_job
->fence
,
883 cache_put
, destroy_put_job
);
888 * Decompresses cache entry, returns true if successful.
891 inflate_cache_data(uint8_t *in_data
, size_t in_data_size
,
892 uint8_t *out_data
, size_t out_data_size
)
896 /* allocate inflate state */
897 strm
.zalloc
= Z_NULL
;
899 strm
.opaque
= Z_NULL
;
900 strm
.next_in
= in_data
;
901 strm
.avail_in
= in_data_size
;
902 strm
.next_out
= out_data
;
903 strm
.avail_out
= out_data_size
;
905 int ret
= inflateInit(&strm
);
909 ret
= inflate(&strm
, Z_NO_FLUSH
);
910 assert(ret
!= Z_STREAM_ERROR
); /* state not clobbered */
912 /* Unless there was an error we should have decompressed everything in one
913 * go as we know the uncompressed file size.
915 if (ret
!= Z_STREAM_END
) {
916 (void)inflateEnd(&strm
);
919 assert(strm
.avail_out
== 0);
921 /* clean up and return */
922 (void)inflateEnd(&strm
);
927 disk_cache_get(struct disk_cache
*cache
, const cache_key key
, size_t *size
)
929 int fd
= -1, ret
, len
;
931 char *filename
= NULL
;
932 uint8_t *data
= NULL
;
933 uint8_t *uncompressed_data
= NULL
;
938 filename
= get_cache_file(cache
, key
);
939 if (filename
== NULL
)
942 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
946 if (fstat(fd
, &sb
) == -1)
949 data
= malloc(sb
.st_size
);
953 /* Load the CRC that was created when the file was written. */
954 struct cache_entry_file_data cf_data
;
955 size_t cf_data_size
= sizeof(cf_data
);
956 assert(sb
.st_size
> cf_data_size
);
957 for (len
= 0; len
< cf_data_size
; len
+= ret
) {
958 ret
= read(fd
, ((uint8_t *) &cf_data
) + len
, cf_data_size
- len
);
963 /* Load the actual cache data. */
964 size_t cache_data_size
= sb
.st_size
- cf_data_size
;
965 for (len
= 0; len
< cache_data_size
; len
+= ret
) {
966 ret
= read(fd
, data
+ len
, cache_data_size
- len
);
971 /* Uncompress the cache data */
972 uncompressed_data
= malloc(cf_data
.uncompressed_size
);
973 if (!inflate_cache_data(data
, cache_data_size
, uncompressed_data
,
974 cf_data
.uncompressed_size
))
977 /* Check the data for corruption */
978 if (cf_data
.crc32
!= util_hash_crc32(uncompressed_data
,
979 cf_data
.uncompressed_size
))
987 *size
= cf_data
.uncompressed_size
;
989 return uncompressed_data
;
994 if (uncompressed_data
)
995 free(uncompressed_data
);
1005 disk_cache_put_key(struct disk_cache
*cache
, const cache_key key
)
1007 const uint32_t *key_chunk
= (const uint32_t *) key
;
1008 int i
= *key_chunk
& CACHE_INDEX_KEY_MASK
;
1009 unsigned char *entry
;
1011 entry
= &cache
->stored_keys
[i
* CACHE_KEY_SIZE
];
1013 memcpy(entry
, key
, CACHE_KEY_SIZE
);
1016 /* This function lets us test whether a given key was previously
1017 * stored in the cache with disk_cache_put_key(). The implement is
1018 * efficient by not using syscalls or hitting the disk. It's not
1019 * race-free, but the races are benign. If we race with someone else
1020 * calling disk_cache_put_key, then that's just an extra cache miss and an
1024 disk_cache_has_key(struct disk_cache
*cache
, const cache_key key
)
1026 const uint32_t *key_chunk
= (const uint32_t *) key
;
1027 int i
= *key_chunk
& CACHE_INDEX_KEY_MASK
;
1028 unsigned char *entry
;
1030 entry
= &cache
->stored_keys
[i
* CACHE_KEY_SIZE
];
1032 return memcmp(entry
, key
, CACHE_KEY_SIZE
) == 0;
1036 disk_cache_compute_key(struct disk_cache
*cache
, const void *data
, size_t size
,
1039 struct mesa_sha1 ctx
;
1041 _mesa_sha1_init(&ctx
);
1042 _mesa_sha1_update(&ctx
, cache
->driver_keys_blob
,
1043 cache
->driver_keys_blob_size
);
1044 _mesa_sha1_update(&ctx
, data
, size
);
1045 _mesa_sha1_final(&ctx
, key
);
1048 #endif /* ENABLE_SHADER_CACHE */