-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathrandom.c
188 lines (159 loc) · 4.98 KB
/
random.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "headers.h"
#include "random.h"
#include <math.h>
/* xorshf96 */
static unsigned long x=123456789, y=362436069, z=521288629;
unsigned long xorshf96(void) { //period 2^96-1
unsigned long t;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
/* Thread safe xorshf96 */
static __thread unsigned long _x=123456789, _y=362436069, _z=521288629;
unsigned long locxorshf96(void) { //period 2^96-1
unsigned long t;
_x ^= _x << 16;
_x ^= _x >> 5;
_x ^= _x << 1;
t = _x;
_x = _y;
_y = _z;
_z = t ^ _x ^ _y;
return _z;
}
/* Init a per thread seed in a thread safe way */
static unsigned int __thread seed;
void init_seed(void) {
seed = rand();
}
/* zipf - from https://bitbucket.org/theoanab/rocksdb-ycsb/src/master/util/zipf.h */
static long items; //initialized in init_zipf_generator function
static long base; //initialized in init_zipf_generator function
static double zipfianconstant; //initialized in init_zipf_generator function
static double alpha; //initialized in init_zipf_generator function
static double zetan; //initialized in init_zipf_generator function
static double eta; //initialized in init_zipf_generator function
static double theta; //initialized in init_zipf_generator function
static double zeta2theta; //initialized in init_zipf_generator function
static long countforzeta; //initialized in init_zipf_generator function
void init_zipf_generator(long min, long max);
double zeta(long st, long n, double initialsum);
double zetastatic(long st, long n, double initialsum);
long next_long(long itemcount);
long zipf_next();
void set_last_value(long val);
void init_zipf_generator(long min, long max){
items = max-min+1;
base = min;
zipfianconstant = 0.99;
theta = zipfianconstant;
zeta2theta = zeta(0, 2, 0);
alpha = 1.0/(1.0-theta);
zetan = zetastatic(0, max-min+1, 0);
countforzeta = items;
eta=(1 - pow(2.0/items,1-theta) )/(1-zeta2theta/zetan);
zipf_next();
}
double zeta(long st, long n, double initialsum) {
countforzeta=n;
return zetastatic(st,n,initialsum);
}
//initialsum is the value of zeta we are computing incrementally from
double zetastatic(long st, long n, double initialsum){
double sum=initialsum;
for (long i=st; i<n; i++){
sum+=1/(pow(i+1,theta));
}
return sum;
}
long next_long(long itemcount){
//from "Quickly Generating Billion-Record Synthetic Databases", Jim Gray et al, SIGMOD 1994
if (itemcount!=countforzeta){
if (itemcount>countforzeta){
printf("WARNING: Incrementally recomputing Zipfian distribtion. (itemcount= %ld; countforzeta= %ld)", itemcount, countforzeta);
//we have added more items. can compute zetan incrementally, which is cheaper
zetan = zeta(countforzeta,itemcount,zetan);
eta = ( 1 - pow(2.0/items,1-theta) ) / (1-zeta2theta/zetan);
}
}
double u = (double)(rand_r(&seed)%RAND_MAX) / ((double)RAND_MAX);
double uz=u*zetan;
if (uz < 1.0){
return base;
}
if (uz<1.0 + pow(0.5,theta)) {
return base + 1;
}
long ret = base + (long)((itemcount) * pow(eta*u - eta + 1, alpha));
return ret;
}
long zipf_next() {
return next_long(items);
}
/* Uniform */
long uniform_next() {
return rand_r(&seed) % items;
}
/* bogus rand */
long bogus_rand() {
return rand_r(&seed) % 1000;
}
/* production workload randomness */
long production_random1(void) {
long rand_key = rand_r(&seed);
long prob = rand_r(&seed) % 10000;
if (prob <13) {
rand_key = 0 + rand_key % 144000000;
} else if (prob < 8130) {
rand_key = 144000000 + rand_key % (314400000-144000000);
} else if (prob < 9444) {
rand_key = 314400000 + rand_key % (450000000-314400000);
} else if (prob < 9742) {
rand_key = 450000000 + rand_key % (480000000-450000000);
} else if (prob < 9920) {
rand_key = 480000000 + rand_key % (490000000-480000000);
} else {
rand_key = 490000000 + rand_key % (500000000-490000000);
}
return rand_key;
}
long production_random2(void) {
long rand_key = rand_r(&seed);
long prob = rand_r(&seed) % 10000;
if (prob < 103487) {
rand_key = rand_key % 47016400;
} else if (prob < 570480) {
rand_key = 47016400 + rand_key % (259179450 - 47016400);
} else if (prob < 849982) {
rand_key = 259179450 + rand_key % (386162550 - 259179450);
} else if (prob < 930511) {
rand_key = 386162550 + rand_key % (422748200 - 386162550);
} else if (prob < 973234) {
rand_key = 422748200 + rand_key % (442158000 - 422748200);
} else if (prob < 986958) {
rand_key = 442158000 + rand_key % (448392900 - 442158000) ;
} else {
rand_key = 448392900 + rand_key % (500000000 - 448392900);
}
return rand_key;
}
/* Helper */
const char *get_function_name(random_gen_t f) {
if(f == zipf_next)
return "Zipf";
if(f == uniform_next)
return "Uniform";
if(f == bogus_rand)
return "Cached";
if(f == production_random1)
return "Production1";
if(f == production_random2)
return "Production2";
return "Unknown random";
}