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_OS_H
25 #define DISK_CACHE_OS_H
26 
27 #include "util/u_queue.h"
28 
29 #if DETECT_OS_WINDOWS
30 
31 /* TODO: implement disk cache support on windows */
32 
33 #else
34 
35 #include "util/fossilize_db.h"
36 #include "util/mesa_cache_db.h"
37 #include "util/mesa_cache_db_multipart.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /* Number of bits to mask off from a cache key to get an index. */
44 #define CACHE_INDEX_KEY_BITS 16
45 
46 /* Mask for computing an index from a key. */
47 #define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
48 
49 /* The number of keys that can be stored in the index. */
50 #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
51 
52 enum disk_cache_type {
53    DISK_CACHE_NONE,
54    DISK_CACHE_MULTI_FILE,
55    DISK_CACHE_SINGLE_FILE,
56    DISK_CACHE_DATABASE,
57 };
58 
59 struct disk_cache {
60    /* The path to the cache directory. */
61    char *path;
62    bool path_init_failed;
63 
64    /* Thread queue for compressing and writing cache entries to disk */
65    struct util_queue cache_queue;
66 
67    struct foz_db foz_db;
68 
69    struct mesa_cache_db_multipart cache_db;
70 
71    enum disk_cache_type type;
72 
73    /* Seed for rand, which is used to pick a random directory */
74    uint64_t seed_xorshift128plus[2];
75 
76    /* A pointer to the mmapped index file within the cache directory. */
77    uint8_t *index_mmap;
78    size_t index_mmap_size;
79 
80    /* Pointer to total size of all objects in cache (within index_mmap) */
81    p_atomic_uint64_t *size;
82 
83    /* Pointer to stored keys, (within index_mmap). */
84    uint8_t *stored_keys;
85 
86    /* Maximum size of all cached objects (in bytes). */
87    uint64_t max_size;
88 
89    /* Driver cache keys. */
90    uint8_t *driver_keys_blob;
91    size_t driver_keys_blob_size;
92 
93    disk_cache_put_cb blob_put_cb;
94    disk_cache_get_cb blob_get_cb;
95 
96    /* Don't compress cached data. This is for testing purposes only. */
97    bool compression_disabled;
98 
99    struct {
100       bool enabled;
101       unsigned hits;
102       unsigned misses;
103    } stats;
104 
105    /* Internal RO FOZ cache for combined use of RO and RW caches. */
106    struct disk_cache *foz_ro_cache;
107 };
108 
109 struct cache_entry_file_data {
110    uint32_t crc32;
111    uint32_t uncompressed_size;
112 };
113 
114 struct disk_cache_put_job {
115    struct util_queue_fence fence;
116 
117    struct disk_cache *cache;
118 
119    cache_key key;
120 
121    /* Copy of cache data to be compressed and written. */
122    void *data;
123 
124    /* Size of data to be compressed and written. */
125    size_t size;
126 
127    struct cache_item_metadata cache_item_metadata;
128 };
129 
130 char *
131 disk_cache_generate_cache_dir(void *mem_ctx, const char *gpu_name,
132                               const char *driver_id,
133                               enum disk_cache_type cache_type);
134 
135 void
136 disk_cache_evict_lru_item(struct disk_cache *cache);
137 
138 void
139 disk_cache_evict_item(struct disk_cache *cache, char *filename);
140 
141 void *
142 disk_cache_load_item_foz(struct disk_cache *cache, const cache_key key,
143                          size_t *size);
144 
145 void *
146 disk_cache_load_item(struct disk_cache *cache, char *filename, size_t *size);
147 
148 char *
149 disk_cache_get_cache_filename(struct disk_cache *cache, const cache_key key);
150 
151 bool
152 disk_cache_write_item_to_disk_foz(struct disk_cache_put_job *dc_job);
153 
154 void
155 disk_cache_write_item_to_disk(struct disk_cache_put_job *dc_job,
156                               char *filename);
157 
158 bool
159 disk_cache_enabled(void);
160 
161 bool
162 disk_cache_load_cache_index_foz(void *mem_ctx, struct disk_cache *cache);
163 
164 bool
165 disk_cache_mmap_cache_index(void *mem_ctx, struct disk_cache *cache,
166                             char *path);
167 
168 void
169 disk_cache_destroy_mmap(struct disk_cache *cache);
170 
171 void *
172 disk_cache_db_load_item(struct disk_cache *cache, const cache_key key,
173                         size_t *size);
174 
175 bool
176 disk_cache_db_write_item_to_disk(struct disk_cache_put_job *dc_job);
177 
178 bool
179 disk_cache_db_load_cache_index(void *mem_ctx, struct disk_cache *cache);
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif
186 
187 #endif /* DISK_CACHE_OS_H */
188