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