1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #ifdef ENABLE_SHADER_CACHE
25
26 #include <ctype.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <sys/file.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <pwd.h>
37 #include <errno.h>
38 #include <dirent.h>
39
40 #include "util/u_atomic.h"
41 #include "util/mesa-sha1.h"
42 #include "util/ralloc.h"
43 #include "main/errors.h"
44
45 #include "disk_cache.h"
46
47 /* Number of bits to mask off from a cache key to get an index. */
48 #define CACHE_INDEX_KEY_BITS 16
49
50 /* Mask for computing an index from a key. */
51 #define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
52
53 /* The number of keys that can be stored in the index. */
54 #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
55
56 struct disk_cache {
57 /* The path to the cache directory. */
58 char *path;
59
60 /* A pointer to the mmapped index file within the cache directory. */
61 uint8_t *index_mmap;
62 size_t index_mmap_size;
63
64 /* Pointer to total size of all objects in cache (within index_mmap) */
65 uint64_t *size;
66
67 /* Pointer to stored keys, (within index_mmap). */
68 uint8_t *stored_keys;
69
70 /* Maximum size of all cached objects (in bytes). */
71 uint64_t max_size;
72 };
73
74 /* Create a directory named 'path' if it does not already exist.
75 *
76 * Returns: 0 if path already exists as a directory or if created.
77 * -1 in all other cases.
78 */
79 static int
mkdir_if_needed(char * path)80 mkdir_if_needed(char *path)
81 {
82 struct stat sb;
83
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.
86 */
87 if (stat(path, &sb) == 0) {
88 if (S_ISDIR(sb.st_mode)) {
89 return 0;
90 } else {
91 fprintf(stderr, "Cannot use %s for shader cache (not a directory)"
92 "---disabling.\n", path);
93 return -1;
94 }
95 }
96
97 int ret = mkdir(path, 0755);
98 if (ret == 0 || (ret == -1 && errno == EEXIST))
99 return 0;
100
101 fprintf(stderr, "Failed to create %s for shader cache (%s)---disabling.\n",
102 path, strerror(errno));
103
104 return -1;
105 }
106
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').
110 *
111 * Returns NULL on any error, such as:
112 *
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
116 */
117 static char *
concatenate_and_mkdir(void * ctx,char * path,char * name)118 concatenate_and_mkdir(void *ctx, char *path, char *name)
119 {
120 char *new_path;
121 struct stat sb;
122
123 if (stat(path, &sb) != 0 || ! S_ISDIR(sb.st_mode))
124 return NULL;
125
126 new_path = ralloc_asprintf(ctx, "%s/%s", path, name);
127
128 if (mkdir_if_needed(new_path) == 0)
129 return new_path;
130 else
131 return NULL;
132 }
133
134 struct disk_cache *
disk_cache_create(void)135 disk_cache_create(void)
136 {
137 void *local;
138 struct disk_cache *cache = NULL;
139 char *path, *max_size_str;
140 uint64_t max_size;
141 int fd = -1;
142 struct stat sb;
143 size_t size;
144
145 /* A ralloc context for transient data during this invocation. */
146 local = ralloc_context(NULL);
147 if (local == NULL)
148 goto fail;
149
150 /* At user request, disable shader cache entirely. */
151 if (getenv("MESA_GLSL_CACHE_DISABLE"))
152 goto fail;
153
154 /* Determine path for cache based on the first defined name as follows:
155 *
156 * $MESA_GLSL_CACHE_DIR
157 * $XDG_CACHE_HOME/mesa
158 * <pwd.pw_dir>/.cache/mesa
159 */
160 path = getenv("MESA_GLSL_CACHE_DIR");
161 if (path && mkdir_if_needed(path) == -1) {
162 goto fail;
163 }
164
165 if (path == NULL) {
166 char *xdg_cache_home = getenv("XDG_CACHE_HOME");
167
168 if (xdg_cache_home) {
169 if (mkdir_if_needed(xdg_cache_home) == -1)
170 goto fail;
171
172 path = concatenate_and_mkdir(local, xdg_cache_home, "mesa");
173 if (path == NULL)
174 goto fail;
175 }
176 }
177
178 if (path == NULL) {
179 char *buf;
180 size_t buf_size;
181 struct passwd pwd, *result;
182
183 buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
184 if (buf_size == -1)
185 buf_size = 512;
186
187 /* Loop until buf_size is large enough to query the directory */
188 while (1) {
189 buf = ralloc_size(local, buf_size);
190
191 getpwuid_r(getuid(), &pwd, buf, buf_size, &result);
192 if (result)
193 break;
194
195 if (errno == ERANGE) {
196 ralloc_free(buf);
197 buf = NULL;
198 buf_size *= 2;
199 } else {
200 goto fail;
201 }
202 }
203
204 path = concatenate_and_mkdir(local, pwd.pw_dir, ".cache");
205 if (path == NULL)
206 goto fail;
207
208 path = concatenate_and_mkdir(local, path, "mesa");
209 if (path == NULL)
210 goto fail;
211 }
212
213 cache = ralloc(NULL, struct disk_cache);
214 if (cache == NULL)
215 goto fail;
216
217 cache->path = ralloc_strdup(cache, path);
218 if (cache->path == NULL)
219 goto fail;
220
221 path = ralloc_asprintf(local, "%s/index", cache->path);
222 if (path == NULL)
223 goto fail;
224
225 fd = open(path, O_RDWR | O_CREAT | O_CLOEXEC, 0644);
226 if (fd == -1)
227 goto fail;
228
229 if (fstat(fd, &sb) == -1)
230 goto fail;
231
232 /* Force the index file to be the expected size. */
233 size = sizeof(*cache->size) + CACHE_INDEX_MAX_KEYS * CACHE_KEY_SIZE;
234 if (sb.st_size != size) {
235 if (ftruncate(fd, size) == -1)
236 goto fail;
237 }
238
239 /* We map this shared so that other processes see updates that we
240 * make.
241 *
242 * Note: We do use atomic addition to ensure that multiple
243 * processes don't scramble the cache size recorded in the
244 * index. But we don't use any locking to prevent multiple
245 * processes from updating the same entry simultaneously. The idea
246 * is that if either result lands entirely in the index, then
247 * that's equivalent to a well-ordered write followed by an
248 * eviction and a write. On the other hand, if the simultaneous
249 * writes result in a corrupt entry, that's not really any
250 * different than both entries being evicted, (since within the
251 * guarantees of the cryptographic hash, a corrupt entry is
252 * unlikely to ever match a real cache key).
253 */
254 cache->index_mmap = mmap(NULL, size, PROT_READ | PROT_WRITE,
255 MAP_SHARED, fd, 0);
256 if (cache->index_mmap == MAP_FAILED)
257 goto fail;
258 cache->index_mmap_size = size;
259
260 close(fd);
261
262 cache->size = (uint64_t *) cache->index_mmap;
263 cache->stored_keys = cache->index_mmap + sizeof(uint64_t);
264
265 max_size = 0;
266
267 max_size_str = getenv("MESA_GLSL_CACHE_MAX_SIZE");
268 if (max_size_str) {
269 char *end;
270 max_size = strtoul(max_size_str, &end, 10);
271 if (end == max_size_str) {
272 max_size = 0;
273 } else {
274 while (*end && isspace(*end))
275 end++;
276 switch (*end) {
277 case 'K':
278 case 'k':
279 max_size *= 1024;
280 break;
281 case 'M':
282 case 'm':
283 max_size *= 1024*1024;
284 break;
285 case '\0':
286 case 'G':
287 case 'g':
288 default:
289 max_size *= 1024*1024*1024;
290 break;
291 }
292 }
293 }
294
295 /* Default to 1GB for maximum cache size. */
296 if (max_size == 0)
297 max_size = 1024*1024*1024;
298
299 cache->max_size = max_size;
300
301 ralloc_free(local);
302
303 return cache;
304
305 fail:
306 if (fd != -1)
307 close(fd);
308 if (cache)
309 ralloc_free(cache);
310 ralloc_free(local);
311
312 return NULL;
313 }
314
315 void
disk_cache_destroy(struct disk_cache * cache)316 disk_cache_destroy(struct disk_cache *cache)
317 {
318 munmap(cache->index_mmap, cache->index_mmap_size);
319
320 ralloc_free(cache);
321 }
322
323 /* Return a filename within the cache's directory corresponding to 'key'. The
324 * returned filename is ralloced with 'cache' as the parent context.
325 *
326 * Returns NULL if out of memory.
327 */
328 static char *
get_cache_file(struct disk_cache * cache,cache_key key)329 get_cache_file(struct disk_cache *cache, cache_key key)
330 {
331 char buf[41];
332
333 _mesa_sha1_format(buf, key);
334
335 return ralloc_asprintf(cache, "%s/%c%c/%s",
336 cache->path, buf[0], buf[1], buf + 2);
337 }
338
339 /* Create the directory that will be needed for the cache file for \key.
340 *
341 * Obviously, the implementation here must closely match
342 * _get_cache_file above.
343 */
344 static void
make_cache_file_directory(struct disk_cache * cache,cache_key key)345 make_cache_file_directory(struct disk_cache *cache, cache_key key)
346 {
347 char *dir;
348 char buf[41];
349
350 _mesa_sha1_format(buf, key);
351
352 dir = ralloc_asprintf(cache, "%s/%c%c", cache->path, buf[0], buf[1]);
353
354 mkdir_if_needed(dir);
355
356 ralloc_free(dir);
357 }
358
359 /* Given a directory path and predicate function, count all entries in
360 * that directory for which the predicate returns true. Then choose a
361 * random entry from among those counted.
362 *
363 * Returns: A malloc'ed string for the path to the chosen file, (or
364 * NULL on any error). The caller should free the string when
365 * finished.
366 */
367 static char *
choose_random_file_matching(const char * dir_path,bool (* predicate)(struct dirent *,const char * dir_path))368 choose_random_file_matching(const char *dir_path,
369 bool (*predicate)(struct dirent *,
370 const char *dir_path))
371 {
372 DIR *dir;
373 struct dirent *entry;
374 unsigned int count, victim;
375 char *filename;
376
377 dir = opendir(dir_path);
378 if (dir == NULL)
379 return NULL;
380
381 count = 0;
382
383 while (1) {
384 entry = readdir(dir);
385 if (entry == NULL)
386 break;
387 if (!predicate(entry, dir_path))
388 continue;
389
390 count++;
391 }
392
393 if (count == 0) {
394 closedir(dir);
395 return NULL;
396 }
397
398 victim = rand() % count;
399
400 rewinddir(dir);
401 count = 0;
402
403 while (1) {
404 entry = readdir(dir);
405 if (entry == NULL)
406 break;
407 if (!predicate(entry, dir_path))
408 continue;
409 if (count == victim)
410 break;
411
412 count++;
413 }
414
415 if (entry == NULL) {
416 closedir(dir);
417 return NULL;
418 }
419
420 if (asprintf(&filename, "%s/%s", dir_path, entry->d_name) < 0)
421 filename = NULL;
422
423 closedir(dir);
424
425 return filename;
426 }
427
428 /* Is entry a regular file, and not having a name with a trailing
429 * ".tmp"
430 */
431 static bool
is_regular_non_tmp_file(struct dirent * entry,const char * path)432 is_regular_non_tmp_file(struct dirent *entry, const char *path)
433 {
434 char *filename;
435 if (asprintf(&filename, "%s/%s", path, entry->d_name) == -1)
436 return false;
437
438 struct stat sb;
439 int res = stat(filename, &sb);
440 free(filename);
441
442 if (res == -1 || !S_ISREG(sb.st_mode))
443 return false;
444
445 size_t len = strlen (entry->d_name);
446 if (len >= 4 && strcmp(&entry->d_name[len-4], ".tmp") == 0)
447 return false;
448
449 return true;
450 }
451
452 /* Returns the size of the deleted file, (or 0 on any error). */
453 static size_t
unlink_random_file_from_directory(const char * path)454 unlink_random_file_from_directory(const char *path)
455 {
456 struct stat sb;
457 char *filename;
458
459 filename = choose_random_file_matching(path, is_regular_non_tmp_file);
460 if (filename == NULL)
461 return 0;
462
463 if (stat(filename, &sb) == -1) {
464 free (filename);
465 return 0;
466 }
467
468 unlink(filename);
469
470 free (filename);
471
472 return sb.st_size;
473 }
474
475 /* Is entry a directory with a two-character name, (and not the
476 * special name of "..")
477 */
478 static bool
is_two_character_sub_directory(struct dirent * entry,const char * path)479 is_two_character_sub_directory(struct dirent *entry, const char *path)
480 {
481 char *subdir;
482 if (asprintf(&subdir, "%s/%s", path, entry->d_name) == -1)
483 return false;
484
485 struct stat sb;
486 int res = stat(subdir, &sb);
487 free(subdir);
488
489 if (res == -1 || !S_ISDIR(sb.st_mode))
490 return false;
491
492 if (strlen(entry->d_name) != 2)
493 return false;
494
495 if (strcmp(entry->d_name, "..") == 0)
496 return false;
497
498 return true;
499 }
500
501 static void
evict_random_item(struct disk_cache * cache)502 evict_random_item(struct disk_cache *cache)
503 {
504 const char hex[] = "0123456789abcde";
505 char *dir_path;
506 int a, b;
507 size_t size;
508
509 /* With a reasonably-sized, full cache, (and with keys generated
510 * from a cryptographic hash), we can choose two random hex digits
511 * and reasonably expect the directory to exist with a file in it.
512 */
513 a = rand() % 16;
514 b = rand() % 16;
515
516 if (asprintf(&dir_path, "%s/%c%c", cache->path, hex[a], hex[b]) < 0)
517 return;
518
519 size = unlink_random_file_from_directory(dir_path);
520
521 free(dir_path);
522
523 if (size) {
524 p_atomic_add(cache->size, - size);
525 return;
526 }
527
528 /* In the case where the random choice of directory didn't find
529 * something, we choose randomly from the existing directories.
530 *
531 * Really, the only reason this code exists is to allow the unit
532 * tests to work, (which use an artificially-small cache to be able
533 * to force a single cached item to be evicted).
534 */
535 dir_path = choose_random_file_matching(cache->path,
536 is_two_character_sub_directory);
537 if (dir_path == NULL)
538 return;
539
540 size = unlink_random_file_from_directory(dir_path);
541
542 free(dir_path);
543
544 if (size)
545 p_atomic_add(cache->size, - size);
546 }
547
548 void
disk_cache_put(struct disk_cache * cache,cache_key key,const void * data,size_t size)549 disk_cache_put(struct disk_cache *cache,
550 cache_key key,
551 const void *data,
552 size_t size)
553 {
554 int fd = -1, fd_final = -1, err, ret;
555 size_t len;
556 char *filename = NULL, *filename_tmp = NULL;
557 const char *p = data;
558
559 filename = get_cache_file(cache, key);
560 if (filename == NULL)
561 goto done;
562
563 /* Write to a temporary file to allow for an atomic rename to the
564 * final destination filename, (to prevent any readers from seeing
565 * a partially written file).
566 */
567 filename_tmp = ralloc_asprintf(cache, "%s.tmp", filename);
568 if (filename_tmp == NULL)
569 goto done;
570
571 fd = open(filename_tmp, O_WRONLY | O_CLOEXEC | O_CREAT, 0644);
572
573 /* Make the two-character subdirectory within the cache as needed. */
574 if (fd == -1) {
575 if (errno != ENOENT)
576 goto done;
577
578 make_cache_file_directory(cache, key);
579
580 fd = open(filename_tmp, O_WRONLY | O_CLOEXEC | O_CREAT, 0644);
581 if (fd == -1)
582 goto done;
583 }
584
585 /* With the temporary file open, we take an exclusive flock on
586 * it. If the flock fails, then another process still has the file
587 * open with the flock held. So just let that file be responsible
588 * for writing the file.
589 */
590 err = flock(fd, LOCK_EX | LOCK_NB);
591 if (err == -1)
592 goto done;
593
594 /* Now that we have the lock on the open temporary file, we can
595 * check to see if the destination file already exists. If so,
596 * another process won the race between when we saw that the file
597 * didn't exist and now. In this case, we don't do anything more,
598 * (to ensure the size accounting of the cache doesn't get off).
599 */
600 fd_final = open(filename, O_RDONLY | O_CLOEXEC);
601 if (fd_final != -1)
602 goto done;
603
604 /* OK, we're now on the hook to write out a file that we know is
605 * not in the cache, and is also not being written out to the cache
606 * by some other process.
607 *
608 * Before we do that, if the cache is too large, evict something
609 * else first.
610 */
611 if (*cache->size + size > cache->max_size)
612 evict_random_item(cache);
613
614 /* Now, finally, write out the contents to the temporary file, then
615 * rename them atomically to the destination filename, and also
616 * perform an atomic increment of the total cache size.
617 */
618 for (len = 0; len < size; len += ret) {
619 ret = write(fd, p + len, size - len);
620 if (ret == -1) {
621 unlink(filename_tmp);
622 goto done;
623 }
624 }
625
626 rename(filename_tmp, filename);
627
628 p_atomic_add(cache->size, size);
629
630 done:
631 if (fd_final != -1)
632 close(fd_final);
633 /* This close finally releases the flock, (now that the final dile
634 * has been renamed into place and the size has been added).
635 */
636 if (fd != -1)
637 close(fd);
638 if (filename_tmp)
639 ralloc_free(filename_tmp);
640 if (filename)
641 ralloc_free(filename);
642 }
643
644 void *
disk_cache_get(struct disk_cache * cache,cache_key key,size_t * size)645 disk_cache_get(struct disk_cache *cache, cache_key key, size_t *size)
646 {
647 int fd = -1, ret, len;
648 struct stat sb;
649 char *filename = NULL;
650 uint8_t *data = NULL;
651
652 if (size)
653 *size = 0;
654
655 filename = get_cache_file(cache, key);
656 if (filename == NULL)
657 goto fail;
658
659 fd = open(filename, O_RDONLY | O_CLOEXEC);
660 if (fd == -1)
661 goto fail;
662
663 if (fstat(fd, &sb) == -1)
664 goto fail;
665
666 data = malloc(sb.st_size);
667 if (data == NULL)
668 goto fail;
669
670 for (len = 0; len < sb.st_size; len += ret) {
671 ret = read(fd, data + len, sb.st_size - len);
672 if (ret == -1)
673 goto fail;
674 }
675
676 ralloc_free(filename);
677 close(fd);
678
679 if (size)
680 *size = sb.st_size;
681
682 return data;
683
684 fail:
685 if (data)
686 free(data);
687 if (filename)
688 ralloc_free(filename);
689 if (fd != -1)
690 close(fd);
691
692 return NULL;
693 }
694
695 void
disk_cache_put_key(struct disk_cache * cache,cache_key key)696 disk_cache_put_key(struct disk_cache *cache, cache_key key)
697 {
698 uint32_t *key_chunk = (uint32_t *) key;
699 int i = *key_chunk & CACHE_INDEX_KEY_MASK;
700 unsigned char *entry;
701
702 entry = &cache->stored_keys[i + CACHE_KEY_SIZE];
703
704 memcpy(entry, key, CACHE_KEY_SIZE);
705 }
706
707 /* This function lets us test whether a given key was previously
708 * stored in the cache with disk_cache_put_key(). The implement is
709 * efficient by not using syscalls or hitting the disk. It's not
710 * race-free, but the races are benign. If we race with someone else
711 * calling disk_cache_put_key, then that's just an extra cache miss and an
712 * extra recompile.
713 */
714 bool
disk_cache_has_key(struct disk_cache * cache,cache_key key)715 disk_cache_has_key(struct disk_cache *cache, cache_key key)
716 {
717 uint32_t *key_chunk = (uint32_t *) key;
718 int i = *key_chunk & CACHE_INDEX_KEY_MASK;
719 unsigned char *entry;
720
721 entry = &cache->stored_keys[i + CACHE_KEY_SIZE];
722
723 return memcmp(entry, key, CACHE_KEY_SIZE) == 0;
724 }
725
726 #endif /* ENABLE_SHADER_CACHE */
727