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>
43 #include "util/crc32.h"
44 #include "util/debug.h"
45 #include "util/rand_xor.h"
46 #include "util/u_atomic.h"
47 #include "util/u_queue.h"
48 #include "util/mesa-sha1.h"
49 #include "util/ralloc.h"
50 #include "main/compiler.h"
51 #include "main/errors.h"
53 #include "disk_cache.h"
55 /* Number of bits to mask off from a cache key to get an index. */
56 #define CACHE_INDEX_KEY_BITS 16
58 /* Mask for computing an index from a key. */
59 #define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
61 /* The number of keys that can be stored in the index. */
62 #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
64 /* The cache version should be bumped whenever a change is made to the
65 * structure of cache entries or the index. This will give any 3rd party
66 * applications reading the cache entries a chance to adjust to the changes.
68 * - The cache version is checked internally when reading a cache entry. If we
69 * ever have a mismatch we are in big trouble as this means we had a cache
70 * collision. In case of such an event please check the skys for giant
71 * asteroids and that the entire Mesa team hasn't been eaten by wolves.
73 * - There is no strict requirement that cache versions be backwards
74 * compatible but effort should be taken to limit disruption where possible.
76 #define CACHE_VERSION 1
79 /* The path to the cache directory. */
81 bool path_init_failed
;
83 /* Thread queue for compressing and writing cache entries to disk */
84 struct util_queue cache_queue
;
86 /* Seed for rand, which is used to pick a random directory */
87 uint64_t seed_xorshift128plus
[2];
89 /* A pointer to the mmapped index file within the cache directory. */
91 size_t index_mmap_size
;
93 /* Pointer to total size of all objects in cache (within index_mmap) */
96 /* Pointer to stored keys, (within index_mmap). */
99 /* Maximum size of all cached objects (in bytes). */
102 /* Driver cache keys. */
103 uint8_t *driver_keys_blob
;
104 size_t driver_keys_blob_size
;
106 disk_cache_put_cb blob_put_cb
;
107 disk_cache_get_cb blob_get_cb
;
110 struct disk_cache_put_job
{
111 struct util_queue_fence fence
;
113 struct disk_cache
*cache
;
117 /* Copy of cache data to be compressed and written. */
120 /* Size of data to be compressed and written. */
123 struct cache_item_metadata cache_item_metadata
;
126 /* Create a directory named 'path' if it does not already exist.
128 * Returns: 0 if path already exists as a directory or if created.
129 * -1 in all other cases.
132 mkdir_if_needed(const char *path
)
136 /* If the path exists already, then our work is done if it's a
137 * directory, but it's an error if it is not.
139 if (stat(path
, &sb
) == 0) {
140 if (S_ISDIR(sb
.st_mode
)) {
143 fprintf(stderr
, "Cannot use %s for shader cache (not a directory)"
144 "---disabling.\n", path
);
149 int ret
= mkdir(path
, 0755);
150 if (ret
== 0 || (ret
== -1 && errno
== EEXIST
))
153 fprintf(stderr
, "Failed to create %s for shader cache (%s)---disabling.\n",
154 path
, strerror(errno
));
159 /* Concatenate an existing path and a new name to form a new path. If the new
160 * path does not exist as a directory, create it then return the resulting
161 * name of the new path (ralloc'ed off of 'ctx').
163 * Returns NULL on any error, such as:
165 * <path> does not exist or is not a directory
166 * <path>/<name> exists but is not a directory
167 * <path>/<name> cannot be created as a directory
170 concatenate_and_mkdir(void *ctx
, const char *path
, const char *name
)
175 if (stat(path
, &sb
) != 0 || ! S_ISDIR(sb
.st_mode
))
178 new_path
= ralloc_asprintf(ctx
, "%s/%s", path
, name
);
180 if (mkdir_if_needed(new_path
) == 0)
186 #define DRV_KEY_CPY(_dst, _src, _src_size) \
188 memcpy(_dst, _src, _src_size); \
193 disk_cache_create(const char *gpu_name
, const char *driver_id
,
194 uint64_t driver_flags
)
197 struct disk_cache
*cache
= NULL
;
198 char *path
, *max_size_str
;
204 uint8_t cache_version
= CACHE_VERSION
;
205 size_t cv_size
= sizeof(cache_version
);
207 /* If running as a users other than the real user disable cache */
208 if (geteuid() != getuid())
211 /* A ralloc context for transient data during this invocation. */
212 local
= ralloc_context(NULL
);
216 /* At user request, disable shader cache entirely. */
217 if (env_var_as_boolean("MESA_GLSL_CACHE_DISABLE", false))
220 cache
= rzalloc(NULL
, struct disk_cache
);
224 /* Assume failure. */
225 cache
->path_init_failed
= true;
227 /* Determine path for cache based on the first defined name as follows:
229 * $MESA_GLSL_CACHE_DIR
230 * $XDG_CACHE_HOME/mesa_shader_cache
231 * <pwd.pw_dir>/.cache/mesa_shader_cache
233 path
= getenv("MESA_GLSL_CACHE_DIR");
235 if (mkdir_if_needed(path
) == -1)
238 path
= concatenate_and_mkdir(local
, path
, CACHE_DIR_NAME
);
244 char *xdg_cache_home
= getenv("XDG_CACHE_HOME");
246 if (xdg_cache_home
) {
247 if (mkdir_if_needed(xdg_cache_home
) == -1)
250 path
= concatenate_and_mkdir(local
, xdg_cache_home
, CACHE_DIR_NAME
);
259 struct passwd pwd
, *result
;
261 buf_size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
265 /* Loop until buf_size is large enough to query the directory */
267 buf
= ralloc_size(local
, buf_size
);
269 getpwuid_r(getuid(), &pwd
, buf
, buf_size
, &result
);
273 if (errno
== ERANGE
) {
282 path
= concatenate_and_mkdir(local
, pwd
.pw_dir
, ".cache");
286 path
= concatenate_and_mkdir(local
, path
, CACHE_DIR_NAME
);
291 cache
->path
= ralloc_strdup(cache
, path
);
292 if (cache
->path
== NULL
)
295 path
= ralloc_asprintf(local
, "%s/index", cache
->path
);
299 fd
= open(path
, O_RDWR
| O_CREAT
| O_CLOEXEC
, 0644);
303 if (fstat(fd
, &sb
) == -1)
306 /* Force the index file to be the expected size. */
307 size
= sizeof(*cache
->size
) + CACHE_INDEX_MAX_KEYS
* CACHE_KEY_SIZE
;
308 if (sb
.st_size
!= size
) {
309 if (ftruncate(fd
, size
) == -1)
313 /* We map this shared so that other processes see updates that we
316 * Note: We do use atomic addition to ensure that multiple
317 * processes don't scramble the cache size recorded in the
318 * index. But we don't use any locking to prevent multiple
319 * processes from updating the same entry simultaneously. The idea
320 * is that if either result lands entirely in the index, then
321 * that's equivalent to a well-ordered write followed by an
322 * eviction and a write. On the other hand, if the simultaneous
323 * writes result in a corrupt entry, that's not really any
324 * different than both entries being evicted, (since within the
325 * guarantees of the cryptographic hash, a corrupt entry is
326 * unlikely to ever match a real cache key).
328 cache
->index_mmap
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
330 if (cache
->index_mmap
== MAP_FAILED
)
332 cache
->index_mmap_size
= size
;
334 cache
->size
= (uint64_t *) cache
->index_mmap
;
335 cache
->stored_keys
= cache
->index_mmap
+ sizeof(uint64_t);
339 max_size_str
= getenv("MESA_GLSL_CACHE_MAX_SIZE");
342 max_size
= strtoul(max_size_str
, &end
, 10);
343 if (end
== max_size_str
) {
353 max_size
*= 1024*1024;
359 max_size
*= 1024*1024*1024;
365 /* Default to 1GB for maximum cache size. */
367 max_size
= 1024*1024*1024;
370 cache
->max_size
= max_size
;
372 /* 4 threads were chosen below because just about all modern CPUs currently
373 * available that run Mesa have *at least* 4 cores. For these CPUs allowing
374 * more threads can result in the queue being processed faster, thus
375 * avoiding excessive memory use due to a backlog of cache entrys building
376 * up in the queue. Since we set the UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY
377 * flag this should have little negative impact on low core systems.
379 * The queue will resize automatically when it's full, so adding new jobs
382 util_queue_init(&cache
->cache_queue
, "disk$", 32, 4,
383 UTIL_QUEUE_INIT_RESIZE_IF_FULL
|
384 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY
|
385 UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY
);
387 cache
->path_init_failed
= false;
394 cache
->driver_keys_blob_size
= cv_size
;
396 /* Create driver id keys */
397 size_t id_size
= strlen(driver_id
) + 1;
398 size_t gpu_name_size
= strlen(gpu_name
) + 1;
399 cache
->driver_keys_blob_size
+= id_size
;
400 cache
->driver_keys_blob_size
+= gpu_name_size
;
402 /* We sometimes store entire structs that contains a pointers in the cache,
403 * use pointer size as a key to avoid hard to debug issues.
405 uint8_t ptr_size
= sizeof(void *);
406 size_t ptr_size_size
= sizeof(ptr_size
);
407 cache
->driver_keys_blob_size
+= ptr_size_size
;
409 size_t driver_flags_size
= sizeof(driver_flags
);
410 cache
->driver_keys_blob_size
+= driver_flags_size
;
412 cache
->driver_keys_blob
=
413 ralloc_size(cache
, cache
->driver_keys_blob_size
);
414 if (!cache
->driver_keys_blob
)
417 uint8_t *drv_key_blob
= cache
->driver_keys_blob
;
418 DRV_KEY_CPY(drv_key_blob
, &cache_version
, cv_size
)
419 DRV_KEY_CPY(drv_key_blob
, driver_id
, id_size
)
420 DRV_KEY_CPY(drv_key_blob
, gpu_name
, gpu_name_size
)
421 DRV_KEY_CPY(drv_key_blob
, &ptr_size
, ptr_size_size
)
422 DRV_KEY_CPY(drv_key_blob
, &driver_flags
, driver_flags_size
)
424 /* Seed our rand function */
425 s_rand_xorshift128plus(cache
->seed_xorshift128plus
, true);
440 disk_cache_destroy(struct disk_cache
*cache
)
442 if (cache
&& !cache
->path_init_failed
) {
443 util_queue_destroy(&cache
->cache_queue
);
444 munmap(cache
->index_mmap
, cache
->index_mmap_size
);
450 /* Return a filename within the cache's directory corresponding to 'key'. The
451 * returned filename is ralloced with 'cache' as the parent context.
453 * Returns NULL if out of memory.
456 get_cache_file(struct disk_cache
*cache
, const cache_key key
)
461 if (cache
->path_init_failed
)
464 _mesa_sha1_format(buf
, key
);
465 if (asprintf(&filename
, "%s/%c%c/%s", cache
->path
, buf
[0],
466 buf
[1], buf
+ 2) == -1)
472 /* Create the directory that will be needed for the cache file for \key.
474 * Obviously, the implementation here must closely match
475 * _get_cache_file above.
478 make_cache_file_directory(struct disk_cache
*cache
, const cache_key key
)
483 _mesa_sha1_format(buf
, key
);
484 if (asprintf(&dir
, "%s/%c%c", cache
->path
, buf
[0], buf
[1]) == -1)
487 mkdir_if_needed(dir
);
491 /* Given a directory path and predicate function, find the entry with
492 * the oldest access time in that directory for which the predicate
495 * Returns: A malloc'ed string for the path to the chosen file, (or
496 * NULL on any error). The caller should free the string when
500 choose_lru_file_matching(const char *dir_path
,
501 bool (*predicate
)(const char *dir_path
,
503 const char *, const size_t))
506 struct dirent
*entry
;
508 char *lru_name
= NULL
;
509 time_t lru_atime
= 0;
511 dir
= opendir(dir_path
);
516 entry
= readdir(dir
);
521 if (fstatat(dirfd(dir
), entry
->d_name
, &sb
, 0) == 0) {
522 if (!lru_atime
|| (sb
.st_atime
< lru_atime
)) {
523 size_t len
= strlen(entry
->d_name
);
525 if (!predicate(dir_path
, &sb
, entry
->d_name
, len
))
528 char *tmp
= realloc(lru_name
, len
+ 1);
531 memcpy(lru_name
, entry
->d_name
, len
+ 1);
532 lru_atime
= sb
.st_atime
;
538 if (lru_name
== NULL
) {
543 if (asprintf(&filename
, "%s/%s", dir_path
, lru_name
) < 0)
552 /* Is entry a regular file, and not having a name with a trailing
556 is_regular_non_tmp_file(const char *path
, const struct stat
*sb
,
557 const char *d_name
, const size_t len
)
559 if (!S_ISREG(sb
->st_mode
))
562 if (len
>= 4 && strcmp(&d_name
[len
-4], ".tmp") == 0)
568 /* Returns the size of the deleted file, (or 0 on any error). */
570 unlink_lru_file_from_directory(const char *path
)
575 filename
= choose_lru_file_matching(path
, is_regular_non_tmp_file
);
576 if (filename
== NULL
)
579 if (stat(filename
, &sb
) == -1) {
587 return sb
.st_blocks
* 512;
590 /* Is entry a directory with a two-character name, (and not the
591 * special name of ".."). We also return false if the dir is empty.
594 is_two_character_sub_directory(const char *path
, const struct stat
*sb
,
595 const char *d_name
, const size_t len
)
597 if (!S_ISDIR(sb
->st_mode
))
603 if (strcmp(d_name
, "..") == 0)
607 if (asprintf(&subdir
, "%s/%s", path
, d_name
) == -1)
609 DIR *dir
= opendir(subdir
);
615 unsigned subdir_entries
= 0;
617 while ((d
= readdir(dir
)) != NULL
) {
618 if(++subdir_entries
> 2)
623 /* If dir only contains '.' and '..' it must be empty */
624 if (subdir_entries
<= 2)
631 evict_lru_item(struct disk_cache
*cache
)
635 /* With a reasonably-sized, full cache, (and with keys generated
636 * from a cryptographic hash), we can choose two random hex digits
637 * and reasonably expect the directory to exist with a file in it.
638 * Provides pseudo-LRU eviction to reduce checking all cache files.
640 uint64_t rand64
= rand_xorshift128plus(cache
->seed_xorshift128plus
);
641 if (asprintf(&dir_path
, "%s/%02" PRIx64
, cache
->path
, rand64
& 0xff) < 0)
644 size_t size
= unlink_lru_file_from_directory(dir_path
);
649 p_atomic_add(cache
->size
, - (uint64_t)size
);
653 /* In the case where the random choice of directory didn't find
654 * something, we choose the least recently accessed from the
655 * existing directories.
657 * Really, the only reason this code exists is to allow the unit
658 * tests to work, (which use an artificially-small cache to be able
659 * to force a single cached item to be evicted).
661 dir_path
= choose_lru_file_matching(cache
->path
,
662 is_two_character_sub_directory
);
663 if (dir_path
== NULL
)
666 size
= unlink_lru_file_from_directory(dir_path
);
671 p_atomic_add(cache
->size
, - (uint64_t)size
);
675 disk_cache_remove(struct disk_cache
*cache
, const cache_key key
)
679 char *filename
= get_cache_file(cache
, key
);
680 if (filename
== NULL
) {
684 if (stat(filename
, &sb
) == -1) {
693 p_atomic_add(cache
->size
, - (uint64_t)sb
.st_blocks
* 512);
697 read_all(int fd
, void *buf
, size_t count
)
703 for (done
= 0; done
< count
; done
+= read_ret
) {
704 read_ret
= read(fd
, in
+ done
, count
- done
);
705 if (read_ret
== -1 || read_ret
== 0)
712 write_all(int fd
, const void *buf
, size_t count
)
714 const char *out
= buf
;
718 for (done
= 0; done
< count
; done
+= written
) {
719 written
= write(fd
, out
+ done
, count
- done
);
726 /* From the zlib docs:
727 * "If the memory is available, buffers sizes on the order of 128K or 256K
728 * bytes should be used."
730 #define BUFSIZE 256 * 1024
733 * Compresses cache entry in memory and writes it to disk. Returns the size
734 * of the data written to disk.
737 deflate_and_write_to_disk(const void *in_data
, size_t in_data_size
, int dest
,
738 const char *filename
)
742 /* allocate deflate state */
744 strm
.zalloc
= Z_NULL
;
746 strm
.opaque
= Z_NULL
;
747 strm
.next_in
= (uint8_t *) in_data
;
748 strm
.avail_in
= in_data_size
;
750 int ret
= deflateInit(&strm
, Z_BEST_COMPRESSION
);
754 /* compress until end of in_data */
755 size_t compressed_size
= 0;
758 out
= malloc(BUFSIZE
* sizeof(unsigned char));
763 int remaining
= in_data_size
- BUFSIZE
;
764 flush
= remaining
> 0 ? Z_NO_FLUSH
: Z_FINISH
;
765 in_data_size
-= BUFSIZE
;
767 /* Run deflate() on input until the output buffer is not full (which
768 * means there is no more data to deflate).
771 strm
.avail_out
= BUFSIZE
;
774 ret
= deflate(&strm
, flush
); /* no bad return value */
775 assert(ret
!= Z_STREAM_ERROR
); /* state not clobbered */
777 size_t have
= BUFSIZE
- strm
.avail_out
;
778 compressed_size
+= have
;
780 ssize_t written
= write_all(dest
, out
, have
);
782 (void)deflateEnd(&strm
);
786 } while (strm
.avail_out
== 0);
788 /* all input should be used */
789 assert(strm
.avail_in
== 0);
791 } while (flush
!= Z_FINISH
);
793 /* stream should be complete */
794 assert(ret
== Z_STREAM_END
);
796 /* clean up and return */
797 (void)deflateEnd(&strm
);
799 return compressed_size
;
802 static struct disk_cache_put_job
*
803 create_put_job(struct disk_cache
*cache
, const cache_key key
,
804 const void *data
, size_t size
,
805 struct cache_item_metadata
*cache_item_metadata
)
807 struct disk_cache_put_job
*dc_job
= (struct disk_cache_put_job
*)
808 malloc(sizeof(struct disk_cache_put_job
) + size
);
811 dc_job
->cache
= cache
;
812 memcpy(dc_job
->key
, key
, sizeof(cache_key
));
813 dc_job
->data
= dc_job
+ 1;
814 memcpy(dc_job
->data
, data
, size
);
817 /* Copy the cache item metadata */
818 if (cache_item_metadata
) {
819 dc_job
->cache_item_metadata
.type
= cache_item_metadata
->type
;
820 if (cache_item_metadata
->type
== CACHE_ITEM_TYPE_GLSL
) {
821 dc_job
->cache_item_metadata
.num_keys
=
822 cache_item_metadata
->num_keys
;
823 dc_job
->cache_item_metadata
.keys
= (cache_key
*)
824 malloc(cache_item_metadata
->num_keys
* sizeof(cache_key
));
826 if (!dc_job
->cache_item_metadata
.keys
)
829 memcpy(dc_job
->cache_item_metadata
.keys
,
830 cache_item_metadata
->keys
,
831 sizeof(cache_key
) * cache_item_metadata
->num_keys
);
834 dc_job
->cache_item_metadata
.type
= CACHE_ITEM_TYPE_UNKNOWN
;
835 dc_job
->cache_item_metadata
.keys
= NULL
;
848 destroy_put_job(void *job
, int thread_index
)
851 struct disk_cache_put_job
*dc_job
= (struct disk_cache_put_job
*) job
;
852 free(dc_job
->cache_item_metadata
.keys
);
858 struct cache_entry_file_data
{
860 uint32_t uncompressed_size
;
864 cache_put(void *job
, int thread_index
)
868 int fd
= -1, fd_final
= -1, err
, ret
;
870 char *filename
= NULL
, *filename_tmp
= NULL
;
871 struct disk_cache_put_job
*dc_job
= (struct disk_cache_put_job
*) job
;
873 filename
= get_cache_file(dc_job
->cache
, dc_job
->key
);
874 if (filename
== NULL
)
877 /* If the cache is too large, evict something else first. */
878 while (*dc_job
->cache
->size
+ dc_job
->size
> dc_job
->cache
->max_size
&&
880 evict_lru_item(dc_job
->cache
);
884 /* Write to a temporary file to allow for an atomic rename to the
885 * final destination filename, (to prevent any readers from seeing
886 * a partially written file).
888 if (asprintf(&filename_tmp
, "%s.tmp", filename
) == -1)
891 fd
= open(filename_tmp
, O_WRONLY
| O_CLOEXEC
| O_CREAT
, 0644);
893 /* Make the two-character subdirectory within the cache as needed. */
898 make_cache_file_directory(dc_job
->cache
, dc_job
->key
);
900 fd
= open(filename_tmp
, O_WRONLY
| O_CLOEXEC
| O_CREAT
, 0644);
905 /* With the temporary file open, we take an exclusive flock on
906 * it. If the flock fails, then another process still has the file
907 * open with the flock held. So just let that file be responsible
908 * for writing the file.
910 err
= flock(fd
, LOCK_EX
| LOCK_NB
);
914 /* Now that we have the lock on the open temporary file, we can
915 * check to see if the destination file already exists. If so,
916 * another process won the race between when we saw that the file
917 * didn't exist and now. In this case, we don't do anything more,
918 * (to ensure the size accounting of the cache doesn't get off).
920 fd_final
= open(filename
, O_RDONLY
| O_CLOEXEC
);
921 if (fd_final
!= -1) {
922 unlink(filename_tmp
);
926 /* OK, we're now on the hook to write out a file that we know is
927 * not in the cache, and is also not being written out to the cache
928 * by some other process.
931 /* Write the driver_keys_blob, this can be used find information about the
932 * mesa version that produced the entry or deal with hash collisions,
933 * should that ever become a real problem.
935 ret
= write_all(fd
, dc_job
->cache
->driver_keys_blob
,
936 dc_job
->cache
->driver_keys_blob_size
);
938 unlink(filename_tmp
);
942 /* Write the cache item metadata. This data can be used to deal with
943 * hash collisions, as well as providing useful information to 3rd party
944 * tools reading the cache files.
946 ret
= write_all(fd
, &dc_job
->cache_item_metadata
.type
,
949 unlink(filename_tmp
);
953 if (dc_job
->cache_item_metadata
.type
== CACHE_ITEM_TYPE_GLSL
) {
954 ret
= write_all(fd
, &dc_job
->cache_item_metadata
.num_keys
,
957 unlink(filename_tmp
);
961 ret
= write_all(fd
, dc_job
->cache_item_metadata
.keys
[0],
962 dc_job
->cache_item_metadata
.num_keys
*
965 unlink(filename_tmp
);
970 /* Create CRC of the data. We will read this when restoring the cache and
971 * use it to check for corruption.
973 struct cache_entry_file_data cf_data
;
974 cf_data
.crc32
= util_hash_crc32(dc_job
->data
, dc_job
->size
);
975 cf_data
.uncompressed_size
= dc_job
->size
;
977 size_t cf_data_size
= sizeof(cf_data
);
978 ret
= write_all(fd
, &cf_data
, cf_data_size
);
980 unlink(filename_tmp
);
984 /* Now, finally, write out the contents to the temporary file, then
985 * rename them atomically to the destination filename, and also
986 * perform an atomic increment of the total cache size.
988 size_t file_size
= deflate_and_write_to_disk(dc_job
->data
, dc_job
->size
,
990 if (file_size
== 0) {
991 unlink(filename_tmp
);
994 ret
= rename(filename_tmp
, filename
);
996 unlink(filename_tmp
);
1001 if (stat(filename
, &sb
) == -1) {
1002 /* Something went wrong remove the file */
1007 p_atomic_add(dc_job
->cache
->size
, sb
.st_blocks
* 512);
1012 /* This close finally releases the flock, (now that the final file
1013 * has been renamed into place and the size has been added).
1022 disk_cache_put(struct disk_cache
*cache
, const cache_key key
,
1023 const void *data
, size_t size
,
1024 struct cache_item_metadata
*cache_item_metadata
)
1026 if (cache
->blob_put_cb
) {
1027 cache
->blob_put_cb(key
, CACHE_KEY_SIZE
, data
, size
);
1031 if (cache
->path_init_failed
)
1034 struct disk_cache_put_job
*dc_job
=
1035 create_put_job(cache
, key
, data
, size
, cache_item_metadata
);
1038 util_queue_fence_init(&dc_job
->fence
);
1039 util_queue_add_job(&cache
->cache_queue
, dc_job
, &dc_job
->fence
,
1040 cache_put
, destroy_put_job
, dc_job
->size
);
1045 * Decompresses cache entry, returns true if successful.
1048 inflate_cache_data(uint8_t *in_data
, size_t in_data_size
,
1049 uint8_t *out_data
, size_t out_data_size
)
1053 /* allocate inflate state */
1054 strm
.zalloc
= Z_NULL
;
1055 strm
.zfree
= Z_NULL
;
1056 strm
.opaque
= Z_NULL
;
1057 strm
.next_in
= in_data
;
1058 strm
.avail_in
= in_data_size
;
1059 strm
.next_out
= out_data
;
1060 strm
.avail_out
= out_data_size
;
1062 int ret
= inflateInit(&strm
);
1066 ret
= inflate(&strm
, Z_NO_FLUSH
);
1067 assert(ret
!= Z_STREAM_ERROR
); /* state not clobbered */
1069 /* Unless there was an error we should have decompressed everything in one
1070 * go as we know the uncompressed file size.
1072 if (ret
!= Z_STREAM_END
) {
1073 (void)inflateEnd(&strm
);
1076 assert(strm
.avail_out
== 0);
1078 /* clean up and return */
1079 (void)inflateEnd(&strm
);
1084 disk_cache_get(struct disk_cache
*cache
, const cache_key key
, size_t *size
)
1088 char *filename
= NULL
;
1089 uint8_t *data
= NULL
;
1090 uint8_t *uncompressed_data
= NULL
;
1091 uint8_t *file_header
= NULL
;
1096 if (cache
->blob_get_cb
) {
1097 /* This is what Android EGL defines as the maxValueSize in egl_cache_t
1098 * class implementation.
1100 const signed long max_blob_size
= 64 * 1024;
1101 void *blob
= malloc(max_blob_size
);
1106 cache
->blob_get_cb(key
, CACHE_KEY_SIZE
, blob
, max_blob_size
);
1118 filename
= get_cache_file(cache
, key
);
1119 if (filename
== NULL
)
1122 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
1126 if (fstat(fd
, &sb
) == -1)
1129 data
= malloc(sb
.st_size
);
1133 size_t ck_size
= cache
->driver_keys_blob_size
;
1134 file_header
= malloc(ck_size
);
1138 if (sb
.st_size
< ck_size
)
1141 ret
= read_all(fd
, file_header
, ck_size
);
1145 /* Check for extremely unlikely hash collisions */
1146 if (memcmp(cache
->driver_keys_blob
, file_header
, ck_size
) != 0) {
1147 assert(!"Mesa cache keys mismatch!");
1151 size_t cache_item_md_size
= sizeof(uint32_t);
1153 ret
= read_all(fd
, &md_type
, cache_item_md_size
);
1157 if (md_type
== CACHE_ITEM_TYPE_GLSL
) {
1159 cache_item_md_size
+= sizeof(uint32_t);
1160 ret
= read_all(fd
, &num_keys
, sizeof(uint32_t));
1164 /* The cache item metadata is currently just used for distributing
1165 * precompiled shaders, they are not used by Mesa so just skip them for
1167 * TODO: pass the metadata back to the caller and do some basic
1170 cache_item_md_size
+= num_keys
* sizeof(cache_key
);
1171 ret
= lseek(fd
, num_keys
* sizeof(cache_key
), SEEK_CUR
);
1176 /* Load the CRC that was created when the file was written. */
1177 struct cache_entry_file_data cf_data
;
1178 size_t cf_data_size
= sizeof(cf_data
);
1179 ret
= read_all(fd
, &cf_data
, cf_data_size
);
1183 /* Load the actual cache data. */
1184 size_t cache_data_size
=
1185 sb
.st_size
- cf_data_size
- ck_size
- cache_item_md_size
;
1186 ret
= read_all(fd
, data
, cache_data_size
);
1190 /* Uncompress the cache data */
1191 uncompressed_data
= malloc(cf_data
.uncompressed_size
);
1192 if (!inflate_cache_data(data
, cache_data_size
, uncompressed_data
,
1193 cf_data
.uncompressed_size
))
1196 /* Check the data for corruption */
1197 if (cf_data
.crc32
!= util_hash_crc32(uncompressed_data
,
1198 cf_data
.uncompressed_size
))
1207 *size
= cf_data
.uncompressed_size
;
1209 return uncompressed_data
;
1214 if (uncompressed_data
)
1215 free(uncompressed_data
);
1227 disk_cache_put_key(struct disk_cache
*cache
, const cache_key key
)
1229 const uint32_t *key_chunk
= (const uint32_t *) key
;
1230 int i
= CPU_TO_LE32(*key_chunk
) & CACHE_INDEX_KEY_MASK
;
1231 unsigned char *entry
;
1233 if (cache
->blob_put_cb
) {
1234 cache
->blob_put_cb(key
, CACHE_KEY_SIZE
, key_chunk
, sizeof(uint32_t));
1238 if (cache
->path_init_failed
)
1241 entry
= &cache
->stored_keys
[i
* CACHE_KEY_SIZE
];
1243 memcpy(entry
, key
, CACHE_KEY_SIZE
);
1246 /* This function lets us test whether a given key was previously
1247 * stored in the cache with disk_cache_put_key(). The implement is
1248 * efficient by not using syscalls or hitting the disk. It's not
1249 * race-free, but the races are benign. If we race with someone else
1250 * calling disk_cache_put_key, then that's just an extra cache miss and an
1254 disk_cache_has_key(struct disk_cache
*cache
, const cache_key key
)
1256 const uint32_t *key_chunk
= (const uint32_t *) key
;
1257 int i
= CPU_TO_LE32(*key_chunk
) & CACHE_INDEX_KEY_MASK
;
1258 unsigned char *entry
;
1260 if (cache
->blob_get_cb
) {
1262 return cache
->blob_get_cb(key
, CACHE_KEY_SIZE
, &blob
, sizeof(uint32_t));
1265 if (cache
->path_init_failed
)
1268 entry
= &cache
->stored_keys
[i
* CACHE_KEY_SIZE
];
1270 return memcmp(entry
, key
, CACHE_KEY_SIZE
) == 0;
1274 disk_cache_compute_key(struct disk_cache
*cache
, const void *data
, size_t size
,
1277 struct mesa_sha1 ctx
;
1279 _mesa_sha1_init(&ctx
);
1280 _mesa_sha1_update(&ctx
, cache
->driver_keys_blob
,
1281 cache
->driver_keys_blob_size
);
1282 _mesa_sha1_update(&ctx
, data
, size
);
1283 _mesa_sha1_final(&ctx
, key
);
1287 disk_cache_set_callbacks(struct disk_cache
*cache
, disk_cache_put_cb put
,
1288 disk_cache_get_cb get
)
1290 cache
->blob_put_cb
= put
;
1291 cache
->blob_get_cb
= get
;
1294 #endif /* ENABLE_SHADER_CACHE */