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 /**
169 * Destroy a cache object, (freeing all associated resources).
170 */
171 void
172 disk_cache_destroy(struct disk_cache *cache);
173
174 /* Wait for all previous disk_cache_put() calls to be processed (used for unit
175 * testing).
176 */
177 void
178 disk_cache_wait_for_idle(struct disk_cache *cache);
179
180 /**
181 * Remove the item in the cache under the name \key.
182 */
183 void
184 disk_cache_remove(struct disk_cache *cache, const cache_key key);
185
186 /**
187 * Store an item in the cache under the name \key.
188 *
189 * The item can be retrieved later with disk_cache_get(), (unless the item has
190 * been evicted in the interim).
191 *
192 * Any call to disk_cache_put() may cause an existing, random item to be
193 * evicted from the cache.
194 */
195 void
196 disk_cache_put(struct disk_cache *cache, const cache_key key,
197 const void *data, size_t size,
198 struct cache_item_metadata *cache_item_metadata);
199
200 /**
201 * Store an item in the cache under the name \key without copying the data param.
202 *
203 * The item can be retrieved later with disk_cache_get(), (unless the item has
204 * been evicted in the interim).
205 *
206 * Any call to disk_cache_put() may cause an existing, random item to be
207 * evicted from the cache.
208 *
209 * @p data will be freed
210 */
211 void
212 disk_cache_put_nocopy(struct disk_cache *cache, const cache_key key,
213 void *data, size_t size,
214 struct cache_item_metadata *cache_item_metadata);
215
216 /**
217 * Retrieve an item previously stored in the cache with the name <key>.
218 *
219 * The item must have been previously stored with a call to disk_cache_put().
220 *
221 * If \size is non-NULL, then, on successful return, it will be set to the
222 * size of the object.
223 *
224 * \return A pointer to the stored object if found. NULL if the object
225 * is not found, or if any error occurs, (memory allocation failure,
226 * filesystem error, etc.). The returned data is malloc'ed so the
227 * caller should call free() it when finished.
228 */
229 void *
230 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size);
231
232 /**
233 * Store the name \key within the cache, (without any associated data).
234 *
235 * Later this key can be checked with disk_cache_has_key(), (unless the key
236 * has been evicted in the interim).
237 *
238 * Any call to disk_cache_put_key() may cause an existing, random key to be
239 * evicted from the cache.
240 */
241 void
242 disk_cache_put_key(struct disk_cache *cache, const cache_key key);
243
244 /**
245 * Test whether the name \key was previously recorded in the cache.
246 *
247 * Return value: True if disk_cache_put_key() was previously called with
248 * \key, (and the key was not evicted in the interim).
249 *
250 * Note: disk_cache_has_key() will only return true for keys passed to
251 * disk_cache_put_key(). Specifically, a call to disk_cache_put() will not cause
252 * disk_cache_has_key() to return true for the same key.
253 */
254 bool
255 disk_cache_has_key(struct disk_cache *cache, const cache_key key);
256
257 /**
258 * Compute the name \key from \data of given \size.
259 */
260 void
261 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
262 cache_key key);
263
264 void
265 disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put,
266 disk_cache_get_cb get);
267
268 #else
269
270 static inline struct disk_cache *
disk_cache_create(const char * gpu_name,const char * timestamp,uint64_t driver_flags)271 disk_cache_create(const char *gpu_name, const char *timestamp,
272 uint64_t driver_flags)
273 {
274 return NULL;
275 }
276
277 static inline void
disk_cache_destroy(struct disk_cache * cache)278 disk_cache_destroy(struct disk_cache *cache)
279 {
280 }
281
282 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)283 disk_cache_put(struct disk_cache *cache, const cache_key key,
284 const void *data, size_t size,
285 struct cache_item_metadata *cache_item_metadata)
286 {
287 }
288
289 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)290 disk_cache_put_nocopy(struct disk_cache *cache, const cache_key key,
291 void *data, size_t size,
292 struct cache_item_metadata *cache_item_metadata)
293 {
294 }
295
296 static inline void
disk_cache_remove(struct disk_cache * cache,const cache_key key)297 disk_cache_remove(struct disk_cache *cache, const cache_key key)
298 {
299 }
300
301 static inline uint8_t *
disk_cache_get(struct disk_cache * cache,const cache_key key,size_t * size)302 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
303 {
304 return NULL;
305 }
306
307 static inline void
disk_cache_put_key(struct disk_cache * cache,const cache_key key)308 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
309 {
310 }
311
312 static inline bool
disk_cache_has_key(struct disk_cache * cache,const cache_key key)313 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
314 {
315 return false;
316 }
317
318 static inline void
disk_cache_compute_key(struct disk_cache * cache,const void * data,size_t size,cache_key key)319 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
320 cache_key key)
321 {
322 }
323
324 static inline void
disk_cache_set_callbacks(struct disk_cache * cache,disk_cache_put_cb put,disk_cache_get_cb get)325 disk_cache_set_callbacks(struct disk_cache *cache, disk_cache_put_cb put,
326 disk_cache_get_cb get)
327 {
328 }
329
330 #endif /* ENABLE_SHADER_CACHE */
331
332 #ifdef __cplusplus
333 }
334 #endif
335
336 #endif /* CACHE_H */
337