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>
42 #include "util/crc32.h"
43 #include "util/rand_xor.h"
44 #include "util/u_atomic.h"
45 #include "util/u_queue.h"
46 #include "util/mesa-sha1.h"
47 #include "util/ralloc.h"
48 #include "main/errors.h"
50 #include "disk_cache.h"
52 /* Number of bits to mask off from a cache key to get an index. */
53 #define CACHE_INDEX_KEY_BITS 16
55 /* Mask for computing an index from a key. */
56 #define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
58 /* The number of keys that can be stored in the index. */
59 #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
61 /* The cache version should be bumped whenever a change is made to the
62 * structure of cache entries or the index. This will give any 3rd party
63 * applications reading the cache entries a chance to adjust to the changes.
65 * - The cache version is checked internally when reading a cache entry. If we
66 * ever have a mismatch we are in big trouble as this means we had a cache
67 * collision. In case of such an event please check the skys for giant
68 * asteroids and that the entire Mesa team hasn't been eaten by wolves.
70 * - There is no strict requirement that cache versions be backwards
71 * compatible but effort should be taken to limit disruption where possible.
73 #define CACHE_VERSION 1
76 /* The path to the cache directory. */
79 /* Thread queue for compressing and writing cache entries to disk */
80 struct util_queue cache_queue
;
82 /* Seed for rand, which is used to pick a random directory */
83 uint64_t seed_xorshift128plus
[2];
85 /* A pointer to the mmapped index file within the cache directory. */
87 size_t index_mmap_size
;
89 /* Pointer to total size of all objects in cache (within index_mmap) */
92 /* Pointer to stored keys, (within index_mmap). */
95 /* Maximum size of all cached objects (in bytes). */
98 /* Driver cache keys. */
99 uint8_t *driver_keys_blob
;
100 size_t driver_keys_blob_size
;
103 struct disk_cache_put_job
{
104 struct util_queue_fence fence
;
106 struct disk_cache
*cache
;
110 /* Copy of cache data to be compressed and written. */
113 /* Size of data to be compressed and written. */
117 /* Create a directory named 'path' if it does not already exist.
119 * Returns: 0 if path already exists as a directory or if created.
120 * -1 in all other cases.
123 mkdir_if_needed(const char *path
)
127 /* If the path exists already, then our work is done if it's a
128 * directory, but it's an error if it is not.
130 if (stat(path
, &sb
) == 0) {
131 if (S_ISDIR(sb
.st_mode
)) {
134 fprintf(stderr
, "Cannot use %s for shader cache (not a directory)"
135 "---disabling.\n", path
);
140 int ret
= mkdir(path
, 0755);
141 if (ret
== 0 || (ret
== -1 && errno
== EEXIST
))
144 fprintf(stderr
, "Failed to create %s for shader cache (%s)---disabling.\n",
145 path
, strerror(errno
));
150 /* Concatenate an existing path and a new name to form a new path. If the new
151 * path does not exist as a directory, create it then return the resulting
152 * name of the new path (ralloc'ed off of 'ctx').
154 * Returns NULL on any error, such as:
156 * <path> does not exist or is not a directory
157 * <path>/<name> exists but is not a directory
158 * <path>/<name> cannot be created as a directory
161 concatenate_and_mkdir(void *ctx
, const char *path
, const char *name
)
166 if (stat(path
, &sb
) != 0 || ! S_ISDIR(sb
.st_mode
))
169 new_path
= ralloc_asprintf(ctx
, "%s/%s", path
, name
);
171 if (mkdir_if_needed(new_path
) == 0)
177 #define DRV_KEY_CPY(_dst, _src, _src_size) \
179 memcpy(_dst, _src, _src_size); \
184 disk_cache_create(const char *gpu_name
, const char *timestamp
,
185 uint64_t driver_flags
)
188 struct disk_cache
*cache
= NULL
;
189 char *path
, *max_size_str
;
195 /* If running as a users other than the real user disable cache */
196 if (geteuid() != getuid())
199 /* A ralloc context for transient data during this invocation. */
200 local
= ralloc_context(NULL
);
204 /* At user request, disable shader cache entirely. */
205 if (getenv("MESA_GLSL_CACHE_DISABLE"))
208 /* Determine path for cache based on the first defined name as follows:
210 * $MESA_GLSL_CACHE_DIR
211 * $XDG_CACHE_HOME/mesa_shader_cache
212 * <pwd.pw_dir>/.cache/mesa_shader_cache
214 path
= getenv("MESA_GLSL_CACHE_DIR");
216 if (mkdir_if_needed(path
) == -1)
219 path
= concatenate_and_mkdir(local
, path
, CACHE_DIR_NAME
);
225 char *xdg_cache_home
= getenv("XDG_CACHE_HOME");
227 if (xdg_cache_home
) {
228 if (mkdir_if_needed(xdg_cache_home
) == -1)
231 path
= concatenate_and_mkdir(local
, xdg_cache_home
, CACHE_DIR_NAME
);
240 struct passwd pwd
, *result
;
242 buf_size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
246 /* Loop until buf_size is large enough to query the directory */
248 buf
= ralloc_size(local
, buf_size
);
250 getpwuid_r(getuid(), &pwd
, buf
, buf_size
, &result
);
254 if (errno
== ERANGE
) {
263 path
= concatenate_and_mkdir(local
, pwd
.pw_dir
, ".cache");
267 path
= concatenate_and_mkdir(local
, path
, CACHE_DIR_NAME
);
272 cache
= ralloc(NULL
, struct disk_cache
);
276 cache
->path
= ralloc_strdup(cache
, path
);
277 if (cache
->path
== NULL
)
280 path
= ralloc_asprintf(local
, "%s/index", cache
->path
);
284 fd
= open(path
, O_RDWR
| O_CREAT
| O_CLOEXEC
, 0644);
288 if (fstat(fd
, &sb
) == -1)
291 /* Force the index file to be the expected size. */
292 size
= sizeof(*cache
->size
) + CACHE_INDEX_MAX_KEYS
* CACHE_KEY_SIZE
;
293 if (sb
.st_size
!= size
) {
294 if (ftruncate(fd
, size
) == -1)
298 /* We map this shared so that other processes see updates that we
301 * Note: We do use atomic addition to ensure that multiple
302 * processes don't scramble the cache size recorded in the
303 * index. But we don't use any locking to prevent multiple
304 * processes from updating the same entry simultaneously. The idea
305 * is that if either result lands entirely in the index, then
306 * that's equivalent to a well-ordered write followed by an
307 * eviction and a write. On the other hand, if the simultaneous
308 * writes result in a corrupt entry, that's not really any
309 * different than both entries being evicted, (since within the
310 * guarantees of the cryptographic hash, a corrupt entry is
311 * unlikely to ever match a real cache key).
313 cache
->index_mmap
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
315 if (cache
->index_mmap
== MAP_FAILED
)
317 cache
->index_mmap_size
= size
;
321 cache
->size
= (uint64_t *) cache
->index_mmap
;
322 cache
->stored_keys
= cache
->index_mmap
+ sizeof(uint64_t);
326 max_size_str
= getenv("MESA_GLSL_CACHE_MAX_SIZE");
329 max_size
= strtoul(max_size_str
, &end
, 10);
330 if (end
== max_size_str
) {
340 max_size
*= 1024*1024;
346 max_size
*= 1024*1024*1024;
352 /* Default to 1GB for maximum cache size. */
354 max_size
= 1024*1024*1024;
357 cache
->max_size
= max_size
;
359 /* A limit of 32 jobs was choosen as observations of Deus Ex start-up times
360 * showed that we reached at most 11 jobs on an Intel i5-6400 CPU@2.70GHz
361 * (a fairly modest desktop CPU). 1 thread was chosen because we don't
362 * really care about getting things to disk quickly just that it's not
363 * blocking other tasks.
365 util_queue_init(&cache
->cache_queue
, "disk_cache", 32, 1, 0);
367 uint8_t cache_version
= CACHE_VERSION
;
368 size_t cv_size
= sizeof(cache_version
);
369 cache
->driver_keys_blob_size
= cv_size
;
371 /* Create driver id keys */
372 size_t ts_size
= strlen(timestamp
) + 1;
373 size_t gpu_name_size
= strlen(gpu_name
) + 1;
374 cache
->driver_keys_blob_size
+= ts_size
;
375 cache
->driver_keys_blob_size
+= gpu_name_size
;
377 /* We sometimes store entire structs that contains a pointers in the cache,
378 * use pointer size as a key to avoid hard to debug issues.
380 uint8_t ptr_size
= sizeof(void *);
381 size_t ptr_size_size
= sizeof(ptr_size
);
382 cache
->driver_keys_blob_size
+= ptr_size_size
;
384 size_t driver_flags_size
= sizeof(driver_flags
);
385 cache
->driver_keys_blob_size
+= driver_flags_size
;
387 cache
->driver_keys_blob
=
388 ralloc_size(cache
, cache
->driver_keys_blob_size
);
389 if (!cache
->driver_keys_blob
)
392 uint8_t *drv_key_blob
= cache
->driver_keys_blob
;
393 DRV_KEY_CPY(drv_key_blob
, &cache_version
, cv_size
)
394 DRV_KEY_CPY(drv_key_blob
, timestamp
, ts_size
)
395 DRV_KEY_CPY(drv_key_blob
, gpu_name
, gpu_name_size
)
396 DRV_KEY_CPY(drv_key_blob
, &ptr_size
, ptr_size_size
)
397 DRV_KEY_CPY(drv_key_blob
, &driver_flags
, driver_flags_size
)
399 /* Seed our rand function */
400 s_rand_xorshift128plus(cache
->seed_xorshift128plus
, true);
417 disk_cache_destroy(struct disk_cache
*cache
)
420 util_queue_destroy(&cache
->cache_queue
);
421 munmap(cache
->index_mmap
, cache
->index_mmap_size
);
427 /* Return a filename within the cache's directory corresponding to 'key'. The
428 * returned filename is ralloced with 'cache' as the parent context.
430 * Returns NULL if out of memory.
433 get_cache_file(struct disk_cache
*cache
, const cache_key key
)
438 _mesa_sha1_format(buf
, key
);
439 if (asprintf(&filename
, "%s/%c%c/%s", cache
->path
, buf
[0],
440 buf
[1], buf
+ 2) == -1)
446 /* Create the directory that will be needed for the cache file for \key.
448 * Obviously, the implementation here must closely match
449 * _get_cache_file above.
452 make_cache_file_directory(struct disk_cache
*cache
, const cache_key key
)
457 _mesa_sha1_format(buf
, key
);
458 if (asprintf(&dir
, "%s/%c%c", cache
->path
, buf
[0], buf
[1]) == -1)
461 mkdir_if_needed(dir
);
465 /* Given a directory path and predicate function, find the entry with
466 * the oldest access time in that directory for which the predicate
469 * Returns: A malloc'ed string for the path to the chosen file, (or
470 * NULL on any error). The caller should free the string when
474 choose_lru_file_matching(const char *dir_path
,
475 bool (*predicate
)(const char *dir_path
,
477 const char *, const size_t))
480 struct dirent
*entry
;
482 char *lru_name
= NULL
;
483 time_t lru_atime
= 0;
485 dir
= opendir(dir_path
);
490 entry
= readdir(dir
);
495 if (fstatat(dirfd(dir
), entry
->d_name
, &sb
, 0) == 0) {
496 if (!lru_atime
|| (sb
.st_atime
< lru_atime
)) {
497 size_t len
= strlen(entry
->d_name
);
499 if (!predicate(dir_path
, &sb
, entry
->d_name
, len
))
502 char *tmp
= realloc(lru_name
, len
+ 1);
505 memcpy(lru_name
, entry
->d_name
, len
+ 1);
506 lru_atime
= sb
.st_atime
;
512 if (lru_name
== NULL
) {
517 if (asprintf(&filename
, "%s/%s", dir_path
, lru_name
) < 0)
526 /* Is entry a regular file, and not having a name with a trailing
530 is_regular_non_tmp_file(const char *path
, const struct stat
*sb
,
531 const char *d_name
, const size_t len
)
533 if (!S_ISREG(sb
->st_mode
))
536 if (len
>= 4 && strcmp(&d_name
[len
-4], ".tmp") == 0)
542 /* Returns the size of the deleted file, (or 0 on any error). */
544 unlink_lru_file_from_directory(const char *path
)
549 filename
= choose_lru_file_matching(path
, is_regular_non_tmp_file
);
550 if (filename
== NULL
)
553 if (stat(filename
, &sb
) == -1) {
561 return sb
.st_blocks
* 512;
564 /* Is entry a directory with a two-character name, (and not the
565 * special name of ".."). We also return false if the dir is empty.
568 is_two_character_sub_directory(const char *path
, const struct stat
*sb
,
569 const char *d_name
, const size_t len
)
571 if (!S_ISDIR(sb
->st_mode
))
577 if (strcmp(d_name
, "..") == 0)
581 if (asprintf(&subdir
, "%s/%s", path
, d_name
) == -1)
583 DIR *dir
= opendir(subdir
);
589 unsigned subdir_entries
= 0;
591 while ((d
= readdir(dir
)) != NULL
) {
592 if(++subdir_entries
> 2)
597 /* If dir only contains '.' and '..' it must be empty */
598 if (subdir_entries
<= 2)
605 evict_lru_item(struct disk_cache
*cache
)
609 /* With a reasonably-sized, full cache, (and with keys generated
610 * from a cryptographic hash), we can choose two random hex digits
611 * and reasonably expect the directory to exist with a file in it.
612 * Provides pseudo-LRU eviction to reduce checking all cache files.
614 uint64_t rand64
= rand_xorshift128plus(cache
->seed_xorshift128plus
);
615 if (asprintf(&dir_path
, "%s/%02" PRIx64
, cache
->path
, rand64
& 0xff) < 0)
618 size_t size
= unlink_lru_file_from_directory(dir_path
);
623 p_atomic_add(cache
->size
, - (uint64_t)size
);
627 /* In the case where the random choice of directory didn't find
628 * something, we choose the least recently accessed from the
629 * existing directories.
631 * Really, the only reason this code exists is to allow the unit
632 * tests to work, (which use an artificially-small cache to be able
633 * to force a single cached item to be evicted).
635 dir_path
= choose_lru_file_matching(cache
->path
,
636 is_two_character_sub_directory
);
637 if (dir_path
== NULL
)
640 size
= unlink_lru_file_from_directory(dir_path
);
645 p_atomic_add(cache
->size
, - (uint64_t)size
);
649 disk_cache_remove(struct disk_cache
*cache
, const cache_key key
)
653 char *filename
= get_cache_file(cache
, key
);
654 if (filename
== NULL
) {
658 if (stat(filename
, &sb
) == -1) {
667 p_atomic_add(cache
->size
, - (uint64_t)sb
.st_blocks
* 512);
671 read_all(int fd
, void *buf
, size_t count
)
677 for (done
= 0; done
< count
; done
+= read_ret
) {
678 read_ret
= read(fd
, in
+ done
, count
- done
);
679 if (read_ret
== -1 || read_ret
== 0)
686 write_all(int fd
, const void *buf
, size_t count
)
688 const char *out
= buf
;
692 for (done
= 0; done
< count
; done
+= written
) {
693 written
= write(fd
, out
+ done
, count
- done
);
700 /* From the zlib docs:
701 * "If the memory is available, buffers sizes on the order of 128K or 256K
702 * bytes should be used."
704 #define BUFSIZE 256 * 1024
707 * Compresses cache entry in memory and writes it to disk. Returns the size
708 * of the data written to disk.
711 deflate_and_write_to_disk(const void *in_data
, size_t in_data_size
, int dest
,
712 const char *filename
)
714 unsigned char out
[BUFSIZE
];
716 /* allocate deflate state */
718 strm
.zalloc
= Z_NULL
;
720 strm
.opaque
= Z_NULL
;
721 strm
.next_in
= (uint8_t *) in_data
;
722 strm
.avail_in
= in_data_size
;
724 int ret
= deflateInit(&strm
, Z_BEST_COMPRESSION
);
728 /* compress until end of in_data */
729 size_t compressed_size
= 0;
732 int remaining
= in_data_size
- BUFSIZE
;
733 flush
= remaining
> 0 ? Z_NO_FLUSH
: Z_FINISH
;
734 in_data_size
-= BUFSIZE
;
736 /* Run deflate() on input until the output buffer is not full (which
737 * means there is no more data to deflate).
740 strm
.avail_out
= BUFSIZE
;
743 ret
= deflate(&strm
, flush
); /* no bad return value */
744 assert(ret
!= Z_STREAM_ERROR
); /* state not clobbered */
746 size_t have
= BUFSIZE
- strm
.avail_out
;
747 compressed_size
+= have
;
749 ssize_t written
= write_all(dest
, out
, have
);
751 (void)deflateEnd(&strm
);
754 } while (strm
.avail_out
== 0);
756 /* all input should be used */
757 assert(strm
.avail_in
== 0);
759 } while (flush
!= Z_FINISH
);
761 /* stream should be complete */
762 assert(ret
== Z_STREAM_END
);
764 /* clean up and return */
765 (void)deflateEnd(&strm
);
766 return compressed_size
;
769 static struct disk_cache_put_job
*
770 create_put_job(struct disk_cache
*cache
, const cache_key key
,
771 const void *data
, size_t size
,
772 struct cache_item_metadata
*cache_item_metadata
)
774 struct disk_cache_put_job
*dc_job
= (struct disk_cache_put_job
*)
775 malloc(sizeof(struct disk_cache_put_job
) + size
);
778 dc_job
->cache
= cache
;
779 memcpy(dc_job
->key
, key
, sizeof(cache_key
));
780 dc_job
->data
= dc_job
+ 1;
781 memcpy(dc_job
->data
, data
, size
);
789 destroy_put_job(void *job
, int thread_index
)
796 struct cache_entry_file_data
{
798 uint32_t uncompressed_size
;
802 cache_put(void *job
, int thread_index
)
806 int fd
= -1, fd_final
= -1, err
, ret
;
808 char *filename
= NULL
, *filename_tmp
= NULL
;
809 struct disk_cache_put_job
*dc_job
= (struct disk_cache_put_job
*) job
;
811 filename
= get_cache_file(dc_job
->cache
, dc_job
->key
);
812 if (filename
== NULL
)
815 /* If the cache is too large, evict something else first. */
816 while (*dc_job
->cache
->size
+ dc_job
->size
> dc_job
->cache
->max_size
&&
818 evict_lru_item(dc_job
->cache
);
822 /* Write to a temporary file to allow for an atomic rename to the
823 * final destination filename, (to prevent any readers from seeing
824 * a partially written file).
826 if (asprintf(&filename_tmp
, "%s.tmp", filename
) == -1)
829 fd
= open(filename_tmp
, O_WRONLY
| O_CLOEXEC
| O_CREAT
, 0644);
831 /* Make the two-character subdirectory within the cache as needed. */
836 make_cache_file_directory(dc_job
->cache
, dc_job
->key
);
838 fd
= open(filename_tmp
, O_WRONLY
| O_CLOEXEC
| O_CREAT
, 0644);
843 /* With the temporary file open, we take an exclusive flock on
844 * it. If the flock fails, then another process still has the file
845 * open with the flock held. So just let that file be responsible
846 * for writing the file.
848 err
= flock(fd
, LOCK_EX
| LOCK_NB
);
852 /* Now that we have the lock on the open temporary file, we can
853 * check to see if the destination file already exists. If so,
854 * another process won the race between when we saw that the file
855 * didn't exist and now. In this case, we don't do anything more,
856 * (to ensure the size accounting of the cache doesn't get off).
858 fd_final
= open(filename
, O_RDONLY
| O_CLOEXEC
);
859 if (fd_final
!= -1) {
860 unlink(filename_tmp
);
864 /* OK, we're now on the hook to write out a file that we know is
865 * not in the cache, and is also not being written out to the cache
866 * by some other process.
869 /* Write the driver_keys_blob, this can be used find information about the
870 * mesa version that produced the entry or deal with hash collisions,
871 * should that ever become a real problem.
873 ret
= write_all(fd
, dc_job
->cache
->driver_keys_blob
,
874 dc_job
->cache
->driver_keys_blob_size
);
876 unlink(filename_tmp
);
880 /* Create CRC of the data. We will read this when restoring the cache and
881 * use it to check for corruption.
883 struct cache_entry_file_data cf_data
;
884 cf_data
.crc32
= util_hash_crc32(dc_job
->data
, dc_job
->size
);
885 cf_data
.uncompressed_size
= dc_job
->size
;
887 size_t cf_data_size
= sizeof(cf_data
);
888 ret
= write_all(fd
, &cf_data
, cf_data_size
);
890 unlink(filename_tmp
);
894 /* Now, finally, write out the contents to the temporary file, then
895 * rename them atomically to the destination filename, and also
896 * perform an atomic increment of the total cache size.
898 size_t file_size
= deflate_and_write_to_disk(dc_job
->data
, dc_job
->size
,
900 if (file_size
== 0) {
901 unlink(filename_tmp
);
904 ret
= rename(filename_tmp
, filename
);
906 unlink(filename_tmp
);
911 if (stat(filename
, &sb
) == -1) {
912 /* Something went wrong remove the file */
917 p_atomic_add(dc_job
->cache
->size
, sb
.st_blocks
* 512);
922 /* This close finally releases the flock, (now that the final file
923 * has been renamed into place and the size has been added).
934 disk_cache_put(struct disk_cache
*cache
, const cache_key key
,
935 const void *data
, size_t size
,
936 struct cache_item_metadata
*cache_item_metadata
)
938 struct disk_cache_put_job
*dc_job
=
939 create_put_job(cache
, key
, data
, size
, cache_item_metadata
);
942 util_queue_fence_init(&dc_job
->fence
);
943 util_queue_add_job(&cache
->cache_queue
, dc_job
, &dc_job
->fence
,
944 cache_put
, destroy_put_job
);
949 * Decompresses cache entry, returns true if successful.
952 inflate_cache_data(uint8_t *in_data
, size_t in_data_size
,
953 uint8_t *out_data
, size_t out_data_size
)
957 /* allocate inflate state */
958 strm
.zalloc
= Z_NULL
;
960 strm
.opaque
= Z_NULL
;
961 strm
.next_in
= in_data
;
962 strm
.avail_in
= in_data_size
;
963 strm
.next_out
= out_data
;
964 strm
.avail_out
= out_data_size
;
966 int ret
= inflateInit(&strm
);
970 ret
= inflate(&strm
, Z_NO_FLUSH
);
971 assert(ret
!= Z_STREAM_ERROR
); /* state not clobbered */
973 /* Unless there was an error we should have decompressed everything in one
974 * go as we know the uncompressed file size.
976 if (ret
!= Z_STREAM_END
) {
977 (void)inflateEnd(&strm
);
980 assert(strm
.avail_out
== 0);
982 /* clean up and return */
983 (void)inflateEnd(&strm
);
988 disk_cache_get(struct disk_cache
*cache
, const cache_key key
, size_t *size
)
992 char *filename
= NULL
;
993 uint8_t *data
= NULL
;
994 uint8_t *uncompressed_data
= NULL
;
995 uint8_t *file_header
= NULL
;
1000 filename
= get_cache_file(cache
, key
);
1001 if (filename
== NULL
)
1004 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
1008 if (fstat(fd
, &sb
) == -1)
1011 data
= malloc(sb
.st_size
);
1015 size_t ck_size
= cache
->driver_keys_blob_size
;
1016 file_header
= malloc(ck_size
);
1020 if (sb
.st_size
< ck_size
)
1023 ret
= read_all(fd
, file_header
, ck_size
);
1027 /* Check for extremely unlikely hash collisions */
1028 if (memcmp(cache
->driver_keys_blob
, file_header
, ck_size
) != 0)
1031 /* Load the CRC that was created when the file was written. */
1032 struct cache_entry_file_data cf_data
;
1033 size_t cf_data_size
= sizeof(cf_data
);
1034 ret
= read_all(fd
, &cf_data
, cf_data_size
);
1038 /* Load the actual cache data. */
1039 size_t cache_data_size
= sb
.st_size
- cf_data_size
- ck_size
;
1040 ret
= read_all(fd
, data
, cache_data_size
);
1044 /* Uncompress the cache data */
1045 uncompressed_data
= malloc(cf_data
.uncompressed_size
);
1046 if (!inflate_cache_data(data
, cache_data_size
, uncompressed_data
,
1047 cf_data
.uncompressed_size
))
1050 /* Check the data for corruption */
1051 if (cf_data
.crc32
!= util_hash_crc32(uncompressed_data
,
1052 cf_data
.uncompressed_size
))
1060 *size
= cf_data
.uncompressed_size
;
1062 return uncompressed_data
;
1067 if (uncompressed_data
)
1068 free(uncompressed_data
);
1080 disk_cache_put_key(struct disk_cache
*cache
, const cache_key key
)
1082 const uint32_t *key_chunk
= (const uint32_t *) key
;
1083 int i
= *key_chunk
& CACHE_INDEX_KEY_MASK
;
1084 unsigned char *entry
;
1086 entry
= &cache
->stored_keys
[i
* CACHE_KEY_SIZE
];
1088 memcpy(entry
, key
, CACHE_KEY_SIZE
);
1091 /* This function lets us test whether a given key was previously
1092 * stored in the cache with disk_cache_put_key(). The implement is
1093 * efficient by not using syscalls or hitting the disk. It's not
1094 * race-free, but the races are benign. If we race with someone else
1095 * calling disk_cache_put_key, then that's just an extra cache miss and an
1099 disk_cache_has_key(struct disk_cache
*cache
, const cache_key key
)
1101 const uint32_t *key_chunk
= (const uint32_t *) key
;
1102 int i
= *key_chunk
& CACHE_INDEX_KEY_MASK
;
1103 unsigned char *entry
;
1105 entry
= &cache
->stored_keys
[i
* CACHE_KEY_SIZE
];
1107 return memcmp(entry
, key
, CACHE_KEY_SIZE
) == 0;
1111 disk_cache_compute_key(struct disk_cache
*cache
, const void *data
, size_t size
,
1114 struct mesa_sha1 ctx
;
1116 _mesa_sha1_init(&ctx
);
1117 _mesa_sha1_update(&ctx
, cache
->driver_keys_blob
,
1118 cache
->driver_keys_blob_size
);
1119 _mesa_sha1_update(&ctx
, data
, size
);
1120 _mesa_sha1_final(&ctx
, key
);
1123 #endif /* ENABLE_SHADER_CACHE */