From ed1860cd8f45677dc592e4fa518dde1cc4c904a5 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Wed, 7 Nov 2012 11:39:30 +0100 Subject: [PATCH] zipf/pareto: mix blocks with hashing We don't want to favor any end of the block spectrum. Mix with a hash. Signed-off-by: Jens Axboe --- hash.h | 9 +++++++-- lib/zipf.c | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/hash.h b/hash.h index 4b8c6bf0..93dd8318 100644 --- a/hash.h +++ b/hash.h @@ -28,7 +28,7 @@ #error Define GOLDEN_RATIO_PRIME for your wordsize. #endif -static inline unsigned long hash_long(unsigned long val, unsigned int bits) +static inline unsigned long __hash_long(unsigned long val) { unsigned long hash = val; @@ -52,8 +52,13 @@ static inline unsigned long hash_long(unsigned long val, unsigned int bits) hash *= GOLDEN_RATIO_PRIME; #endif + return hash; +} + +static inline unsigned long hash_long(unsigned long val, unsigned int bits) +{ /* High bits are more random, so use them. */ - return hash >> (BITS_PER_LONG - bits); + return __hash_long(val) >> (BITS_PER_LONG - bits); } static inline unsigned long hash_ptr(void *ptr, unsigned int bits) diff --git a/lib/zipf.c b/lib/zipf.c index 28e8d77e..527ae294 100644 --- a/lib/zipf.c +++ b/lib/zipf.c @@ -9,6 +9,7 @@ #include "../log.h" #include "zipf.h" #include "../minmax.h" +#include "../hash.h" #include "../os/os.h" struct fio_zipf_disk { @@ -124,7 +125,7 @@ unsigned long long zipf_next(struct zipf_state *zs) else val = 1 + (unsigned long long)(n * pow(eta*rand_uni - eta + 1.0, alpha)); - return val - 1; + return __hash_long(val - 1) % zs->nranges; } void pareto_init(struct zipf_state *zs, unsigned long nranges, double h) @@ -142,5 +143,5 @@ unsigned long long pareto_next(struct zipf_state *zs) double rand = (double) __rand(&zs->rand) / (double) FRAND_MAX; unsigned long long n = zs->nranges - 1; - return n * pow(rand, zs->pareto_pow); + return __hash_long(n * pow(rand, zs->pareto_pow)) % zs->nranges; } -- 2.25.1