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