From 1922c9f55016c5de0f67ba04cdd22422b8d31eb9 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Sat, 2 Jul 2022 10:23:57 -0600 Subject: [PATCH] rand: prepare for adding a third random type We currently have 32 and 64-bit variants. Rather than have a bool that tells us which one to use, add enum fio_rand_type so we have flexibility to add another one. Signed-off-by: Jens Axboe --- init.c | 67 ++++++++++++++++++++++++++++++-------------------- lib/rand.c | 25 +++++++++++++------ lib/rand.h | 62 +++++++++++++++++++++++++++++++++------------- verify-state.h | 2 +- verify.c | 10 ++++---- 5 files changed, 109 insertions(+), 57 deletions(-) diff --git a/init.c b/init.c index da800776..f6d724a3 100644 --- a/init.c +++ b/init.c @@ -1009,18 +1009,19 @@ static void init_rand_file_service(struct thread_data *td) void td_fill_verify_state_seed(struct thread_data *td) { - bool use64; + enum fio_rand_type rand_type; if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) - use64 = true; + rand_type = FIO_RAND_64; else - use64 = false; + rand_type = FIO_RAND_32; init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF], - use64); + rand_type); } -static void td_fill_rand_seeds_internal(struct thread_data *td, bool use64) +static void td_fill_rand_seeds_internal(struct thread_data *td, + enum fio_rand_type rand_type) { uint64_t read_seed = td->rand_seeds[FIO_RAND_BS_OFF]; uint64_t write_seed = td->rand_seeds[FIO_RAND_BS1_OFF]; @@ -1039,28 +1040,39 @@ static void td_fill_rand_seeds_internal(struct thread_data *td, bool use64) write_seed = read_seed; if (td_trimwrite(td)) trim_seed = write_seed; - init_rand_seed(&td->bsrange_state[DDIR_READ], read_seed, use64); - init_rand_seed(&td->bsrange_state[DDIR_WRITE], write_seed, use64); - init_rand_seed(&td->bsrange_state[DDIR_TRIM], trim_seed, use64); + init_rand_seed(&td->bsrange_state[DDIR_READ], read_seed, rand_type); + init_rand_seed(&td->bsrange_state[DDIR_WRITE], write_seed, rand_type); + init_rand_seed(&td->bsrange_state[DDIR_TRIM], trim_seed, rand_type); td_fill_verify_state_seed(td); init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF], false); if (td->o.file_service_type == FIO_FSERVICE_RANDOM) - init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], use64); + init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], rand_type); else if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM) init_rand_file_service(td); - init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], use64); - init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF], use64); - init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY], use64); - init_rand_seed(&td->poisson_state[0], td->rand_seeds[FIO_RAND_POISSON_OFF], 0); - init_rand_seed(&td->poisson_state[1], td->rand_seeds[FIO_RAND_POISSON2_OFF], 0); - init_rand_seed(&td->poisson_state[2], td->rand_seeds[FIO_RAND_POISSON3_OFF], 0); - init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], false); - init_rand_seed(&td->zone_state, td->rand_seeds[FIO_RAND_ZONE_OFF], false); - init_rand_seed(&td->prio_state, td->rand_seeds[FIO_RAND_PRIO_CMDS], false); - init_rand_seed(&td->dedupe_working_set_index_state, td->rand_seeds[FIO_RAND_DEDUPE_WORKING_SET_IX], use64); + init_rand_seed(&td->file_size_state, + td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], rand_type); + init_rand_seed(&td->trim_state, + td->rand_seeds[FIO_RAND_TRIM_OFF], rand_type); + init_rand_seed(&td->delay_state, + td->rand_seeds[FIO_RAND_START_DELAY], rand_type); + init_rand_seed(&td->poisson_state[0], + td->rand_seeds[FIO_RAND_POISSON_OFF], FIO_RAND_32); + init_rand_seed(&td->poisson_state[1], + td->rand_seeds[FIO_RAND_POISSON2_OFF], FIO_RAND_32); + init_rand_seed(&td->poisson_state[2], + td->rand_seeds[FIO_RAND_POISSON3_OFF], FIO_RAND_32); + init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], + FIO_RAND_32); + init_rand_seed(&td->zone_state, td->rand_seeds[FIO_RAND_ZONE_OFF], + FIO_RAND_32); + init_rand_seed(&td->prio_state, td->rand_seeds[FIO_RAND_PRIO_CMDS], + FIO_RAND_32); + init_rand_seed(&td->dedupe_working_set_index_state, + td->rand_seeds[FIO_RAND_DEDUPE_WORKING_SET_IX], + rand_type); if (!td_random(td)) return; @@ -1068,18 +1080,20 @@ static void td_fill_rand_seeds_internal(struct thread_data *td, bool use64) if (td->o.rand_repeatable) td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number; - init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], use64); + init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], + rand_type); for (i = 0; i < DDIR_RWDIR_CNT; i++) { struct frand_state *s = &td->seq_rand_state[i]; - init_rand_seed(s, td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], false); + init_rand_seed(s, td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], + FIO_RAND_32); } } void td_fill_rand_seeds(struct thread_data *td) { - bool use64; + enum fio_rand_type rand_type; if (td->o.allrand_repeatable) { unsigned int i; @@ -1090,13 +1104,14 @@ void td_fill_rand_seeds(struct thread_data *td) } if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) - use64 = true; + rand_type = FIO_RAND_64; else - use64 = false; + rand_type = FIO_RAND_32; - td_fill_rand_seeds_internal(td, use64); + td_fill_rand_seeds_internal(td, rand_type); - init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], use64); + init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], + rand_type); frand_copy(&td->buf_state_prev, &td->buf_state); } diff --git a/lib/rand.c b/lib/rand.c index 0e787a62..2c593569 100644 --- a/lib/rand.c +++ b/lib/rand.c @@ -75,24 +75,33 @@ void __init_rand64(struct taus258_state *state, uint64_t seed) __rand64(state); } -void init_rand(struct frand_state *state, bool use64) +void init_rand(struct frand_state *state, enum fio_rand_type rand_type) { - state->use64 = use64; + state->rand_type = rand_type; - if (!use64) + switch (rand_type) { + case FIO_RAND_32: __init_rand32(&state->state32, 1); - else + break; + case FIO_RAND_64: __init_rand64(&state->state64, 1); + break; + } } -void init_rand_seed(struct frand_state *state, uint64_t seed, bool use64) +void init_rand_seed(struct frand_state *state, uint64_t seed, + enum fio_rand_type rand_type) { - state->use64 = use64; + state->rand_type = rand_type; - if (!use64) + switch (rand_type) { + case FIO_RAND_32: __init_rand32(&state->state32, (unsigned int) seed); - else + break; + case FIO_RAND_64: __init_rand64(&state->state64, seed); + break; + } } void __fill_random_buf_small(void *buf, unsigned int len, uint64_t seed) diff --git a/lib/rand.h b/lib/rand.h index 2b4be788..77548f90 100644 --- a/lib/rand.h +++ b/lib/rand.h @@ -3,6 +3,7 @@ #include #include +#include #include "types.h" #define FRAND32_MAX (-1U) @@ -10,6 +11,11 @@ #define FRAND64_MAX (-1ULL) #define FRAND64_MAX_PLUS_ONE (1.0 * (1ULL << 32) * (1ULL << 32)) +enum fio_rand_type { + FIO_RAND_32, + FIO_RAND_64, +}; + struct taus88_state { unsigned int s1, s2, s3; }; @@ -19,7 +25,7 @@ struct taus258_state { }; struct frand_state { - unsigned int use64; + enum fio_rand_type rand_type; union { struct taus88_state state32; struct taus258_state state64; @@ -28,10 +34,14 @@ struct frand_state { static inline uint64_t rand_max(struct frand_state *state) { - if (state->use64) + switch (state->rand_type) { + case FIO_RAND_64: return FRAND64_MAX; - else + case FIO_RAND_32: return FRAND32_MAX; + default: + return ~0UL; + } } static inline void __frand32_copy(struct taus88_state *dst, @@ -54,12 +64,15 @@ static inline void __frand64_copy(struct taus258_state *dst, static inline void frand_copy(struct frand_state *dst, struct frand_state *src) { - if (src->use64) + switch (src->rand_type) { + case FIO_RAND_64: __frand64_copy(&dst->state64, &src->state64); - else + break; + case FIO_RAND_32: __frand32_copy(&dst->state32, &src->state32); - - dst->use64 = src->use64; + break; + } + dst->rand_type = src->rand_type; } static inline unsigned int __rand32(struct taus88_state *state) @@ -97,22 +110,32 @@ static inline uint64_t __rand64(struct taus258_state *state) static inline uint64_t __rand(struct frand_state *state) { - if (state->use64) + switch (state->rand_type) { + case FIO_RAND_64: return __rand64(&state->state64); - else + case FIO_RAND_32: return __rand32(&state->state32); + default: + return 0; + } } static inline double __rand_0_1(struct frand_state *state) { - if (state->use64) { + switch (state->rand_type) { + case FIO_RAND_64: { uint64_t val = __rand64(&state->state64); return (val + 1.0) / FRAND64_MAX_PLUS_ONE; - } else { + } + case FIO_RAND_32: { uint32_t val = __rand32(&state->state32); return (val + 1.0) / FRAND32_MAX_PLUS_ONE; + } + default: + fprintf(stderr, "fio: illegal rand type for 0..1 generation\n"); + return 0.0; } } @@ -120,7 +143,7 @@ static inline uint32_t rand32_upto(struct frand_state *state, uint32_t end) { uint32_t r; - assert(!state->use64); + assert(state->rand_type == FIO_RAND_32); r = __rand32(&state->state32); end++; @@ -131,7 +154,7 @@ static inline uint64_t rand64_upto(struct frand_state *state, uint64_t end) { uint64_t r; - assert(state->use64); + assert(state->rand_type == FIO_RAND_64); r = __rand64(&state->state64); end++; @@ -144,10 +167,15 @@ static inline uint64_t rand64_upto(struct frand_state *state, uint64_t end) static inline uint64_t rand_between(struct frand_state *state, uint64_t start, uint64_t end) { - if (state->use64) + switch (state->rand_type) { + case FIO_RAND_64: return start + rand64_upto(state, end - start); - else + case FIO_RAND_32: return start + rand32_upto(state, end - start); + default: + fprintf(stderr, "fio: illegal rand type for rand_between\n"); + return start; + } } static inline uint64_t __get_next_seed(struct frand_state *fs) @@ -160,8 +188,8 @@ static inline uint64_t __get_next_seed(struct frand_state *fs) return r; } -extern void init_rand(struct frand_state *, bool); -extern void init_rand_seed(struct frand_state *, uint64_t seed, bool); +extern void init_rand(struct frand_state *, enum fio_rand_type); +extern void init_rand_seed(struct frand_state *, uint64_t seed, enum fio_rand_type); void __init_rand64(struct taus258_state *state, uint64_t seed); extern void __fill_random_buf(void *buf, unsigned int len, uint64_t seed); extern uint64_t fill_random_buf(struct frand_state *, void *buf, unsigned int len); diff --git a/verify-state.h b/verify-state.h index 6da1585b..57ff9918 100644 --- a/verify-state.h +++ b/verify-state.h @@ -15,7 +15,7 @@ struct thread_rand64_state { }; struct thread_rand_state { - uint64_t use64; + uint64_t rand_type; union { struct thread_rand32_state state32; struct thread_rand64_state state64; diff --git a/verify.c b/verify.c index 0e1e4639..ab8e784a 100644 --- a/verify.c +++ b/verify.c @@ -1616,20 +1616,20 @@ struct all_io_list *get_all_io_list(int save_mask, size_t *sz) s->nofiles = cpu_to_le64((uint64_t) td->o.nr_files); s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]); s->index = cpu_to_le64((uint64_t) i); - if (td->random_state.use64) { + if (td->random_state.rand_type == FIO_RAND_64) { s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1); s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2); s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3); s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4); s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5); s->rand.state64.s[5] = 0; - s->rand.use64 = cpu_to_le64((uint64_t)1); + s->rand.rand_type = cpu_to_le64((uint64_t)FIO_RAND_64); } else { s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1); s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2); s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3); s->rand.state32.s[3] = 0; - s->rand.use64 = 0; + s->rand.rand_type = cpu_to_le64((uint64_t)FIO_RAND_32); } snprintf((char *) s->name, sizeof(s->name), "%s", td->o.name); next = io_list_next(s); @@ -1742,9 +1742,9 @@ void verify_assign_state(struct thread_data *td, void *p) s->depth = le32_to_cpu(s->depth); s->nofiles = le32_to_cpu(s->nofiles); s->numberio = le64_to_cpu(s->numberio); - s->rand.use64 = le64_to_cpu(s->rand.use64); + s->rand.rand_type = le64_to_cpu(s->rand.rand_type); - if (s->rand.use64) { + if (s->rand.rand_type == FIO_RAND_64) { for (i = 0; i < 6; i++) s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]); } else { -- 2.25.1