/*1* Copyright © 2014 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#ifndef DISK_CACHE_OS_H24#define DISK_CACHE_OS_H2526#include "util/u_queue.h"2728#if DETECT_OS_WINDOWS2930/* TODO: implement disk cache support on windows */3132#else3334#include "util/fossilize_db.h"3536/* Number of bits to mask off from a cache key to get an index. */37#define CACHE_INDEX_KEY_BITS 163839/* Mask for computing an index from a key. */40#define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)4142/* The number of keys that can be stored in the index. */43#define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)4445struct disk_cache {46/* The path to the cache directory. */47char *path;48bool path_init_failed;4950/* Thread queue for compressing and writing cache entries to disk */51struct util_queue cache_queue;5253struct foz_db foz_db;5455/* Seed for rand, which is used to pick a random directory */56uint64_t seed_xorshift128plus[2];5758/* A pointer to the mmapped index file within the cache directory. */59uint8_t *index_mmap;60size_t index_mmap_size;6162/* Pointer to total size of all objects in cache (within index_mmap) */63uint64_t *size;6465/* Pointer to stored keys, (within index_mmap). */66uint8_t *stored_keys;6768/* Maximum size of all cached objects (in bytes). */69uint64_t max_size;7071/* Driver cache keys. */72uint8_t *driver_keys_blob;73size_t driver_keys_blob_size;7475disk_cache_put_cb blob_put_cb;76disk_cache_get_cb blob_get_cb;77};7879struct disk_cache_put_job {80struct util_queue_fence fence;8182struct disk_cache *cache;8384cache_key key;8586/* Copy of cache data to be compressed and written. */87void *data;8889/* Size of data to be compressed and written. */90size_t size;9192struct cache_item_metadata cache_item_metadata;93};9495char *96disk_cache_generate_cache_dir(void *mem_ctx, const char *gpu_name,97const char *driver_id);9899void100disk_cache_evict_lru_item(struct disk_cache *cache);101102void103disk_cache_evict_item(struct disk_cache *cache, char *filename);104105void *106disk_cache_load_item_foz(struct disk_cache *cache, const cache_key key,107size_t *size);108109void *110disk_cache_load_item(struct disk_cache *cache, char *filename, size_t *size);111112char *113disk_cache_get_cache_filename(struct disk_cache *cache, const cache_key key);114115bool116disk_cache_write_item_to_disk_foz(struct disk_cache_put_job *dc_job);117118void119disk_cache_write_item_to_disk(struct disk_cache_put_job *dc_job,120char *filename);121122bool123disk_cache_enabled(void);124125bool126disk_cache_load_cache_index(void *mem_ctx, struct disk_cache *cache);127128bool129disk_cache_mmap_cache_index(void *mem_ctx, struct disk_cache *cache,130char *path);131132void133disk_cache_destroy_mmap(struct disk_cache *cache);134135#endif136137#endif /* DISK_CACHE_OS_H */138139140