util/disk_cache: rename mesa cache dir and introduce cache versioning
authorTimothy Arceri <tarceri@itsqueeze.com>
Thu, 24 Aug 2017 01:11:40 +0000 (11:11 +1000)
committerTimothy Arceri <tarceri@itsqueeze.com>
Fri, 25 Aug 2017 03:20:29 +0000 (13:20 +1000)
Steam is already analysing cache items, unfortunatly we did not
introduce a versioning mechanism for identifying structural changes
to cache entries earlier so the only way to do so is to rename the
cache directory.

Since we are renaming it we take the opportunity to give the directory
a more meaningful name.

Adding a version field to the header of cache entries will help us to
avoid having to rename the directory in future. Please note this is
versioning for the internal structure of the entries as defined in
disk_cache.{c,h} as opposed to the structure of the data provided to
the disk cache by the GLSL compiler and the various driver backends.

Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com>
src/compiler/glsl/tests/cache_test.c
src/util/disk_cache.c
src/util/disk_cache.h

index af1b66fb3d450f7dd7af4a7106ff2abb59ef103f..3796ce617047bef3b27a76ee088578b481130396 100644 (file)
@@ -185,7 +185,8 @@ test_disk_cache_create(void)
    cache = disk_cache_create("test", "make_check", 0);
    expect_non_null(cache, "disk_cache_create with XDG_CACHE_HOME set");
 
-   check_directories_created(CACHE_TEST_TMP "/xdg-cache-home/mesa");
+   check_directories_created(CACHE_TEST_TMP "/xdg-cache-home/"
+                             CACHE_DIR_NAME);
 
    disk_cache_destroy(cache);
 
@@ -202,7 +203,8 @@ test_disk_cache_create(void)
    cache = disk_cache_create("test", "make_check", 0);
    expect_non_null(cache, "disk_cache_create with MESA_GLSL_CACHE_DIR set");
 
-   check_directories_created(CACHE_TEST_TMP "/mesa-glsl-cache-dir/mesa");
+   check_directories_created(CACHE_TEST_TMP "/mesa-glsl-cache-dir/"
+                             CACHE_DIR_NAME);
 
    disk_cache_destroy(cache);
 }
index b2229874e0180155c296a3639058a35135887fc8..6f467027d8e4a49e922b9104d2c80e495c103403 100644 (file)
 /* The number of keys that can be stored in the index. */
 #define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
 
+/* The cache version should be bumped whenever a change is made to the
+ * structure of cache entries or the index. This will give any 3rd party
+ * applications reading the cache entries a chance to adjust to the changes.
+ *
+ * - The cache version is checked internally when reading a cache entry. If we
+ *   ever have a mismatch we are in big trouble as this means we had a cache
+ *   collision. In case of such an event please check the skys for giant
+ *   asteroids and that the entire Mesa team hasn't been eaten by wolves.
+ *
+ * - There is no strict requirement that cache versions be backwards
+ *   compatible but effort should be taken to limit disruption where possible.
+ */
+#define CACHE_VERSION 1
+
 struct disk_cache {
    /* The path to the cache directory. */
    char *path;
@@ -160,6 +174,12 @@ concatenate_and_mkdir(void *ctx, const char *path, const char *name)
       return NULL;
 }
 
+#define DRV_KEY_CPY(_dst, _src, _src_size) \
+do {                                       \
+   memcpy(_dst, _src, _src_size);          \
+   _dst += _src_size;                      \
+} while (0);
+
 struct disk_cache *
 disk_cache_create(const char *gpu_name, const char *timestamp,
                   uint64_t driver_flags)
@@ -188,15 +208,15 @@ disk_cache_create(const char *gpu_name, const char *timestamp,
    /* Determine path for cache based on the first defined name as follows:
     *
     *   $MESA_GLSL_CACHE_DIR
-    *   $XDG_CACHE_HOME/mesa
-    *   <pwd.pw_dir>/.cache/mesa
+    *   $XDG_CACHE_HOME/mesa_shader_cache
+    *   <pwd.pw_dir>/.cache/mesa_shader_cache
     */
    path = getenv("MESA_GLSL_CACHE_DIR");
    if (path) {
       if (mkdir_if_needed(path) == -1)
          goto fail;
 
-      path = concatenate_and_mkdir(local, path, "mesa");
+      path = concatenate_and_mkdir(local, path, CACHE_DIR_NAME);
       if (path == NULL)
          goto fail;
    }
@@ -208,7 +228,7 @@ disk_cache_create(const char *gpu_name, const char *timestamp,
          if (mkdir_if_needed(xdg_cache_home) == -1)
             goto fail;
 
-         path = concatenate_and_mkdir(local, xdg_cache_home, "mesa");
+         path = concatenate_and_mkdir(local, xdg_cache_home, CACHE_DIR_NAME);
          if (path == NULL)
             goto fail;
       }
@@ -244,7 +264,7 @@ disk_cache_create(const char *gpu_name, const char *timestamp,
       if (path == NULL)
          goto fail;
 
-      path = concatenate_and_mkdir(local, path, "mesa");
+      path = concatenate_and_mkdir(local, path, CACHE_DIR_NAME);
       if (path == NULL)
          goto fail;
    }
@@ -344,10 +364,14 @@ disk_cache_create(const char *gpu_name, const char *timestamp,
     */
    util_queue_init(&cache->cache_queue, "disk_cache", 32, 1, 0);
 
+   uint8_t cache_version = CACHE_VERSION;
+   size_t cv_size = sizeof(cache_version);
+   cache->driver_keys_blob_size = cv_size;
+
    /* Create driver id keys */
    size_t ts_size = strlen(timestamp) + 1;
    size_t gpu_name_size = strlen(gpu_name) + 1;
-   cache->driver_keys_blob_size = ts_size;
+   cache->driver_keys_blob_size += ts_size;
    cache->driver_keys_blob_size += gpu_name_size;
 
    /* We sometimes store entire structs that contains a pointers in the cache,
@@ -365,12 +389,12 @@ disk_cache_create(const char *gpu_name, const char *timestamp,
    if (!cache->driver_keys_blob)
       goto fail;
 
-   memcpy(cache->driver_keys_blob, timestamp, ts_size);
-   memcpy(cache->driver_keys_blob + ts_size, gpu_name, gpu_name_size);
-   memcpy(cache->driver_keys_blob + ts_size + gpu_name_size, &ptr_size,
-          ptr_size_size);
-   memcpy(cache->driver_keys_blob + ts_size + gpu_name_size + ptr_size_size,
-          &driver_flags, driver_flags_size);
+   uint8_t *drv_key_blob = cache->driver_keys_blob;
+   DRV_KEY_CPY(drv_key_blob, &cache_version, cv_size)
+   DRV_KEY_CPY(drv_key_blob, timestamp, ts_size)
+   DRV_KEY_CPY(drv_key_blob, gpu_name, gpu_name_size)
+   DRV_KEY_CPY(drv_key_blob, &ptr_size, ptr_size_size)
+   DRV_KEY_CPY(drv_key_blob, &driver_flags, driver_flags_size)
 
    /* Seed our rand function */
    s_rand_xorshift128plus(cache->seed_xorshift128plus, true);
index 9aade16a9ee9fad773e301830541a2ccb82e80bf..3521ef4a6b5ecfbd2171832ec5d39d658e1fa386 100644 (file)
@@ -39,6 +39,8 @@ extern "C" {
 /* Size of cache keys in bytes. */
 #define CACHE_KEY_SIZE 20
 
+#define CACHE_DIR_NAME "mesa_shader_cache"
+
 typedef uint8_t cache_key[CACHE_KEY_SIZE];
 
 struct disk_cache;