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