bloom: hashes take byte lengths, not nwords
[fio.git] / lib / zipf.c
CommitLineData
e25839d4
JA
1#include <math.h>
2#include <string.h>
3#include <inttypes.h>
4#include <stdio.h>
5#include <unistd.h>
6#include <sys/types.h>
7#include <fcntl.h>
8#include "ieee754.h"
9#include "../log.h"
10#include "zipf.h"
11#include "../minmax.h"
ed1860cd 12#include "../hash.h"
e25839d4 13
e6f735f0 14#define ZIPF_MAX_GEN 10000000UL
e25839d4
JA
15
16static void zipf_update(struct zipf_state *zs)
17{
4c9060e3 18 unsigned long to_gen;
e25839d4
JA
19 unsigned int i;
20
4c9060e3
JA
21 /*
22 * It can become very costly to generate long sequences. Just cap it at
e4839591
JA
23 * 10M max, that should be doable in 1-2s on even slow machines.
24 * Precision will take a slight hit, but nothing major.
4c9060e3 25 */
761c2729 26 to_gen = min(zs->nranges, (uint64_t) ZIPF_MAX_GEN);
e25839d4 27
4c9060e3
JA
28 for (i = 0; i < to_gen; i++)
29 zs->zetan += pow(1.0 / (double) (i + 1), zs->theta);
e25839d4
JA
30}
31
2316296a
JA
32static void shared_rand_init(struct zipf_state *zs, unsigned long nranges,
33 unsigned int seed)
b2b0b753
JA
34{
35 memset(zs, 0, sizeof(*zs));
36 zs->nranges = nranges;
37
c3546b53 38 init_rand_seed(&zs->rand, seed, 0);
b2b0b753
JA
39 zs->rand_off = __rand(&zs->rand);
40}
41
2316296a
JA
42void zipf_init(struct zipf_state *zs, unsigned long nranges, double theta,
43 unsigned int seed)
e25839d4 44{
2316296a 45 shared_rand_init(zs, nranges, seed);
e25839d4 46
e25839d4 47 zs->theta = theta;
1442ba18 48 zs->zeta2 = pow(1.0, zs->theta) + pow(0.5, zs->theta);
e25839d4 49
4c9060e3 50 zipf_update(zs);
e25839d4
JA
51}
52
53unsigned long long zipf_next(struct zipf_state *zs)
54{
e25839d4
JA
55 double alpha, eta, rand_uni, rand_z;
56 unsigned long long n = zs->nranges;
57 unsigned long long val;
58
59 alpha = 1.0 / (1.0 - zs->theta);
60 eta = (1.0 - pow(2.0 / n, 1.0 - zs->theta)) / (1.0 - zs->zeta2 / zs->zetan);
61
c3546b53 62 rand_uni = (double) __rand(&zs->rand) / (double) FRAND32_MAX;
e25839d4
JA
63 rand_z = rand_uni * zs->zetan;
64
65 if (rand_z < 1.0)
66 val = 1;
67 else if (rand_z < (1.0 + pow(0.5, zs->theta)))
68 val = 2;
69 else
70 val = 1 + (unsigned long long)(n * pow(eta*rand_uni - eta + 1.0, alpha));
71
8c5e96a4
JA
72 val--;
73
8348daf9 74 if (!zs->disable_hash)
8c5e96a4 75 val = __hash_u64(val);
8348daf9 76
8c5e96a4 77 return (val + zs->rand_off) % zs->nranges;
e25839d4 78}
925fee33 79
2316296a
JA
80void pareto_init(struct zipf_state *zs, unsigned long nranges, double h,
81 unsigned int seed)
925fee33 82{
2316296a 83 shared_rand_init(zs, nranges, seed);
925fee33 84 zs->pareto_pow = log(h) / log(1.0 - h);
925fee33
JA
85}
86
87unsigned long long pareto_next(struct zipf_state *zs)
88{
c3546b53 89 double rand = (double) __rand(&zs->rand) / (double) FRAND32_MAX;
8c5e96a4
JA
90 unsigned long long n;
91
92 n = (zs->nranges - 1) * pow(rand, zs->pareto_pow);
925fee33 93
8348daf9 94 if (!zs->disable_hash)
8c5e96a4 95 n = __hash_u64(n);
8348daf9 96
8c5e96a4 97 return (n + zs->rand_off) % zs->nranges;
8348daf9
JA
98}
99
100void zipf_disable_hash(struct zipf_state *zs)
101{
102 zs->disable_hash = true;
925fee33 103}