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
31 #include <sys/types.h>
40 #include "util/u_atomic.h"
41 #include "util/mesa-sha1.h"
42 #include "util/ralloc.h"
43 #include "main/errors.h"
45 #include "disk_cache.h"
47 /* Number of bits to mask off from a cache key to get an index. */
48 #define CACHE_INDEX_KEY_BITS 16
50 /* Mask for computing an index from a key. */
51 #define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
53 /* The number of keys that can be stored in the index. */
54 #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
57 /* The path to the cache directory. */
60 /* A pointer to the mmapped index file within the cache directory. */
62 size_t index_mmap_size
;
64 /* Pointer to total size of all objects in cache (within index_mmap) */
67 /* Pointer to stored keys, (within index_mmap). */
70 /* Maximum size of all cached objects (in bytes). */
74 /* Create a directory named 'path' if it does not already exist.
76 * Returns: 0 if path already exists as a directory or if created.
77 * -1 in all other cases.
80 mkdir_if_needed(char *path
)
84 /* If the path exists already, then our work is done if it's a
85 * directory, but it's an error if it is not.
87 if (stat(path
, &sb
) == 0) {
88 if (S_ISDIR(sb
.st_mode
)) {
91 fprintf(stderr
, "Cannot use %s for shader cache (not a directory)"
92 "---disabling.\n", path
);
97 int ret
= mkdir(path
, 0755);
98 if (ret
== 0 || (ret
== -1 && errno
== EEXIST
))
101 fprintf(stderr
, "Failed to create %s for shader cache (%s)---disabling.\n",
102 path
, strerror(errno
));
107 /* Concatenate an existing path and a new name to form a new path. If the new
108 * path does not exist as a directory, create it then return the resulting
109 * name of the new path (ralloc'ed off of 'ctx').
111 * Returns NULL on any error, such as:
113 * <path> does not exist or is not a directory
114 * <path>/<name> exists but is not a directory
115 * <path>/<name> cannot be created as a directory
118 concatenate_and_mkdir(void *ctx
, char *path
, char *name
)
123 if (stat(path
, &sb
) != 0 || ! S_ISDIR(sb
.st_mode
))
126 new_path
= ralloc_asprintf(ctx
, "%s/%s", path
, name
);
128 if (mkdir_if_needed(new_path
) == 0)
135 disk_cache_create(void)
138 struct disk_cache
*cache
= NULL
;
139 char *path
, *max_size_str
;
145 /* A ralloc context for transient data during this invocation. */
146 local
= ralloc_context(NULL
);
150 /* At user request, disable shader cache entirely. */
151 if (getenv("MESA_GLSL_CACHE_DISABLE"))
154 /* As a temporary measure, (while the shader cache is under
155 * development, and known to not be fully functional), also require
156 * the MESA_GLSL_CACHE_ENABLE variable to be set.
158 if (!getenv("MESA_GLSL_CACHE_ENABLE"))
161 /* Determine path for cache based on the first defined name as follows:
163 * $MESA_GLSL_CACHE_DIR
164 * $XDG_CACHE_HOME/mesa
165 * <pwd.pw_dir>/.cache/mesa
167 path
= getenv("MESA_GLSL_CACHE_DIR");
168 if (path
&& mkdir_if_needed(path
) == -1) {
173 char *xdg_cache_home
= getenv("XDG_CACHE_HOME");
175 if (xdg_cache_home
) {
176 if (mkdir_if_needed(xdg_cache_home
) == -1)
179 path
= concatenate_and_mkdir(local
, xdg_cache_home
, "mesa");
188 struct passwd pwd
, *result
;
190 buf_size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
194 /* Loop until buf_size is large enough to query the directory */
196 buf
= ralloc_size(local
, buf_size
);
198 getpwuid_r(getuid(), &pwd
, buf
, buf_size
, &result
);
202 if (errno
== ERANGE
) {
211 path
= concatenate_and_mkdir(local
, pwd
.pw_dir
, ".cache");
215 path
= concatenate_and_mkdir(local
, path
, "mesa");
220 cache
= ralloc(NULL
, struct disk_cache
);
224 cache
->path
= ralloc_strdup(cache
, path
);
225 if (cache
->path
== NULL
)
228 path
= ralloc_asprintf(local
, "%s/index", cache
->path
);
232 fd
= open(path
, O_RDWR
| O_CREAT
| O_CLOEXEC
, 0644);
236 if (fstat(fd
, &sb
) == -1)
239 /* Force the index file to be the expected size. */
240 size
= sizeof(*cache
->size
) + CACHE_INDEX_MAX_KEYS
* CACHE_KEY_SIZE
;
241 if (sb
.st_size
!= size
) {
242 if (ftruncate(fd
, size
) == -1)
246 /* We map this shared so that other processes see updates that we
249 * Note: We do use atomic addition to ensure that multiple
250 * processes don't scramble the cache size recorded in the
251 * index. But we don't use any locking to prevent multiple
252 * processes from updating the same entry simultaneously. The idea
253 * is that if either result lands entirely in the index, then
254 * that's equivalent to a well-ordered write followed by an
255 * eviction and a write. On the other hand, if the simultaneous
256 * writes result in a corrupt entry, that's not really any
257 * different than both entries being evicted, (since within the
258 * guarantees of the cryptographic hash, a corrupt entry is
259 * unlikely to ever match a real cache key).
261 cache
->index_mmap
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
263 if (cache
->index_mmap
== MAP_FAILED
)
265 cache
->index_mmap_size
= size
;
269 cache
->size
= (uint64_t *) cache
->index_mmap
;
270 cache
->stored_keys
= cache
->index_mmap
+ sizeof(uint64_t);
274 max_size_str
= getenv("MESA_GLSL_CACHE_MAX_SIZE");
277 max_size
= strtoul(max_size_str
, &end
, 10);
278 if (end
== max_size_str
) {
281 while (*end
&& isspace(*end
))
290 max_size
*= 1024*1024;
296 max_size
*= 1024*1024*1024;
302 /* Default to 1GB for maximum cache size. */
304 max_size
= 1024*1024*1024;
306 cache
->max_size
= max_size
;
323 disk_cache_destroy(struct disk_cache
*cache
)
325 munmap(cache
->index_mmap
, cache
->index_mmap_size
);
330 /* Return a filename within the cache's directory corresponding to 'key'. The
331 * returned filename is ralloced with 'cache' as the parent context.
333 * Returns NULL if out of memory.
336 get_cache_file(struct disk_cache
*cache
, cache_key key
)
341 _mesa_sha1_format(buf
, key
);
342 if (asprintf(&filename
, "%s/%c%c/%s", cache
->path
, buf
[0],
343 buf
[1], buf
+ 2) == -1)
349 /* Create the directory that will be needed for the cache file for \key.
351 * Obviously, the implementation here must closely match
352 * _get_cache_file above.
355 make_cache_file_directory(struct disk_cache
*cache
, cache_key key
)
360 _mesa_sha1_format(buf
, key
);
361 if (asprintf(&dir
, "%s/%c%c", cache
->path
, buf
[0], buf
[1]) == -1)
364 mkdir_if_needed(dir
);
368 /* Given a directory path and predicate function, count all entries in
369 * that directory for which the predicate returns true. Then choose a
370 * random entry from among those counted.
372 * Returns: A malloc'ed string for the path to the chosen file, (or
373 * NULL on any error). The caller should free the string when
377 choose_random_file_matching(const char *dir_path
,
378 bool (*predicate
)(struct dirent
*,
379 const char *dir_path
))
382 struct dirent
*entry
;
383 unsigned int count
, victim
;
386 dir
= opendir(dir_path
);
393 entry
= readdir(dir
);
396 if (!predicate(entry
, dir_path
))
407 victim
= rand() % count
;
413 entry
= readdir(dir
);
416 if (!predicate(entry
, dir_path
))
429 if (asprintf(&filename
, "%s/%s", dir_path
, entry
->d_name
) < 0)
437 /* Is entry a regular file, and not having a name with a trailing
441 is_regular_non_tmp_file(struct dirent
*entry
, const char *path
)
444 if (asprintf(&filename
, "%s/%s", path
, entry
->d_name
) == -1)
448 int res
= stat(filename
, &sb
);
451 if (res
== -1 || !S_ISREG(sb
.st_mode
))
454 size_t len
= strlen (entry
->d_name
);
455 if (len
>= 4 && strcmp(&entry
->d_name
[len
-4], ".tmp") == 0)
461 /* Returns the size of the deleted file, (or 0 on any error). */
463 unlink_random_file_from_directory(const char *path
)
468 filename
= choose_random_file_matching(path
, is_regular_non_tmp_file
);
469 if (filename
== NULL
)
472 if (stat(filename
, &sb
) == -1) {
484 /* Is entry a directory with a two-character name, (and not the
485 * special name of "..")
488 is_two_character_sub_directory(struct dirent
*entry
, const char *path
)
491 if (asprintf(&subdir
, "%s/%s", path
, entry
->d_name
) == -1)
495 int res
= stat(subdir
, &sb
);
498 if (res
== -1 || !S_ISDIR(sb
.st_mode
))
501 if (strlen(entry
->d_name
) != 2)
504 if (strcmp(entry
->d_name
, "..") == 0)
511 evict_random_item(struct disk_cache
*cache
)
513 const char hex
[] = "0123456789abcde";
518 /* With a reasonably-sized, full cache, (and with keys generated
519 * from a cryptographic hash), we can choose two random hex digits
520 * and reasonably expect the directory to exist with a file in it.
525 if (asprintf(&dir_path
, "%s/%c%c", cache
->path
, hex
[a
], hex
[b
]) < 0)
528 size
= unlink_random_file_from_directory(dir_path
);
533 p_atomic_add(cache
->size
, - size
);
537 /* In the case where the random choice of directory didn't find
538 * something, we choose randomly from the existing directories.
540 * Really, the only reason this code exists is to allow the unit
541 * tests to work, (which use an artificially-small cache to be able
542 * to force a single cached item to be evicted).
544 dir_path
= choose_random_file_matching(cache
->path
,
545 is_two_character_sub_directory
);
546 if (dir_path
== NULL
)
549 size
= unlink_random_file_from_directory(dir_path
);
554 p_atomic_add(cache
->size
, - size
);
558 disk_cache_remove(struct disk_cache
*cache
, cache_key key
)
562 char *filename
= get_cache_file(cache
, key
);
563 if (filename
== NULL
) {
567 if (stat(filename
, &sb
) == -1) {
576 p_atomic_add(cache
->size
, - sb
.st_size
);
580 disk_cache_put(struct disk_cache
*cache
,
585 int fd
= -1, fd_final
= -1, err
, ret
;
587 char *filename
= NULL
, *filename_tmp
= NULL
;
588 const char *p
= data
;
590 filename
= get_cache_file(cache
, key
);
591 if (filename
== NULL
)
594 /* Write to a temporary file to allow for an atomic rename to the
595 * final destination filename, (to prevent any readers from seeing
596 * a partially written file).
598 if (asprintf(&filename_tmp
, "%s.tmp", filename
) == -1)
601 fd
= open(filename_tmp
, O_WRONLY
| O_CLOEXEC
| O_CREAT
, 0644);
603 /* Make the two-character subdirectory within the cache as needed. */
608 make_cache_file_directory(cache
, key
);
610 fd
= open(filename_tmp
, O_WRONLY
| O_CLOEXEC
| O_CREAT
, 0644);
615 /* With the temporary file open, we take an exclusive flock on
616 * it. If the flock fails, then another process still has the file
617 * open with the flock held. So just let that file be responsible
618 * for writing the file.
620 err
= flock(fd
, LOCK_EX
| LOCK_NB
);
624 /* Now that we have the lock on the open temporary file, we can
625 * check to see if the destination file already exists. If so,
626 * another process won the race between when we saw that the file
627 * didn't exist and now. In this case, we don't do anything more,
628 * (to ensure the size accounting of the cache doesn't get off).
630 fd_final
= open(filename
, O_RDONLY
| O_CLOEXEC
);
634 /* OK, we're now on the hook to write out a file that we know is
635 * not in the cache, and is also not being written out to the cache
636 * by some other process.
638 * Before we do that, if the cache is too large, evict something
641 if (*cache
->size
+ size
> cache
->max_size
)
642 evict_random_item(cache
);
644 /* Now, finally, write out the contents to the temporary file, then
645 * rename them atomically to the destination filename, and also
646 * perform an atomic increment of the total cache size.
648 for (len
= 0; len
< size
; len
+= ret
) {
649 ret
= write(fd
, p
+ len
, size
- len
);
651 unlink(filename_tmp
);
656 rename(filename_tmp
, filename
);
658 p_atomic_add(cache
->size
, size
);
663 /* This close finally releases the flock, (now that the final dile
664 * has been renamed into place and the size has been added).
675 disk_cache_get(struct disk_cache
*cache
, cache_key key
, size_t *size
)
677 int fd
= -1, ret
, len
;
679 char *filename
= NULL
;
680 uint8_t *data
= NULL
;
685 filename
= get_cache_file(cache
, key
);
686 if (filename
== NULL
)
689 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
693 if (fstat(fd
, &sb
) == -1)
696 data
= malloc(sb
.st_size
);
700 for (len
= 0; len
< sb
.st_size
; len
+= ret
) {
701 ret
= read(fd
, data
+ len
, sb
.st_size
- len
);
726 disk_cache_put_key(struct disk_cache
*cache
, cache_key key
)
728 uint32_t *key_chunk
= (uint32_t *) key
;
729 int i
= *key_chunk
& CACHE_INDEX_KEY_MASK
;
730 unsigned char *entry
;
732 entry
= &cache
->stored_keys
[i
+ CACHE_KEY_SIZE
];
734 memcpy(entry
, key
, CACHE_KEY_SIZE
);
737 /* This function lets us test whether a given key was previously
738 * stored in the cache with disk_cache_put_key(). The implement is
739 * efficient by not using syscalls or hitting the disk. It's not
740 * race-free, but the races are benign. If we race with someone else
741 * calling disk_cache_put_key, then that's just an extra cache miss and an
745 disk_cache_has_key(struct disk_cache
*cache
, cache_key key
)
747 uint32_t *key_chunk
= (uint32_t *) key
;
748 int i
= *key_chunk
& CACHE_INDEX_KEY_MASK
;
749 unsigned char *entry
;
751 entry
= &cache
->stored_keys
[i
+ CACHE_KEY_SIZE
];
753 return memcmp(entry
, key
, CACHE_KEY_SIZE
) == 0;
756 #endif /* ENABLE_SHADER_CACHE */