Module: Mesa
Branch: main
Commit: 2a9b4ad1774d69aa3ae8161ab964fe2d58aacee2
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=2a9b4ad1774d69aa3ae8161ab964fe2d58aacee2

Author: Dmitry Osipenko <[email protected]>
Date:   Mon Jun 13 18:59:57 2022 +0300

util/disk_cache: Add option to disable compression

The shader cache files produced by multi-file cache have a minimum size
limitation imposed by filesystem, usually it's 4KB. In order to make
cache tests suitable for a single file caches, we need to bypass the data
compression, making size of written out data determined for the eviction
testing.

The disk_cache_create() now checks whether driver_id name is set to
"make_check_uncompressed" in order to disable cache data compression.

Reviewed-by: Timothy Arceri <[email protected]>
Reviewed-by: Emil Velikov <[email protected]>
Signed-off-by: Dmitry Osipenko <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16888>

---

 src/util/disk_cache.c    |  7 +++++++
 src/util/disk_cache_os.c | 38 +++++++++++++++++++++++++++++---------
 src/util/disk_cache_os.h |  3 +++
 3 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c
index 8dbe0938d11..2e62688f7aa 100644
--- a/src/util/disk_cache.c
+++ b/src/util/disk_cache.c
@@ -111,6 +111,13 @@ disk_cache_create(const char *gpu_name, const char 
*driver_id,
    if (cache->path == NULL)
       goto path_fail;
 
+   /* Cache tests that want to have a disabled cache compression are using
+    * the "make_check_uncompressed" for the driver_id name.  Hence here we
+    * disable disk cache compression when mesa's build tests require it.
+    */
+   if (strcmp(driver_id, "make_check_uncompressed") == 0)
+      cache->compression_disabled = true;
+
    if (env_var_as_boolean("MESA_DISK_CACHE_SINGLE_FILE", false)) {
       if (!disk_cache_load_cache_index(local, cache))
          goto path_fail;
diff --git a/src/util/disk_cache_os.c b/src/util/disk_cache_os.c
index a1d60321511..cfcfbd42434 100644
--- a/src/util/disk_cache_os.c
+++ b/src/util/disk_cache_os.c
@@ -550,10 +550,20 @@ parse_and_validate_cache_item(struct disk_cache *cache, 
void *cache_item,
 
    /* Uncompress the cache data */
    uncompressed_data = malloc(cf_data->uncompressed_size);
-   if (!util_compress_inflate(data, cache_data_size, uncompressed_data,
-                              cf_data->uncompressed_size))
+   if (!uncompressed_data)
       goto fail;
 
+   if (cache->compression_disabled) {
+      if (cf_data->uncompressed_size != cache_data_size)
+         goto fail;
+
+      memcpy(uncompressed_data, data, cache_data_size);
+   } else {
+      if (!util_compress_inflate(data, cache_data_size, uncompressed_data,
+                                 cf_data->uncompressed_size))
+         goto fail;
+   }
+
    if (size)
       *size = cf_data->uncompressed_size;
 
@@ -638,15 +648,21 @@ create_cache_item_header_and_blob(struct 
disk_cache_put_job *dc_job,
 
    /* Compress the cache item data */
    size_t max_buf = util_compress_max_compressed_len(dc_job->size);
+   size_t compressed_size;
    void *compressed_data = malloc(max_buf);
    if (compressed_data == NULL)
       return false;
 
-   size_t compressed_size =
-      util_compress_deflate(dc_job->data, dc_job->size,
-                            compressed_data, max_buf);
-   if (compressed_size == 0)
-      goto fail;
+   if (dc_job->cache->compression_disabled) {
+      compressed_size = dc_job->size;
+      compressed_data = dc_job->data;
+   } else {
+      compressed_size =
+         util_compress_deflate(dc_job->data, dc_job->size,
+                              compressed_data, max_buf);
+      if (compressed_size == 0)
+         goto fail;
+   }
 
    /* Copy the driver_keys_blob, this can be used find information about the
     * mesa version that produced the entry or deal with hash collisions,
@@ -688,11 +704,15 @@ create_cache_item_header_and_blob(struct 
disk_cache_put_job *dc_job,
    if (!blob_write_bytes(cache_blob, compressed_data, compressed_size))
       goto fail;
 
-   free(compressed_data);
+   if (!dc_job->cache->compression_disabled)
+      free(compressed_data);
+
    return true;
 
  fail:
-   free(compressed_data);
+   if (!dc_job->cache->compression_disabled)
+      free(compressed_data);
+
    return false;
 }
 
diff --git a/src/util/disk_cache_os.h b/src/util/disk_cache_os.h
index a87140bf6ac..f6111b3cc22 100644
--- a/src/util/disk_cache_os.h
+++ b/src/util/disk_cache_os.h
@@ -75,6 +75,9 @@ struct disk_cache {
 
    disk_cache_put_cb blob_put_cb;
    disk_cache_get_cb blob_get_cb;
+
+   /* Don't compress cached data. This is for testing purposes only. */
+   bool compression_disabled;
 };
 
 struct disk_cache_put_job {

Reply via email to