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