summaryrefslogtreecommitdiffstats
path: root/src/compiler/glsl/cache.h
diff options
context:
space:
mode:
authorTimothy Arceri <[email protected]>2016-09-28 08:55:02 +1000
committerTimothy Arceri <[email protected]>2016-09-28 09:16:31 +1000
commit87ab26b2ab35a29d446ae66f1795d40c184c0739 (patch)
tree18db7f28e1cb85d6338c91f5df6087a587d70fd8 /src/compiler/glsl/cache.h
parent44bcf1ffcced04fd7f2b537a922ffa85dd1418e2 (diff)
glsl: Add initial functions to implement an on-disk cache
This code provides for an on-disk cache of objects. Objects are stored and retrieved via names that are arbitrary 20-byte sequences, (intended to be SHA-1 hashes of something identifying for the content). The directory used for the cache can be specified by means of environment variables in the following priority order: $MESA_GLSL_CACHE_DIR $XDG_CACHE_HOME/mesa <user-home-directory>/.cache/mesa By default the cache will be limited to a maximum size of 1GB. The environment variable: $MESA_GLSL_CACHE_MAX_SIZE can be set (at the time of GL context creation) to choose some other size. This variable is a number that can optionally be followed by 'K', 'M', or 'G' to select a size in kilobytes, megabytes, or gigabytes. By default, an unadorned value will be interpreted as gigabytes. The cache will be entirely disabled at runtime if the variable MESA_GLSL_CACHE_DISABLE is set at the time of GL context creation. Many thanks to Kristian Høgsberg <[email protected]> for the initial implementation of code that led to this patch. In particular, the idea of using an mmapped file, (indexed by a portion of the SHA-1), for the efficent implementation of cache_has_key was entirely his idea. Kristian also provided some very helpful advice in discussions regarding various race conditions to be avoided in this code. Signed-off-by: Timothy Arceri <[email protected]> Reviewed-by: Eric Anholt <[email protected]>
Diffstat (limited to 'src/compiler/glsl/cache.h')
-rw-r--r--src/compiler/glsl/cache.h172
1 files changed, 172 insertions, 0 deletions
diff --git a/src/compiler/glsl/cache.h b/src/compiler/glsl/cache.h
new file mode 100644
index 00000000000..78df32b6c54
--- /dev/null
+++ b/src/compiler/glsl/cache.h
@@ -0,0 +1,172 @@
+/*
+ * Copyright © 2014 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#pragma once
+#ifndef CACHE_H
+#define CACHE_H
+
+#ifdef __cplusplus
+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];
+
+/* 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().
+ *
+ * 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().
+ *
+ * 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);
+
+/**
+ * Destroy a cache object, (freeing all associated resources).
+ */
+void
+cache_destroy(struct program_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
+ * been evicted in the interim).
+ *
+ * Any call to 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);
+
+/**
+ * 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().
+ *
+ * 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);
+
+/**
+ * Store the name \key within the cache, (without any associated data).
+ *
+ * Later this key can be checked with 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);
+
+/**
+ * Test whether the name \key was previously recorded in the cache.
+ *
+ * Return value: True if 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.
+ */
+bool
+cache_has_key(struct program_cache *cache, cache_key key);
+
+#else
+
+static inline struct program_cache *
+cache_create(void)
+{
+ return NULL;
+}
+
+static inline void
+cache_put(struct program_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)
+{
+ return NULL;
+}
+
+static inline void
+cache_put_key(struct program_cache *cache, cache_key key)
+{
+ return;
+}
+
+static inline bool
+cache_has_key(struct program_cache *cache, cache_key key)
+{
+ return false;
+}
+
+#endif /* ENABLE_SHADER_CACHE */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CACHE_H */