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 <[email protected]>
diff --git a/hash.h b/hash.h
index 4b8c6bf..93dd831 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 @@
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 28e8d77..527ae29 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 @@
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 @@
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;
}