blob: 9b6ce6334836baa1f0f3c0c238da24f319ad4c3b [file] [log] [blame]
Jens Axboee25839d2012-11-06 10:49:42 +01001#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"
Jens Axboeed1860c2012-11-07 11:39:30 +010012#include "../hash.h"
Jens Axboee25839d2012-11-06 10:49:42 +010013
Jens Axboe4c9060e2012-11-08 12:45:58 +010014#define ZIPF_MAX_GEN 10000000
Jens Axboee25839d2012-11-06 10:49:42 +010015
16static void zipf_update(struct zipf_state *zs)
17{
Jens Axboe4c9060e2012-11-08 12:45:58 +010018 unsigned long to_gen;
Jens Axboee25839d2012-11-06 10:49:42 +010019 unsigned int i;
20
Jens Axboe4c9060e2012-11-08 12:45:58 +010021 /*
22 * It can become very costly to generate long sequences. Just cap it at
Jens Axboee4839592012-11-08 13:43:21 +010023 * 10M max, that should be doable in 1-2s on even slow machines.
24 * Precision will take a slight hit, but nothing major.
Jens Axboe4c9060e2012-11-08 12:45:58 +010025 */
26 to_gen = min(zs->nranges, ZIPF_MAX_GEN);
27
28 for (i = 0; i < to_gen; i++)
Jens Axboee25839d2012-11-06 10:49:42 +010029 zs->zetan += pow(1.0 / (double) (i + 1), zs->theta);
Jens Axboee25839d2012-11-06 10:49:42 +010030}
31
Jens Axboe23162962012-11-07 19:47:47 +010032static void shared_rand_init(struct zipf_state *zs, unsigned long nranges,
33 unsigned int seed)
Jens Axboeb2b0b752012-11-07 14:04:11 +010034{
35 memset(zs, 0, sizeof(*zs));
36 zs->nranges = nranges;
37
Jens Axboe23162962012-11-07 19:47:47 +010038 init_rand_seed(&zs->rand, seed);
Jens Axboeb2b0b752012-11-07 14:04:11 +010039 zs->rand_off = __rand(&zs->rand);
40}
41
Jens Axboe23162962012-11-07 19:47:47 +010042void zipf_init(struct zipf_state *zs, unsigned long nranges, double theta,
43 unsigned int seed)
Jens Axboee25839d2012-11-06 10:49:42 +010044{
Jens Axboe23162962012-11-07 19:47:47 +010045 shared_rand_init(zs, nranges, seed);
Jens Axboee25839d2012-11-06 10:49:42 +010046
Jens Axboee25839d2012-11-06 10:49:42 +010047 zs->theta = theta;
Jens Axboe1442ba12012-11-07 16:12:43 +010048 zs->zeta2 = pow(1.0, zs->theta) + pow(0.5, zs->theta);
Jens Axboee25839d2012-11-06 10:49:42 +010049
Jens Axboe4c9060e2012-11-08 12:45:58 +010050 zipf_update(zs);
Jens Axboee25839d2012-11-06 10:49:42 +010051}
52
53unsigned long long zipf_next(struct zipf_state *zs)
54{
Jens Axboee25839d2012-11-06 10:49:42 +010055 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
62 rand_uni = (double) __rand(&zs->rand) / (double) FRAND_MAX;
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
Jens Axboea5a4fdf2012-11-11 08:27:24 +010072 return (__hash_u64(val - 1) + zs->rand_off) % zs->nranges;
Jens Axboee25839d2012-11-06 10:49:42 +010073}
Jens Axboe925fee32012-11-06 13:50:32 +010074
Jens Axboe23162962012-11-07 19:47:47 +010075void pareto_init(struct zipf_state *zs, unsigned long nranges, double h,
76 unsigned int seed)
Jens Axboe925fee32012-11-06 13:50:32 +010077{
Jens Axboe23162962012-11-07 19:47:47 +010078 shared_rand_init(zs, nranges, seed);
Jens Axboe925fee32012-11-06 13:50:32 +010079 zs->pareto_pow = log(h) / log(1.0 - h);
Jens Axboe925fee32012-11-06 13:50:32 +010080}
81
82unsigned long long pareto_next(struct zipf_state *zs)
83{
84 double rand = (double) __rand(&zs->rand) / (double) FRAND_MAX;
85 unsigned long long n = zs->nranges - 1;
86
Jens Axboea5a4fdf2012-11-11 08:27:24 +010087 return (__hash_u64(n * pow(rand, zs->pareto_pow)) + zs->rand_off) % zs->nranges;
Jens Axboe925fee32012-11-06 13:50:32 +010088}