1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef DISK_CACHE_OS_H
25bf215546Sopenharmony_ci#define DISK_CACHE_OS_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/u_queue.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#if DETECT_OS_WINDOWS
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci/* TODO: implement disk cache support on windows */
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#else
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "util/fossilize_db.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci/* Number of bits to mask off from a cache key to get an index. */
38bf215546Sopenharmony_ci#define CACHE_INDEX_KEY_BITS 16
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci/* Mask for computing an index from a key. */
41bf215546Sopenharmony_ci#define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci/* The number of keys that can be stored in the index. */
44bf215546Sopenharmony_ci#define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_cistruct disk_cache {
47bf215546Sopenharmony_ci   /* The path to the cache directory. */
48bf215546Sopenharmony_ci   char *path;
49bf215546Sopenharmony_ci   bool path_init_failed;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   /* Thread queue for compressing and writing cache entries to disk */
52bf215546Sopenharmony_ci   struct util_queue cache_queue;
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   struct foz_db foz_db;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   /* Seed for rand, which is used to pick a random directory */
57bf215546Sopenharmony_ci   uint64_t seed_xorshift128plus[2];
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci   /* A pointer to the mmapped index file within the cache directory. */
60bf215546Sopenharmony_ci   uint8_t *index_mmap;
61bf215546Sopenharmony_ci   size_t index_mmap_size;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   /* Pointer to total size of all objects in cache (within index_mmap) */
64bf215546Sopenharmony_ci   uint64_t *size;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   /* Pointer to stored keys, (within index_mmap). */
67bf215546Sopenharmony_ci   uint8_t *stored_keys;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   /* Maximum size of all cached objects (in bytes). */
70bf215546Sopenharmony_ci   uint64_t max_size;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   /* Driver cache keys. */
73bf215546Sopenharmony_ci   uint8_t *driver_keys_blob;
74bf215546Sopenharmony_ci   size_t driver_keys_blob_size;
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   disk_cache_put_cb blob_put_cb;
77bf215546Sopenharmony_ci   disk_cache_get_cb blob_get_cb;
78bf215546Sopenharmony_ci};
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_cistruct disk_cache_put_job {
81bf215546Sopenharmony_ci   struct util_queue_fence fence;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   struct disk_cache *cache;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   cache_key key;
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   /* Copy of cache data to be compressed and written. */
88bf215546Sopenharmony_ci   void *data;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   /* Size of data to be compressed and written. */
91bf215546Sopenharmony_ci   size_t size;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   struct cache_item_metadata cache_item_metadata;
94bf215546Sopenharmony_ci};
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_cichar *
97bf215546Sopenharmony_cidisk_cache_generate_cache_dir(void *mem_ctx, const char *gpu_name,
98bf215546Sopenharmony_ci                              const char *driver_id);
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_civoid
101bf215546Sopenharmony_cidisk_cache_evict_lru_item(struct disk_cache *cache);
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_civoid
104bf215546Sopenharmony_cidisk_cache_evict_item(struct disk_cache *cache, char *filename);
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_civoid *
107bf215546Sopenharmony_cidisk_cache_load_item_foz(struct disk_cache *cache, const cache_key key,
108bf215546Sopenharmony_ci                         size_t *size);
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_civoid *
111bf215546Sopenharmony_cidisk_cache_load_item(struct disk_cache *cache, char *filename, size_t *size);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_cichar *
114bf215546Sopenharmony_cidisk_cache_get_cache_filename(struct disk_cache *cache, const cache_key key);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_cibool
117bf215546Sopenharmony_cidisk_cache_write_item_to_disk_foz(struct disk_cache_put_job *dc_job);
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_civoid
120bf215546Sopenharmony_cidisk_cache_write_item_to_disk(struct disk_cache_put_job *dc_job,
121bf215546Sopenharmony_ci                              char *filename);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_cibool
124bf215546Sopenharmony_cidisk_cache_enabled(void);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_cibool
127bf215546Sopenharmony_cidisk_cache_load_cache_index(void *mem_ctx, struct disk_cache *cache);
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_cibool
130bf215546Sopenharmony_cidisk_cache_mmap_cache_index(void *mem_ctx, struct disk_cache *cache,
131bf215546Sopenharmony_ci                            char *path);
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_civoid
134bf215546Sopenharmony_cidisk_cache_destroy_mmap(struct disk_cache *cache);
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci#endif
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci#endif /* DISK_CACHE_OS_H */
139