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
)
340 _mesa_sha1_format(buf
, key
);
342 return ralloc_asprintf(cache
, "%s/%c%c/%s",
343 cache
->path
, buf
[0], buf
[1], buf
+ 2);
346 /* Create the directory that will be needed for the cache file for \key.
348 * Obviously, the implementation here must closely match
349 * _get_cache_file above.
352 make_cache_file_directory(struct disk_cache
*cache
, cache_key key
)
357 _mesa_sha1_format(buf
, key
);
359 dir
= ralloc_asprintf(cache
, "%s/%c%c", cache
->path
, buf
[0], buf
[1]);
361 mkdir_if_needed(dir
);
366 /* Given a directory path and predicate function, count all entries in
367 * that directory for which the predicate returns true. Then choose a
368 * random entry from among those counted.
370 * Returns: A malloc'ed string for the path to the chosen file, (or
371 * NULL on any error). The caller should free the string when
375 choose_random_file_matching(const char *dir_path
,
376 bool (*predicate
)(struct dirent
*))
379 struct dirent
*entry
;
380 unsigned int count
, victim
;
383 dir
= opendir(dir_path
);
390 entry
= readdir(dir
);
393 if (! predicate(entry
))
404 victim
= rand() % count
;
410 entry
= readdir(dir
);
413 if (! predicate(entry
))
426 if (asprintf(&filename
, "%s/%s", dir_path
, entry
->d_name
) < 0)
434 /* Is entry a regular file, and not having a name with a trailing
438 is_regular_non_tmp_file(struct dirent
*entry
)
442 if (entry
->d_type
!= DT_REG
)
445 len
= strlen (entry
->d_name
);
446 if (len
>= 4 && strcmp(&entry
->d_name
[len
-4], ".tmp") == 0)
452 /* Returns the size of the deleted file, (or 0 on any error). */
454 unlink_random_file_from_directory(const char *path
)
459 filename
= choose_random_file_matching(path
, is_regular_non_tmp_file
);
460 if (filename
== NULL
)
463 if (stat(filename
, &sb
) == -1) {
475 /* Is entry a directory with a two-character name, (and not the
476 * special name of "..")
479 is_two_character_sub_directory(struct dirent
*entry
)
481 if (entry
->d_type
!= DT_DIR
)
484 if (strlen(entry
->d_name
) != 2)
487 if (strcmp(entry
->d_name
, "..") == 0)
494 evict_random_item(struct disk_cache
*cache
)
496 const char hex
[] = "0123456789abcde";
501 /* With a reasonably-sized, full cache, (and with keys generated
502 * from a cryptographic hash), we can choose two random hex digits
503 * and reasonably expect the directory to exist with a file in it.
508 if (asprintf(&dir_path
, "%s/%c%c", cache
->path
, hex
[a
], hex
[b
]) < 0)
511 size
= unlink_random_file_from_directory(dir_path
);
516 p_atomic_add(cache
->size
, - size
);
520 /* In the case where the random choice of directory didn't find
521 * something, we choose randomly from the existing directories.
523 * Really, the only reason this code exists is to allow the unit
524 * tests to work, (which use an artificially-small cache to be able
525 * to force a single cached item to be evicted).
527 dir_path
= choose_random_file_matching(cache
->path
,
528 is_two_character_sub_directory
);
529 if (dir_path
== NULL
)
532 size
= unlink_random_file_from_directory(dir_path
);
537 p_atomic_add(cache
->size
, - size
);
541 disk_cache_put(struct disk_cache
*cache
,
546 int fd
= -1, fd_final
= -1, err
, ret
;
548 char *filename
= NULL
, *filename_tmp
= NULL
;
549 const char *p
= data
;
551 filename
= get_cache_file(cache
, key
);
552 if (filename
== NULL
)
555 /* Write to a temporary file to allow for an atomic rename to the
556 * final destination filename, (to prevent any readers from seeing
557 * a partially written file).
559 filename_tmp
= ralloc_asprintf(cache
, "%s.tmp", filename
);
560 if (filename_tmp
== NULL
)
563 fd
= open(filename_tmp
, O_WRONLY
| O_CLOEXEC
| O_CREAT
, 0644);
565 /* Make the two-character subdirectory within the cache as needed. */
570 make_cache_file_directory(cache
, key
);
572 fd
= open(filename_tmp
, O_WRONLY
| O_CLOEXEC
| O_CREAT
, 0644);
577 /* With the temporary file open, we take an exclusive flock on
578 * it. If the flock fails, then another process still has the file
579 * open with the flock held. So just let that file be responsible
580 * for writing the file.
582 err
= flock(fd
, LOCK_EX
| LOCK_NB
);
586 /* Now that we have the lock on the open temporary file, we can
587 * check to see if the destination file already exists. If so,
588 * another process won the race between when we saw that the file
589 * didn't exist and now. In this case, we don't do anything more,
590 * (to ensure the size accounting of the cache doesn't get off).
592 fd_final
= open(filename
, O_RDONLY
| O_CLOEXEC
);
596 /* OK, we're now on the hook to write out a file that we know is
597 * not in the cache, and is also not being written out to the cache
598 * by some other process.
600 * Before we do that, if the cache is too large, evict something
603 if (*cache
->size
+ size
> cache
->max_size
)
604 evict_random_item(cache
);
606 /* Now, finally, write out the contents to the temporary file, then
607 * rename them atomically to the destination filename, and also
608 * perform an atomic increment of the total cache size.
610 for (len
= 0; len
< size
; len
+= ret
) {
611 ret
= write(fd
, p
+ len
, size
- len
);
613 unlink(filename_tmp
);
618 rename(filename_tmp
, filename
);
620 p_atomic_add(cache
->size
, size
);
625 /* This close finally releases the flock, (now that the final dile
626 * has been renamed into place and the size has been added).
631 ralloc_free(filename_tmp
);
633 ralloc_free(filename
);
637 disk_cache_get(struct disk_cache
*cache
, cache_key key
, size_t *size
)
639 int fd
= -1, ret
, len
;
641 char *filename
= NULL
;
642 uint8_t *data
= NULL
;
647 filename
= get_cache_file(cache
, key
);
648 if (filename
== NULL
)
651 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
655 if (fstat(fd
, &sb
) == -1)
658 data
= malloc(sb
.st_size
);
662 for (len
= 0; len
< sb
.st_size
; len
+= ret
) {
663 ret
= read(fd
, data
+ len
, sb
.st_size
- len
);
668 ralloc_free(filename
);
680 ralloc_free(filename
);
688 disk_cache_put_key(struct disk_cache
*cache
, cache_key key
)
690 uint32_t *key_chunk
= (uint32_t *) key
;
691 int i
= *key_chunk
& CACHE_INDEX_KEY_MASK
;
692 unsigned char *entry
;
694 entry
= &cache
->stored_keys
[i
+ CACHE_KEY_SIZE
];
696 memcpy(entry
, key
, CACHE_KEY_SIZE
);
699 /* This function lets us test whether a given key was previously
700 * stored in the cache with disk_cache_put_key(). The implement is
701 * efficient by not using syscalls or hitting the disk. It's not
702 * race-free, but the races are benign. If we race with someone else
703 * calling disk_cache_put_key, then that's just an extra cache miss and an
707 disk_cache_has_key(struct disk_cache
*cache
, cache_key key
)
709 uint32_t *key_chunk
= (uint32_t *) key
;
710 int i
= *key_chunk
& CACHE_INDEX_KEY_MASK
;
711 unsigned char *entry
;
713 entry
= &cache
->stored_keys
[i
+ CACHE_KEY_SIZE
];
715 return memcmp(entry
, key
, CACHE_KEY_SIZE
) == 0;
718 #endif /* ENABLE_SHADER_CACHE */