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 <fcntl.h>
36 #include <errno.h>
37 #include <dirent.h>
38 #include <inttypes.h>
39
40 #include "util/compress.h"
41 #include "util/crc32.h"
42 #include "util/u_debug.h"
43 #include "util/rand_xor.h"
44 #include "util/u_atomic.h"
45 #include "util/mesa-sha1.h"
46 #include "util/perf/cpu_trace.h"
47 #include "util/ralloc.h"
48 #include "util/compiler.h"
49
50 #include "disk_cache.h"
51 #include "disk_cache_os.h"
52
53 /* The cache version should be bumped whenever a change is made to the
54 * structure of cache entries or the index. This will give any 3rd party
55 * applications reading the cache entries a chance to adjust to the changes.
56 *
57 * - The cache version is checked internally when reading a cache entry. If we
58 * ever have a mismatch we are in big trouble as this means we had a cache
59 * collision. In case of such an event please check the skys for giant
60 * asteroids and that the entire Mesa team hasn't been eaten by wolves.
61 *
62 * - There is no strict requirement that cache versions be backwards
63 * compatible but effort should be taken to limit disruption where possible.
64 */
65 #define CACHE_VERSION 1
66
67 #define DRV_KEY_CPY(_dst, _src, _src_size) \
68 do { \
69 memcpy(_dst, _src, _src_size); \
70 _dst += _src_size; \
71 } while (0);
72
73 static bool
disk_cache_init_queue(struct disk_cache * cache)74 disk_cache_init_queue(struct disk_cache *cache)
75 {
76 if (util_queue_is_initialized(&cache->cache_queue))
77 return true;
78
79 /* 4 threads were chosen below because just about all modern CPUs currently
80 * available that run Mesa have *at least* 4 cores. For these CPUs allowing
81 * more threads can result in the queue being processed faster, thus
82 * avoiding excessive memory use due to a backlog of cache entrys building
83 * up in the queue. Since we set the UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY
84 * flag this should have little negative impact on low core systems.
85 *
86 * The queue will resize automatically when it's full, so adding new jobs
87 * doesn't stall.
88 */
89 return util_queue_init(&cache->cache_queue, "disk$", 32, 4,
90 UTIL_QUEUE_INIT_RESIZE_IF_FULL |
91 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY |
92 UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY, NULL);
93 }
94
95 static struct disk_cache *
disk_cache_type_create(const char * gpu_name,const char * driver_id,const char * cache_dir_name,uint64_t driver_flags,enum disk_cache_type cache_type,uint64_t max_size)96 disk_cache_type_create(const char *gpu_name,
97 const char *driver_id,
98 const char *cache_dir_name,
99 uint64_t driver_flags,
100 enum disk_cache_type cache_type,
101 uint64_t max_size)
102 {
103 void *local;
104 struct disk_cache *cache = NULL;
105
106 uint8_t cache_version = CACHE_VERSION;
107 size_t cv_size = sizeof(cache_version);
108
109 /* A ralloc context for transient data during this invocation. */
110 local = ralloc_context(NULL);
111 if (local == NULL)
112 goto fail;
113
114 cache = rzalloc(NULL, struct disk_cache);
115 if (cache == NULL)
116 goto fail;
117
118 /* Assume failure. */
119 cache->path_init_failed = true;
120 cache->type = DISK_CACHE_NONE;
121
122 if (!disk_cache_enabled())
123 goto path_fail;
124
125 char *path = disk_cache_generate_cache_dir(local, gpu_name, driver_id,
126 cache_dir_name, cache_type);
127 if (!path)
128 goto path_fail;
129
130 cache->path = ralloc_strdup(cache, path);
131 if (cache->path == NULL)
132 goto path_fail;
133
134 /* Cache tests that want to have a disabled cache compression are using
135 * the "make_check_uncompressed" for the driver_id name. Hence here we
136 * disable disk cache compression when mesa's build tests require it.
137 */
138 if (strcmp(driver_id, "make_check_uncompressed") == 0)
139 cache->compression_disabled = true;
140
141 if (cache_type == DISK_CACHE_SINGLE_FILE) {
142 if (!disk_cache_load_cache_index_foz(local, cache))
143 goto path_fail;
144 } else if (cache_type == DISK_CACHE_DATABASE) {
145 if (!disk_cache_db_load_cache_index(local, cache))
146 goto path_fail;
147 }
148
149 if (!getenv("MESA_SHADER_CACHE_DIR") && !getenv("MESA_GLSL_CACHE_DIR"))
150 disk_cache_touch_cache_user_marker(cache->path);
151
152 cache->type = cache_type;
153
154 cache->stats.enabled = debug_get_bool_option("MESA_SHADER_CACHE_SHOW_STATS",
155 false);
156
157 if (!disk_cache_mmap_cache_index(local, cache, path))
158 goto path_fail;
159
160 cache->max_size = max_size;
161
162 if (cache->type == DISK_CACHE_DATABASE)
163 mesa_cache_db_multipart_set_size_limit(&cache->cache_db, cache->max_size);
164
165 if (!disk_cache_init_queue(cache))
166 goto fail;
167
168 cache->path_init_failed = false;
169
170 path_fail:
171
172 cache->driver_keys_blob_size = cv_size;
173
174 /* Create driver id keys */
175 size_t id_size = strlen(driver_id) + 1;
176 size_t gpu_name_size = strlen(gpu_name) + 1;
177 cache->driver_keys_blob_size += id_size;
178 cache->driver_keys_blob_size += gpu_name_size;
179
180 /* We sometimes store entire structs that contains a pointers in the cache,
181 * use pointer size as a key to avoid hard to debug issues.
182 */
183 uint8_t ptr_size = sizeof(void *);
184 size_t ptr_size_size = sizeof(ptr_size);
185 cache->driver_keys_blob_size += ptr_size_size;
186
187 size_t driver_flags_size = sizeof(driver_flags);
188 cache->driver_keys_blob_size += driver_flags_size;
189
190 cache->driver_keys_blob =
191 ralloc_size(cache, cache->driver_keys_blob_size);
192 if (!cache->driver_keys_blob)
193 goto fail;
194
195 uint8_t *drv_key_blob = cache->driver_keys_blob;
196 DRV_KEY_CPY(drv_key_blob, &cache_version, cv_size)
197 DRV_KEY_CPY(drv_key_blob, driver_id, id_size)
198 DRV_KEY_CPY(drv_key_blob, gpu_name, gpu_name_size)
199 DRV_KEY_CPY(drv_key_blob, &ptr_size, ptr_size_size)
200 DRV_KEY_CPY(drv_key_blob, &driver_flags, driver_flags_size)
201
202 /* Seed our rand function */
203 s_rand_xorshift128plus(cache->seed_xorshift128plus, true);
204
205 ralloc_free(local);
206
207 return cache;
208
209 fail:
210 if (cache)
211 ralloc_free(cache);
212 ralloc_free(local);
213
214 return NULL;
215 }
216
217 struct disk_cache *
disk_cache_create(const char * gpu_name,const char * driver_id,uint64_t driver_flags)218 disk_cache_create(const char *gpu_name, const char *driver_id,
219 uint64_t driver_flags)
220 {
221 enum disk_cache_type cache_type;
222 struct disk_cache *cache;
223 uint64_t max_size = 0;
224 char *max_size_str;
225
226 if (debug_get_bool_option("MESA_DISK_CACHE_SINGLE_FILE", false))
227 cache_type = DISK_CACHE_SINGLE_FILE;
228 else if (debug_get_bool_option("MESA_DISK_CACHE_MULTI_FILE", false))
229 cache_type = DISK_CACHE_MULTI_FILE;
230 else {
231 cache_type = DISK_CACHE_DATABASE;
232 /* Since switching the default cache to <mesa_shader_cache_db>, remove the
233 * old cache folder if it hasn't been modified for more than 7 days.
234 */
235 if (!getenv("MESA_SHADER_CACHE_DIR") && !getenv("MESA_GLSL_CACHE_DIR") && disk_cache_enabled())
236 disk_cache_delete_old_cache();
237 }
238
239 max_size_str = getenv("MESA_SHADER_CACHE_MAX_SIZE");
240
241 if (!max_size_str) {
242 max_size_str = getenv("MESA_GLSL_CACHE_MAX_SIZE");
243 if (max_size_str)
244 fprintf(stderr,
245 "*** MESA_GLSL_CACHE_MAX_SIZE is deprecated; "
246 "use MESA_SHADER_CACHE_MAX_SIZE instead ***\n");
247 }
248
249 #ifdef MESA_SHADER_CACHE_MAX_SIZE
250 if (!max_size_str) {
251 max_size_str = MESA_SHADER_CACHE_MAX_SIZE;
252 }
253 #endif
254
255 if (max_size_str) {
256 char *end;
257 max_size = strtoul(max_size_str, &end, 10);
258 if (end == max_size_str) {
259 max_size = 0;
260 } else {
261 switch (*end) {
262 case 'K':
263 case 'k':
264 max_size *= 1024;
265 break;
266 case 'M':
267 case 'm':
268 max_size *= 1024*1024;
269 break;
270 case '\0':
271 case 'G':
272 case 'g':
273 default:
274 max_size *= 1024*1024*1024;
275 break;
276 }
277 }
278 }
279
280 /* Default to 1GB for maximum cache size. */
281 if (max_size == 0) {
282 max_size = 1024*1024*1024;
283 }
284
285 /* Create main writable cache. */
286 cache = disk_cache_type_create(gpu_name, driver_id, NULL, driver_flags,
287 cache_type, max_size);
288 if (!cache)
289 return NULL;
290
291 /* If MESA_DISK_CACHE_SINGLE_FILE is unset and MESA_DISK_CACHE_COMBINE_RW_WITH_RO_FOZ
292 * is set, then enable additional Fossilize RO caches together with the RW
293 * cache. At first we will check cache entry presence in the RO caches and
294 * if entry isn't found there, then we'll fall back to the RW cache.
295 */
296 if (cache_type != DISK_CACHE_SINGLE_FILE && !cache->path_init_failed &&
297 debug_get_bool_option("MESA_DISK_CACHE_COMBINE_RW_WITH_RO_FOZ", false)) {
298
299 /* Create read-only cache used for sharing prebuilt shaders.
300 * If cache entry will be found in this cache, then the main cache
301 * will be bypassed.
302 */
303 cache->foz_ro_cache = disk_cache_type_create(gpu_name, driver_id, NULL,
304 driver_flags,
305 DISK_CACHE_SINGLE_FILE,
306 max_size);
307 }
308
309 return cache;
310 }
311
312 struct disk_cache *
disk_cache_create_custom(const char * gpu_name,const char * driver_id,uint64_t driver_flags,const char * cache_dir_name,uint32_t max_size)313 disk_cache_create_custom(const char *gpu_name, const char *driver_id,
314 uint64_t driver_flags, const char *cache_dir_name,
315 uint32_t max_size)
316 {
317 return disk_cache_type_create(gpu_name, driver_id, cache_dir_name, 0,
318 DISK_CACHE_DATABASE, max_size);
319 }
320
321 void
disk_cache_destroy(struct disk_cache * cache)322 disk_cache_destroy(struct disk_cache *cache)
323 {
324 if (unlikely(cache && cache->stats.enabled)) {
325 printf("disk shader cache: hits = %u, misses = %u\n",
326 cache->stats.hits,
327 cache->stats.misses);
328 }
329
330 if (cache && util_queue_is_initialized(&cache->cache_queue)) {
331 util_queue_finish(&cache->cache_queue);
332 util_queue_destroy(&cache->cache_queue);
333
334 if (cache->foz_ro_cache)
335 disk_cache_destroy(cache->foz_ro_cache);
336
337 if (cache->type == DISK_CACHE_SINGLE_FILE)
338 foz_destroy(&cache->foz_db);
339
340 if (cache->type == DISK_CACHE_DATABASE)
341 mesa_cache_db_multipart_close(&cache->cache_db);
342
343 disk_cache_destroy_mmap(cache);
344 }
345
346 ralloc_free(cache);
347 }
348
349 void
disk_cache_wait_for_idle(struct disk_cache * cache)350 disk_cache_wait_for_idle(struct disk_cache *cache)
351 {
352 util_queue_finish(&cache->cache_queue);
353 }
354
355 void
disk_cache_remove(struct disk_cache * cache,const cache_key key)356 disk_cache_remove(struct disk_cache *cache, const cache_key key)
357 {
358 if (cache->type == DISK_CACHE_DATABASE) {
359 mesa_cache_db_multipart_entry_remove(&cache->cache_db, key);
360 return;
361 }
362
363 char *filename = disk_cache_get_cache_filename(cache, key);
364 if (filename == NULL) {
365 return;
366 }
367
368 disk_cache_evict_item(cache, filename);
369 }
370
371 static struct disk_cache_put_job *
create_put_job(struct disk_cache * cache,const cache_key key,void * data,size_t size,struct cache_item_metadata * cache_item_metadata,bool take_ownership)372 create_put_job(struct disk_cache *cache, const cache_key key,
373 void *data, size_t size,
374 struct cache_item_metadata *cache_item_metadata,
375 bool take_ownership)
376 {
377 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *)
378 malloc(sizeof(struct disk_cache_put_job) + (take_ownership ? 0 : size));
379
380 if (dc_job) {
381 dc_job->cache = cache;
382 memcpy(dc_job->key, key, sizeof(cache_key));
383 if (take_ownership) {
384 dc_job->data = data;
385 } else {
386 dc_job->data = dc_job + 1;
387 memcpy(dc_job->data, data, size);
388 }
389 dc_job->size = size;
390
391 /* Copy the cache item metadata */
392 if (cache_item_metadata) {
393 dc_job->cache_item_metadata.type = cache_item_metadata->type;
394 if (cache_item_metadata->type == CACHE_ITEM_TYPE_GLSL) {
395 dc_job->cache_item_metadata.num_keys =
396 cache_item_metadata->num_keys;
397 dc_job->cache_item_metadata.keys = (cache_key *)
398 malloc(cache_item_metadata->num_keys * sizeof(cache_key));
399
400 if (!dc_job->cache_item_metadata.keys)
401 goto fail;
402
403 memcpy(dc_job->cache_item_metadata.keys,
404 cache_item_metadata->keys,
405 sizeof(cache_key) * cache_item_metadata->num_keys);
406 }
407 } else {
408 dc_job->cache_item_metadata.type = CACHE_ITEM_TYPE_UNKNOWN;
409 dc_job->cache_item_metadata.keys = NULL;
410 }
411 }
412
413 return dc_job;
414
415 fail:
416 free(dc_job);
417
418 return NULL;
419 }
420
421 static void
destroy_put_job(void * job,void * gdata,int thread_index)422 destroy_put_job(void *job, void *gdata, int thread_index)
423 {
424 if (job) {
425 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
426 free(dc_job->cache_item_metadata.keys);
427 free(job);
428 }
429 }
430
431 static void
destroy_put_job_nocopy(void * job,void * gdata,int thread_index)432 destroy_put_job_nocopy(void *job, void *gdata, int thread_index)
433 {
434 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
435 free(dc_job->data);
436 destroy_put_job(job, gdata, thread_index);
437 }
438
439 static void
440 blob_put_compressed(struct disk_cache *cache, const cache_key key,
441 const void *data, size_t size);
442
443 static void
cache_put(void * job,void * gdata,int thread_index)444 cache_put(void *job, void *gdata, int thread_index)
445 {
446 assert(job);
447
448 unsigned i = 0;
449 char *filename = NULL;
450 struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
451
452 if (dc_job->cache->blob_put_cb) {
453 blob_put_compressed(dc_job->cache, dc_job->key, dc_job->data, dc_job->size);
454 } else if (dc_job->cache->type == DISK_CACHE_SINGLE_FILE) {
455 disk_cache_write_item_to_disk_foz(dc_job);
456 } else if (dc_job->cache->type == DISK_CACHE_DATABASE) {
457 disk_cache_db_write_item_to_disk(dc_job);
458 } else if (dc_job->cache->type == DISK_CACHE_MULTI_FILE) {
459 filename = disk_cache_get_cache_filename(dc_job->cache, dc_job->key);
460 if (filename == NULL)
461 goto done;
462
463 /* If the cache is too large, evict something else first. */
464 while (p_atomic_read_relaxed(&dc_job->cache->size->value) + dc_job->size > dc_job->cache->max_size &&
465 i < 8) {
466 disk_cache_evict_lru_item(dc_job->cache);
467 i++;
468 }
469
470 disk_cache_write_item_to_disk(dc_job, filename);
471
472 done:
473 free(filename);
474 }
475 }
476
477 struct blob_cache_entry {
478 uint32_t uncompressed_size;
479 uint8_t compressed_data[];
480 };
481
482 static void
blob_put_compressed(struct disk_cache * cache,const cache_key key,const void * data,size_t size)483 blob_put_compressed(struct disk_cache *cache, const cache_key key,
484 const void *data, size_t size)
485 {
486 MESA_TRACE_FUNC();
487
488 size_t max_buf = util_compress_max_compressed_len(size);
489 struct blob_cache_entry *entry = malloc(max_buf + sizeof(*entry));
490 if (!entry)
491 goto out;
492
493 entry->uncompressed_size = size;
494
495 size_t compressed_size =
496 util_compress_deflate(data, size, entry->compressed_data, max_buf);
497 if (!compressed_size)
498 goto out;
499
500 unsigned entry_size = compressed_size + sizeof(*entry);
501 // The curly brackets are here to only trace the blob_put_cb call
502 {
503 MESA_TRACE_SCOPE("blob_put");
504 cache->blob_put_cb(key, CACHE_KEY_SIZE, entry, entry_size);
505 }
506
507 out:
508 free(entry);
509 }
510
511 static void *
blob_get_compressed(struct disk_cache * cache,const cache_key key,size_t * size)512 blob_get_compressed(struct disk_cache *cache, const cache_key key,
513 size_t *size)
514 {
515 MESA_TRACE_FUNC();
516
517 /* This is what Android EGL defines as the maxValueSize in egl_cache_t
518 * class implementation.
519 */
520 const signed long max_blob_size = 64 * 1024;
521 struct blob_cache_entry *entry = malloc(max_blob_size);
522 if (!entry)
523 return NULL;
524
525 signed long entry_size;
526 // The curly brackets are here to only trace the blob_get_cb call
527 {
528 MESA_TRACE_SCOPE("blob_get");
529 entry_size = cache->blob_get_cb(key, CACHE_KEY_SIZE, entry, max_blob_size);
530 }
531
532 if (!entry_size) {
533 free(entry);
534 return NULL;
535 }
536
537 void *data = malloc(entry->uncompressed_size);
538 if (!data) {
539 free(entry);
540 return NULL;
541 }
542
543 unsigned compressed_size = entry_size - sizeof(*entry);
544 bool ret = util_compress_inflate(entry->compressed_data, compressed_size,
545 data, entry->uncompressed_size);
546 if (!ret) {
547 free(data);
548 free(entry);
549 return NULL;
550 }
551
552 if (size)
553 *size = entry->uncompressed_size;
554
555 free(entry);
556
557 return data;
558 }
559
560 void
disk_cache_put(struct disk_cache * cache,const cache_key key,const void * data,size_t size,struct cache_item_metadata * cache_item_metadata)561 disk_cache_put(struct disk_cache *cache, const cache_key key,
562 const void *data, size_t size,
563 struct cache_item_metadata *cache_item_metadata)
564 {
565 if (!util_queue_is_initialized(&cache->cache_queue))
566 return;
567
568 struct disk_cache_put_job *dc_job =
569 create_put_job(cache, key, (void*)data, size, cache_item_metadata, false);
570
571 if (dc_job) {
572 util_queue_fence_init(&dc_job->fence);
573 util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence,
574 cache_put, destroy_put_job, dc_job->size);
575 }
576 }
577
578 void
disk_cache_put_nocopy(struct disk_cache * cache,const cache_key key,void * data,size_t size,struct cache_item_metadata * cache_item_metadata)579 disk_cache_put_nocopy(struct disk_cache *cache, const cache_key key,
580 void *data, size_t size,
581 struct cache_item_metadata *cache_item_metadata)
582 {
583 if (!util_queue_is_initialized(&cache->cache_queue)) {
584 free(data);
585 return;
586 }
587
588 struct disk_cache_put_job *dc_job =
589 create_put_job(cache, key, data, size, cache_item_metadata, true);
590
591 if (dc_job) {
592 util_queue_fence_init(&dc_job->fence);
593 util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence,
594 cache_put, destroy_put_job_nocopy, dc_job->size);
595 }
596 }
597
598 void *
disk_cache_get(struct disk_cache * cache,const cache_key key,size_t * size)599 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
600 {
601 void *buf = NULL;
602
603 if (size)
604 *size = 0;
605
606 if (cache->foz_ro_cache)
607 buf = disk_cache_load_item_foz(cache->foz_ro_cache, key, size);
608
609 if (!buf) {
610 if (cache->blob_get_cb) {
611 buf = blob_get_compressed(cache, key, size);
612 } else if (cache->type == DISK_CACHE_SINGLE_FILE) {
613 buf = disk_cache_load_item_foz(cache, key, size);
614 } else if (cache->type == DISK_CACHE_DATABASE) {
615 buf = disk_cache_db_load_item(cache, key, size);
616 } else if (cache->type == DISK_CACHE_MULTI_FILE) {
617 char *filename = disk_cache_get_cache_filename(cache, key);
618 if (filename)
619 buf = disk_cache_load_item(cache, filename, size);
620 }
621 }
622
623 if (unlikely(cache->stats.enabled)) {
624 if (buf)
625 p_atomic_inc(&cache->stats.hits);
626 else
627 p_atomic_inc(&cache->stats.misses);
628 }
629
630 return buf;
631 }
632
633 void
disk_cache_put_key(struct disk_cache * cache,const cache_key key)634 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
635 {
636 const uint32_t *key_chunk = (const uint32_t *) key;
637 int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK;
638 unsigned char *entry;
639
640 if (cache->blob_put_cb) {
641 cache->blob_put_cb(key, CACHE_KEY_SIZE, key_chunk, sizeof(uint32_t));
642 return;
643 }
644
645 if (cache->path_init_failed)
646 return;
647
648 entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
649
650 memcpy(entry, key, CACHE_KEY_SIZE);
651 }
652
653 /* This function lets us test whether a given key was previously
654 * stored in the cache with disk_cache_put_key(). The implement is
655 * efficient by not using syscalls or hitting the disk. It's not
656 * race-free, but the races are benign. If we race with someone else
657 * calling disk_cache_put_key, then that's just an extra cache miss and an
658 * extra recompile.
659 */
660 bool
disk_cache_has_key(struct disk_cache * cache,const cache_key key)661 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
662 {
663 const uint32_t *key_chunk = (const uint32_t *) key;
664 int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK;
665 unsigned char *entry;
666
667 if (cache->blob_get_cb) {
668 uint32_t blob;
669 return cache->blob_get_cb(key, CACHE_KEY_SIZE, &blob, sizeof(uint32_t));
670 }
671
672 if (cache->path_init_failed)
673 return false;
674
675 entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
676
677 return memcmp(entry, key, CACHE_KEY_SIZE) == 0;
678 }
679
680 void
disk_cache_compute_key(struct disk_cache * cache,const void * data,size_t size,cache_key key)681 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
682 cache_key key)
683 {
684 struct mesa_sha1 ctx;
685
686 _mesa_sha1_init(&ctx);
687 _mesa_sha1_update(&ctx, cache->driver_keys_blob,
688 cache->driver_keys_blob_size);
689 _mesa_sha1_update(&ctx, data, size);
690 _mesa_sha1_final(&ctx, key);
691 }
692
693 void
disk_cache_set_callbacks(struct disk_cache * cache,disk_cache_put_cb put,disk_cache_get_cb get)694 disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put,
695 disk_cache_get_cb get)
696 {
697 cache->blob_put_cb = put;
698 cache->blob_get_cb = get;
699 disk_cache_init_queue(cache);
700 }
701
702 #endif /* ENABLE_SHADER_CACHE */
703