Merge branch 'master' of ssh://router/data/git/fio
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index 41b5cdbb04894192cb06e2d86a49bdf28d48be9d..96683954a0378f5ad5eb6163ea6e424e401d73ca 100644 (file)
--- a/io_u.c
+++ b/io_u.c
@@ -231,11 +231,6 @@ static int get_next_offset(struct thread_data *td, struct io_u *io_u)
        return 0;
 }
 
-static inline int is_power_of_2(unsigned int val)
-{
-       return (val != 0 && ((val & (val - 1)) == 0));
-}
-
 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
 {
        const int ddir = io_u->ddir;
@@ -406,7 +401,7 @@ static enum fio_ddir get_rw_ddir(struct thread_data *td)
        return td->rwmix_ddir;
 }
 
-static void put_file_log(struct thread_data *td, struct fio_file *f)
+void put_file_log(struct thread_data *td, struct fio_file *f)
 {
        int ret = put_file(td, f);
 
@@ -416,16 +411,21 @@ static void put_file_log(struct thread_data *td, struct fio_file *f)
 
 void put_io_u(struct thread_data *td, struct io_u *io_u)
 {
+       td_io_u_lock(td);
+
        assert((io_u->flags & IO_U_F_FREE) == 0);
        io_u->flags |= IO_U_F_FREE;
+       io_u->flags &= ~IO_U_F_FREE_DEF;
 
        if (io_u->file)
                put_file_log(td, io_u->file);
 
        io_u->file = NULL;
-       flist_del(&io_u->list);
+       flist_del_init(&io_u->list);
        flist_add(&io_u->list, &td->io_u_freelist);
        td->cur_depth--;
+       td_io_u_unlock(td);
+       td_io_u_free_notify(td);
 }
 
 void clear_io_u(struct thread_data *td, struct io_u *io_u)
@@ -440,6 +440,8 @@ void requeue_io_u(struct thread_data *td, struct io_u **io_u)
 
        dprint(FD_IO, "requeue %p\n", __io_u);
 
+       td_io_u_lock(td);
+
        __io_u->flags |= IO_U_F_FREE;
        if ((__io_u->flags & IO_U_F_FLIGHT) && !ddir_sync(__io_u->ddir))
                td->io_issues[__io_u->ddir]--;
@@ -449,6 +451,7 @@ void requeue_io_u(struct thread_data *td, struct io_u **io_u)
        flist_del(&__io_u->list);
        flist_add_tail(&__io_u->list, &td->io_u_requeues);
        td->cur_depth--;
+       td_io_u_unlock(td);
        *io_u = NULL;
 }
 
@@ -831,6 +834,9 @@ struct io_u *__get_io_u(struct thread_data *td)
 {
        struct io_u *io_u = NULL;
 
+       td_io_u_lock(td);
+
+again:
        if (!flist_empty(&td->io_u_requeues))
                io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
        else if (!queue_full(td)) {
@@ -842,9 +848,18 @@ struct io_u *__get_io_u(struct thread_data *td)
                io_u->end_io = NULL;
        }
 
+       /*
+        * We ran out, wait for async verify threads to finish and return one
+        */
+       if (!io_u && td->o.verify_async) {
+               pthread_cond_wait(&td->free_cond, &td->io_u_lock);
+               goto again;
+       }
+
        if (io_u) {
                assert(io_u->flags & IO_U_F_FREE);
                io_u->flags &= ~IO_U_F_FREE;
+               io_u->flags &= ~IO_U_F_FREE_DEF;
 
                io_u->error = 0;
                flist_del(&io_u->list);
@@ -852,6 +867,7 @@ struct io_u *__get_io_u(struct thread_data *td)
                td->cur_depth++;
        }
 
+       td_io_u_unlock(td);
        return io_u;
 }
 
@@ -983,7 +999,7 @@ static void io_completed(struct thread_data *td, struct io_u *io_u,
                                                        &icd->time);
 
                        if (!td->o.disable_clat) {
-                               add_clat_sample(td, idx, usec, bytes);
+                               add_clat_sample(td, idx, lusec, bytes);
                                io_u_mark_latency(td, lusec);
                        }
                        if (!td->o.disable_bw)
@@ -1047,7 +1063,9 @@ static void ios_completed(struct thread_data *td,
                io_u = td->io_ops->event(td, i);
 
                io_completed(td, io_u, icd);
-               put_io_u(td, io_u);
+
+               if (!(io_u->flags & IO_U_F_FREE_DEF))
+                       put_io_u(td, io_u);
        }
 }
 
@@ -1061,7 +1079,9 @@ int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
 
        init_icd(td, &icd, 1);
        io_completed(td, io_u, &icd);
-       put_io_u(td, io_u);
+
+       if (!(io_u->flags & IO_U_F_FREE_DEF))
+               put_io_u(td, io_u);
 
        if (icd.error) {
                td_verror(td, icd.error, "io_u_sync_complete");