xref: /aosp_15_r20/external/mesa3d/src/util/disk_cache.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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,uint64_t driver_flags,enum disk_cache_type cache_type)96 disk_cache_type_create(const char *gpu_name,
97                        const char *driver_id,
98                        uint64_t driver_flags,
99                        enum disk_cache_type cache_type)
100 {
101    void *local;
102    struct disk_cache *cache = NULL;
103    char *max_size_str;
104    uint64_t max_size;
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_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    max_size = 0;
161 
162    max_size_str = getenv("MESA_SHADER_CACHE_MAX_SIZE");
163 
164    if (!max_size_str) {
165       max_size_str = getenv("MESA_GLSL_CACHE_MAX_SIZE");
166       if (max_size_str)
167          fprintf(stderr,
168                  "*** MESA_GLSL_CACHE_MAX_SIZE is deprecated; "
169                  "use MESA_SHADER_CACHE_MAX_SIZE instead ***\n");
170    }
171 
172    #ifdef MESA_SHADER_CACHE_MAX_SIZE
173    if( !max_size_str ) {
174       max_size_str = MESA_SHADER_CACHE_MAX_SIZE;
175    }
176    #endif
177 
178    if (max_size_str) {
179       char *end;
180       max_size = strtoul(max_size_str, &end, 10);
181       if (end == max_size_str) {
182          max_size = 0;
183       } else {
184          switch (*end) {
185          case 'K':
186          case 'k':
187             max_size *= 1024;
188             break;
189          case 'M':
190          case 'm':
191             max_size *= 1024*1024;
192             break;
193          case '\0':
194          case 'G':
195          case 'g':
196          default:
197             max_size *= 1024*1024*1024;
198             break;
199          }
200       }
201    }
202 
203    /* Default to 1GB for maximum cache size. */
204    if (max_size == 0) {
205       max_size = 1024*1024*1024;
206    }
207 
208    cache->max_size = max_size;
209 
210    if (cache->type == DISK_CACHE_DATABASE)
211       mesa_cache_db_multipart_set_size_limit(&cache->cache_db, cache->max_size);
212 
213    if (!disk_cache_init_queue(cache))
214       goto fail;
215 
216    cache->path_init_failed = false;
217 
218  path_fail:
219 
220    cache->driver_keys_blob_size = cv_size;
221 
222    /* Create driver id keys */
223    size_t id_size = strlen(driver_id) + 1;
224    size_t gpu_name_size = strlen(gpu_name) + 1;
225    cache->driver_keys_blob_size += id_size;
226    cache->driver_keys_blob_size += gpu_name_size;
227 
228    /* We sometimes store entire structs that contains a pointers in the cache,
229     * use pointer size as a key to avoid hard to debug issues.
230     */
231    uint8_t ptr_size = sizeof(void *);
232    size_t ptr_size_size = sizeof(ptr_size);
233    cache->driver_keys_blob_size += ptr_size_size;
234 
235    size_t driver_flags_size = sizeof(driver_flags);
236    cache->driver_keys_blob_size += driver_flags_size;
237 
238    cache->driver_keys_blob =
239       ralloc_size(cache, cache->driver_keys_blob_size);
240    if (!cache->driver_keys_blob)
241       goto fail;
242 
243    uint8_t *drv_key_blob = cache->driver_keys_blob;
244    DRV_KEY_CPY(drv_key_blob, &cache_version, cv_size)
245    DRV_KEY_CPY(drv_key_blob, driver_id, id_size)
246    DRV_KEY_CPY(drv_key_blob, gpu_name, gpu_name_size)
247    DRV_KEY_CPY(drv_key_blob, &ptr_size, ptr_size_size)
248    DRV_KEY_CPY(drv_key_blob, &driver_flags, driver_flags_size)
249 
250    /* Seed our rand function */
251    s_rand_xorshift128plus(cache->seed_xorshift128plus, true);
252 
253    ralloc_free(local);
254 
255    return cache;
256 
257  fail:
258    if (cache)
259       ralloc_free(cache);
260    ralloc_free(local);
261 
262    return NULL;
263 }
264 
265 struct disk_cache *
disk_cache_create(const char * gpu_name,const char * driver_id,uint64_t driver_flags)266 disk_cache_create(const char *gpu_name, const char *driver_id,
267                   uint64_t driver_flags)
268 {
269    enum disk_cache_type cache_type;
270    struct disk_cache *cache;
271 
272    if (debug_get_bool_option("MESA_DISK_CACHE_SINGLE_FILE", false))
273       cache_type = DISK_CACHE_SINGLE_FILE;
274    else if (debug_get_bool_option("MESA_DISK_CACHE_MULTI_FILE", false))
275       cache_type = DISK_CACHE_MULTI_FILE;
276    else {
277       cache_type = DISK_CACHE_DATABASE;
278       /* Since switching the default cache to <mesa_shader_cache_db>, remove the
279        * old cache folder if it hasn't been modified for more than 7 days.
280        */
281       if (!getenv("MESA_SHADER_CACHE_DIR") && !getenv("MESA_GLSL_CACHE_DIR"))
282          disk_cache_delete_old_cache();
283    }
284 
285    /* Create main writable cache. */
286    cache = disk_cache_type_create(gpu_name, driver_id, driver_flags,
287                                   cache_type);
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,
304                                                    driver_flags,
305                                                    DISK_CACHE_SINGLE_FILE);
306    }
307 
308    return cache;
309 }
310 
311 void
disk_cache_destroy(struct disk_cache * cache)312 disk_cache_destroy(struct disk_cache *cache)
313 {
314    if (unlikely(cache && cache->stats.enabled)) {
315       printf("disk shader cache:  hits = %u, misses = %u\n",
316              cache->stats.hits,
317              cache->stats.misses);
318    }
319 
320    if (cache && util_queue_is_initialized(&cache->cache_queue)) {
321       util_queue_finish(&cache->cache_queue);
322       util_queue_destroy(&cache->cache_queue);
323 
324       if (cache->foz_ro_cache)
325          disk_cache_destroy(cache->foz_ro_cache);
326 
327       if (cache->type == DISK_CACHE_SINGLE_FILE)
328          foz_destroy(&cache->foz_db);
329 
330       if (cache->type == DISK_CACHE_DATABASE)
331          mesa_cache_db_multipart_close(&cache->cache_db);
332 
333       disk_cache_destroy_mmap(cache);
334    }
335 
336    ralloc_free(cache);
337 }
338 
339 void
disk_cache_wait_for_idle(struct disk_cache * cache)340 disk_cache_wait_for_idle(struct disk_cache *cache)
341 {
342    util_queue_finish(&cache->cache_queue);
343 }
344 
345 void
disk_cache_remove(struct disk_cache * cache,const cache_key key)346 disk_cache_remove(struct disk_cache *cache, const cache_key key)
347 {
348    if (cache->type == DISK_CACHE_DATABASE) {
349       mesa_cache_db_multipart_entry_remove(&cache->cache_db, key);
350       return;
351    }
352 
353    char *filename = disk_cache_get_cache_filename(cache, key);
354    if (filename == NULL) {
355       return;
356    }
357 
358    disk_cache_evict_item(cache, filename);
359 }
360 
361 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)362 create_put_job(struct disk_cache *cache, const cache_key key,
363                void *data, size_t size,
364                struct cache_item_metadata *cache_item_metadata,
365                bool take_ownership)
366 {
367    struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *)
368       malloc(sizeof(struct disk_cache_put_job) + (take_ownership ? 0 : size));
369 
370    if (dc_job) {
371       dc_job->cache = cache;
372       memcpy(dc_job->key, key, sizeof(cache_key));
373       if (take_ownership) {
374          dc_job->data = data;
375       } else {
376          dc_job->data = dc_job + 1;
377          memcpy(dc_job->data, data, size);
378       }
379       dc_job->size = size;
380 
381       /* Copy the cache item metadata */
382       if (cache_item_metadata) {
383          dc_job->cache_item_metadata.type = cache_item_metadata->type;
384          if (cache_item_metadata->type == CACHE_ITEM_TYPE_GLSL) {
385             dc_job->cache_item_metadata.num_keys =
386                cache_item_metadata->num_keys;
387             dc_job->cache_item_metadata.keys = (cache_key *)
388                malloc(cache_item_metadata->num_keys * sizeof(cache_key));
389 
390             if (!dc_job->cache_item_metadata.keys)
391                goto fail;
392 
393             memcpy(dc_job->cache_item_metadata.keys,
394                    cache_item_metadata->keys,
395                    sizeof(cache_key) * cache_item_metadata->num_keys);
396          }
397       } else {
398          dc_job->cache_item_metadata.type = CACHE_ITEM_TYPE_UNKNOWN;
399          dc_job->cache_item_metadata.keys = NULL;
400       }
401    }
402 
403    return dc_job;
404 
405 fail:
406    free(dc_job);
407 
408    return NULL;
409 }
410 
411 static void
destroy_put_job(void * job,void * gdata,int thread_index)412 destroy_put_job(void *job, void *gdata, int thread_index)
413 {
414    if (job) {
415       struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
416       free(dc_job->cache_item_metadata.keys);
417       free(job);
418    }
419 }
420 
421 static void
destroy_put_job_nocopy(void * job,void * gdata,int thread_index)422 destroy_put_job_nocopy(void *job, void *gdata, int thread_index)
423 {
424    struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
425    free(dc_job->data);
426    destroy_put_job(job, gdata, thread_index);
427 }
428 
429 static void
430 blob_put_compressed(struct disk_cache *cache, const cache_key key,
431          const void *data, size_t size);
432 
433 static void
cache_put(void * job,void * gdata,int thread_index)434 cache_put(void *job, void *gdata, int thread_index)
435 {
436    assert(job);
437 
438    unsigned i = 0;
439    char *filename = NULL;
440    struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job;
441 
442    if (dc_job->cache->blob_put_cb) {
443       blob_put_compressed(dc_job->cache, dc_job->key, dc_job->data, dc_job->size);
444    } else if (dc_job->cache->type == DISK_CACHE_SINGLE_FILE) {
445       disk_cache_write_item_to_disk_foz(dc_job);
446    } else if (dc_job->cache->type == DISK_CACHE_DATABASE) {
447       disk_cache_db_write_item_to_disk(dc_job);
448    } else if (dc_job->cache->type == DISK_CACHE_MULTI_FILE) {
449       filename = disk_cache_get_cache_filename(dc_job->cache, dc_job->key);
450       if (filename == NULL)
451          goto done;
452 
453       /* If the cache is too large, evict something else first. */
454       while (p_atomic_read_relaxed(&dc_job->cache->size->value) + dc_job->size > dc_job->cache->max_size &&
455              i < 8) {
456          disk_cache_evict_lru_item(dc_job->cache);
457          i++;
458       }
459 
460       disk_cache_write_item_to_disk(dc_job, filename);
461 
462 done:
463       free(filename);
464    }
465 }
466 
467 struct blob_cache_entry {
468    uint32_t uncompressed_size;
469    uint8_t compressed_data[];
470 };
471 
472 static void
blob_put_compressed(struct disk_cache * cache,const cache_key key,const void * data,size_t size)473 blob_put_compressed(struct disk_cache *cache, const cache_key key,
474          const void *data, size_t size)
475 {
476    MESA_TRACE_FUNC();
477 
478    size_t max_buf = util_compress_max_compressed_len(size);
479    struct blob_cache_entry *entry = malloc(max_buf + sizeof(*entry));
480    if (!entry)
481       goto out;
482 
483    entry->uncompressed_size = size;
484 
485    size_t compressed_size =
486          util_compress_deflate(data, size, entry->compressed_data, max_buf);
487    if (!compressed_size)
488       goto out;
489 
490    unsigned entry_size = compressed_size + sizeof(*entry);
491    // The curly brackets are here to only trace the blob_put_cb call
492    {
493       MESA_TRACE_SCOPE("blob_put");
494       cache->blob_put_cb(key, CACHE_KEY_SIZE, entry, entry_size);
495    }
496 
497 out:
498    free(entry);
499 }
500 
501 static void *
blob_get_compressed(struct disk_cache * cache,const cache_key key,size_t * size)502 blob_get_compressed(struct disk_cache *cache, const cache_key key,
503                     size_t *size)
504 {
505    MESA_TRACE_FUNC();
506 
507    /* This is what Android EGL defines as the maxValueSize in egl_cache_t
508     * class implementation.
509     */
510    const signed long max_blob_size = 64 * 1024;
511    struct blob_cache_entry *entry = malloc(max_blob_size);
512    if (!entry)
513       return NULL;
514 
515    signed long entry_size;
516    // The curly brackets are here to only trace the blob_get_cb call
517    {
518       MESA_TRACE_SCOPE("blob_get");
519       entry_size = cache->blob_get_cb(key, CACHE_KEY_SIZE, entry, max_blob_size);
520    }
521 
522    if (!entry_size) {
523       free(entry);
524       return NULL;
525    }
526 
527    void *data = malloc(entry->uncompressed_size);
528    if (!data) {
529       free(entry);
530       return NULL;
531    }
532 
533    unsigned compressed_size = entry_size - sizeof(*entry);
534    bool ret = util_compress_inflate(entry->compressed_data, compressed_size,
535                                     data, entry->uncompressed_size);
536    if (!ret) {
537       free(data);
538       free(entry);
539       return NULL;
540    }
541 
542    if (size)
543       *size = entry->uncompressed_size;
544 
545    free(entry);
546 
547    return data;
548 }
549 
550 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)551 disk_cache_put(struct disk_cache *cache, const cache_key key,
552                const void *data, size_t size,
553                struct cache_item_metadata *cache_item_metadata)
554 {
555    if (!util_queue_is_initialized(&cache->cache_queue))
556       return;
557 
558    struct disk_cache_put_job *dc_job =
559       create_put_job(cache, key, (void*)data, size, cache_item_metadata, false);
560 
561    if (dc_job) {
562       util_queue_fence_init(&dc_job->fence);
563       util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence,
564                          cache_put, destroy_put_job, dc_job->size);
565    }
566 }
567 
568 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)569 disk_cache_put_nocopy(struct disk_cache *cache, const cache_key key,
570                       void *data, size_t size,
571                       struct cache_item_metadata *cache_item_metadata)
572 {
573    if (!util_queue_is_initialized(&cache->cache_queue)) {
574       free(data);
575       return;
576    }
577 
578    struct disk_cache_put_job *dc_job =
579       create_put_job(cache, key, data, size, cache_item_metadata, true);
580 
581    if (dc_job) {
582       util_queue_fence_init(&dc_job->fence);
583       util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence,
584                          cache_put, destroy_put_job_nocopy, dc_job->size);
585    }
586 }
587 
588 void *
disk_cache_get(struct disk_cache * cache,const cache_key key,size_t * size)589 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
590 {
591    void *buf = NULL;
592 
593    if (size)
594       *size = 0;
595 
596    if (cache->foz_ro_cache)
597       buf = disk_cache_load_item_foz(cache->foz_ro_cache, key, size);
598 
599    if (!buf) {
600       if (cache->blob_get_cb) {
601          buf = blob_get_compressed(cache, key, size);
602       } else if (cache->type == DISK_CACHE_SINGLE_FILE) {
603          buf = disk_cache_load_item_foz(cache, key, size);
604       } else if (cache->type == DISK_CACHE_DATABASE) {
605          buf = disk_cache_db_load_item(cache, key, size);
606       } else if (cache->type == DISK_CACHE_MULTI_FILE) {
607          char *filename = disk_cache_get_cache_filename(cache, key);
608          if (filename)
609             buf = disk_cache_load_item(cache, filename, size);
610       }
611    }
612 
613    if (unlikely(cache->stats.enabled)) {
614       if (buf)
615          p_atomic_inc(&cache->stats.hits);
616       else
617          p_atomic_inc(&cache->stats.misses);
618    }
619 
620    return buf;
621 }
622 
623 void
disk_cache_put_key(struct disk_cache * cache,const cache_key key)624 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
625 {
626    const uint32_t *key_chunk = (const uint32_t *) key;
627    int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK;
628    unsigned char *entry;
629 
630    if (cache->blob_put_cb) {
631       cache->blob_put_cb(key, CACHE_KEY_SIZE, key_chunk, sizeof(uint32_t));
632       return;
633    }
634 
635    if (cache->path_init_failed)
636       return;
637 
638    entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
639 
640    memcpy(entry, key, CACHE_KEY_SIZE);
641 }
642 
643 /* This function lets us test whether a given key was previously
644  * stored in the cache with disk_cache_put_key(). The implement is
645  * efficient by not using syscalls or hitting the disk. It's not
646  * race-free, but the races are benign. If we race with someone else
647  * calling disk_cache_put_key, then that's just an extra cache miss and an
648  * extra recompile.
649  */
650 bool
disk_cache_has_key(struct disk_cache * cache,const cache_key key)651 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
652 {
653    const uint32_t *key_chunk = (const uint32_t *) key;
654    int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK;
655    unsigned char *entry;
656 
657    if (cache->blob_get_cb) {
658       uint32_t blob;
659       return cache->blob_get_cb(key, CACHE_KEY_SIZE, &blob, sizeof(uint32_t));
660    }
661 
662    if (cache->path_init_failed)
663       return false;
664 
665    entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
666 
667    return memcmp(entry, key, CACHE_KEY_SIZE) == 0;
668 }
669 
670 void
disk_cache_compute_key(struct disk_cache * cache,const void * data,size_t size,cache_key key)671 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
672                        cache_key key)
673 {
674    struct mesa_sha1 ctx;
675 
676    _mesa_sha1_init(&ctx);
677    _mesa_sha1_update(&ctx, cache->driver_keys_blob,
678                      cache->driver_keys_blob_size);
679    _mesa_sha1_update(&ctx, data, size);
680    _mesa_sha1_final(&ctx, key);
681 }
682 
683 void
disk_cache_set_callbacks(struct disk_cache * cache,disk_cache_put_cb put,disk_cache_get_cb get)684 disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put,
685                          disk_cache_get_cb get)
686 {
687    cache->blob_put_cb = put;
688    cache->blob_get_cb = get;
689    disk_cache_init_queue(cache);
690 }
691 
692 #endif /* ENABLE_SHADER_CACHE */
693