forked from filebench/filebench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb_random.c
371 lines (306 loc) · 8.79 KB
/
fb_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <fcntl.h>
/* this definition prevents warning about
using undefined round() function */
#include <math.h>
#include "filebench.h"
#include "ipc.h"
#include "gamma_dist.h"
#include "cvars/mtwist/mtwist.h"
/*
* Generates a 64-bit random number using mtwist or from a provided random
* variable "avd".
*
* Returned random number "randp" is clipped by the "max" value and rounded off
* by the "round" value. Returns 0 on success, shuts down Filebench on
* failure.
*/
void
fb_random64(uint64_t *randp, uint64_t max, uint64_t round, avd_t avd)
{
double random_normalized;
uint64_t random = 0;
if (avd) {
/* get it from the random variable */
if (!AVD_IS_RANDOM(avd)) {
/* trying to get random value not from
random variable. That's a clear error. */
filebench_log(LOG_ERROR, "filebench_randomno64: trying"
" to get a random value from not a random variable");
filebench_shutdown(1);
/* NOT REACHABLE */
} else {
random = avd_get_int(avd);
}
} else {
random = mt_llrand();
}
/*
* If round is not zero, then caller will get a random number in the
* range [0; max - round]. This allows the caller to use this
* function, for example, to obtain a pointer in an allocated memory
* region of [0; max] and read/write round bytes safely starting
* at this pointer.
*/
max = max - round;
random_normalized = (double)random / UINT64_MAX;
random = random_normalized * max;
if (round) {
random = random / round;
random = random * round;
}
*randp = random;
}
/*
* Same as filebench_randomno64, but for 32 bit integers.
*/
void
fb_random32(uint32_t *randp,
uint32_t max, uint32_t round, avd_t avd)
{
uint64_t rand64;
fb_random64(&rand64, max, round, avd);
/* rand64 always fits uint32, since "max" above was 32 bit */
*randp = (uint32_t)rand64;
}
/*
* Same as filebench_randomno64, but for probability [0-1].
*/
static double
fb_random_probability()
{
uint64_t randnum;
fb_random64(&randnum, UINT64_MAX, 0, NULL);
/* convert to 0-1 probability */
return (double)randnum / (double)(UINT64_MAX);
}
/****************************************
* *
* randist related functions *
* *
****************************************/
static double
fb_rand_src_rand48(unsigned short *xi)
{
return (erand48(xi));
}
static double
fb_rand_src_random(unsigned short *xi)
{
return fb_random_probability();
}
/*
* fetch a uniformly distributed random number from the supplied
* random object.
*/
static double
rand_uniform_get(randdist_t *rndp)
{
double dprob, dmin, dres, dround;
dmin = (double)rndp->rnd_vint_min;
dround = (double)rndp->rnd_vint_round;
dprob = (*rndp->rnd_src)(rndp->rnd_xi);
dres = (dprob * (2.0 * (rndp->rnd_dbl_mean - dmin))) + dmin;
if (dround == 0.0)
return (dres);
else
return (round(dres / dround) * dround);
}
/*
* fetch a gamma distributed random number from the supplied
* random object.
*/
static double
rand_gamma_get(randdist_t *rndp)
{
double dmult, dres, dmin, dround;
dmin = (double)rndp->rnd_vint_min;
dround = (double)rndp->rnd_vint_round;
dmult = (rndp->rnd_dbl_mean - dmin) / rndp->rnd_dbl_gamma;
dres = gamma_dist_knuth_src(rndp->rnd_dbl_gamma,
dmult, rndp->rnd_src, rndp->rnd_xi) + dmin;
if (dround == 0.0)
return (dres);
else
return (round(dres / dround) * dround);
}
/*
* fetch a table driven random number from the supplied
* random object.
*/
static double
rand_table_get(randdist_t *rndp)
{
double dprob, dprcnt, dtabres, dsclres, dmin, dround;
int idx;
dmin = (double)rndp->rnd_vint_min;
dround = (double)rndp->rnd_vint_round;
dprob = (*rndp->rnd_src)(rndp->rnd_xi);
dprcnt = (dprob * (double)(PF_TAB_SIZE));
idx = (int)dprcnt;
dtabres = (rndp->rnd_rft[idx].rf_base +
(rndp->rnd_rft[idx].rf_range * (dprcnt - (double)idx)));
dsclres = (dtabres * (rndp->rnd_dbl_mean - dmin)) + dmin;
if (dround == 0.0)
return (dsclres);
else
return (round(dsclres / dround) * dround);
}
/*
* Set the random seed in the supplied random object.
*/
static void
rand_seed_set(randdist_t *rndp)
{
union {
uint64_t ll;
uint16_t w[4];
} temp1;
int idx;
temp1.ll = (uint64_t)avd_get_int(rndp->rnd_seed);
for (idx = 0; idx < 3; idx++) {
#ifdef _BIG_ENDIAN
rndp->rnd_xi[idx] = temp1.w[3-idx];
#else
rndp->rnd_xi[idx] = temp1.w[idx];
#endif
}
}
/*
* Define a random entity which will contain the parameters of a random
* distribution.
*/
randdist_t *
randdist_alloc(void)
{
randdist_t *rndp;
if ((rndp = (randdist_t *)ipc_malloc(FILEBENCH_RANDDIST)) == NULL) {
filebench_log(LOG_ERROR, "Out of memory for random dist");
return (NULL);
}
/* place on global list */
rndp->rnd_next = filebench_shm->shm_rand_list;
filebench_shm->shm_rand_list = rndp;
return (rndp);
}
/*
* Initializes a random distribution entity, converting avd_t parameters to
* doubles, and converting the list of probability density function table
* entries, if supplied, into a probablilty function table.
*/
void
randdist_init(randdist_t *rndp)
{
probtabent_t *rdte_hdp, *ptep;
double tablemean, tablemin = 0;
int pteidx;
/* convert parameters to doubles */
rndp->rnd_dbl_gamma = (double)avd_get_int(rndp->rnd_gamma) / 1000.0;
if (rndp->rnd_mean != NULL)
rndp->rnd_dbl_mean = (double)avd_get_int(rndp->rnd_mean);
else
rndp->rnd_dbl_mean = rndp->rnd_dbl_gamma;
/* de-reference min and round amounts for later use */
rndp->rnd_vint_min = avd_get_int(rndp->rnd_min);
rndp->rnd_vint_round = avd_get_int(rndp->rnd_round);
filebench_log(LOG_DEBUG_IMPL,
"init random var: Mean = %6.0llf, Gamma = %6.3llf, Min = %llu",
rndp->rnd_dbl_mean, rndp->rnd_dbl_gamma,
(u_longlong_t)rndp->rnd_vint_min);
/* initialize distribution to apply */
switch (rndp->rnd_type & RAND_TYPE_MASK) {
case RAND_TYPE_UNIFORM:
rndp->rnd_get = rand_uniform_get;
break;
case RAND_TYPE_GAMMA:
rndp->rnd_get = rand_gamma_get;
break;
case RAND_TYPE_TABLE:
rndp->rnd_get = rand_table_get;
break;
default:
filebench_log(LOG_DEBUG_IMPL, "Random Type not Specified");
filebench_shutdown(1);
return;
}
/* initialize source of random numbers */
if (rndp->rnd_type & RAND_SRC_GENERATOR) {
rndp->rnd_src = fb_rand_src_rand48;
rand_seed_set(rndp);
} else {
rndp->rnd_src = fb_rand_src_random;
}
/* any random distribution table to convert? */
if ((rdte_hdp = rndp->rnd_probtabs) == NULL)
return;
/* determine random distribution max and mins and initialize table */
pteidx = 0;
tablemean = 0.0;
for (ptep = rdte_hdp; ptep; ptep = ptep->pte_next) {
double dmin, dmax;
int entcnt;
dmax = (double)avd_get_int(ptep->pte_segmax);
dmin = (double)avd_get_int(ptep->pte_segmin);
/* initialize table minimum on first pass */
if (pteidx == 0)
tablemin = dmin;
/* update table minimum */
if (tablemin > dmin)
tablemin = dmin;
entcnt = (int)avd_get_int(ptep->pte_percent);
tablemean += (((dmin + dmax)/2.0) * (double)entcnt);
/* populate the lookup table */
for (; entcnt > 0; entcnt--) {
rndp->rnd_rft[pteidx].rf_base = dmin;
rndp->rnd_rft[pteidx].rf_range = dmax - dmin;
pteidx++;
}
}
/* check to see if probability equals 100% */
if (pteidx != PF_TAB_SIZE)
filebench_log(LOG_ERROR,
"Prob table only totals %d%%", pteidx);
/* If table is not supplied with a mean value, set it to table mean */
if (rndp->rnd_dbl_mean == 0.0)
rndp->rnd_dbl_mean = (double)tablemean / (double)PF_TAB_SIZE;
/* now normalize the entries for a min value of 0, mean of 1 */
tablemean = (tablemean / 100.0) - tablemin;
/* special case if really a constant value */
if (tablemean == 0.0) {
for (pteidx = 0; pteidx < PF_TAB_SIZE; pteidx++) {
rndp->rnd_rft[pteidx].rf_base = 0.0;
rndp->rnd_rft[pteidx].rf_range = 0.0;
}
return;
}
for (pteidx = 0; pteidx < PF_TAB_SIZE; pteidx++) {
rndp->rnd_rft[pteidx].rf_base =
((rndp->rnd_rft[pteidx].rf_base - tablemin) / tablemean);
rndp->rnd_rft[pteidx].rf_range =
(rndp->rnd_rft[pteidx].rf_range / tablemean);
}
}