util/disk_cache: hash pointer size and gpu name into cache keys
[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 ENABLE_SHADER_CACHE
28 #include <dlfcn.h>
29 #endif
30 #include <assert.h>
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <sys/stat.h>
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /* Size of cache keys in bytes. */
40 #define CACHE_KEY_SIZE 20
41
42 typedef uint8_t cache_key[CACHE_KEY_SIZE];
43
44 struct disk_cache;
45
46 static inline bool
47 disk_cache_get_function_timestamp(void *ptr, uint32_t* timestamp)
48 {
49 #ifdef ENABLE_SHADER_CACHE
50 Dl_info info;
51 struct stat st;
52 if (!dladdr(ptr, &info) || !info.dli_fname) {
53 return false;
54 }
55 if (stat(info.dli_fname, &st)) {
56 return false;
57 }
58 *timestamp = st.st_mtime;
59 return true;
60 #else
61 return false;
62 #endif
63 }
64
65 /* Provide inlined stub functions if the shader cache is disabled. */
66
67 #ifdef ENABLE_SHADER_CACHE
68
69 /**
70 * Create a new cache object.
71 *
72 * This function creates the handle necessary for all subsequent cache_*
73 * functions.
74 *
75 * This cache provides two distinct operations:
76 *
77 * o Storage and retrieval of arbitrary objects by cryptographic
78 * name (or "key"). This is provided via disk_cache_put() and
79 * disk_cache_get().
80 *
81 * o The ability to store a key alone and check later whether the
82 * key was previously stored. This is provided via disk_cache_put_key()
83 * and disk_cache_has_key().
84 *
85 * The put_key()/has_key() operations are conceptually identical to
86 * put()/get() with no data, but are provided separately to allow for
87 * a more efficient implementation.
88 *
89 * In all cases, the keys are sequences of 20 bytes. It is anticipated
90 * that callers will compute appropriate SHA-1 signatures for keys,
91 * (though nothing in this implementation directly relies on how the
92 * names are computed). See mesa-sha1.h and _mesa_sha1_compute for
93 * assistance in computing SHA-1 signatures.
94 */
95 struct disk_cache *
96 disk_cache_create(const char *gpu_name, const char *timestamp);
97
98 /**
99 * Destroy a cache object, (freeing all associated resources).
100 */
101 void
102 disk_cache_destroy(struct disk_cache *cache);
103
104 /**
105 * Remove the item in the cache under the name \key.
106 */
107 void
108 disk_cache_remove(struct disk_cache *cache, const cache_key key);
109
110 /**
111 * Store an item in the cache under the name \key.
112 *
113 * The item can be retrieved later with disk_cache_get(), (unless the item has
114 * been evicted in the interim).
115 *
116 * Any call to disk_cache_put() may cause an existing, random item to be
117 * evicted from the cache.
118 */
119 void
120 disk_cache_put(struct disk_cache *cache, const cache_key key,
121 const void *data, size_t size);
122
123 /**
124 * Retrieve an item previously stored in the cache with the name <key>.
125 *
126 * The item must have been previously stored with a call to disk_cache_put().
127 *
128 * If \size is non-NULL, then, on successful return, it will be set to the
129 * size of the object.
130 *
131 * \return A pointer to the stored object if found. NULL if the object
132 * is not found, or if any error occurs, (memory allocation failure,
133 * filesystem error, etc.). The returned data is malloc'ed so the
134 * caller should call free() it when finished.
135 */
136 void *
137 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size);
138
139 /**
140 * Store the name \key within the cache, (without any associated data).
141 *
142 * Later this key can be checked with disk_cache_has_key(), (unless the key
143 * has been evicted in the interim).
144 *
145 * Any call to cache_record() may cause an existing, random key to be
146 * evicted from the cache.
147 */
148 void
149 disk_cache_put_key(struct disk_cache *cache, const cache_key key);
150
151 /**
152 * Test whether the name \key was previously recorded in the cache.
153 *
154 * Return value: True if disk_cache_put_key() was previously called with
155 * \key, (and the key was not evicted in the interim).
156 *
157 * Note: disk_cache_has_key() will only return true for keys passed to
158 * disk_cache_put_key(). Specifically, a call to disk_cache_put() will not cause
159 * disk_cache_has_key() to return true for the same key.
160 */
161 bool
162 disk_cache_has_key(struct disk_cache *cache, const cache_key key);
163
164 /**
165 * Compute the name \key from \data of given \size.
166 */
167 void
168 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
169 cache_key key);
170
171 #else
172
173 static inline struct disk_cache *
174 disk_cache_create(const char *gpu_name, const char *timestamp)
175 {
176 return NULL;
177 }
178
179 static inline void
180 disk_cache_destroy(struct disk_cache *cache) {
181 return;
182 }
183
184 static inline void
185 disk_cache_put(struct disk_cache *cache, const cache_key key,
186 const void *data, size_t size)
187 {
188 return;
189 }
190
191 static inline void
192 disk_cache_remove(struct disk_cache *cache, const cache_key key)
193 {
194 return;
195 }
196
197 static inline uint8_t *
198 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
199 {
200 return NULL;
201 }
202
203 static inline void
204 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
205 {
206 return;
207 }
208
209 static inline bool
210 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
211 {
212 return false;
213 }
214
215 static inline void
216 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
217 const cache_key key)
218 {
219 return;
220 }
221
222 #endif /* ENABLE_SHADER_CACHE */
223
224 #ifdef __cplusplus
225 }
226 #endif
227
228 #endif /* CACHE_H */