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