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