summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorTimothy Arceri <tarceri@itsqueeze.com>2017-03-21 19:35:22 +1100
committerTimothy Arceri <tarceri@itsqueeze.com>2017-03-23 08:16:29 +1100
commit6a9020f8dcedd7aa7abc3768d429ce17a6e7865a (patch)
tree47640f9e03fee3b6ff8dbe97123c784f325b2f2e /src/util
parentdd00a3c923ba94986efba2289c1b0e22b7c12c97 (diff)
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 <nicolai.haehnle@amd.com>
Diffstat (limited to 'src/util')
-rw-r--r--src/util/disk_cache.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c
index dd3cadb15a..17a6b5e0c4 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);