| 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 |
#ifndef DISK_CACHE_H |
| 25 |
#define DISK_CACHE_H |
| 26 |
|
| 27 |
#ifdef HAVE_DLFCN_H |
| 28 |
#include <dlfcn.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include "util/build_id.h" |
| 31 |
#endif |
| 32 |
#include <assert.h> |
| 33 |
#include <stdint.h> |
| 34 |
#include <stdbool.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include "util/mesa-sha1.h" |
| 37 |
|
| 38 |
#ifdef __cplusplus |
| 39 |
extern "C" { |
| 40 |
#endif |
| 41 |
|
| 42 |
/* Size of cache keys in bytes. */ |
| 43 |
#define CACHE_KEY_SIZE 20 |
| 44 |
|
| 45 |
#define CACHE_DIR_NAME "mesa_shader_cache" |
| 46 |
|
| 47 |
typedef uint8_t cache_key[CACHE_KEY_SIZE]; |
| 48 |
|
| 49 |
/* WARNING: 3rd party applications might be reading the cache item metadata. |
| 50 |
* Do not change these values without making the change widely known. |
| 51 |
* Please contact Valve developers and make them aware of this change. |
| 52 |
*/ |
| 53 |
#define CACHE_ITEM_TYPE_UNKNOWN 0x0 |
| 54 |
#define CACHE_ITEM_TYPE_GLSL 0x1 |
| 55 |
|
| 56 |
typedef void |
| 57 |
(*disk_cache_put_cb) (const void *key, signed long keySize, |
| 58 |
const void *value, signed long valueSize); |
| 59 |
|
| 60 |
typedef signed long |
| 61 |
(*disk_cache_get_cb) (const void *key, signed long keySize, |
| 62 |
void *value, signed long valueSize); |
| 63 |
|
| 64 |
struct cache_item_metadata { |
| 65 |
/** |
| 66 |
* The cache item type. This could be used to identify a GLSL cache item, |
| 67 |
* a certain type of IR (tgsi, nir, etc), or signal that it is the final |
| 68 |
* binary form of the shader. |
| 69 |
*/ |
| 70 |
uint32_t type; |
| 71 |
|
| 72 |
/** GLSL cache item metadata */ |
| 73 |
cache_key *keys; /* sha1 list of shaders that make up the cache item */ |
| 74 |
uint32_t num_keys; |
| 75 |
}; |
| 76 |
|
| 77 |
struct disk_cache; |
| 78 |
|
| 79 |
static inline char * |
| 80 |
disk_cache_format_hex_id(char *buf, const uint8_t *hex_id, unsigned size) |
| 81 |
{ |
| 82 |
static const char hex_digits[] = "0123456789abcdef"; |
| 83 |
unsigned i; |
| 84 |
|
| 85 |
for (i = 0; i < size; i += 2) { |
| 86 |
buf[i] = hex_digits[hex_id[i >> 1] >> 4]; |
| 87 |
buf[i + 1] = hex_digits[hex_id[i >> 1] & 0x0f]; |
| 88 |
} |
| 89 |
buf[i] = '\0'; |
| 90 |
|
| 91 |
return buf; |
| 92 |
} |
| 93 |
|
| 94 |
#ifdef HAVE_DLADDR |
| 95 |
static inline bool |
| 96 |
disk_cache_get_function_timestamp(void *ptr, uint32_t* timestamp) |
| 97 |
{ |
| 98 |
Dl_info info; |
| 99 |
struct stat st; |
| 100 |
if (!dladdr(ptr, &info) || !info.dli_fname) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
if (stat(info.dli_fname, &st)) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
if (!st.st_mtime) { |
| 108 |
fprintf(stderr, "Mesa: The provided filesystem timestamp for the cache " |
| 109 |
"is bogus! Disabling On-disk cache.\n"); |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
*timestamp = st.st_mtime; |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
static inline bool |
| 119 |
disk_cache_get_function_identifier(void *ptr, struct mesa_sha1 *ctx) |
| 120 |
{ |
| 121 |
uint32_t timestamp; |
| 122 |
|
| 123 |
#ifdef HAVE_DL_ITERATE_PHDR |
| 124 |
const struct build_id_note *note = NULL; |
| 125 |
if ((note = build_id_find_nhdr_for_addr(ptr))) { |
| 126 |
_mesa_sha1_update(ctx, build_id_data(note), build_id_length(note)); |
| 127 |
} else |
| 128 |
#endif |
| 129 |
if (disk_cache_get_function_timestamp(ptr, ×tamp)) { |
| 130 |
_mesa_sha1_update(ctx, ×tamp, sizeof(timestamp)); |
| 131 |
} else |
| 132 |
return false; |
| 133 |
return true; |
| 134 |
} |
| 135 |
#endif |
| 136 |
|
| 137 |
/* Provide inlined stub functions if the shader cache is disabled. */ |
| 138 |
|
| 139 |
#ifdef ENABLE_SHADER_CACHE |
| 140 |
|
| 141 |
/** |
| 142 |
* Create a new cache object. |
| 143 |
* |
| 144 |
* This function creates the handle necessary for all subsequent cache_* |
| 145 |
* functions. |
| 146 |
* |
| 147 |
* This cache provides two distinct operations: |
| 148 |
* |
| 149 |
* o Storage and retrieval of arbitrary objects by cryptographic |
| 150 |
* name (or "key"). This is provided via disk_cache_put() and |
| 151 |
* disk_cache_get(). |
| 152 |
* |
| 153 |
* o The ability to store a key alone and check later whether the |
| 154 |
* key was previously stored. This is provided via disk_cache_put_key() |
| 155 |
* and disk_cache_has_key(). |
| 156 |
* |
| 157 |
* The put_key()/has_key() operations are conceptually identical to |
| 158 |
* put()/get() with no data, but are provided separately to allow for |
| 159 |
* a more efficient implementation. |
| 160 |
* |
| 161 |
* In all cases, the keys are sequences of 20 bytes. It is anticipated |
| 162 |
* that callers will compute appropriate SHA-1 signatures for keys, |
| 163 |
* (though nothing in this implementation directly relies on how the |
| 164 |
* names are computed). See mesa-sha1.h and _mesa_sha1_compute for |
| 165 |
* assistance in computing SHA-1 signatures. |
| 166 |
*/ |
| 167 |
struct disk_cache * |
| 168 |
disk_cache_create(const char *gpu_name, const char *timestamp, |
| 169 |
uint64_t driver_flags); |
| 170 |
|
| 171 |
/** |
| 172 |
* Destroy a cache object, (freeing all associated resources). |
| 173 |
*/ |
| 174 |
void |
| 175 |
disk_cache_destroy(struct disk_cache *cache); |
| 176 |
|
| 177 |
/* Wait for all previous disk_cache_put() calls to be processed (used for unit |
| 178 |
* testing). |
| 179 |
*/ |
| 180 |
void |
| 181 |
disk_cache_wait_for_idle(struct disk_cache *cache); |
| 182 |
|
| 183 |
/** |
| 184 |
* Remove the item in the cache under the name \key. |
| 185 |
*/ |
| 186 |
void |
| 187 |
disk_cache_remove(struct disk_cache *cache, const cache_key key); |
| 188 |
|
| 189 |
/** |
| 190 |
* Store an item in the cache under the name \key. |
| 191 |
* |
| 192 |
* The item can be retrieved later with disk_cache_get(), (unless the item has |
| 193 |
* been evicted in the interim). |
| 194 |
* |
| 195 |
* Any call to disk_cache_put() may cause an existing, random item to be |
| 196 |
* evicted from the cache. |
| 197 |
*/ |
| 198 |
void |
| 199 |
disk_cache_put(struct disk_cache *cache, const cache_key key, |
| 200 |
const void *data, size_t size, |
| 201 |
struct cache_item_metadata *cache_item_metadata); |
| 202 |
|
| 203 |
/** |
| 204 |
* Retrieve an item previously stored in the cache with the name <key>. |
| 205 |
* |
| 206 |
* The item must have been previously stored with a call to disk_cache_put(). |
| 207 |
* |
| 208 |
* If \size is non-NULL, then, on successful return, it will be set to the |
| 209 |
* size of the object. |
| 210 |
* |
| 211 |
* \return A pointer to the stored object if found. NULL if the object |
| 212 |
* is not found, or if any error occurs, (memory allocation failure, |
| 213 |
* filesystem error, etc.). The returned data is malloc'ed so the |
| 214 |
* caller should call free() it when finished. |
| 215 |
*/ |
| 216 |
void * |
| 217 |
disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size); |
| 218 |
|
| 219 |
/** |
| 220 |
* Store the name \key within the cache, (without any associated data). |
| 221 |
* |
| 222 |
* Later this key can be checked with disk_cache_has_key(), (unless the key |
| 223 |
* has been evicted in the interim). |
| 224 |
* |
| 225 |
* Any call to disk_cache_put_key() may cause an existing, random key to be |
| 226 |
* evicted from the cache. |
| 227 |
*/ |
| 228 |
void |
| 229 |
disk_cache_put_key(struct disk_cache *cache, const cache_key key); |
| 230 |
|
| 231 |
/** |
| 232 |
* Test whether the name \key was previously recorded in the cache. |
| 233 |
* |
| 234 |
* Return value: True if disk_cache_put_key() was previously called with |
| 235 |
* \key, (and the key was not evicted in the interim). |
| 236 |
* |
| 237 |
* Note: disk_cache_has_key() will only return true for keys passed to |
| 238 |
* disk_cache_put_key(). Specifically, a call to disk_cache_put() will not cause |
| 239 |
* disk_cache_has_key() to return true for the same key. |
| 240 |
*/ |
| 241 |
bool |
| 242 |
disk_cache_has_key(struct disk_cache *cache, const cache_key key); |
| 243 |
|
| 244 |
/** |
| 245 |
* Compute the name \key from \data of given \size. |
| 246 |
*/ |
| 247 |
void |
| 248 |
disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size, |
| 249 |
cache_key key); |
| 250 |
|
| 251 |
void |
| 252 |
disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put, |
| 253 |
disk_cache_get_cb get); |
| 254 |
|
| 255 |
#else |
| 256 |
|
| 257 |
static inline struct disk_cache * |
| 258 |
disk_cache_create(const char *gpu_name, const char *timestamp, |
| 259 |
uint64_t driver_flags) |
| 260 |
{ |
| 261 |
return NULL; |
| 262 |
} |
| 263 |
|
| 264 |
static inline void |
| 265 |
disk_cache_destroy(struct disk_cache *cache) { |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
static inline void |
| 270 |
disk_cache_put(struct disk_cache *cache, const cache_key key, |
| 271 |
const void *data, size_t size, |
| 272 |
struct cache_item_metadata *cache_item_metadata) |
| 273 |
{ |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
static inline void |
| 278 |
disk_cache_remove(struct disk_cache *cache, const cache_key key) |
| 279 |
{ |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
static inline uint8_t * |
| 284 |
disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size) |
| 285 |
{ |
| 286 |
return NULL; |
| 287 |
} |
| 288 |
|
| 289 |
static inline void |
| 290 |
disk_cache_put_key(struct disk_cache *cache, const cache_key key) |
| 291 |
{ |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
static inline bool |
| 296 |
disk_cache_has_key(struct disk_cache *cache, const cache_key key) |
| 297 |
{ |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
static inline void |
| 302 |
disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size, |
| 303 |
const cache_key key) |
| 304 |
{ |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
static inline void |
| 309 |
disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put, |
| 310 |
disk_cache_get_cb get) |
| 311 |
{ |
| 312 |
return; |
| 313 |
} |
| 314 |
|
| 315 |
#endif /* ENABLE_SHADER_CACHE */ |
| 316 |
|
| 317 |
#ifdef __cplusplus |
| 318 |
} |
| 319 |
#endif |
| 320 |
|
| 321 |
#endif /* CACHE_H */ |