From 3f119a1fd8deaa46c4342837d13096cd7873c8d2 Mon Sep 17 00:00:00 2001 From: Bas Nieuwenhuizen Date: Fri, 19 Jan 2024 14:35:51 +0100 Subject: [PATCH] util/disk_cache: Add marker on cache usage. To be able to delete it later if it isn't used. Checking the mtime on e.g. the index doesn't work if we always hit the cache so add something that writes unconditionally. Only once per day to avoid excessive writes. (See https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22339) Part-of: --- src/util/disk_cache.c | 3 +++ src/util/disk_cache_os.c | 24 ++++++++++++++++++++++++ src/util/disk_cache_os.h | 3 +++ 3 files changed, 30 insertions(+) diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c index aac052bf9eb..61825fa1360 100644 --- a/src/util/disk_cache.c +++ b/src/util/disk_cache.c @@ -146,6 +146,9 @@ disk_cache_type_create(const char *gpu_name, goto path_fail; } + if (!getenv("MESA_SHADER_CACHE_DIR") && !getenv("MESA_GLSL_CACHE_DIR")) + disk_cache_touch_cache_user_marker(cache->path); + cache->type = cache_type; cache->stats.enabled = debug_get_bool_option("MESA_SHADER_CACHE_SHOW_STATS", diff --git a/src/util/disk_cache_os.c b/src/util/disk_cache_os.c index d8bd8a0f532..201041a7fce 100644 --- a/src/util/disk_cache_os.c +++ b/src/util/disk_cache_os.c @@ -96,6 +96,7 @@ disk_cache_get_function_identifier(void *ptr, struct mesa_sha1 *ctx) #include #include #include +#include "utime.h" #include "util/blob.h" #include "util/crc32.h" @@ -1044,6 +1045,29 @@ disk_cache_load_cache_index_foz(void *mem_ctx, struct disk_cache *cache) return foz_prepare(&cache->foz_db, cache->path); } + +void +disk_cache_touch_cache_user_marker(char *path) +{ + char *marker_path = NULL; + asprintf(&marker_path, "%s/marker", path); + if (!marker_path) + return; + + time_t now = time(NULL); + + struct stat attr; + if (stat(marker_path, &attr) == -1) { + int fd = open(marker_path, O_WRONLY | O_CREAT | O_CLOEXEC, 0644); + if (fd != -1) { + close(fd); + } + } else if (now - attr.st_mtime < 60 * 60 * 24 /* One day */) { + (void)utime(marker_path, NULL); + } + free(marker_path); +} + bool disk_cache_mmap_cache_index(void *mem_ctx, struct disk_cache *cache, char *path) diff --git a/src/util/disk_cache_os.h b/src/util/disk_cache_os.h index 0f5f3923304..2b543cf27be 100644 --- a/src/util/disk_cache_os.h +++ b/src/util/disk_cache_os.h @@ -161,6 +161,9 @@ disk_cache_enabled(void); bool disk_cache_load_cache_index_foz(void *mem_ctx, struct disk_cache *cache); +void +disk_cache_touch_cache_user_marker(char *path); + bool disk_cache_mmap_cache_index(void *mem_ctx, struct disk_cache *cache, char *path);