Add distclean target
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index e474b48f9ca0c4ab5a26f49363f90e2a4bacc1d2..d03049e52332e2b01510364863af812ecfd8e496 100644 (file)
--- a/io_u.c
+++ b/io_u.c
@@ -191,6 +191,25 @@ static inline int should_sort_io(struct thread_data *td)
        return 1;
 }
 
+static int should_do_random(struct thread_data *td)
+{
+       unsigned int v;
+       unsigned long r;
+
+       if (td->o.perc_rand == 100)
+               return 1;
+
+       if (td->o.use_os_rand) {
+               r = os_random_long(&td->seq_rand_state);
+               v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
+       } else {
+               r = __rand(&td->__seq_rand_state);
+               v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
+       }
+
+       return v <= td->o.perc_rand;
+}
+
 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
                                enum fio_ddir ddir, uint64_t *b)
 {
@@ -285,9 +304,16 @@ static int get_next_block(struct thread_data *td, struct io_u *io_u,
        b = offset = -1ULL;
 
        if (rw_seq) {
-               if (td_random(td))
-                       ret = get_next_rand_block(td, f, ddir, &b);
-               else
+               if (td_random(td)) {
+                       if (should_do_random(td))
+                               ret = get_next_rand_block(td, f, ddir, &b);
+                       else {
+                               io_u->flags |= IO_U_F_BUSY_OK;
+                               ret = get_next_seq_offset(td, f, ddir, &offset);
+                               if (ret)
+                                       ret = get_next_rand_block(td, f, ddir, &b);
+                       }
+               } else
                        ret = get_next_seq_offset(td, f, ddir, &offset);
        } else {
                io_u->flags |= IO_U_F_BUSY_OK;
@@ -314,8 +340,7 @@ static int get_next_block(struct thread_data *td, struct io_u *io_u,
                else if (b != -1ULL)
                        io_u->offset = b * td->o.ba[ddir];
                else {
-                       log_err("fio: bug in offset generation: offset=%llu, b=%llu\n",
-                                                               offset, b);
+                       log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b);
                        ret = 1;
                }
        }
@@ -531,10 +556,10 @@ static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
         * io's that have been actually submitted to an async engine,
         * and cur_depth is meaningless for sync engines.
         */
-       if (td->io_u_in_flight) {
+       while (td->io_u_in_flight) {
                int fio_unused ret;
 
-               ret = io_u_queued_complete(td, td->io_u_in_flight, NULL);
+               ret = io_u_queued_complete(td, 1, NULL);
        }
 
        fio_gettime(&t, NULL);