LFSR: Do not ignore returning the seed
authorAlex Pyrgiotis <apyrgio@grnet.gr>
Tue, 27 Aug 2013 16:58:34 +0000 (10:58 -0600)
committerJens Axboe <axboe@kernel.dk>
Tue, 27 Aug 2013 16:58:34 +0000 (10:58 -0600)
Fix a situation where we would spin prematurely and would hop over the
seed value.

Also, add some more comments to the code.

Signed-off-by: Alex Pyrgiotis <apyrgio@grnet.gr>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
lib/lfsr.c

index b10ba7a7e1b3cfc07609b53f6c68317fa14cb53e..927b2a10cdeeefc4441a9b7aa720677b6939a9be 100644 (file)
@@ -87,7 +87,6 @@ static inline void __lfsr_next(struct fio_lfsr *fl, unsigned int spin)
         * this switch.
         */
        switch (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);
                case 15: __LFSR_NEXT(fl, fl->last_val);
                case 14: __LFSR_NEXT(fl, fl->last_val);
                case 13: __LFSR_NEXT(fl, fl->last_val);
@@ -126,21 +125,16 @@ static inline void __lfsr_next(struct fio_lfsr *fl, unsigned int spin)
  */
 int lfsr_next(struct fio_lfsr *fl, uint64_t *off, uint64_t last)
 {
  */
 int lfsr_next(struct fio_lfsr *fl, uint64_t *off, uint64_t last)
 {
-       unsigned int spin = fl->spin;
-
        if (fl->num_vals++ > fl->max_val)
                return 1;
 
        do {
        if (fl->num_vals++ > fl->max_val)
                return 1;
 
        do {
-               if (fl->cycle_length) {
-                       fl->cycle_length--;
-                       if (!fl->cycle_length) {
-                               __lfsr_next(fl, fl->spin + 1);
-                               fl->cycle_length = fl->cached_cycle_length;
-                               goto check;
-                       }
+               if (fl->cycle_length && !--fl->cycle_length) {
+                       __lfsr_next(fl, fl->spin + 1);
+                       fl->cycle_length = fl->cached_cycle_length;
+                       goto check;
                }
                }
-               __lfsr_next(fl, spin);
+               __lfsr_next(fl, fl->spin);
 check: ;
        } while (fl->last_val > fl->max_val);
 
 check: ;
        } while (fl->last_val > fl->max_val);
 
@@ -163,8 +157,13 @@ static uint8_t *find_lfsr(uint64_t size)
 {
        int i;
 
 {
        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++)
        for (i = 3; i < 64; i++)
-               if ((1UL << i) > size) /* TODO: Explain why. */
+               if ((1UL << i) > size)
                        return taps[i];
 
        return NULL;
                        return taps[i];
 
        return NULL;
@@ -210,6 +209,12 @@ int prepare_spin(struct fio_lfsr *fl, unsigned int spin)
        }
        fl->cached_cycle_length = fl->cycle_length;
 
        }
        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;
 }
 
        return 0;
 }