From: Timothy Arceri Date: Tue, 21 Mar 2017 08:35:22 +0000 (+1100) Subject: util/disk_cache: use rand_xorshift128plus() to get our random int X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6a9020f8dcedd7aa7abc3768d429ce17a6e7865a;p=mesa.git util/disk_cache: use rand_xorshift128plus() to get our random int Otherwise for apps that don't seed the regular rand() we will always remove old cache entries from the same dirs. V2: assume bits returned by rand are independent uniformly distributed bits and grab our hex value without taking the modulus of the whole value, this also fixes a bug where 'f' was always missing. Reviewed-by: Nicolai Hähnle --- diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c index dd3cadb15a9..17a6b5e0c4f 100644 --- a/src/util/disk_cache.c +++ b/src/util/disk_cache.c @@ -41,6 +41,7 @@ #include "zlib.h" #include "util/crc32.h" +#include "util/rand_xor.h" #include "util/u_atomic.h" #include "util/u_queue.h" #include "util/mesa-sha1.h" @@ -66,6 +67,9 @@ struct disk_cache { /* Thread queue for compressing and writing cache entries to disk */ struct util_queue cache_queue; + /* Seed for rand, which is used to pick a random directory */ + uint64_t seed_xorshift128plus[2]; + /* A pointer to the mmapped index file within the cache directory. */ uint8_t *index_mmap; size_t index_mmap_size; @@ -408,6 +412,9 @@ disk_cache_create(const char *gpu_name, const char *timestamp) */ util_queue_init(&cache->cache_queue, "disk_cache", 32, 1); + /* Seed our rand function */ + s_rand_xorshift128plus(cache->seed_xorshift128plus, true); + ralloc_free(local); return cache; @@ -613,23 +620,18 @@ is_two_character_sub_directory(const char *path, const struct stat *sb, static void evict_lru_item(struct disk_cache *cache) { - const char hex[] = "0123456789abcde"; char *dir_path; - int a, b; - size_t size; /* With a reasonably-sized, full cache, (and with keys generated * from a cryptographic hash), we can choose two random hex digits * and reasonably expect the directory to exist with a file in it. * Provides pseudo-LRU eviction to reduce checking all cache files. */ - a = rand() % 16; - b = rand() % 16; - - if (asprintf(&dir_path, "%s/%c%c", cache->path, hex[a], hex[b]) < 0) + uint64_t rand64 = rand_xorshift128plus(cache->seed_xorshift128plus); + if (asprintf(&dir_path, "%s/%02" PRIx64 , cache->path, rand64 & 0xff) < 0) return; - size = unlink_lru_file_from_directory(dir_path); + size_t size = unlink_lru_file_from_directory(dir_path); free(dir_path);