From: Marek Olšák <marek.ol...@amd.com> --- src/compiler/glsl/tests/cache_test.c | 140 +++++++++++++++++------------------ src/util/disk_cache.c | 30 ++++---- src/util/disk_cache.h | 56 +++++++------- 3 files changed, 114 insertions(+), 112 deletions(-)
diff --git a/src/compiler/glsl/tests/cache_test.c b/src/compiler/glsl/tests/cache_test.c index fce2bf4..2bbd67d 100644 --- a/src/compiler/glsl/tests/cache_test.c +++ b/src/compiler/glsl/tests/cache_test.c @@ -128,292 +128,292 @@ rmrf_local(const char *path) { if (path == NULL || *path == '\0' || *path != '.') return -1; return nftw(path, remove_entry, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT); } #define CACHE_TEST_TMP "./cache-test-tmp" static void -test_cache_create(void) +test_disk_cache_create(void) { - struct program_cache *cache; + struct disk_cache *cache; int err; /* Before doing anything else, ensure that with - * MESA_GLSL_CACHE_DISABLE set, that cache_create returns NULL. + * MESA_GLSL_CACHE_DISABLE set, that disk_cache_create returns NULL. */ setenv("MESA_GLSL_CACHE_DISABLE", "1", 1); - cache = cache_create(); - expect_null(cache, "cache_create with MESA_GLSL_CACHE_DISABLE set"); + cache = disk_cache_create(); + expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DISABLE set"); unsetenv("MESA_GLSL_CACHE_DISABLE"); - /* For the first real cache_create() clear these environment + /* For the first real disk_cache_create() clear these environment * variables to test creation of cache in home directory. */ unsetenv("MESA_GLSL_CACHE_DIR"); unsetenv("XDG_CACHE_HOME"); - cache = cache_create(); - expect_non_null(cache, "cache_create with no environment variables"); + cache = disk_cache_create(); + expect_non_null(cache, "disk_cache_create with no environment variables"); - cache_destroy(cache); + disk_cache_destroy(cache); /* Test with XDG_CACHE_HOME set */ setenv("XDG_CACHE_HOME", CACHE_TEST_TMP "/xdg-cache-home", 1); - cache = cache_create(); - expect_null(cache, "cache_create with XDG_CACHE_HOME set with" + cache = disk_cache_create(); + expect_null(cache, "disk_cache_create with XDG_CACHE_HOME set with" "a non-existing parent directory"); mkdir(CACHE_TEST_TMP, 0755); - cache = cache_create(); - expect_non_null(cache, "cache_create with XDG_CACHE_HOME set"); + cache = disk_cache_create(); + expect_non_null(cache, "disk_cache_create with XDG_CACHE_HOME set"); - cache_destroy(cache); + disk_cache_destroy(cache); /* Test with MESA_GLSL_CACHE_DIR set */ err = rmrf_local(CACHE_TEST_TMP); expect_equal(err, 0, "Removing " CACHE_TEST_TMP); setenv("MESA_GLSL_CACHE_DIR", CACHE_TEST_TMP "/mesa-glsl-cache-dir", 1); - cache = cache_create(); - expect_null(cache, "cache_create with MESA_GLSL_CACHE_DIR set with" + cache = disk_cache_create(); + expect_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set with" "a non-existing parent directory"); mkdir(CACHE_TEST_TMP, 0755); - cache = cache_create(); - expect_non_null(cache, "cache_create with MESA_GLSL_CACHE_DIR set"); + cache = disk_cache_create(); + expect_non_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set"); - cache_destroy(cache); + disk_cache_destroy(cache); } static bool -does_cache_contain(struct program_cache *cache, cache_key key) +does_cache_contain(struct disk_cache *cache, cache_key key) { void *result; - result = cache_get(cache, key, NULL); + result = disk_cache_get(cache, key, NULL); if (result) { free(result); return true; } return false; } static void test_put_and_get(void) { - struct program_cache *cache; + struct disk_cache *cache; /* If the text of this blob is changed, then blob_key_byte_zero * also needs to be updated. */ char blob[] = "This is a blob of thirty-seven bytes"; uint8_t blob_key[20]; uint8_t blob_key_byte_zero = 0xca; char string[] = "While this string has thirty-four"; uint8_t string_key[20]; char *result; size_t size; uint8_t *one_KB, *one_MB; uint8_t one_KB_key[20], one_MB_key[20]; int count; - cache = cache_create(); + cache = disk_cache_create(); _mesa_sha1_compute(blob, sizeof(blob), blob_key); - /* Ensure that cache_get returns nothing before anything is added. */ - result = cache_get(cache, blob_key, &size); - expect_null(result, "cache_get with non-existent item (pointer)"); - expect_equal(size, 0, "cache_get with non-existent item (size)"); + /* Ensure that disk_cache_get returns nothing before anything is added. */ + result = disk_cache_get(cache, blob_key, &size); + expect_null(result, "disk_cache_get with non-existent item (pointer)"); + expect_equal(size, 0, "disk_cache_get with non-existent item (size)"); /* Simple test of put and get. */ - cache_put(cache, blob_key, blob, sizeof(blob)); + disk_cache_put(cache, blob_key, blob, sizeof(blob)); - result = cache_get(cache, blob_key, &size); - expect_equal_str(blob, result, "cache_get of existing item (pointer)"); - expect_equal(size, sizeof(blob), "cache_get of existing item (size)"); + result = disk_cache_get(cache, blob_key, &size); + expect_equal_str(blob, result, "disk_cache_get of existing item (pointer)"); + expect_equal(size, sizeof(blob), "disk_cache_get of existing item (size)"); free(result); /* Test put and get of a second item. */ _mesa_sha1_compute(string, sizeof(string), string_key); - cache_put(cache, string_key, string, sizeof(string)); + disk_cache_put(cache, string_key, string, sizeof(string)); - result = cache_get(cache, string_key, &size); - expect_equal_str(result, string, "2nd cache_get of existing item (pointer)"); - expect_equal(size, sizeof(string), "2nd cache_get of existing item (size)"); + result = disk_cache_get(cache, string_key, &size); + expect_equal_str(result, string, "2nd disk_cache_get of existing item (pointer)"); + expect_equal(size, sizeof(string), "2nd disk_cache_get of existing item (size)"); free(result); /* Set the cache size to 1KB and add a 1KB item to force an eviction. */ - cache_destroy(cache); + disk_cache_destroy(cache); setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1); - cache = cache_create(); + cache = disk_cache_create(); one_KB = calloc(1, 1024); /* Obviously the SHA-1 hash of 1024 zero bytes isn't particularly * interesting. But we do have want to take some special care with * the hash we use here. The issue is that in this artificial case, * (with only three files in the cache), the probability is good * that each of the three files will end up in their own * directory. Then, if the directory containing the .tmp file for - * the new item being added for cache_put() is the chosen victim + * the new item being added for disk_cache_put() is the chosen victim * directory for eviction, then no suitable file will be found and * nothing will be evicted. * * That's actually expected given how the eviction code is * implemented, (which expects to only evict once things are more * interestingly full than that). * * For this test, we force this signature to land in the same * directory as the original blob first written to the cache. */ _mesa_sha1_compute(one_KB, 1024, one_KB_key); one_KB_key[0] = blob_key_byte_zero; - cache_put(cache, one_KB_key, one_KB, 1024); + disk_cache_put(cache, one_KB_key, one_KB, 1024); free(one_KB); - result = cache_get(cache, one_KB_key, &size); - expect_non_null(result, "3rd cache_get of existing item (pointer)"); - expect_equal(size, 1024, "3rd cache_get of existing item (size)"); + result = disk_cache_get(cache, one_KB_key, &size); + expect_non_null(result, "3rd disk_cache_get of existing item (pointer)"); + expect_equal(size, 1024, "3rd disk_cache_get of existing item (size)"); free(result); /* Ensure eviction happened by checking that only one of the two * previously-added items can still be fetched. */ count = 0; if (does_cache_contain(cache, blob_key)) count++; if (does_cache_contain(cache, string_key)) count++; - expect_equal(count, 1, "cache_put eviction with MAX_SIZE=1K"); + expect_equal(count, 1, "disk_cache_put eviction with MAX_SIZE=1K"); /* Now increase the size to 1M, add back both items, and ensure all - * three that have been added are available via cache_get. + * three that have been added are available via disk_cache_get. */ - cache_destroy(cache); + disk_cache_destroy(cache); setenv("MESA_GLSL_CACHE_MAX_SIZE", "1M", 1); - cache = cache_create(); + cache = disk_cache_create(); - cache_put(cache, blob_key, blob, sizeof(blob)); - cache_put(cache, string_key, string, sizeof(string)); + disk_cache_put(cache, blob_key, blob, sizeof(blob)); + disk_cache_put(cache, string_key, string, sizeof(string)); count = 0; if (does_cache_contain(cache, blob_key)) count++; if (does_cache_contain(cache, string_key)) count++; if (does_cache_contain(cache, one_KB_key)) count++; expect_equal(count, 3, "no eviction before overflow with MAX_SIZE=1M"); /* Finally, check eviction again after adding an object of size 1M. */ one_MB = calloc(1024, 1024); _mesa_sha1_compute(one_MB, 1024 * 1024, one_MB_key); one_MB_key[0] = blob_key_byte_zero;; - cache_put(cache, one_MB_key, one_MB, 1024 * 1024); + disk_cache_put(cache, one_MB_key, one_MB, 1024 * 1024); free(one_MB); count = 0; if (does_cache_contain(cache, blob_key)) count++; if (does_cache_contain(cache, string_key)) count++; if (does_cache_contain(cache, one_KB_key)) count++; expect_equal(count, 2, "eviction after overflow with MAX_SIZE=1M"); - cache_destroy(cache); + disk_cache_destroy(cache); } static void test_put_key_and_get_key(void) { - struct program_cache *cache; + struct disk_cache *cache; bool result; uint8_t key_a[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; uint8_t key_b[20] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 33, 32, 33, 34, 35, 36, 37, 38, 39}; uint8_t key_a_collide[20] = { 0, 1, 42, 43, 44, 45, 46, 47, 48, 49, 50, 55, 52, 53, 54, 55, 56, 57, 58, 59}; - cache = cache_create(); + cache = disk_cache_create(); - /* First test that cache_has_key returns false before cache_put_key */ - result = cache_has_key(cache, key_a); - expect_equal(result, 0, "cache_has_key before key added"); + /* First test that disk_cache_has_key returns false before disk_cache_put_key */ + result = disk_cache_has_key(cache, key_a); + expect_equal(result, 0, "disk_cache_has_key before key added"); - /* Then a couple of tests of cache_put_key followed by cache_has_key */ - cache_put_key(cache, key_a); - result = cache_has_key(cache, key_a); - expect_equal(result, 1, "cache_has_key after key added"); + /* Then a couple of tests of disk_cache_put_key followed by disk_cache_has_key */ + disk_cache_put_key(cache, key_a); + result = disk_cache_has_key(cache, key_a); + expect_equal(result, 1, "disk_cache_has_key after key added"); - cache_put_key(cache, key_b); - result = cache_has_key(cache, key_b); - expect_equal(result, 1, "2nd cache_has_key after key added"); + disk_cache_put_key(cache, key_b); + result = disk_cache_has_key(cache, key_b); + expect_equal(result, 1, "2nd disk_cache_has_key after key added"); /* Test that a key with the same two bytes as an existing key * forces an eviction. */ - cache_put_key(cache, key_a_collide); - result = cache_has_key(cache, key_a_collide); + disk_cache_put_key(cache, key_a_collide); + result = disk_cache_has_key(cache, key_a_collide); expect_equal(result, 1, "put_key of a colliding key lands in the cache"); - result = cache_has_key(cache, key_a); + result = disk_cache_has_key(cache, key_a); expect_equal(result, 0, "put_key of a colliding key evicts from the cache"); /* And finally test that we can re-add the original key to re-evict * the colliding key. */ - cache_put_key(cache, key_a); - result = cache_has_key(cache, key_a); + disk_cache_put_key(cache, key_a); + result = disk_cache_has_key(cache, key_a); expect_equal(result, 1, "put_key of original key lands again"); - result = cache_has_key(cache, key_a_collide); + result = disk_cache_has_key(cache, key_a_collide); expect_equal(result, 0, "put_key of orginal key evicts the colliding key"); - cache_destroy(cache); + disk_cache_destroy(cache); } #endif /* ENABLE_SHADER_CACHE */ int main(void) { #ifdef ENABLE_SHADER_CACHE int err; - test_cache_create(); + test_disk_cache_create(); test_put_and_get(); test_put_key_and_get_key(); err = rmrf_local(CACHE_TEST_TMP); expect_equal(err, 0, "Removing " CACHE_TEST_TMP " again"); #endif /* ENABLE_SHADER_CACHE */ return error ? 1 : 0; diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c index 6a97d44..f7ef3ec 100644 --- a/src/util/disk_cache.c +++ b/src/util/disk_cache.c @@ -46,21 +46,21 @@ /* Number of bits to mask off from a cache key to get an index. */ #define CACHE_INDEX_KEY_BITS 16 /* Mask for computing an index from a key. */ #define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1) /* The number of keys that can be stored in the index. */ #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS) -struct program_cache { +struct disk_cache { /* The path to the cache directory. */ char *path; /* A pointer to the mmapped index file within the cache directory. */ uint8_t *index_mmap; size_t index_mmap_size; /* Pointer to total size of all objects in cache (within index_mmap) */ uint64_t *size; @@ -124,25 +124,25 @@ concatenate_and_mkdir(void *ctx, char *path, char *name) return NULL; new_path = ralloc_asprintf(ctx, "%s/%s", path, name); if (mkdir_if_needed(new_path) == 0) return new_path; else return NULL; } -struct program_cache * -cache_create(void) +struct disk_cache * +disk_cache_create(void) { void *local; - struct program_cache *cache = NULL; + struct disk_cache *cache = NULL; char *path, *max_size_str; uint64_t max_size; int fd = -1; struct stat sb; size_t size; /* A ralloc context for transient data during this invocation. */ local = ralloc_context(NULL); if (local == NULL) goto fail; @@ -203,21 +203,21 @@ cache_create(void) path = concatenate_and_mkdir(local, pwd.pw_dir, ".cache"); if (path == NULL) goto fail; path = concatenate_and_mkdir(local, path, "mesa"); if (path == NULL) goto fail; } - cache = ralloc(NULL, struct program_cache); + cache = ralloc(NULL, struct disk_cache); if (cache == NULL) goto fail; cache->path = ralloc_strdup(cache, path); if (cache->path == NULL) goto fail; path = ralloc_asprintf(local, "%s/index", cache->path); if (path == NULL) goto fail; @@ -306,50 +306,50 @@ cache_create(void) if (fd != -1) close(fd); if (cache) ralloc_free(cache); ralloc_free(local); return NULL; } void -cache_destroy(struct program_cache *cache) +disk_cache_destroy(struct disk_cache *cache) { munmap(cache->index_mmap, cache->index_mmap_size); ralloc_free(cache); } /* Return a filename within the cache's directory corresponding to 'key'. The * returned filename is ralloced with 'cache' as the parent context. * * Returns NULL if out of memory. */ static char * -get_cache_file(struct program_cache *cache, cache_key key) +get_cache_file(struct disk_cache *cache, cache_key key) { char buf[41]; _mesa_sha1_format(buf, key); return ralloc_asprintf(cache, "%s/%c%c/%s", cache->path, buf[0], buf[1], buf + 2); } /* Create the directory that will be needed for the cache file for \key. * * Obviously, the implementation here must closely match * _get_cache_file above. */ static void -make_cache_file_directory(struct program_cache *cache, cache_key key) +make_cache_file_directory(struct disk_cache *cache, cache_key key) { char *dir; char buf[41]; _mesa_sha1_format(buf, key); dir = ralloc_asprintf(cache, "%s/%c%c", cache->path, buf[0], buf[1]); mkdir_if_needed(dir); @@ -477,21 +477,21 @@ is_two_character_sub_directory(struct dirent *entry) if (strlen(entry->d_name) != 2) return false; if (strcmp(entry->d_name, "..") == 0) return false; return true; } static void -evict_random_item(struct program_cache *cache) +evict_random_item(struct disk_cache *cache) { const char hex[] = "0123456789abcde"; char *dir_path; int a, b; size_t size; /* With a reasonably-sized, full cache, (and with keys generated * from a cryptographic hash), we can choose two random hex digits * and reasonably expect the directory to exist with a file in it. */ @@ -524,21 +524,21 @@ evict_random_item(struct program_cache *cache) size = unlink_random_file_from_directory(dir_path); free(dir_path); if (size) p_atomic_add(cache->size, - size); } void -cache_put(struct program_cache *cache, +disk_cache_put(struct disk_cache *cache, cache_key key, const void *data, size_t size) { int fd = -1, fd_final = -1, err, ret; size_t len; char *filename = NULL, *filename_tmp = NULL; const char *p = data; filename = get_cache_file(cache, key); @@ -621,21 +621,21 @@ cache_put(struct program_cache *cache, done: if (filename_tmp) ralloc_free(filename_tmp); if (filename) ralloc_free(filename); if (fd != -1) close(fd); } void * -cache_get(struct program_cache *cache, cache_key key, size_t *size) +disk_cache_get(struct disk_cache *cache, cache_key key, size_t *size) { int fd = -1, ret, len; struct stat sb; char *filename = NULL; uint8_t *data = NULL; if (size) *size = 0; filename = get_cache_file(cache, key); @@ -672,40 +672,40 @@ cache_get(struct program_cache *cache, cache_key key, size_t *size) free(data); if (filename) ralloc_free(filename); if (fd != -1) close(fd); return NULL; } void -cache_put_key(struct program_cache *cache, cache_key key) +disk_cache_put_key(struct disk_cache *cache, cache_key key) { uint32_t *key_chunk = (uint32_t *) key; int i = *key_chunk & CACHE_INDEX_KEY_MASK; unsigned char *entry; entry = &cache->stored_keys[i + CACHE_KEY_SIZE]; memcpy(entry, key, CACHE_KEY_SIZE); } /* This function lets us test whether a given key was previously - * stored in the cache with cache_put_key(). The implement is + * stored in the cache with disk_cache_put_key(). The implement is * efficient by not using syscalls or hitting the disk. It's not * race-free, but the races are benign. If we race with someone else - * calling cache_put_key, then that's just an extra cache miss and an + * calling disk_cache_put_key, then that's just an extra cache miss and an * extra recompile. */ bool -cache_has_key(struct program_cache *cache, cache_key key) +disk_cache_has_key(struct disk_cache *cache, cache_key key) { uint32_t *key_chunk = (uint32_t *) key; int i = *key_chunk & CACHE_INDEX_KEY_MASK; unsigned char *entry; entry = &cache->stored_keys[i + CACHE_KEY_SIZE]; return memcmp(entry, key, CACHE_KEY_SIZE) == 0; } diff --git a/src/util/disk_cache.h b/src/util/disk_cache.h index 066233f..679f1f9 100644 --- a/src/util/disk_cache.h +++ b/src/util/disk_cache.h @@ -30,147 +30,149 @@ extern "C" { #endif #include <stdint.h> #include <stdbool.h> /* Size of cache keys in bytes. */ #define CACHE_KEY_SIZE 20 typedef uint8_t cache_key[CACHE_KEY_SIZE]; +struct disk_cache; + /* Provide inlined stub functions if the shader cache is disabled. */ #ifdef ENABLE_SHADER_CACHE /** * Create a new cache object. * * This function creates the handle necessary for all subsequent cache_* * functions. * * This cache provides two distinct operations: * * o Storage and retrieval of arbitrary objects by cryptographic - * name (or "key"). This is provided via cache_put() and - * cache_get(). + * name (or "key"). This is provided via disk_cache_put() and + * disk_cache_get(). * * o The ability to store a key alone and check later whether the - * key was previously stored. This is provided via cache_put_key() - * and cache_has_key(). + * key was previously stored. This is provided via disk_cache_put_key() + * and disk_cache_has_key(). * * The put_key()/has_key() operations are conceptually identical to * put()/get() with no data, but are provided separately to allow for * a more efficient implementation. * * In all cases, the keys are sequences of 20 bytes. It is anticipated * that callers will compute appropriate SHA-1 signatures for keys, * (though nothing in this implementation directly relies on how the * names are computed). See mesa-sha1.h and _mesa_sha1_compute for * assistance in computing SHA-1 signatures. */ -struct program_cache * -cache_create(void); +struct disk_cache * +disk_cache_create(void); /** * Destroy a cache object, (freeing all associated resources). */ void -cache_destroy(struct program_cache *cache); +disk_cache_destroy(struct disk_cache *cache); /** * Store an item in the cache under the name \key. * - * The item can be retrieved later with cache_get(), (unless the item has + * The item can be retrieved later with disk_cache_get(), (unless the item has * been evicted in the interim). * - * Any call to cache_put() may cause an existing, random item to be + * Any call to disk_cache_put() may cause an existing, random item to be * evicted from the cache. */ void -cache_put(struct program_cache *cache, cache_key key, - const void *data, size_t size); +disk_cache_put(struct disk_cache *cache, cache_key key, + const void *data, size_t size); /** * Retrieve an item previously stored in the cache with the name <key>. * - * The item must have been previously stored with a call to cache_put(). + * The item must have been previously stored with a call to disk_cache_put(). * * If \size is non-NULL, then, on successful return, it will be set to the * size of the object. * * \return A pointer to the stored object if found. NULL if the object * is not found, or if any error occurs, (memory allocation failure, * filesystem error, etc.). The returned data is malloc'ed so the * caller should call free() it when finished. */ void * -cache_get(struct program_cache *cache, cache_key key, size_t *size); +disk_cache_get(struct disk_cache *cache, cache_key key, size_t *size); /** * Store the name \key within the cache, (without any associated data). * - * Later this key can be checked with cache_has_key(), (unless the key + * Later this key can be checked with disk_cache_has_key(), (unless the key * has been evicted in the interim). * * Any call to cache_record() may cause an existing, random key to be * evicted from the cache. */ void -cache_put_key(struct program_cache *cache, cache_key key); +disk_cache_put_key(struct disk_cache *cache, cache_key key); /** * Test whether the name \key was previously recorded in the cache. * - * Return value: True if cache_put_key() was previously called with + * Return value: True if disk_cache_put_key() was previously called with * \key, (and the key was not evicted in the interim). * - * Note: cache_has_key() will only return true for keys passed to - * cache_put_key(). Specifically, a call to cache_put() will not cause - * cache_has_key() to return true for the same key. + * Note: disk_cache_has_key() will only return true for keys passed to + * disk_cache_put_key(). Specifically, a call to disk_cache_put() will not cause + * disk_cache_has_key() to return true for the same key. */ bool -cache_has_key(struct program_cache *cache, cache_key key); +disk_cache_has_key(struct disk_cache *cache, cache_key key); #else -static inline struct program_cache * -cache_create(void) +static inline struct disk_cache * +disk_cache_create(void) { return NULL; } static inline void -cache_destroy(struct program_cache *cache) { +disk_cache_destroy(struct disk_cache *cache) { return; } static inline void -cache_put(struct program_cache *cache, cache_key key, +disk_cache_put(struct disk_cache *cache, cache_key key, const void *data, size_t size) { return; } static inline uint8_t * -cache_get(struct program_cache *cache, cache_key key, size_t *size) +disk_cache_get(struct disk_cache *cache, cache_key key, size_t *size) { return NULL; } static inline void -cache_put_key(struct program_cache *cache, cache_key key) +disk_cache_put_key(struct disk_cache *cache, cache_key key) { return; } static inline bool -cache_has_key(struct program_cache *cache, cache_key key) +disk_cache_has_key(struct disk_cache *cache, cache_key key) { return false; } #endif /* ENABLE_SHADER_CACHE */ #ifdef __cplusplus } #endif -- 2.7.4 _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev