X-Git-Url: https://git.kernel.dk/?a=blobdiff_plain;f=lib%2Flfsr.c;h=0c0072ccb39b15e970eeb6bd92342b67a85cd8bb;hb=92330695e64a42fd5dc54a6970a4b8122d3cd80e;hp=4c15c6256f9a1d41ab95a1bfdb0d8e59fb92796a;hpb=a64ea63f648e77d583b5287634f44bea7da79b4b;p=fio.git diff --git a/lib/lfsr.c b/lib/lfsr.c index 4c15c625..0c0072cc 100644 --- a/lib/lfsr.c +++ b/lib/lfsr.c @@ -2,6 +2,7 @@ #include #include "lfsr.h" +#include "../compiler/compiler.h" /* * LFSR taps retrieved from: @@ -10,7 +11,7 @@ * The memory overhead of the following tap table should be relatively small, * no more than 400 bytes. */ -static uint8_t taps[64][FIO_MAX_TAPS] = +static uint8_t lfsr_taps[64][FIO_MAX_TAPS] = { {0}, {0}, {0}, //LFSRs with less that 3-bits cannot exist {3, 2}, //Tap position for 3-bit LFSR @@ -87,7 +88,6 @@ static inline void __lfsr_next(struct fio_lfsr *fl, unsigned int spin) * this switch. */ switch (spin) { - case 16: __LFSR_NEXT(fl, fl->last_val); case 15: __LFSR_NEXT(fl, fl->last_val); case 14: __LFSR_NEXT(fl, fl->last_val); case 13: __LFSR_NEXT(fl, fl->last_val); @@ -112,27 +112,30 @@ static inline void __lfsr_next(struct fio_lfsr *fl, unsigned int spin) * lfsr_next does the following: * * a. Return if the number of max values has been exceeded. - * b. Check if the next iteration(s) produce a cycle (due to spin) and add "1" - * where necessary. - * c. Calculate the next value and return. + * b. Check if we have a spin value that produces a repeating subsequence. + * This is previously calculated in `prepare_spin` and cycle_length should + * be > 0. If we do have such a spin: + * + * i. Decrement the calculated cycle. + * ii. If it reaches zero, add "+1" to the spin and reset the cycle_length + * (we have it cached in the struct fio_lfsr) + * + * In either case, continue with the calculation of the next value. + * c. Check if the calculated value exceeds the desirable range. In this case, + * go back to b, else return. */ -int lfsr_next(struct fio_lfsr *fl, uint64_t *off, uint64_t last) +int lfsr_next(struct fio_lfsr *fl, uint64_t *off) { - int repeat; - unsigned int spin; - if (fl->num_vals++ > fl->max_val) return 1; - repeat = fl->num_vals % fl->cycle_length; - if (repeat == 0) - spin = fl->spin + 1; - else - spin = fl->spin; - do { - __lfsr_next(fl, spin); - } while (fl->last_val > fl->max_val); + if (fl->cycle_length && !--fl->cycle_length) { + __lfsr_next(fl, fl->spin + 1); + fl->cycle_length = fl->cached_cycle_length; + } else + __lfsr_next(fl, fl->spin); + } while (fio_unlikely(fl->last_val > fl->max_val)); *off = fl->last_val; return 0; @@ -153,9 +156,14 @@ static uint8_t *find_lfsr(uint64_t size) { int i; + /* + * For an LFSR, there is always a prohibited state (all ones). + * Thus, if we need to find the proper LFSR for our size, we must + * take that into account. + */ for (i = 3; i < 64; i++) - if ((1UL << i) > size) /* TODO: Explain why. */ - return taps[i]; + if ((1UL << i) > size) + return lfsr_taps[i]; return NULL; } @@ -178,7 +186,7 @@ static uint8_t *find_lfsr(uint64_t size) * Thus, [1] is equivalent to (y * i) % (spin + 1) == 0; * Also, the cycle's length will be (x * i) + (y * i) / (spin + 1) */ -int prepare_spin(struct fio_lfsr *fl, unsigned int spin) +static int prepare_spin(struct fio_lfsr *fl, unsigned int spin) { uint64_t max = (fl->cached_bit << 1) - 1; uint64_t x, y; @@ -189,7 +197,7 @@ int prepare_spin(struct fio_lfsr *fl, unsigned int spin) x = max / (spin + 1); y = max % (spin + 1); - fl->cycle_length = max; /* This is the expected cycle */ + fl->cycle_length = 0; /* No cycle occurs, other than the expected */ fl->spin = spin; for (i = 1; i <= spin; i++) { @@ -198,6 +206,13 @@ int prepare_spin(struct fio_lfsr *fl, unsigned int spin) break; } } + fl->cached_cycle_length = fl->cycle_length; + + /* + * Increment cycle length for the first time only since the stored value + * will not be printed otherwise. + */ + fl->cycle_length++; return 0; } @@ -219,15 +234,15 @@ int lfsr_reset(struct fio_lfsr *fl, unsigned long seed) int lfsr_init(struct fio_lfsr *fl, uint64_t nums, unsigned long seed, unsigned int spin) { - uint8_t *lfsr_taps; + uint8_t *taps; - lfsr_taps = find_lfsr(nums); - if (!lfsr_taps) + taps = find_lfsr(nums); + if (!taps) return 1; fl->max_val = nums - 1; - fl->xormask = lfsr_create_xormask(lfsr_taps); - fl->cached_bit = 1UL << (lfsr_taps[0] - 1); + fl->xormask = lfsr_create_xormask(taps); + fl->cached_bit = 1UL << (taps[0] - 1); if (prepare_spin(fl, spin)) return 1;