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