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

Author: Eric Engestrom <[email protected]>
Date:   Thu Nov 10 11:36:46 2022 +0000

disk_cache: add env var to show stats

Signed-off-by: Eric Engestrom <[email protected]>
Reviewed-by: Alyssa Rosenzweig <[email protected]>
Reviewed-by: Emma Anholt <[email protected]>
Tested-by: Chris Healy <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19644>

---

 docs/envvars.rst         |  3 +++
 src/util/disk_cache.c    | 35 +++++++++++++++++++++++++++--------
 src/util/disk_cache_os.h |  6 ++++++
 3 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/docs/envvars.rst b/docs/envvars.rst
index 8c65714b387..bd31a16c4e2 100644
--- a/docs/envvars.rst
+++ b/docs/envvars.rst
@@ -160,6 +160,9 @@ Core Mesa environment variables
    will be stored in ``$XDG_CACHE_HOME/mesa_shader_cache`` (if that
    variable is set), or else within ``.cache/mesa_shader_cache`` within
    the user's home directory.
+:envvar:`MESA_SHADER_CACHE_SHOW_STATS`
+   if set to ``true``, keeps hit/miss statistics for the shader cache.
+   These statistics are printed when the app terminates.
 :envvar:`MESA_GLSL`
    :ref:`shading language compiler options <envvars>`
 :envvar:`MESA_NO_MINMAX_CACHE`
diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c
index 1447e5e1f96..f2c0fc0e8d5 100644
--- a/src/util/disk_cache.c
+++ b/src/util/disk_cache.c
@@ -130,6 +130,9 @@ disk_cache_create(const char *gpu_name, const char 
*driver_id,
       cache->use_cache_db = true;
    }
 
+   cache->stats.enabled = debug_get_bool_option("MESA_SHADER_CACHE_SHOW_STATS",
+                                                false);
+
    if (!disk_cache_mmap_cache_index(local, cache, path))
       goto path_fail;
 
@@ -255,6 +258,12 @@ disk_cache_create(const char *gpu_name, const char 
*driver_id,
 void
 disk_cache_destroy(struct disk_cache *cache)
 {
+   if (unlikely(cache && cache->stats.enabled)) {
+      printf("disk shader cache:  hits = %u, misses = %u\n",
+             cache->stats.hits,
+             cache->stats.misses);
+   }
+
    if (cache && !cache->path_init_failed) {
       util_queue_finish(&cache->cache_queue);
       util_queue_destroy(&cache->cache_queue);
@@ -523,23 +532,33 @@ disk_cache_put_nocopy(struct disk_cache *cache, const 
cache_key key,
 void *
 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
 {
+   void *buf;
+
    if (size)
       *size = 0;
 
-   if (cache->blob_get_cb)
-       return blob_get_compressed(cache, key, size);
-
-   if (debug_get_bool_option("MESA_DISK_CACHE_SINGLE_FILE", false)) {
-      return disk_cache_load_item_foz(cache, key, size);
+   if (cache->blob_get_cb) {
+      buf = blob_get_compressed(cache, key, size);
+   } else if (debug_get_bool_option("MESA_DISK_CACHE_SINGLE_FILE", false)) {
+      buf = disk_cache_load_item_foz(cache, key, size);
    } else if (cache->use_cache_db) {
-      return disk_cache_db_load_item(cache, key, size);
+      buf = disk_cache_db_load_item(cache, key, size);
    } else {
       char *filename = disk_cache_get_cache_filename(cache, key);
       if (filename == NULL)
-         return NULL;
+         buf = NULL;
+      else
+         buf = disk_cache_load_item(cache, filename, size);
+   }
 
-      return disk_cache_load_item(cache, filename, size);
+   if (unlikely(cache->stats.enabled)) {
+      if (buf)
+         p_atomic_inc(&cache->stats.hits);
+      else
+         p_atomic_inc(&cache->stats.misses);
    }
+
+   return buf;
 }
 
 void
diff --git a/src/util/disk_cache_os.h b/src/util/disk_cache_os.h
index a1d159bc06c..5a34a59961f 100644
--- a/src/util/disk_cache_os.h
+++ b/src/util/disk_cache_os.h
@@ -87,6 +87,12 @@ struct disk_cache {
 
    /* Don't compress cached data. This is for testing purposes only. */
    bool compression_disabled;
+
+   struct {
+      bool enabled;
+      unsigned hits;
+      unsigned misses;
+   } stats;
 };
 
 struct cache_entry_file_data {

Reply via email to