1*61046927SAndroid Build Coastguard Worker /* 2*61046927SAndroid Build Coastguard Worker * Copyright © 2020 Valve Corporation 3*61046927SAndroid Build Coastguard Worker * 4*61046927SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a 5*61046927SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"), 6*61046927SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation 7*61046927SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8*61046927SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the 9*61046927SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions: 10*61046927SAndroid Build Coastguard Worker * 11*61046927SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next 12*61046927SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the 13*61046927SAndroid Build Coastguard Worker * Software. 14*61046927SAndroid Build Coastguard Worker * 15*61046927SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16*61046927SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17*61046927SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18*61046927SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19*61046927SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20*61046927SAndroid Build Coastguard Worker * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21*61046927SAndroid Build Coastguard Worker * IN THE SOFTWARE. 22*61046927SAndroid Build Coastguard Worker */ 23*61046927SAndroid Build Coastguard Worker 24*61046927SAndroid Build Coastguard Worker /* This is a basic c implementation of a fossilize db like format intended for 25*61046927SAndroid Build Coastguard Worker * use with the Mesa shader cache. 26*61046927SAndroid Build Coastguard Worker * 27*61046927SAndroid Build Coastguard Worker * The format is compatible enough to allow the fossilize db tools to be used 28*61046927SAndroid Build Coastguard Worker * to do things like merge db collections, but unlike fossilize db which uses 29*61046927SAndroid Build Coastguard Worker * a zlib implementation for compression of data entries, we use zstd for 30*61046927SAndroid Build Coastguard Worker * compression. 31*61046927SAndroid Build Coastguard Worker */ 32*61046927SAndroid Build Coastguard Worker 33*61046927SAndroid Build Coastguard Worker #ifndef FOSSILIZE_DB_H 34*61046927SAndroid Build Coastguard Worker #define FOSSILIZE_DB_H 35*61046927SAndroid Build Coastguard Worker 36*61046927SAndroid Build Coastguard Worker #ifdef HAVE_FLOCK 37*61046927SAndroid Build Coastguard Worker #define FOZ_DB_UTIL 1 38*61046927SAndroid Build Coastguard Worker #endif 39*61046927SAndroid Build Coastguard Worker 40*61046927SAndroid Build Coastguard Worker #ifdef HAVE_SYS_INOTIFY_H 41*61046927SAndroid Build Coastguard Worker #define FOZ_DB_UTIL_DYNAMIC_LIST 1 42*61046927SAndroid Build Coastguard Worker #endif 43*61046927SAndroid Build Coastguard Worker 44*61046927SAndroid Build Coastguard Worker #include <stdbool.h> 45*61046927SAndroid Build Coastguard Worker #include <stdint.h> 46*61046927SAndroid Build Coastguard Worker #include <stdio.h> 47*61046927SAndroid Build Coastguard Worker 48*61046927SAndroid Build Coastguard Worker #include "simple_mtx.h" 49*61046927SAndroid Build Coastguard Worker 50*61046927SAndroid Build Coastguard Worker /* Max number of DBs our implementation can read from at once */ 51*61046927SAndroid Build Coastguard Worker #define FOZ_MAX_DBS 9 /* Default DB + 8 Read only DBs */ 52*61046927SAndroid Build Coastguard Worker 53*61046927SAndroid Build Coastguard Worker #define FOSSILIZE_BLOB_HASH_LENGTH 40 54*61046927SAndroid Build Coastguard Worker 55*61046927SAndroid Build Coastguard Worker enum { 56*61046927SAndroid Build Coastguard Worker FOSSILIZE_COMPRESSION_NONE = 1, 57*61046927SAndroid Build Coastguard Worker FOSSILIZE_COMPRESSION_DEFLATE = 2 58*61046927SAndroid Build Coastguard Worker }; 59*61046927SAndroid Build Coastguard Worker 60*61046927SAndroid Build Coastguard Worker enum { 61*61046927SAndroid Build Coastguard Worker FOSSILIZE_FORMAT_VERSION = 6, 62*61046927SAndroid Build Coastguard Worker FOSSILIZE_FORMAT_MIN_COMPAT_VERSION = 5 63*61046927SAndroid Build Coastguard Worker }; 64*61046927SAndroid Build Coastguard Worker 65*61046927SAndroid Build Coastguard Worker struct foz_payload_header { 66*61046927SAndroid Build Coastguard Worker uint32_t payload_size; 67*61046927SAndroid Build Coastguard Worker uint32_t format; 68*61046927SAndroid Build Coastguard Worker uint32_t crc; 69*61046927SAndroid Build Coastguard Worker uint32_t uncompressed_size; 70*61046927SAndroid Build Coastguard Worker }; 71*61046927SAndroid Build Coastguard Worker 72*61046927SAndroid Build Coastguard Worker struct foz_db_entry { 73*61046927SAndroid Build Coastguard Worker uint8_t file_idx; 74*61046927SAndroid Build Coastguard Worker uint8_t key[20]; 75*61046927SAndroid Build Coastguard Worker uint64_t offset; 76*61046927SAndroid Build Coastguard Worker struct foz_payload_header header; 77*61046927SAndroid Build Coastguard Worker }; 78*61046927SAndroid Build Coastguard Worker 79*61046927SAndroid Build Coastguard Worker struct foz_dbs_list_updater { 80*61046927SAndroid Build Coastguard Worker int inotify_fd; 81*61046927SAndroid Build Coastguard Worker int inotify_wd; /* watch descriptor */ 82*61046927SAndroid Build Coastguard Worker const char *list_filename; 83*61046927SAndroid Build Coastguard Worker thrd_t thrd; 84*61046927SAndroid Build Coastguard Worker }; 85*61046927SAndroid Build Coastguard Worker 86*61046927SAndroid Build Coastguard Worker struct foz_db { 87*61046927SAndroid Build Coastguard Worker FILE *file[FOZ_MAX_DBS]; /* An array of all foz dbs */ 88*61046927SAndroid Build Coastguard Worker FILE *db_idx; /* The default writable foz db idx */ 89*61046927SAndroid Build Coastguard Worker simple_mtx_t mtx; /* Mutex for file/hash table read/writes */ 90*61046927SAndroid Build Coastguard Worker simple_mtx_t flock_mtx; /* Mutex for flocking the file for writes */ 91*61046927SAndroid Build Coastguard Worker void *mem_ctx; 92*61046927SAndroid Build Coastguard Worker struct hash_table_u64 *index_db; /* Hash table of all foz db entries */ 93*61046927SAndroid Build Coastguard Worker bool alive; 94*61046927SAndroid Build Coastguard Worker const char *cache_path; 95*61046927SAndroid Build Coastguard Worker struct foz_dbs_list_updater updater; 96*61046927SAndroid Build Coastguard Worker }; 97*61046927SAndroid Build Coastguard Worker 98*61046927SAndroid Build Coastguard Worker bool 99*61046927SAndroid Build Coastguard Worker foz_prepare(struct foz_db *foz_db, char *cache_path); 100*61046927SAndroid Build Coastguard Worker 101*61046927SAndroid Build Coastguard Worker void 102*61046927SAndroid Build Coastguard Worker foz_destroy(struct foz_db *foz_db); 103*61046927SAndroid Build Coastguard Worker 104*61046927SAndroid Build Coastguard Worker void * 105*61046927SAndroid Build Coastguard Worker foz_read_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit, 106*61046927SAndroid Build Coastguard Worker size_t *size); 107*61046927SAndroid Build Coastguard Worker 108*61046927SAndroid Build Coastguard Worker bool 109*61046927SAndroid Build Coastguard Worker foz_write_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit, 110*61046927SAndroid Build Coastguard Worker const void *blob, size_t size); 111*61046927SAndroid Build Coastguard Worker 112*61046927SAndroid Build Coastguard Worker #endif /* FOSSILIZE_DB_H */ 113