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 
37 /* Number of bits to mask off from a cache key to get an index. */
38 #define CACHE_INDEX_KEY_BITS 16
39 
40 /* Mask for computing an index from a key. */
41 #define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
42 
43 /* The number of keys that can be stored in the index. */
44 #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
45 
46 struct disk_cache {
47    /* The path to the cache directory. */
48    char *path;
49    bool path_init_failed;
50 
51    /* Thread queue for compressing and writing cache entries to disk */
52    struct util_queue cache_queue;
53 
54    struct foz_db foz_db;
55 
56    /* Seed for rand, which is used to pick a random directory */
57    uint64_t seed_xorshift128plus[2];
58 
59    /* A pointer to the mmapped index file within the cache directory. */
60    uint8_t *index_mmap;
61    size_t index_mmap_size;
62 
63    /* Pointer to total size of all objects in cache (within index_mmap) */
64    uint64_t *size;
65 
66    /* Pointer to stored keys, (within index_mmap). */
67    uint8_t *stored_keys;
68 
69    /* Maximum size of all cached objects (in bytes). */
70    uint64_t max_size;
71 
72    /* Driver cache keys. */
73    uint8_t *driver_keys_blob;
74    size_t driver_keys_blob_size;
75 
76    disk_cache_put_cb blob_put_cb;
77    disk_cache_get_cb blob_get_cb;
78 };
79 
80 struct disk_cache_put_job {
81    struct util_queue_fence fence;
82 
83    struct disk_cache *cache;
84 
85    cache_key key;
86 
87    /* Copy of cache data to be compressed and written. */
88    void *data;
89 
90    /* Size of data to be compressed and written. */
91    size_t size;
92 
93    struct cache_item_metadata cache_item_metadata;
94 };
95 
96 char *
97 disk_cache_generate_cache_dir(void *mem_ctx, const char *gpu_name,
98                               const char *driver_id);
99 
100 void
101 disk_cache_evict_lru_item(struct disk_cache *cache);
102 
103 void
104 disk_cache_evict_item(struct disk_cache *cache, char *filename);
105 
106 void *
107 disk_cache_load_item_foz(struct disk_cache *cache, const cache_key key,
108                          size_t *size);
109 
110 void *
111 disk_cache_load_item(struct disk_cache *cache, char *filename, size_t *size);
112 
113 char *
114 disk_cache_get_cache_filename(struct disk_cache *cache, const cache_key key);
115 
116 bool
117 disk_cache_write_item_to_disk_foz(struct disk_cache_put_job *dc_job);
118 
119 void
120 disk_cache_write_item_to_disk(struct disk_cache_put_job *dc_job,
121                               char *filename);
122 
123 bool
124 disk_cache_enabled(void);
125 
126 bool
127 disk_cache_load_cache_index(void *mem_ctx, struct disk_cache *cache);
128 
129 bool
130 disk_cache_mmap_cache_index(void *mem_ctx, struct disk_cache *cache,
131                             char *path);
132 
133 void
134 disk_cache_destroy_mmap(struct disk_cache *cache);
135 
136 #endif
137 
138 #endif /* DISK_CACHE_OS_H */
139