rand: cleanup rand_between() and helpers
authorJens Axboe <axboe@kernel.dk>
Tue, 12 Jun 2018 14:42:49 +0000 (08:42 -0600)
committerJens Axboe <axboe@kernel.dk>
Tue, 12 Jun 2018 14:48:55 +0000 (08:48 -0600)
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 <axboe@kernel.dk>
engines/sync.c
io_u.c
lib/rand.h
t/gen-rand.c

index 3f36da824ec30e1e95111561f480abadfa5780e8..b3e1c9db8ba3eeb9d87567dc53b69de5759be715 100644 (file)
@@ -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 945aa19288bdc333c0d7a5e6087c54813ea420d3..580c414622db161fe78061413ac5bef6de7e0efb 100644 (file)
--- 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;
index 6be53072a64751c4ae864a439e33a374205e3c4b..4f10fb3c57afe6946e931b0f95233db3f68d6aaf 100644 (file)
@@ -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);
index c379053db61718fca125f0113137a3ca20cbea9c..b050bd715a6c067ddc349392f9dc909cddeb9d19 100644 (file)
@@ -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]++;
        }