101e04c3fSmrg/* 201e04c3fSmrg * Copyright © 2014 Intel Corporation 301e04c3fSmrg * 401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 501e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 601e04c3fSmrg * to deal in the Software without restriction, including without limitation 701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 901e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1001e04c3fSmrg * 1101e04c3fSmrg * The above copyright notice and this permission notice (including the next 1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the 1301e04c3fSmrg * Software. 1401e04c3fSmrg * 1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2101e04c3fSmrg * IN THE SOFTWARE. 2201e04c3fSmrg */ 2301e04c3fSmrg 2401e04c3fSmrg#ifdef ENABLE_SHADER_CACHE 2501e04c3fSmrg 2601e04c3fSmrg#include <ctype.h> 2701e04c3fSmrg#include <ftw.h> 2801e04c3fSmrg#include <string.h> 2901e04c3fSmrg#include <stdlib.h> 3001e04c3fSmrg#include <stdio.h> 3101e04c3fSmrg#include <sys/file.h> 3201e04c3fSmrg#include <sys/types.h> 3301e04c3fSmrg#include <sys/stat.h> 3401e04c3fSmrg#include <sys/mman.h> 3501e04c3fSmrg#include <fcntl.h> 3601e04c3fSmrg#include <errno.h> 3701e04c3fSmrg#include <dirent.h> 381463c08dSmrg#include <inttypes.h> 3901e04c3fSmrg 4001e04c3fSmrg#include "util/crc32.h" 4101e04c3fSmrg#include "util/debug.h" 4201e04c3fSmrg#include "util/rand_xor.h" 4301e04c3fSmrg#include "util/u_atomic.h" 4401e04c3fSmrg#include "util/mesa-sha1.h" 4501e04c3fSmrg#include "util/ralloc.h" 461463c08dSmrg#include "util/compiler.h" 4701e04c3fSmrg 4801e04c3fSmrg#include "disk_cache.h" 491463c08dSmrg#include "disk_cache_os.h" 5001e04c3fSmrg 5101e04c3fSmrg/* The cache version should be bumped whenever a change is made to the 5201e04c3fSmrg * structure of cache entries or the index. This will give any 3rd party 5301e04c3fSmrg * applications reading the cache entries a chance to adjust to the changes. 5401e04c3fSmrg * 5501e04c3fSmrg * - The cache version is checked internally when reading a cache entry. If we 5601e04c3fSmrg * ever have a mismatch we are in big trouble as this means we had a cache 5701e04c3fSmrg * collision. In case of such an event please check the skys for giant 5801e04c3fSmrg * asteroids and that the entire Mesa team hasn't been eaten by wolves. 5901e04c3fSmrg * 6001e04c3fSmrg * - There is no strict requirement that cache versions be backwards 6101e04c3fSmrg * compatible but effort should be taken to limit disruption where possible. 6201e04c3fSmrg */ 6301e04c3fSmrg#define CACHE_VERSION 1 6401e04c3fSmrg 6501e04c3fSmrg#define DRV_KEY_CPY(_dst, _src, _src_size) \ 6601e04c3fSmrgdo { \ 6701e04c3fSmrg memcpy(_dst, _src, _src_size); \ 6801e04c3fSmrg _dst += _src_size; \ 6901e04c3fSmrg} while (0); 7001e04c3fSmrg 7101e04c3fSmrgstruct disk_cache * 7201e04c3fSmrgdisk_cache_create(const char *gpu_name, const char *driver_id, 7301e04c3fSmrg uint64_t driver_flags) 7401e04c3fSmrg{ 7501e04c3fSmrg void *local; 7601e04c3fSmrg struct disk_cache *cache = NULL; 771463c08dSmrg char *max_size_str; 7801e04c3fSmrg uint64_t max_size; 7901e04c3fSmrg 8001e04c3fSmrg uint8_t cache_version = CACHE_VERSION; 8101e04c3fSmrg size_t cv_size = sizeof(cache_version); 8201e04c3fSmrg 831463c08dSmrg if (!disk_cache_enabled()) 8401e04c3fSmrg return NULL; 8501e04c3fSmrg 8601e04c3fSmrg /* A ralloc context for transient data during this invocation. */ 8701e04c3fSmrg local = ralloc_context(NULL); 8801e04c3fSmrg if (local == NULL) 8901e04c3fSmrg goto fail; 9001e04c3fSmrg 9101e04c3fSmrg cache = rzalloc(NULL, struct disk_cache); 9201e04c3fSmrg if (cache == NULL) 9301e04c3fSmrg goto fail; 9401e04c3fSmrg 9501e04c3fSmrg /* Assume failure. */ 9601e04c3fSmrg cache->path_init_failed = true; 9701e04c3fSmrg 981463c08dSmrg#ifdef ANDROID 991463c08dSmrg /* Android needs the "disk cache" to be enabled for 1001463c08dSmrg * EGL_ANDROID_blob_cache's callbacks to be called, but it doesn't actually 1011463c08dSmrg * want any storing to disk to happen inside of the driver. 10201e04c3fSmrg */ 1031463c08dSmrg goto path_fail; 1041463c08dSmrg#endif 10501e04c3fSmrg 1061463c08dSmrg char *path = disk_cache_generate_cache_dir(local, gpu_name, driver_id); 1071463c08dSmrg if (!path) 1081463c08dSmrg goto path_fail; 10901e04c3fSmrg 11001e04c3fSmrg cache->path = ralloc_strdup(cache, path); 11101e04c3fSmrg if (cache->path == NULL) 11201e04c3fSmrg goto path_fail; 11301e04c3fSmrg 1141463c08dSmrg if (env_var_as_boolean("MESA_DISK_CACHE_SINGLE_FILE", false)) { 1151463c08dSmrg if (!disk_cache_load_cache_index(local, cache)) 11601e04c3fSmrg goto path_fail; 11701e04c3fSmrg } 11801e04c3fSmrg 1191463c08dSmrg if (!disk_cache_mmap_cache_index(local, cache, path)) 12001e04c3fSmrg goto path_fail; 12101e04c3fSmrg 12201e04c3fSmrg max_size = 0; 12301e04c3fSmrg 12401e04c3fSmrg max_size_str = getenv("MESA_GLSL_CACHE_MAX_SIZE"); 1251463c08dSmrg 1261463c08dSmrg #ifdef MESA_GLSL_CACHE_MAX_SIZE 1271463c08dSmrg if( !max_size_str ) { 1281463c08dSmrg max_size_str = MESA_GLSL_CACHE_MAX_SIZE; 1291463c08dSmrg } 1301463c08dSmrg #endif 1311463c08dSmrg 13201e04c3fSmrg if (max_size_str) { 13301e04c3fSmrg char *end; 13401e04c3fSmrg max_size = strtoul(max_size_str, &end, 10); 13501e04c3fSmrg if (end == max_size_str) { 13601e04c3fSmrg max_size = 0; 13701e04c3fSmrg } else { 13801e04c3fSmrg switch (*end) { 13901e04c3fSmrg case 'K': 14001e04c3fSmrg case 'k': 14101e04c3fSmrg max_size *= 1024; 14201e04c3fSmrg break; 14301e04c3fSmrg case 'M': 14401e04c3fSmrg case 'm': 14501e04c3fSmrg max_size *= 1024*1024; 14601e04c3fSmrg break; 14701e04c3fSmrg case '\0': 14801e04c3fSmrg case 'G': 14901e04c3fSmrg case 'g': 15001e04c3fSmrg default: 15101e04c3fSmrg max_size *= 1024*1024*1024; 15201e04c3fSmrg break; 15301e04c3fSmrg } 15401e04c3fSmrg } 15501e04c3fSmrg } 15601e04c3fSmrg 15701e04c3fSmrg /* Default to 1GB for maximum cache size. */ 15801e04c3fSmrg if (max_size == 0) { 15901e04c3fSmrg max_size = 1024*1024*1024; 16001e04c3fSmrg } 16101e04c3fSmrg 16201e04c3fSmrg cache->max_size = max_size; 16301e04c3fSmrg 1641463c08dSmrg /* 4 threads were chosen below because just about all modern CPUs currently 1651463c08dSmrg * available that run Mesa have *at least* 4 cores. For these CPUs allowing 1661463c08dSmrg * more threads can result in the queue being processed faster, thus 1671463c08dSmrg * avoiding excessive memory use due to a backlog of cache entrys building 1681463c08dSmrg * up in the queue. Since we set the UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY 1691463c08dSmrg * flag this should have little negative impact on low core systems. 17001e04c3fSmrg * 17101e04c3fSmrg * The queue will resize automatically when it's full, so adding new jobs 17201e04c3fSmrg * doesn't stall. 17301e04c3fSmrg */ 1741463c08dSmrg if (!util_queue_init(&cache->cache_queue, "disk$", 32, 4, 1751463c08dSmrg UTIL_QUEUE_INIT_SCALE_THREADS | 1761463c08dSmrg UTIL_QUEUE_INIT_RESIZE_IF_FULL | 1771463c08dSmrg UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY | 1781463c08dSmrg UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY, NULL)) 1791463c08dSmrg goto fail; 18001e04c3fSmrg 18101e04c3fSmrg cache->path_init_failed = false; 18201e04c3fSmrg 18301e04c3fSmrg path_fail: 18401e04c3fSmrg 18501e04c3fSmrg cache->driver_keys_blob_size = cv_size; 18601e04c3fSmrg 18701e04c3fSmrg /* Create driver id keys */ 18801e04c3fSmrg size_t id_size = strlen(driver_id) + 1; 18901e04c3fSmrg size_t gpu_name_size = strlen(gpu_name) + 1; 19001e04c3fSmrg cache->driver_keys_blob_size += id_size; 19101e04c3fSmrg cache->driver_keys_blob_size += gpu_name_size; 19201e04c3fSmrg 19301e04c3fSmrg /* We sometimes store entire structs that contains a pointers in the cache, 19401e04c3fSmrg * use pointer size as a key to avoid hard to debug issues. 19501e04c3fSmrg */ 19601e04c3fSmrg uint8_t ptr_size = sizeof(void *); 19701e04c3fSmrg size_t ptr_size_size = sizeof(ptr_size); 19801e04c3fSmrg cache->driver_keys_blob_size += ptr_size_size; 19901e04c3fSmrg 20001e04c3fSmrg size_t driver_flags_size = sizeof(driver_flags); 20101e04c3fSmrg cache->driver_keys_blob_size += driver_flags_size; 20201e04c3fSmrg 20301e04c3fSmrg cache->driver_keys_blob = 20401e04c3fSmrg ralloc_size(cache, cache->driver_keys_blob_size); 20501e04c3fSmrg if (!cache->driver_keys_blob) 20601e04c3fSmrg goto fail; 20701e04c3fSmrg 20801e04c3fSmrg uint8_t *drv_key_blob = cache->driver_keys_blob; 20901e04c3fSmrg DRV_KEY_CPY(drv_key_blob, &cache_version, cv_size) 21001e04c3fSmrg DRV_KEY_CPY(drv_key_blob, driver_id, id_size) 21101e04c3fSmrg DRV_KEY_CPY(drv_key_blob, gpu_name, gpu_name_size) 21201e04c3fSmrg DRV_KEY_CPY(drv_key_blob, &ptr_size, ptr_size_size) 21301e04c3fSmrg DRV_KEY_CPY(drv_key_blob, &driver_flags, driver_flags_size) 21401e04c3fSmrg 21501e04c3fSmrg /* Seed our rand function */ 21601e04c3fSmrg s_rand_xorshift128plus(cache->seed_xorshift128plus, true); 21701e04c3fSmrg 21801e04c3fSmrg ralloc_free(local); 21901e04c3fSmrg 22001e04c3fSmrg return cache; 22101e04c3fSmrg 22201e04c3fSmrg fail: 22301e04c3fSmrg if (cache) 22401e04c3fSmrg ralloc_free(cache); 22501e04c3fSmrg ralloc_free(local); 22601e04c3fSmrg 22701e04c3fSmrg return NULL; 22801e04c3fSmrg} 22901e04c3fSmrg 23001e04c3fSmrgvoid 23101e04c3fSmrgdisk_cache_destroy(struct disk_cache *cache) 23201e04c3fSmrg{ 23301e04c3fSmrg if (cache && !cache->path_init_failed) { 2341463c08dSmrg util_queue_finish(&cache->cache_queue); 23501e04c3fSmrg util_queue_destroy(&cache->cache_queue); 23601e04c3fSmrg 2371463c08dSmrg if (env_var_as_boolean("MESA_DISK_CACHE_SINGLE_FILE", false)) 2381463c08dSmrg foz_destroy(&cache->foz_db); 23901e04c3fSmrg 2401463c08dSmrg disk_cache_destroy_mmap(cache); 24101e04c3fSmrg } 24201e04c3fSmrg 2431463c08dSmrg ralloc_free(cache); 24401e04c3fSmrg} 24501e04c3fSmrg 2461463c08dSmrgvoid 2471463c08dSmrgdisk_cache_wait_for_idle(struct disk_cache *cache) 24801e04c3fSmrg{ 2491463c08dSmrg util_queue_finish(&cache->cache_queue); 25001e04c3fSmrg} 25101e04c3fSmrg 25201e04c3fSmrgvoid 25301e04c3fSmrgdisk_cache_remove(struct disk_cache *cache, const cache_key key) 25401e04c3fSmrg{ 2551463c08dSmrg char *filename = disk_cache_get_cache_filename(cache, key); 25601e04c3fSmrg if (filename == NULL) { 25701e04c3fSmrg return; 25801e04c3fSmrg } 25901e04c3fSmrg 2601463c08dSmrg disk_cache_evict_item(cache, filename); 26101e04c3fSmrg} 26201e04c3fSmrg 26301e04c3fSmrgstatic struct disk_cache_put_job * 26401e04c3fSmrgcreate_put_job(struct disk_cache *cache, const cache_key key, 2651463c08dSmrg void *data, size_t size, 2661463c08dSmrg struct cache_item_metadata *cache_item_metadata, 2671463c08dSmrg bool take_ownership) 26801e04c3fSmrg{ 26901e04c3fSmrg struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) 2701463c08dSmrg malloc(sizeof(struct disk_cache_put_job) + (take_ownership ? 0 : size)); 27101e04c3fSmrg 27201e04c3fSmrg if (dc_job) { 27301e04c3fSmrg dc_job->cache = cache; 27401e04c3fSmrg memcpy(dc_job->key, key, sizeof(cache_key)); 2751463c08dSmrg if (take_ownership) { 2761463c08dSmrg dc_job->data = data; 2771463c08dSmrg } else { 2781463c08dSmrg dc_job->data = dc_job + 1; 2791463c08dSmrg memcpy(dc_job->data, data, size); 2801463c08dSmrg } 28101e04c3fSmrg dc_job->size = size; 28201e04c3fSmrg 28301e04c3fSmrg /* Copy the cache item metadata */ 28401e04c3fSmrg if (cache_item_metadata) { 28501e04c3fSmrg dc_job->cache_item_metadata.type = cache_item_metadata->type; 28601e04c3fSmrg if (cache_item_metadata->type == CACHE_ITEM_TYPE_GLSL) { 28701e04c3fSmrg dc_job->cache_item_metadata.num_keys = 28801e04c3fSmrg cache_item_metadata->num_keys; 28901e04c3fSmrg dc_job->cache_item_metadata.keys = (cache_key *) 29001e04c3fSmrg malloc(cache_item_metadata->num_keys * sizeof(cache_key)); 29101e04c3fSmrg 29201e04c3fSmrg if (!dc_job->cache_item_metadata.keys) 29301e04c3fSmrg goto fail; 29401e04c3fSmrg 29501e04c3fSmrg memcpy(dc_job->cache_item_metadata.keys, 29601e04c3fSmrg cache_item_metadata->keys, 29701e04c3fSmrg sizeof(cache_key) * cache_item_metadata->num_keys); 29801e04c3fSmrg } 29901e04c3fSmrg } else { 30001e04c3fSmrg dc_job->cache_item_metadata.type = CACHE_ITEM_TYPE_UNKNOWN; 30101e04c3fSmrg dc_job->cache_item_metadata.keys = NULL; 30201e04c3fSmrg } 30301e04c3fSmrg } 30401e04c3fSmrg 30501e04c3fSmrg return dc_job; 30601e04c3fSmrg 30701e04c3fSmrgfail: 30801e04c3fSmrg free(dc_job); 30901e04c3fSmrg 31001e04c3fSmrg return NULL; 31101e04c3fSmrg} 31201e04c3fSmrg 31301e04c3fSmrgstatic void 3141463c08dSmrgdestroy_put_job(void *job, void *gdata, int thread_index) 31501e04c3fSmrg{ 31601e04c3fSmrg if (job) { 31701e04c3fSmrg struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job; 31801e04c3fSmrg free(dc_job->cache_item_metadata.keys); 31901e04c3fSmrg free(job); 32001e04c3fSmrg } 32101e04c3fSmrg} 32201e04c3fSmrg 3231463c08dSmrgstatic void 3241463c08dSmrgdestroy_put_job_nocopy(void *job, void *gdata, int thread_index) 3251463c08dSmrg{ 3261463c08dSmrg struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job; 3271463c08dSmrg free(dc_job->data); 3281463c08dSmrg destroy_put_job(job, gdata, thread_index); 3291463c08dSmrg} 33001e04c3fSmrg 33101e04c3fSmrgstatic void 3321463c08dSmrgcache_put(void *job, void *gdata, int thread_index) 33301e04c3fSmrg{ 33401e04c3fSmrg assert(job); 33501e04c3fSmrg 33601e04c3fSmrg unsigned i = 0; 3371463c08dSmrg char *filename = NULL; 33801e04c3fSmrg struct disk_cache_put_job *dc_job = (struct disk_cache_put_job *) job; 33901e04c3fSmrg 3401463c08dSmrg if (env_var_as_boolean("MESA_DISK_CACHE_SINGLE_FILE", false)) { 3411463c08dSmrg disk_cache_write_item_to_disk_foz(dc_job); 3421463c08dSmrg } else { 3431463c08dSmrg filename = disk_cache_get_cache_filename(dc_job->cache, dc_job->key); 3441463c08dSmrg if (filename == NULL) 34501e04c3fSmrg goto done; 34601e04c3fSmrg 3471463c08dSmrg /* If the cache is too large, evict something else first. */ 3481463c08dSmrg while (*dc_job->cache->size + dc_job->size > dc_job->cache->max_size && 3491463c08dSmrg i < 8) { 3501463c08dSmrg disk_cache_evict_lru_item(dc_job->cache); 3511463c08dSmrg i++; 35201e04c3fSmrg } 35301e04c3fSmrg 3541463c08dSmrg disk_cache_write_item_to_disk(dc_job, filename); 35501e04c3fSmrg 3561463c08dSmrgdone: 3571463c08dSmrg free(filename); 35801e04c3fSmrg } 35901e04c3fSmrg} 36001e04c3fSmrg 36101e04c3fSmrgvoid 36201e04c3fSmrgdisk_cache_put(struct disk_cache *cache, const cache_key key, 36301e04c3fSmrg const void *data, size_t size, 36401e04c3fSmrg struct cache_item_metadata *cache_item_metadata) 36501e04c3fSmrg{ 36601e04c3fSmrg if (cache->blob_put_cb) { 36701e04c3fSmrg cache->blob_put_cb(key, CACHE_KEY_SIZE, data, size); 36801e04c3fSmrg return; 36901e04c3fSmrg } 37001e04c3fSmrg 37101e04c3fSmrg if (cache->path_init_failed) 37201e04c3fSmrg return; 37301e04c3fSmrg 37401e04c3fSmrg struct disk_cache_put_job *dc_job = 3751463c08dSmrg create_put_job(cache, key, (void*)data, size, cache_item_metadata, false); 37601e04c3fSmrg 37701e04c3fSmrg if (dc_job) { 37801e04c3fSmrg util_queue_fence_init(&dc_job->fence); 37901e04c3fSmrg util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence, 3801463c08dSmrg cache_put, destroy_put_job, dc_job->size); 38101e04c3fSmrg } 38201e04c3fSmrg} 38301e04c3fSmrg 3841463c08dSmrgvoid 3851463c08dSmrgdisk_cache_put_nocopy(struct disk_cache *cache, const cache_key key, 3861463c08dSmrg void *data, size_t size, 3871463c08dSmrg struct cache_item_metadata *cache_item_metadata) 38801e04c3fSmrg{ 3891463c08dSmrg if (cache->blob_put_cb) { 3901463c08dSmrg cache->blob_put_cb(key, CACHE_KEY_SIZE, data, size); 3911463c08dSmrg free(data); 3921463c08dSmrg return; 3931463c08dSmrg } 3941463c08dSmrg 3951463c08dSmrg if (cache->path_init_failed) { 3961463c08dSmrg free(data); 3971463c08dSmrg return; 39801e04c3fSmrg } 39901e04c3fSmrg 4001463c08dSmrg struct disk_cache_put_job *dc_job = 4011463c08dSmrg create_put_job(cache, key, data, size, cache_item_metadata, true); 4021463c08dSmrg 4031463c08dSmrg if (dc_job) { 4041463c08dSmrg util_queue_fence_init(&dc_job->fence); 4051463c08dSmrg util_queue_add_job(&cache->cache_queue, dc_job, &dc_job->fence, 4061463c08dSmrg cache_put, destroy_put_job_nocopy, dc_job->size); 4071463c08dSmrg } 40801e04c3fSmrg} 40901e04c3fSmrg 41001e04c3fSmrgvoid * 41101e04c3fSmrgdisk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size) 41201e04c3fSmrg{ 41301e04c3fSmrg if (size) 41401e04c3fSmrg *size = 0; 41501e04c3fSmrg 41601e04c3fSmrg if (cache->blob_get_cb) { 41701e04c3fSmrg /* This is what Android EGL defines as the maxValueSize in egl_cache_t 41801e04c3fSmrg * class implementation. 41901e04c3fSmrg */ 42001e04c3fSmrg const signed long max_blob_size = 64 * 1024; 42101e04c3fSmrg void *blob = malloc(max_blob_size); 42201e04c3fSmrg if (!blob) 42301e04c3fSmrg return NULL; 42401e04c3fSmrg 42501e04c3fSmrg signed long bytes = 42601e04c3fSmrg cache->blob_get_cb(key, CACHE_KEY_SIZE, blob, max_blob_size); 42701e04c3fSmrg 42801e04c3fSmrg if (!bytes) { 42901e04c3fSmrg free(blob); 43001e04c3fSmrg return NULL; 43101e04c3fSmrg } 43201e04c3fSmrg 43301e04c3fSmrg if (size) 43401e04c3fSmrg *size = bytes; 43501e04c3fSmrg return blob; 43601e04c3fSmrg } 43701e04c3fSmrg 4381463c08dSmrg if (env_var_as_boolean("MESA_DISK_CACHE_SINGLE_FILE", false)) { 4391463c08dSmrg return disk_cache_load_item_foz(cache, key, size); 4401463c08dSmrg } else { 4411463c08dSmrg char *filename = disk_cache_get_cache_filename(cache, key); 4421463c08dSmrg if (filename == NULL) 4431463c08dSmrg return NULL; 44401e04c3fSmrg 4451463c08dSmrg return disk_cache_load_item(cache, filename, size); 44601e04c3fSmrg } 44701e04c3fSmrg} 44801e04c3fSmrg 44901e04c3fSmrgvoid 45001e04c3fSmrgdisk_cache_put_key(struct disk_cache *cache, const cache_key key) 45101e04c3fSmrg{ 45201e04c3fSmrg const uint32_t *key_chunk = (const uint32_t *) key; 45301e04c3fSmrg int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK; 45401e04c3fSmrg unsigned char *entry; 45501e04c3fSmrg 45601e04c3fSmrg if (cache->blob_put_cb) { 45701e04c3fSmrg cache->blob_put_cb(key, CACHE_KEY_SIZE, key_chunk, sizeof(uint32_t)); 45801e04c3fSmrg return; 45901e04c3fSmrg } 46001e04c3fSmrg 46101e04c3fSmrg if (cache->path_init_failed) 46201e04c3fSmrg return; 46301e04c3fSmrg 46401e04c3fSmrg entry = &cache->stored_keys[i * CACHE_KEY_SIZE]; 46501e04c3fSmrg 46601e04c3fSmrg memcpy(entry, key, CACHE_KEY_SIZE); 46701e04c3fSmrg} 46801e04c3fSmrg 46901e04c3fSmrg/* This function lets us test whether a given key was previously 47001e04c3fSmrg * stored in the cache with disk_cache_put_key(). The implement is 47101e04c3fSmrg * efficient by not using syscalls or hitting the disk. It's not 47201e04c3fSmrg * race-free, but the races are benign. If we race with someone else 47301e04c3fSmrg * calling disk_cache_put_key, then that's just an extra cache miss and an 47401e04c3fSmrg * extra recompile. 47501e04c3fSmrg */ 47601e04c3fSmrgbool 47701e04c3fSmrgdisk_cache_has_key(struct disk_cache *cache, const cache_key key) 47801e04c3fSmrg{ 47901e04c3fSmrg const uint32_t *key_chunk = (const uint32_t *) key; 48001e04c3fSmrg int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK; 48101e04c3fSmrg unsigned char *entry; 48201e04c3fSmrg 48301e04c3fSmrg if (cache->blob_get_cb) { 48401e04c3fSmrg uint32_t blob; 48501e04c3fSmrg return cache->blob_get_cb(key, CACHE_KEY_SIZE, &blob, sizeof(uint32_t)); 48601e04c3fSmrg } 48701e04c3fSmrg 48801e04c3fSmrg if (cache->path_init_failed) 48901e04c3fSmrg return false; 49001e04c3fSmrg 49101e04c3fSmrg entry = &cache->stored_keys[i * CACHE_KEY_SIZE]; 49201e04c3fSmrg 49301e04c3fSmrg return memcmp(entry, key, CACHE_KEY_SIZE) == 0; 49401e04c3fSmrg} 49501e04c3fSmrg 49601e04c3fSmrgvoid 49701e04c3fSmrgdisk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size, 49801e04c3fSmrg cache_key key) 49901e04c3fSmrg{ 50001e04c3fSmrg struct mesa_sha1 ctx; 50101e04c3fSmrg 50201e04c3fSmrg _mesa_sha1_init(&ctx); 50301e04c3fSmrg _mesa_sha1_update(&ctx, cache->driver_keys_blob, 50401e04c3fSmrg cache->driver_keys_blob_size); 50501e04c3fSmrg _mesa_sha1_update(&ctx, data, size); 50601e04c3fSmrg _mesa_sha1_final(&ctx, key); 50701e04c3fSmrg} 50801e04c3fSmrg 50901e04c3fSmrgvoid 51001e04c3fSmrgdisk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put, 51101e04c3fSmrg disk_cache_get_cb get) 51201e04c3fSmrg{ 51301e04c3fSmrg cache->blob_put_cb = put; 51401e04c3fSmrg cache->blob_get_cb = get; 51501e04c3fSmrg} 51601e04c3fSmrg 51701e04c3fSmrg#endif /* ENABLE_SHADER_CACHE */ 518