disk_cache.h revision b8e80941
1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2014 Intel Corporation 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21b8e80941Smrg * IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg#ifndef DISK_CACHE_H 25b8e80941Smrg#define DISK_CACHE_H 26b8e80941Smrg 27b8e80941Smrg#ifdef HAVE_DLFCN_H 28b8e80941Smrg#include <dlfcn.h> 29b8e80941Smrg#include <stdio.h> 30b8e80941Smrg#include "util/build_id.h" 31b8e80941Smrg#endif 32b8e80941Smrg#include <assert.h> 33b8e80941Smrg#include <stdint.h> 34b8e80941Smrg#include <stdbool.h> 35b8e80941Smrg#include <sys/stat.h> 36b8e80941Smrg#include "util/mesa-sha1.h" 37b8e80941Smrg 38b8e80941Smrg#ifdef __cplusplus 39b8e80941Smrgextern "C" { 40b8e80941Smrg#endif 41b8e80941Smrg 42b8e80941Smrg/* Size of cache keys in bytes. */ 43b8e80941Smrg#define CACHE_KEY_SIZE 20 44b8e80941Smrg 45b8e80941Smrg#define CACHE_DIR_NAME "mesa_shader_cache" 46b8e80941Smrg 47b8e80941Smrgtypedef uint8_t cache_key[CACHE_KEY_SIZE]; 48b8e80941Smrg 49b8e80941Smrg/* WARNING: 3rd party applications might be reading the cache item metadata. 50b8e80941Smrg * Do not change these values without making the change widely known. 51b8e80941Smrg * Please contact Valve developers and make them aware of this change. 52b8e80941Smrg */ 53b8e80941Smrg#define CACHE_ITEM_TYPE_UNKNOWN 0x0 54b8e80941Smrg#define CACHE_ITEM_TYPE_GLSL 0x1 55b8e80941Smrg 56b8e80941Smrgtypedef void 57b8e80941Smrg(*disk_cache_put_cb) (const void *key, signed long keySize, 58b8e80941Smrg const void *value, signed long valueSize); 59b8e80941Smrg 60b8e80941Smrgtypedef signed long 61b8e80941Smrg(*disk_cache_get_cb) (const void *key, signed long keySize, 62b8e80941Smrg void *value, signed long valueSize); 63b8e80941Smrg 64b8e80941Smrgstruct cache_item_metadata { 65b8e80941Smrg /** 66b8e80941Smrg * The cache item type. This could be used to identify a GLSL cache item, 67b8e80941Smrg * a certain type of IR (tgsi, nir, etc), or signal that it is the final 68b8e80941Smrg * binary form of the shader. 69b8e80941Smrg */ 70b8e80941Smrg uint32_t type; 71b8e80941Smrg 72b8e80941Smrg /** GLSL cache item metadata */ 73b8e80941Smrg cache_key *keys; /* sha1 list of shaders that make up the cache item */ 74b8e80941Smrg uint32_t num_keys; 75b8e80941Smrg}; 76b8e80941Smrg 77b8e80941Smrgstruct disk_cache; 78b8e80941Smrg 79b8e80941Smrgstatic inline char * 80b8e80941Smrgdisk_cache_format_hex_id(char *buf, const uint8_t *hex_id, unsigned size) 81b8e80941Smrg{ 82b8e80941Smrg static const char hex_digits[] = "0123456789abcdef"; 83b8e80941Smrg unsigned i; 84b8e80941Smrg 85b8e80941Smrg for (i = 0; i < size; i += 2) { 86b8e80941Smrg buf[i] = hex_digits[hex_id[i >> 1] >> 4]; 87b8e80941Smrg buf[i + 1] = hex_digits[hex_id[i >> 1] & 0x0f]; 88b8e80941Smrg } 89b8e80941Smrg buf[i] = '\0'; 90b8e80941Smrg 91b8e80941Smrg return buf; 92b8e80941Smrg} 93b8e80941Smrg 94b8e80941Smrg#ifdef HAVE_DLFCN_H 95b8e80941Smrgstatic inline bool 96b8e80941Smrgdisk_cache_get_function_timestamp(void *ptr, uint32_t* timestamp) 97b8e80941Smrg{ 98b8e80941Smrg Dl_info info; 99b8e80941Smrg struct stat st; 100b8e80941Smrg if (!dladdr(ptr, &info) || !info.dli_fname) { 101b8e80941Smrg return false; 102b8e80941Smrg } 103b8e80941Smrg if (stat(info.dli_fname, &st)) { 104b8e80941Smrg return false; 105b8e80941Smrg } 106b8e80941Smrg 107b8e80941Smrg if (!st.st_mtime) { 108b8e80941Smrg fprintf(stderr, "Mesa: The provided filesystem timestamp for the cache " 109b8e80941Smrg "is bogus! Disabling On-disk cache.\n"); 110b8e80941Smrg return false; 111b8e80941Smrg } 112b8e80941Smrg 113b8e80941Smrg *timestamp = st.st_mtime; 114b8e80941Smrg 115b8e80941Smrg return true; 116b8e80941Smrg} 117b8e80941Smrg 118b8e80941Smrgstatic inline bool 119b8e80941Smrgdisk_cache_get_function_identifier(void *ptr, struct mesa_sha1 *ctx) 120b8e80941Smrg{ 121b8e80941Smrg uint32_t timestamp; 122b8e80941Smrg 123b8e80941Smrg#ifdef HAVE_DL_ITERATE_PHDR 124b8e80941Smrg const struct build_id_note *note = NULL; 125b8e80941Smrg if ((note = build_id_find_nhdr_for_addr(ptr))) { 126b8e80941Smrg _mesa_sha1_update(ctx, build_id_data(note), build_id_length(note)); 127b8e80941Smrg } else 128b8e80941Smrg#endif 129b8e80941Smrg if (disk_cache_get_function_timestamp(ptr, ×tamp)) { 130b8e80941Smrg _mesa_sha1_update(ctx, ×tamp, sizeof(timestamp)); 131b8e80941Smrg } else 132b8e80941Smrg return false; 133b8e80941Smrg return true; 134b8e80941Smrg} 135b8e80941Smrg#endif 136b8e80941Smrg 137b8e80941Smrg/* Provide inlined stub functions if the shader cache is disabled. */ 138b8e80941Smrg 139b8e80941Smrg#ifdef ENABLE_SHADER_CACHE 140b8e80941Smrg 141b8e80941Smrg/** 142b8e80941Smrg * Create a new cache object. 143b8e80941Smrg * 144b8e80941Smrg * This function creates the handle necessary for all subsequent cache_* 145b8e80941Smrg * functions. 146b8e80941Smrg * 147b8e80941Smrg * This cache provides two distinct operations: 148b8e80941Smrg * 149b8e80941Smrg * o Storage and retrieval of arbitrary objects by cryptographic 150b8e80941Smrg * name (or "key"). This is provided via disk_cache_put() and 151b8e80941Smrg * disk_cache_get(). 152b8e80941Smrg * 153b8e80941Smrg * o The ability to store a key alone and check later whether the 154b8e80941Smrg * key was previously stored. This is provided via disk_cache_put_key() 155b8e80941Smrg * and disk_cache_has_key(). 156b8e80941Smrg * 157b8e80941Smrg * The put_key()/has_key() operations are conceptually identical to 158b8e80941Smrg * put()/get() with no data, but are provided separately to allow for 159b8e80941Smrg * a more efficient implementation. 160b8e80941Smrg * 161b8e80941Smrg * In all cases, the keys are sequences of 20 bytes. It is anticipated 162b8e80941Smrg * that callers will compute appropriate SHA-1 signatures for keys, 163b8e80941Smrg * (though nothing in this implementation directly relies on how the 164b8e80941Smrg * names are computed). See mesa-sha1.h and _mesa_sha1_compute for 165b8e80941Smrg * assistance in computing SHA-1 signatures. 166b8e80941Smrg */ 167b8e80941Smrgstruct disk_cache * 168b8e80941Smrgdisk_cache_create(const char *gpu_name, const char *timestamp, 169b8e80941Smrg uint64_t driver_flags); 170b8e80941Smrg 171b8e80941Smrg/** 172b8e80941Smrg * Destroy a cache object, (freeing all associated resources). 173b8e80941Smrg */ 174b8e80941Smrgvoid 175b8e80941Smrgdisk_cache_destroy(struct disk_cache *cache); 176b8e80941Smrg 177b8e80941Smrg/** 178b8e80941Smrg * Remove the item in the cache under the name \key. 179b8e80941Smrg */ 180b8e80941Smrgvoid 181b8e80941Smrgdisk_cache_remove(struct disk_cache *cache, const cache_key key); 182b8e80941Smrg 183b8e80941Smrg/** 184b8e80941Smrg * Store an item in the cache under the name \key. 185b8e80941Smrg * 186b8e80941Smrg * The item can be retrieved later with disk_cache_get(), (unless the item has 187b8e80941Smrg * been evicted in the interim). 188b8e80941Smrg * 189b8e80941Smrg * Any call to disk_cache_put() may cause an existing, random item to be 190b8e80941Smrg * evicted from the cache. 191b8e80941Smrg */ 192b8e80941Smrgvoid 193b8e80941Smrgdisk_cache_put(struct disk_cache *cache, const cache_key key, 194b8e80941Smrg const void *data, size_t size, 195b8e80941Smrg struct cache_item_metadata *cache_item_metadata); 196b8e80941Smrg 197b8e80941Smrg/** 198b8e80941Smrg * Retrieve an item previously stored in the cache with the name <key>. 199b8e80941Smrg * 200b8e80941Smrg * The item must have been previously stored with a call to disk_cache_put(). 201b8e80941Smrg * 202b8e80941Smrg * If \size is non-NULL, then, on successful return, it will be set to the 203b8e80941Smrg * size of the object. 204b8e80941Smrg * 205b8e80941Smrg * \return A pointer to the stored object if found. NULL if the object 206b8e80941Smrg * is not found, or if any error occurs, (memory allocation failure, 207b8e80941Smrg * filesystem error, etc.). The returned data is malloc'ed so the 208b8e80941Smrg * caller should call free() it when finished. 209b8e80941Smrg */ 210b8e80941Smrgvoid * 211b8e80941Smrgdisk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size); 212b8e80941Smrg 213b8e80941Smrg/** 214b8e80941Smrg * Store the name \key within the cache, (without any associated data). 215b8e80941Smrg * 216b8e80941Smrg * Later this key can be checked with disk_cache_has_key(), (unless the key 217b8e80941Smrg * has been evicted in the interim). 218b8e80941Smrg * 219b8e80941Smrg * Any call to disk_cache_put_key() may cause an existing, random key to be 220b8e80941Smrg * evicted from the cache. 221b8e80941Smrg */ 222b8e80941Smrgvoid 223b8e80941Smrgdisk_cache_put_key(struct disk_cache *cache, const cache_key key); 224b8e80941Smrg 225b8e80941Smrg/** 226b8e80941Smrg * Test whether the name \key was previously recorded in the cache. 227b8e80941Smrg * 228b8e80941Smrg * Return value: True if disk_cache_put_key() was previously called with 229b8e80941Smrg * \key, (and the key was not evicted in the interim). 230b8e80941Smrg * 231b8e80941Smrg * Note: disk_cache_has_key() will only return true for keys passed to 232b8e80941Smrg * disk_cache_put_key(). Specifically, a call to disk_cache_put() will not cause 233b8e80941Smrg * disk_cache_has_key() to return true for the same key. 234b8e80941Smrg */ 235b8e80941Smrgbool 236b8e80941Smrgdisk_cache_has_key(struct disk_cache *cache, const cache_key key); 237b8e80941Smrg 238b8e80941Smrg/** 239b8e80941Smrg * Compute the name \key from \data of given \size. 240b8e80941Smrg */ 241b8e80941Smrgvoid 242b8e80941Smrgdisk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size, 243b8e80941Smrg cache_key key); 244b8e80941Smrg 245b8e80941Smrgvoid 246b8e80941Smrgdisk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put, 247b8e80941Smrg disk_cache_get_cb get); 248b8e80941Smrg 249b8e80941Smrg#else 250b8e80941Smrg 251b8e80941Smrgstatic inline struct disk_cache * 252b8e80941Smrgdisk_cache_create(const char *gpu_name, const char *timestamp, 253b8e80941Smrg uint64_t driver_flags) 254b8e80941Smrg{ 255b8e80941Smrg return NULL; 256b8e80941Smrg} 257b8e80941Smrg 258b8e80941Smrgstatic inline void 259b8e80941Smrgdisk_cache_destroy(struct disk_cache *cache) { 260b8e80941Smrg return; 261b8e80941Smrg} 262b8e80941Smrg 263b8e80941Smrgstatic inline void 264b8e80941Smrgdisk_cache_put(struct disk_cache *cache, const cache_key key, 265b8e80941Smrg const void *data, size_t size, 266b8e80941Smrg struct cache_item_metadata *cache_item_metadata) 267b8e80941Smrg{ 268b8e80941Smrg return; 269b8e80941Smrg} 270b8e80941Smrg 271b8e80941Smrgstatic inline void 272b8e80941Smrgdisk_cache_remove(struct disk_cache *cache, const cache_key key) 273b8e80941Smrg{ 274b8e80941Smrg return; 275b8e80941Smrg} 276b8e80941Smrg 277b8e80941Smrgstatic inline uint8_t * 278b8e80941Smrgdisk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size) 279b8e80941Smrg{ 280b8e80941Smrg return NULL; 281b8e80941Smrg} 282b8e80941Smrg 283b8e80941Smrgstatic inline void 284b8e80941Smrgdisk_cache_put_key(struct disk_cache *cache, const cache_key key) 285b8e80941Smrg{ 286b8e80941Smrg return; 287b8e80941Smrg} 288b8e80941Smrg 289b8e80941Smrgstatic inline bool 290b8e80941Smrgdisk_cache_has_key(struct disk_cache *cache, const cache_key key) 291b8e80941Smrg{ 292b8e80941Smrg return false; 293b8e80941Smrg} 294b8e80941Smrg 295b8e80941Smrgstatic inline void 296b8e80941Smrgdisk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size, 297b8e80941Smrg const cache_key key) 298b8e80941Smrg{ 299b8e80941Smrg return; 300b8e80941Smrg} 301b8e80941Smrg 302b8e80941Smrgstatic inline void 303b8e80941Smrgdisk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put, 304b8e80941Smrg disk_cache_get_cb get) 305b8e80941Smrg{ 306b8e80941Smrg return; 307b8e80941Smrg} 308b8e80941Smrg 309b8e80941Smrg#endif /* ENABLE_SHADER_CACHE */ 310b8e80941Smrg 311b8e80941Smrg#ifdef __cplusplus 312b8e80941Smrg} 313b8e80941Smrg#endif 314b8e80941Smrg 315b8e80941Smrg#endif /* CACHE_H */ 316