Fix segfault with verify_async
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index 0bc83d2277633c8057c81c53a84e6685547af840..fc3ee498e2b497d9d410970c1427bc06006e97d2 100644 (file)
--- a/io_u.c
+++ b/io_u.c
@@ -158,7 +158,7 @@ static int get_next_free_block(struct thread_data *td, struct fio_file *f,
 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
                                enum fio_ddir ddir, unsigned long long *b)
 {
-       unsigned long long r, lastb;
+       unsigned long long rmax, r, lastb;
        int loops = 5;
 
        lastb = last_block(td, f, ddir);
@@ -168,14 +168,14 @@ static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
        if (f->failed_rands >= 200)
                goto ffz;
 
+       rmax = td->o.use_os_rand ? OS_RAND_MAX : FRAND_MAX;
        do {
-               if (td->o.use_os_rand) {
+               if (td->o.use_os_rand)
                        r = os_random_long(&td->random_state);
-                       *b = (lastb - 1) * (r / ((unsigned long long) OS_RAND_MAX + 1.0));
-               } else {
+               else
                        r = __rand(&td->__random_state);
-                       *b = (lastb - 1) * (r / ((unsigned long long) FRAND_MAX + 1.0));
-               }
+
+               *b = (lastb - 1) * (r / ((unsigned long long) rmax + 1.0));
 
                dprint(FD_RANDOM, "off rand %llu\n", r);
 
@@ -207,7 +207,7 @@ static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
        loops = 10;
        do {
                f->last_free_lookup = (f->num_maps - 1) *
-                                       (r / (OS_RAND_MAX + 1.0));
+                                       (r / ((unsigned long long) rmax + 1.0));
                if (!get_next_free_block(td, f, ddir, b))
                        goto ret;
 
@@ -597,13 +597,12 @@ void put_io_u(struct thread_data *td, struct io_u *io_u)
 {
        td_io_u_lock(td);
 
-       io_u->flags |= IO_U_F_FREE;
-       io_u->flags &= ~IO_U_F_FREE_DEF;
-
-       if (io_u->file)
+       if (io_u->file && !(io_u->flags & IO_U_F_FREE_DEF))
                put_file_log(td, io_u->file);
-
        io_u->file = NULL;
+       io_u->flags &= ~IO_U_F_FREE_DEF;
+       io_u->flags |= IO_U_F_FREE;
+
        if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
                td->cur_depth--;
        flist_del_init(&io_u->list);
@@ -1293,6 +1292,8 @@ static void account_io_completion(struct thread_data *td, struct io_u *io_u,
 
        if (!td->o.disable_bw)
                add_bw_sample(td, idx, bytes, &icd->time);
+
+       add_iops_sample(td, idx, &icd->time);
 }
 
 static void io_completed(struct thread_data *td, struct io_u *io_u,
@@ -1332,6 +1333,7 @@ static void io_completed(struct thread_data *td, struct io_u *io_u,
                int ret;
 
                td->io_blocks[idx]++;
+               td->this_io_blocks[idx]++;
                td->io_bytes[idx] += bytes;
                td->this_io_bytes[idx] += bytes;
 
@@ -1347,7 +1349,7 @@ static void io_completed(struct thread_data *td, struct io_u *io_u,
                        }
                }
 
-               if (ramp_time_over(td)) {
+               if (ramp_time_over(td) && td->runstate == TD_RUNNING) {
                        account_io_completion(td, io_u, icd, idx, bytes);
 
                        if (__should_check_rate(td, idx)) {