17a6b5e0c4f75bf0ac91815453f8f8894b8f9eb1
[mesa.git] / src / util / disk_cache.c
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 <ftw.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <sys/file.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/statvfs.h>
35 #include <sys/mman.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <pwd.h>
39 #include <errno.h>
40 #include <dirent.h>
41 #include "zlib.h"
42
43 #include "util/crc32.h"
44 #include "util/rand_xor.h"
45 #include "util/u_atomic.h"
46 #include "util/u_queue.h"
47 #include "util/mesa-sha1.h"
48 #include "util/ralloc.h"
49 #include "main/errors.h"
50 #include "util/macros.h"
51
52 #include "disk_cache.h"
53
54 /* Number of bits to mask off from a cache key to get an index. */
55 #define CACHE_INDEX_KEY_BITS 16
56
57 /* Mask for computing an index from a key. */
58 #define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
59
60 /* The number of keys that can be stored in the index. */
61 #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
62
63 struct disk_cache {
64 /* The path to the cache directory. */
65 char *path;
66
67 /* Thread queue for compressing and writing cache entries to disk */
68 struct util_queue cache_queue;
69
70 /* Seed for rand, which is used to pick a random directory */
71 uint64_t seed_xorshift128plus[2];
72
73 /* A pointer to the mmapped index file within the cache directory. */
74 uint8_t *index_mmap;
75 size_t index_mmap_size;
76
77 /* Pointer to total size of all objects in cache (within index_mmap) */
78 uint64_t *size;
79
80 /* Pointer to stored keys, (within index_mmap). */
81 uint8_t *stored_keys;
82
83 /* Maximum size of all cached objects (in bytes). */
84 uint64_t max_size;
85 };
86
87 struct disk_cache_put_job {
88 struct util_queue_fence fence;
89
90 struct disk_cache *cache;
91
92 cache_key key;
93
94 /* Copy of cache data to be compressed and written. */
95 void *data;
96
97 /* Size of data to be compressed and written. */
98 size_t size;
99 };
100
101 /* Create a directory named 'path' if it does not already exist.
102 *
103 * Returns: 0 if path already exists as a directory or if created.
104 * -1 in all other cases.
105 */
106 static int
107 mkdir_if_needed(const char *path)
108 {
109 struct stat sb;
110
111 /* If the path exists already, then our work is done if it's a
112 * directory, but it's an error if it is not.
113 */
114 if (stat(path, &sb) == 0) {
115 if (S_ISDIR(sb.st_mode)) {
116 return 0;
117 } else {
118 fprintf(stderr, "Cannot use %s for shader cache (not a directory)"
119 "---disabling.\n", path);
120 return -1;
121 }
122 }
123
124 int ret = mkdir(path, 0755);
125 if (ret == 0 || (ret == -1 && errno == EEXIST))
126 return 0;
127
128 fprintf(stderr, "Failed to create %s for shader cache (%s)---disabling.\n",
129 path, strerror(errno));
130
131 return -1;
132 }
133
134 /* Concatenate an existing path and a new name to form a new path. If the new
135 * path does not exist as a directory, create it then return the resulting
136 * name of the new path (ralloc'ed off of 'ctx').
137 *
138 * Returns NULL on any error, such as:
139 *
140 * <path> does not exist or is not a directory
141 * <path>/<name> exists but is not a directory
142 * <path>/<name> cannot be created as a directory
143 */
144 static char *
145 concatenate_and_mkdir(void *ctx, const char *path, const char *name)
146 {
147 char *new_path;
148 struct stat sb;
149
150 if (stat(path, &sb) != 0 || ! S_ISDIR(sb.st_mode))
151 return NULL;
152
153 new_path = ralloc_asprintf(ctx, "%s/%s", path, name);
154
155 if (mkdir_if_needed(new_path) == 0)
156 return new_path;
157 else
158 return NULL;
159 }
160
161 static int
162 remove_dir(const char *fpath, const struct stat *sb,
163 int typeflag, struct FTW *ftwbuf)
164 {
165 if (S_ISREG(sb->st_mode))
166 unlink(fpath);
167 else if (S_ISDIR(sb->st_mode))
168 rmdir(fpath);
169
170 return 0;
171 }
172
173 static void
174 remove_old_cache_directories(void *mem_ctx, const char *path,
175 const char *timestamp)
176 {
177 DIR *dir = opendir(path);
178
179 struct dirent* d_entry;
180 while((d_entry = readdir(dir)) != NULL)
181 {
182 char *full_path =
183 ralloc_asprintf(mem_ctx, "%s/%s", path, d_entry->d_name);
184
185 struct stat sb;
186 if (stat(full_path, &sb) == 0 && S_ISDIR(sb.st_mode) &&
187 strcmp(d_entry->d_name, timestamp) != 0 &&
188 strcmp(d_entry->d_name, "..") != 0 &&
189 strcmp(d_entry->d_name, ".") != 0) {
190 nftw(full_path, remove_dir, 20, FTW_DEPTH);
191 }
192 }
193
194 closedir(dir);
195 }
196
197 static char *
198 create_mesa_cache_dir(void *mem_ctx, const char *path, const char *timestamp,
199 const char *gpu_name)
200 {
201 char *new_path = concatenate_and_mkdir(mem_ctx, path, "mesa");
202 if (new_path == NULL)
203 return NULL;
204
205 /* Create a parent architecture directory so that we don't remove cache
206 * files for other architectures. In theory we could share the cache
207 * between architectures but we have no way of knowing if they were created
208 * by a compatible Mesa version.
209 */
210 new_path = concatenate_and_mkdir(mem_ctx, new_path, get_arch_bitness_str());
211 if (new_path == NULL)
212 return NULL;
213
214 /* Remove cache directories for old Mesa versions */
215 remove_old_cache_directories(mem_ctx, new_path, timestamp);
216
217 new_path = concatenate_and_mkdir(mem_ctx, new_path, timestamp);
218 if (new_path == NULL)
219 return NULL;
220
221 new_path = concatenate_and_mkdir(mem_ctx, new_path, gpu_name);
222 if (new_path == NULL)
223 return NULL;
224
225 return new_path;
226 }
227
228 struct disk_cache *
229 disk_cache_create(const char *gpu_name, const char *timestamp)
230 {
231 void *local;
232 struct disk_cache *cache = NULL;
233 char *path, *max_size_str;
234 uint64_t max_size;
235 int fd = -1;
236 struct stat sb;
237 struct statvfs vfs = { 0 };
238 size_t size;
239
240 /* If running as a users other than the real user disable cache */
241 if (geteuid() != getuid())
242 return NULL;
243
244 /* A ralloc context for transient data during this invocation. */
245 local = ralloc_context(NULL);
246 if (local == NULL)
247 goto fail;
248
249 /* At user request, disable shader cache entirely. */
250 if (getenv("MESA_GLSL_CACHE_DISABLE"))
251 goto fail;
252
253 /* Determine path for cache based on the first defined name as follows:
254 *
255 * $MESA_GLSL_CACHE_DIR
256 * $XDG_CACHE_HOME/mesa
257 * <pwd.pw_dir>/.cache/mesa
258 */
259 path = getenv("MESA_GLSL_CACHE_DIR");
260 if (path) {
261 if (mkdir_if_needed(path) == -1)
262 goto fail;
263
264 path = create_mesa_cache_dir(local, path, timestamp,
265 gpu_name);
266 if (path == NULL)
267 goto fail;
268 }
269
270 if (path == NULL) {
271 char *xdg_cache_home = getenv("XDG_CACHE_HOME");
272
273 if (xdg_cache_home) {
274 if (mkdir_if_needed(xdg_cache_home) == -1)
275 goto fail;
276
277 path = create_mesa_cache_dir(local, xdg_cache_home, timestamp,
278 gpu_name);
279 if (path == NULL)
280 goto fail;
281 }
282 }
283
284 if (path == NULL) {
285 char *buf;
286 size_t buf_size;
287 struct passwd pwd, *result;
288
289 buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
290 if (buf_size == -1)
291 buf_size = 512;
292
293 /* Loop until buf_size is large enough to query the directory */
294 while (1) {
295 buf = ralloc_size(local, buf_size);
296
297 getpwuid_r(getuid(), &pwd, buf, buf_size, &result);
298 if (result)
299 break;
300
301 if (errno == ERANGE) {
302 ralloc_free(buf);
303 buf = NULL;
304 buf_size *= 2;
305 } else {
306 goto fail;
307 }
308 }
309
310 path = concatenate_and_mkdir(local, pwd.pw_dir, ".cache");
311 if (path == NULL)
312 goto fail;
313
314 path = create_mesa_cache_dir(local, path, timestamp, gpu_name);
315 if (path == NULL)
316 goto fail;
317 }
318
319 cache = ralloc(NULL, struct disk_cache);
320 if (cache == NULL)
321 goto fail;
322
323 cache->path = ralloc_strdup(cache, path);
324 if (cache->path == NULL)
325 goto fail;
326
327 path = ralloc_asprintf(local, "%s/index", cache->path);
328 if (path == NULL)
329 goto fail;
330
331 fd = open(path, O_RDWR | O_CREAT | O_CLOEXEC, 0644);
332 if (fd == -1)
333 goto fail;
334
335 if (fstat(fd, &sb) == -1)
336 goto fail;
337
338 /* Force the index file to be the expected size. */
339 size = sizeof(*cache->size) + CACHE_INDEX_MAX_KEYS * CACHE_KEY_SIZE;
340 if (sb.st_size != size) {
341 if (ftruncate(fd, size) == -1)
342 goto fail;
343 }
344
345 /* We map this shared so that other processes see updates that we
346 * make.
347 *
348 * Note: We do use atomic addition to ensure that multiple
349 * processes don't scramble the cache size recorded in the
350 * index. But we don't use any locking to prevent multiple
351 * processes from updating the same entry simultaneously. The idea
352 * is that if either result lands entirely in the index, then
353 * that's equivalent to a well-ordered write followed by an
354 * eviction and a write. On the other hand, if the simultaneous
355 * writes result in a corrupt entry, that's not really any
356 * different than both entries being evicted, (since within the
357 * guarantees of the cryptographic hash, a corrupt entry is
358 * unlikely to ever match a real cache key).
359 */
360 cache->index_mmap = mmap(NULL, size, PROT_READ | PROT_WRITE,
361 MAP_SHARED, fd, 0);
362 if (cache->index_mmap == MAP_FAILED)
363 goto fail;
364 cache->index_mmap_size = size;
365
366 close(fd);
367
368 cache->size = (uint64_t *) cache->index_mmap;
369 cache->stored_keys = cache->index_mmap + sizeof(uint64_t);
370
371 max_size = 0;
372
373 max_size_str = getenv("MESA_GLSL_CACHE_MAX_SIZE");
374 if (max_size_str) {
375 char *end;
376 max_size = strtoul(max_size_str, &end, 10);
377 if (end == max_size_str) {
378 max_size = 0;
379 } else {
380 switch (*end) {
381 case 'K':
382 case 'k':
383 max_size *= 1024;
384 break;
385 case 'M':
386 case 'm':
387 max_size *= 1024*1024;
388 break;
389 case '\0':
390 case 'G':
391 case 'g':
392 default:
393 max_size *= 1024*1024*1024;
394 break;
395 }
396 }
397 }
398
399 /* Default to 1GB or 10% of filesystem for maximum cache size. */
400 if (max_size == 0) {
401 statvfs(path, &vfs);
402 max_size = MAX2(1024*1024*1024, vfs.f_blocks * vfs.f_bsize / 10);
403 }
404
405 cache->max_size = max_size;
406
407 /* A limit of 32 jobs was choosen as observations of Deus Ex start-up times
408 * showed that we reached at most 11 jobs on an Intel i5-6400 CPU@2.70GHz
409 * (a fairly modest desktop CPU). 1 thread was chosen because we don't
410 * really care about getting things to disk quickly just that it's not
411 * blocking other tasks.
412 */
413 util_queue_init(&cache->cache_queue, "disk_cache", 32, 1);
414
415 /* Seed our rand function */
416 s_rand_xorshift128plus(cache->seed_xorshift128plus, true);
417
418 ralloc_free(local);
419
420 return cache;
421
422 fail:
423 if (fd != -1)
424 close(fd);
425 if (cache)
426 ralloc_free(cache);
427 ralloc_free(local);
428
429 return NULL;
430 }
431
432 void
433 disk_cache_destroy(struct disk_cache *cache)
434 {
435 if (cache) {
436 util_queue_destroy(&cache->cache_queue);
437 munmap(cache->index_mmap, cache->index_mmap_size);
438 }
439
440 ralloc_free(cache);
441 }
442
443 /* Return a filename within the cache's directory corresponding to 'key'. The
444 * returned filename is ralloced with 'cache' as the parent context.
445 *
446 * Returns NULL if out of memory.
447 */
448 static char *
449 get_cache_file(struct disk_cache *cache, const cache_key key)
450 {
451 char buf[41];
452 char *filename;
453
454 _mesa_sha1_format(buf, key);
455 if (asprintf(&filename, "%s/%c%c/%s", cache->path, buf[0],
456 buf[1], buf + 2) == -1)
457 return NULL;
458
459 return filename;
460 }
461
462 /* Create the directory that will be needed for the cache file for \key.
463 *
464 * Obviously, the implementation here must closely match
465 * _get_cache_file above.
466 */
467 static void
468 make_cache_file_directory(struct disk_cache *cache, const cache_key key)
469 {
470 char *dir;
471 char buf[41];
472
473 _mesa_sha1_format(buf, key);
474 if (asprintf(&dir, "%s/%c%c", cache->path, buf[0], buf[1]) == -1)
475 return;
476
477 mkdir_if_needed(dir);
478 free(dir);
479 }
480
481 /* Given a directory path and predicate function, find the entry with
482 * the oldest access time in that directory for which the predicate
483 * returns true.
484 *
485 * Returns: A malloc'ed string for the path to the chosen file, (or
486 * NULL on any error). The caller should free the string when
487 * finished.
488 */
489 static char *
490 choose_lru_file_matching(const char *dir_path,
491 bool (*predicate)(const char *dir_path,
492 const struct stat *,
493 const char *, const size_t))
494 {
495 DIR *dir;
496 struct dirent *entry;
497 char *filename;
498 char *lru_name = NULL;
499 time_t lru_atime = 0;
500
501 dir = opendir(dir_path);
502 if (dir == NULL)
503 return NULL;
504
505 while (1) {
506 entry = readdir(dir);
507 if (entry == NULL)
508 break;
509
510 struct stat sb;
511 if (fstatat(dirfd(dir), entry->d_name, &sb, 0) == 0) {
512 if (!lru_atime || (sb.st_atime < lru_atime)) {
513 size_t len = strlen(entry->d_name);
514
515 if (!predicate(dir_path, &sb, entry->d_name, len))
516 continue;
517
518 char *tmp = realloc(lru_name, len + 1);
519 if (tmp) {
520 lru_name = tmp;
521 memcpy(lru_name, entry->d_name, len + 1);
522 lru_atime = sb.st_atime;
523 }
524 }
525 }
526 }
527
528 if (lru_name == NULL) {
529 closedir(dir);
530 return NULL;
531 }
532
533 if (asprintf(&filename, "%s/%s", dir_path, lru_name) < 0)
534 filename = NULL;
535
536 free(lru_name);
537 closedir(dir);
538
539 return filename;
540 }
541
542 /* Is entry a regular file, and not having a name with a trailing
543 * ".tmp"
544 */
545 static bool
546 is_regular_non_tmp_file(const char *path, const struct stat *sb,
547 const char *d_name, const size_t len)
548 {
549 if (!S_ISREG(sb->st_mode))
550 return false;
551
552 if (len >= 4 && strcmp(&d_name[len-4], ".tmp") == 0)
553 return false;
554
555 return true;
556 }
557
558 /* Returns the size of the deleted file, (or 0 on any error). */
559 static size_t
560 unlink_lru_file_from_directory(const char *path)
561 {
562 struct stat sb;
563 char *filename;
564
565 filename = choose_lru_file_matching(path, is_regular_non_tmp_file);
566 if (filename == NULL)
567 return 0;
568
569 if (stat(filename, &sb) == -1) {
570 free (filename);
571 return 0;
572 }
573
574 unlink(filename);
575 free (filename);
576
577 return sb.st_size;
578 }
579
580 /* Is entry a directory with a two-character name, (and not the
581 * special name of ".."). We also return false if the dir is empty.
582 */
583 static bool
584 is_two_character_sub_directory(const char *path, const struct stat *sb,
585 const char *d_name, const size_t len)
586 {
587 if (!S_ISDIR(sb->st_mode))
588 return false;
589
590 if (len != 2)
591 return false;
592
593 if (strcmp(d_name, "..") == 0)
594 return false;
595
596 char *subdir;
597 if (asprintf(&subdir, "%s/%s", path, d_name) == -1)
598 return false;
599 DIR *dir = opendir(subdir);
600 free(subdir);
601
602 if (dir == NULL)
603 return false;
604
605 unsigned subdir_entries = 0;
606 struct dirent *d;
607 while ((d = readdir(dir)) != NULL) {
608 if(++subdir_entries > 2)
609 break;
610 }
611 closedir(dir);
612
613 /* If dir only contains '.' and '..' it must be empty */
614 if (subdir_entries <= 2)
615 return false;
616
617 return true;
618 }
619
620 static void
621 evict_lru_item(struct disk_cache *cache)
622 {
623 char *dir_path;
624
625 /* With a reasonably-sized, full cache, (and with keys generated
626 * from a cryptographic hash), we can choose two random hex digits
627 * and reasonably expect the directory to exist with a file in it.
628 * Provides pseudo-LRU eviction to reduce checking all cache files.
629 */
630 uint64_t rand64 = rand_xorshift128plus(cache->seed_xorshift128plus);
631 if (asprintf(&dir_path, "%s/%02" PRIx64 , cache->path, rand64 & 0xff) < 0)
632 return;
633
634 size_t size = unlink_lru_file_from_directory(dir_path);
635
636 free(dir_path);
637
638 if (size) {
639 p_atomic_add(cache->size, - (uint64_t)size);
640 return;
641 }
642
643 /* In the case where the random choice of directory didn't find
644 * something, we choose the least recently accessed from the
645 * existing directories.
646 *
647 * Really, the only reason this code exists is to allow the unit
648 * tests to work, (which use an artificially-small cache to be able
649 * to force a single cached item to be evicted).
650 */
651 dir_path = choose_lru_file_matching(cache->path,
652 is_two_character_sub_directory);
653 if (dir_path == NULL)
654 return;
655
656 size = unlink_lru_file_from_directory(dir_path);
657
658 free(dir_path);
659
660 if (size)
661 p_atomic_add(cache->size, - (uint64_t)size);
662 }
663
664 void
665 disk_cache_remove(struct disk_cache *cache, const cache_key key)
666 {
667 struct stat sb;
668
669 char *filename = get_cache_file(cache, key);
670 if (filename == NULL) {
671 return;
672 }
673
674 if (stat(filename, &sb) == -1) {
675 free(filename);
676 return;
677 }
678
679 unlink(filename);
680 free(filename);
681
682 if (sb.st_size)
683 p_atomic_add(cache->size, - (uint64_t)sb.st_size);
684 }
685
686 static ssize_t
687 write_all(int fd, const void *buf, size_t count)
688 {
689 const char *out = buf;
690 ssize_t written;
691 size_t done;
692
693 for (done = 0; done < count; done += written) {
694 written = write(fd, out + done, count - done);
695 if (written == -1)
696 return -1;
697 }
698 return done;
699 }
700
701 /* From the zlib docs:
702 * "If the memory is available, buffers sizes on the order of 128K or 256K
703 * bytes should be used."
704 */
705 #define BUFSIZE 256 * 1024
706
707 /**
708 * Compresses cache entry in memory and writes it to disk. Returns the size
709 * of the data written to disk.
710 */
711 static size_t
712 deflate_and_write_to_disk(const void *in_data, size_t in_data_size, int dest,
713 const char *filename)
714 {
715 unsigned char out[BUFSIZE];
716
717 /* allocate deflate state */
718 z_stream strm;
719 strm.zalloc = Z_NULL;
720 strm.zfree = Z_NULL;
721 strm.opaque = Z_NULL;
722 strm.next_in = (uint8_t *) in_data;
723 strm.avail_in = in_data_size;
724
725 int ret = deflateInit(&strm, Z_BEST_COMPRESSION);
726 if (ret != Z_OK)
727 return 0;
728
729 /* compress until end of in_data */
730 size_t compressed_size = 0;
731 int flush;
732 do {
733 int remaining = in_data_size - BUFSIZE;
734 flush = remaining > 0 ? Z_NO_FLUSH : Z_FINISH;
735 in_data_size -= BUFSIZE;
736
737 /* Run deflate() on input until the output buffer is not full (which
738 * means there is no more data to deflate).
739 */
740 do {
741 strm.avail_out = BUFSIZE;
742 strm.next_out = out;
743
744 ret = deflate(&strm, flush); /* no bad return value */
745 assert(ret != Z_STREAM_ERROR); /* state not clobbered */
746
747 size_t have = BUFSIZE - strm.avail_out;
748 compressed_size += have;
749
750 ssize_t written = write_all(dest, out, have);
751 if (written == -1) {
752 (void)deflateEnd(&strm);
753 return 0;
754 }
755 } while (strm.avail_out == 0);
756
757 /* all input should be used */
758 assert(strm.avail_in == 0);
759
760 } while (flush != Z_FINISH);
761
762 /* stream should be complete */
763 assert(ret == Z_STREAM_END);
764
765 /* clean up and return */
766 (void)deflateEnd(&strm);
767 return compressed_size;
768 }
769
770 static struct disk_cache_put_job *
771 create_put_job(struct disk_cache *cache, const cache_key key,
772 const void *data, size_t size)
773 {
774 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *)
775 malloc(sizeof(struct disk_cache_put_job) + size);
776
777 if (dc_job) {
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);
782 dc_job->size = size;
783 }
784
785 return dc_job;
786 }
787
788 static void
789 destroy_put_job(void *job, int thread_index)
790 {
791 if (job) {
792 free(job);
793 }
794 }
795
796 struct cache_entry_file_data {
797 uint32_t crc32;
798 uint32_t uncompressed_size;
799 };
800
801 static void
802 cache_put(void *job, int thread_index)
803 {
804 assert(job);
805
806 int fd = -1, fd_final = -1, err, ret;
807 unsigned i = 0;
808 char *filename = NULL, *filename_tmp = NULL;
809 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
810
811 filename = get_cache_file(dc_job->cache, dc_job->key);
812 if (filename == NULL)
813 goto done;
814
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 &&
817 i < 8) {
818 evict_lru_item(dc_job->cache);
819 i++;
820 }
821
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).
825 */
826 if (asprintf(&filename_tmp, "%s.tmp", filename) == -1)
827 goto done;
828
829 fd = open(filename_tmp, O_WRONLY | O_CLOEXEC | O_CREAT, 0644);
830
831 /* Make the two-character subdirectory within the cache as needed. */
832 if (fd == -1) {
833 if (errno != ENOENT)
834 goto done;
835
836 make_cache_file_directory(dc_job->cache, dc_job->key);
837
838 fd = open(filename_tmp, O_WRONLY | O_CLOEXEC | O_CREAT, 0644);
839 if (fd == -1)
840 goto done;
841 }
842
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.
847 */
848 err = flock(fd, LOCK_EX | LOCK_NB);
849 if (err == -1)
850 goto done;
851
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).
857 */
858 fd_final = open(filename, O_RDONLY | O_CLOEXEC);
859 if (fd_final != -1) {
860 unlink(filename_tmp);
861 goto done;
862 }
863
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.
867 *
868 * Create CRC of the data and store at the start of the file. We will
869 * read this when restoring the cache and use it to check for corruption.
870 */
871 struct cache_entry_file_data cf_data;
872 cf_data.crc32 = util_hash_crc32(dc_job->data, dc_job->size);
873 cf_data.uncompressed_size = dc_job->size;
874
875 size_t cf_data_size = sizeof(cf_data);
876 ret = write_all(fd, &cf_data, cf_data_size);
877 if (ret == -1) {
878 unlink(filename_tmp);
879 goto done;
880 }
881
882 /* Now, finally, write out the contents to the temporary file, then
883 * rename them atomically to the destination filename, and also
884 * perform an atomic increment of the total cache size.
885 */
886 size_t file_size = deflate_and_write_to_disk(dc_job->data, dc_job->size,
887 fd, filename_tmp);
888 if (file_size == 0) {
889 unlink(filename_tmp);
890 goto done;
891 }
892 ret = rename(filename_tmp, filename);
893 if (ret == -1) {
894 unlink(filename_tmp);
895 goto done;
896 }
897
898 file_size += cf_data_size;
899 p_atomic_add(dc_job->cache->size, file_size);
900
901 done:
902 if (fd_final != -1)
903 close(fd_final);
904 /* This close finally releases the flock, (now that the final file
905 * has been renamed into place and the size has been added).
906 */
907 if (fd != -1)
908 close(fd);
909 if (filename_tmp)
910 free(filename_tmp);
911 if (filename)
912 free(filename);
913 }
914
915 void
916 disk_cache_put(struct disk_cache *cache, const cache_key key,
917 const void *data, size_t size)
918 {
919 struct disk_cache_put_job *dc_job =
920 create_put_job(cache, key, data, size);
921
922 if (dc_job) {
923 util_queue_fence_init(&dc_job->fence);
924 util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence,
925 cache_put, destroy_put_job);
926 }
927 }
928
929 /**
930 * Decompresses cache entry, returns true if successful.
931 */
932 static bool
933 inflate_cache_data(uint8_t *in_data, size_t in_data_size,
934 uint8_t *out_data, size_t out_data_size)
935 {
936 z_stream strm;
937
938 /* allocate inflate state */
939 strm.zalloc = Z_NULL;
940 strm.zfree = Z_NULL;
941 strm.opaque = Z_NULL;
942 strm.next_in = in_data;
943 strm.avail_in = in_data_size;
944 strm.next_out = out_data;
945 strm.avail_out = out_data_size;
946
947 int ret = inflateInit(&strm);
948 if (ret != Z_OK)
949 return false;
950
951 ret = inflate(&strm, Z_NO_FLUSH);
952 assert(ret != Z_STREAM_ERROR); /* state not clobbered */
953
954 /* Unless there was an error we should have decompressed everything in one
955 * go as we know the uncompressed file size.
956 */
957 if (ret != Z_STREAM_END) {
958 (void)inflateEnd(&strm);
959 return false;
960 }
961 assert(strm.avail_out == 0);
962
963 /* clean up and return */
964 (void)inflateEnd(&strm);
965 return true;
966 }
967
968 void *
969 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
970 {
971 int fd = -1, ret, len;
972 struct stat sb;
973 char *filename = NULL;
974 uint8_t *data = NULL;
975 uint8_t *uncompressed_data = NULL;
976
977 if (size)
978 *size = 0;
979
980 filename = get_cache_file(cache, key);
981 if (filename == NULL)
982 goto fail;
983
984 fd = open(filename, O_RDONLY | O_CLOEXEC);
985 if (fd == -1)
986 goto fail;
987
988 if (fstat(fd, &sb) == -1)
989 goto fail;
990
991 data = malloc(sb.st_size);
992 if (data == NULL)
993 goto fail;
994
995 /* Load the CRC that was created when the file was written. */
996 struct cache_entry_file_data cf_data;
997 size_t cf_data_size = sizeof(cf_data);
998 assert(sb.st_size > cf_data_size);
999 for (len = 0; len < cf_data_size; len += ret) {
1000 ret = read(fd, ((uint8_t *) &cf_data) + len, cf_data_size - len);
1001 if (ret == -1)
1002 goto fail;
1003 }
1004
1005 /* Load the actual cache data. */
1006 size_t cache_data_size = sb.st_size - cf_data_size;
1007 for (len = 0; len < cache_data_size; len += ret) {
1008 ret = read(fd, data + len, cache_data_size - len);
1009 if (ret == -1)
1010 goto fail;
1011 }
1012
1013 /* Uncompress the cache data */
1014 uncompressed_data = malloc(cf_data.uncompressed_size);
1015 if (!inflate_cache_data(data, cache_data_size, uncompressed_data,
1016 cf_data.uncompressed_size))
1017 goto fail;
1018
1019 /* Check the data for corruption */
1020 if (cf_data.crc32 != util_hash_crc32(uncompressed_data,
1021 cf_data.uncompressed_size))
1022 goto fail;
1023
1024 free(data);
1025 free(filename);
1026 close(fd);
1027
1028 if (size)
1029 *size = cf_data.uncompressed_size;
1030
1031 return uncompressed_data;
1032
1033 fail:
1034 if (data)
1035 free(data);
1036 if (uncompressed_data)
1037 free(uncompressed_data);
1038 if (filename)
1039 free(filename);
1040 if (fd != -1)
1041 close(fd);
1042
1043 return NULL;
1044 }
1045
1046 void
1047 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
1048 {
1049 const uint32_t *key_chunk = (const uint32_t *) key;
1050 int i = *key_chunk & CACHE_INDEX_KEY_MASK;
1051 unsigned char *entry;
1052
1053 entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
1054
1055 memcpy(entry, key, CACHE_KEY_SIZE);
1056 }
1057
1058 /* This function lets us test whether a given key was previously
1059 * stored in the cache with disk_cache_put_key(). The implement is
1060 * efficient by not using syscalls or hitting the disk. It's not
1061 * race-free, but the races are benign. If we race with someone else
1062 * calling disk_cache_put_key, then that's just an extra cache miss and an
1063 * extra recompile.
1064 */
1065 bool
1066 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
1067 {
1068 const uint32_t *key_chunk = (const uint32_t *) key;
1069 int i = *key_chunk & CACHE_INDEX_KEY_MASK;
1070 unsigned char *entry;
1071
1072 entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
1073
1074 return memcmp(entry, key, CACHE_KEY_SIZE) == 0;
1075 }
1076
1077 void
1078 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
1079 cache_key key)
1080 {
1081 _mesa_sha1_compute(data, size, key);
1082 }
1083
1084 #endif /* ENABLE_SHADER_CACHE */