From 1bd5d21380cdafe36db5a8945fb7c755d8ce3d43 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Tue, 12 Jun 2018 08:42:49 -0600 Subject: [PATCH] rand: cleanup rand_between() and helpers Make the 32/64-bit helper just return a random number up to a certain value, and let the generic helper handle the range part. Signed-off-by: Jens Axboe --- engines/sync.c | 2 +- io_u.c | 10 +++++----- lib/rand.h | 15 ++++++++------- t/gen-rand.c | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/engines/sync.c b/engines/sync.c index 3f36da82..b3e1c9db 100644 --- a/engines/sync.c +++ b/engines/sync.c @@ -150,7 +150,7 @@ static enum fio_q_status fio_pvsyncio2_queue(struct thread_data *td, fio_ro_check(td, io_u); if (o->hipri && - (rand32_between(&sd->rand_state, 1, 100) <= o->hipri_percentage)) + (rand_between(&sd->rand_state, 1, 100) <= o->hipri_percentage)) flags |= RWF_HIPRI; iov->iov_base = io_u->xfer_buf; diff --git a/io_u.c b/io_u.c index 945aa192..580c4146 100644 --- a/io_u.c +++ b/io_u.c @@ -168,7 +168,7 @@ bail: /* * Generate a value, v, between 1 and 100, both inclusive */ - v = rand32_between(&td->zone_state, 1, 100); + v = rand_between(&td->zone_state, 1, 100); /* * Find our generated table. 'send' is the end block of this zone, @@ -225,7 +225,7 @@ bail: /* * Generate a value, v, between 1 and 100, both inclusive */ - v = rand32_between(&td->zone_state, 1, 100); + v = rand_between(&td->zone_state, 1, 100); zsi = &td->zone_state_index[ddir][v - 1]; stotal = zsi->size_perc_prev; @@ -300,7 +300,7 @@ static bool should_do_random(struct thread_data *td, enum fio_ddir ddir) if (td->o.perc_rand[ddir] == 100) return true; - v = rand32_between(&td->seq_rand_state[ddir], 1, 100); + v = rand_between(&td->seq_rand_state[ddir], 1, 100); return v <= td->o.perc_rand[ddir]; } @@ -589,7 +589,7 @@ static inline enum fio_ddir get_rand_ddir(struct thread_data *td) { unsigned int v; - v = rand32_between(&td->rwmix_state, 1, 100); + v = rand_between(&td->rwmix_state, 1, 100); if (v <= td->o.rwmix[DDIR_READ]) return DDIR_READ; @@ -2069,7 +2069,7 @@ static struct frand_state *get_buf_state(struct thread_data *td) return &td->buf_state; } - v = rand32_between(&td->dedupe_state, 1, 100); + v = rand_between(&td->dedupe_state, 1, 100); if (v <= td->o.dedupe_percentage) return &td->buf_state_prev; diff --git a/lib/rand.h b/lib/rand.h index 6be53072..4f10fb3c 100644 --- a/lib/rand.h +++ b/lib/rand.h @@ -117,32 +117,33 @@ static inline double __rand_0_1(struct frand_state *state) /* * Generate a random value between 'start' and 'end', both inclusive */ -static inline int rand32_between(struct frand_state *state, int start, int end) +static inline uint32_t rand32_upto(struct frand_state *state, uint32_t end) { uint32_t r; assert(!state->use64); r = __rand32(&state->state32); - return start + (int) ((double)end * (r / (FRAND32_MAX + 1.0))); + return (int) ((double)end * (r / (FRAND32_MAX + 1.0))); } -static inline uint64_t rand64_between(struct frand_state *state, uint64_t start, - uint64_t end) +static inline uint64_t rand64_upto(struct frand_state *state, uint64_t end) { uint64_t r; + assert(state->use64); + r = __rand64(&state->state64); - return start + (uint64_t) ((double)end * (r / (FRAND64_MAX + 1.0))); + return (uint64_t) ((double)end * (r / (FRAND64_MAX + 1.0))); } static inline uint64_t rand_between(struct frand_state *state, uint64_t start, uint64_t end) { if (state->use64) - return rand32_between(state, start, end); + return start + rand64_upto(state, end - start); else - return rand64_between(state, start, end); + return start + rand32_upto(state, end - start); } extern void init_rand(struct frand_state *, bool); diff --git a/t/gen-rand.c b/t/gen-rand.c index c379053d..b050bd71 100644 --- a/t/gen-rand.c +++ b/t/gen-rand.c @@ -34,7 +34,7 @@ int main(int argc, char *argv[]) init_rand(&s, false); for (i = 0; i < nvalues; i++) { - int v = rand32_between(&s, start, end); + int v = rand_between(&s, start, end); buckets[v - start]++; } -- 2.25.1