verify: fix a bug with verify_async
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index 61038a4d07e899fa3af3c952014f504bfd96fdbd..ba192a32a985d88e87378d196404cddd10e93936 100644 (file)
--- a/io_u.c
+++ b/io_u.c
@@ -104,7 +104,7 @@ static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
 
                dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
 
-               *b = (lastb - 1) * (r / ((uint64_t) rmax + 1.0));
+               *b = lastb * (r / ((uint64_t) rmax + 1.0));
        } else {
                uint64_t off = 0;
 
@@ -223,7 +223,7 @@ static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
        if (!flist_empty(&td->next_rand_list)) {
                struct rand_off *r;
 fetch:
-               r = flist_entry(td->next_rand_list.next, struct rand_off, list);
+               r = flist_first_entry(&td->next_rand_list, struct rand_off, list);
                flist_del(&r->list);
                *b = r->off;
                free(r);
@@ -273,7 +273,7 @@ static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
 {
        assert(ddir_rw(ddir));
 
-       if (f->last_pos >= f->io_size + get_start_offset(td) && td->o.time_based)
+       if (f->last_pos >= f->io_size + get_start_offset(td, f) && td->o.time_based)
                f->last_pos = f->last_pos - f->io_size;
 
        if (f->last_pos < f->real_file_size) {
@@ -415,7 +415,7 @@ static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
 {
        struct fio_file *f = io_u->file;
 
-       return io_u->offset + buflen <= f->io_size + get_start_offset(td);
+       return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
 }
 
 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
@@ -426,12 +426,10 @@ static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
        unsigned int minbs, maxbs;
        unsigned long r, rand_max;
 
-       assert(ddir_rw(io_u->ddir));
+       assert(ddir_rw(ddir));
 
        if (td->o.bs_is_seq_rand)
                ddir = is_random ? DDIR_WRITE: DDIR_READ;
-       else
-               ddir = io_u->ddir;
 
        minbs = td->o.min_bs[ddir];
        maxbs = td->o.max_bs[ddir];
@@ -680,7 +678,7 @@ static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
 
 void put_file_log(struct thread_data *td, struct fio_file *f)
 {
-       int ret = put_file(td, f);
+       unsigned int ret = put_file(td, f);
 
        if (ret)
                td_verror(td, ret, "file close");
@@ -690,10 +688,10 @@ void put_io_u(struct thread_data *td, struct io_u *io_u)
 {
        td_io_u_lock(td);
 
-       if (io_u->file && !(io_u->flags & IO_U_F_FREE_DEF))
+       if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT))
                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)
@@ -1093,13 +1091,16 @@ static struct fio_file *__get_next_file(struct thread_data *td)
        td->file_service_file = f;
        td->file_service_left = td->file_service_nr - 1;
 out:
-       dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
+       if (f)
+               dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
+       else
+               dprint(FD_FILE, "get_next_file: NULL\n");
        return f;
 }
 
 static struct fio_file *get_next_file(struct thread_data *td)
 {
-       if (!(td->flags & TD_F_PROFILE_OPS)) {
+       if (td->flags & TD_F_PROFILE_OPS) {
                struct prof_io_ops *ops = &td->prof_io_ops;
 
                if (ops->get_next_file)
@@ -1162,6 +1163,10 @@ static int __lat_target_failed(struct thread_data *td)
                return 1;
 
        td->latency_qd_high = td->latency_qd;
+
+       if (td->latency_qd == td->latency_qd_low)
+               td->latency_qd_low--;
+
        td->latency_qd = (td->latency_qd + td->latency_qd_low) / 2;
 
        dprint(FD_RATE, "Ramped down: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
@@ -1186,6 +1191,8 @@ static int lat_target_failed(struct thread_data *td)
 
 void lat_target_init(struct thread_data *td)
 {
+       td->latency_end_run = 0;
+
        if (td->o.latency_target) {
                dprint(FD_RATE, "Latency target=%llu\n", td->o.latency_target);
                fio_gettime(&td->latency_ts, NULL);
@@ -1197,9 +1204,16 @@ void lat_target_init(struct thread_data *td)
                td->latency_qd = td->o.iodepth;
 }
 
+void lat_target_reset(struct thread_data *td)
+{
+       if (!td->latency_end_run)
+               lat_target_init(td);
+}
+
 static void lat_target_success(struct thread_data *td)
 {
        const unsigned int qd = td->latency_qd;
+       struct thread_options *o = &td->o;
 
        td->latency_qd_low = td->latency_qd;
 
@@ -1208,20 +1222,32 @@ static void lat_target_success(struct thread_data *td)
         * of bisecting from highest possible queue depth. If we have set
         * a limit other than td->o.iodepth, bisect between that.
         */
-       if (td->latency_qd_high != td->o.iodepth)
+       if (td->latency_qd_high != o->iodepth)
                td->latency_qd = (td->latency_qd + td->latency_qd_high) / 2;
        else
                td->latency_qd *= 2;
 
-       if (td->latency_qd > td->o.iodepth)
-               td->latency_qd = td->o.iodepth;
+       if (td->latency_qd > o->iodepth)
+               td->latency_qd = o->iodepth;
 
        dprint(FD_RATE, "Ramped up: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
+
        /*
-        * Same as last one, we are done
+        * Same as last one, we are done. Let it run a latency cycle, so
+        * we get only the results from the targeted depth.
         */
-       if (td->latency_qd == qd)
-               td->done = 1;
+       if (td->latency_qd == qd) {
+               if (td->latency_end_run) {
+                       dprint(FD_RATE, "We are done\n");
+                       td->done = 1;
+               } else {
+                       dprint(FD_RATE, "Quiesce and final run\n");
+                       io_u_quiesce(td);
+                       td->latency_end_run = 1;
+                       reset_all_stats(td);
+                       reset_io_stats(td);
+               }
+       }
 
        lat_new_cycle(td);
 }
@@ -1269,7 +1295,7 @@ int queue_full(struct thread_data *td)
 
 struct io_u *__get_io_u(struct thread_data *td)
 {
-       struct io_u *io_u;
+       struct io_u *io_u = NULL;
 
        td_io_u_lock(td);
 
@@ -1279,17 +1305,17 @@ again:
        else if (!queue_full(td)) {
                io_u = io_u_qpop(&td->io_u_freelist);
 
+               io_u->file = NULL;
                io_u->buflen = 0;
                io_u->resid = 0;
-               io_u->file = NULL;
                io_u->end_io = NULL;
        }
 
        if (io_u) {
                assert(io_u->flags & IO_U_F_FREE);
-               io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
-               io_u->flags &= ~(IO_U_F_TRIMMED | IO_U_F_BARRIER);
-               io_u->flags &= ~IO_U_F_VER_LIST;
+               io_u->flags &= ~(IO_U_F_FREE | IO_U_F_NO_FILE_PUT |
+                                IO_U_F_TRIMMED | IO_U_F_BARRIER |
+                                IO_U_F_VER_LIST);
 
                io_u->error = 0;
                io_u->acct_ddir = -1;
@@ -1462,7 +1488,8 @@ struct io_u *get_io_u(struct thread_data *td)
                        if (td->flags & TD_F_REFILL_BUFFERS) {
                                io_u_fill_buffer(td, io_u,
                                        io_u->xfer_buflen, io_u->xfer_buflen);
-                       } else if (td->flags & TD_F_SCRAMBLE_BUFFERS)
+                       } else if ((td->flags & TD_F_SCRAMBLE_BUFFERS) &&
+                                  !(td->flags & TD_F_COMPRESS))
                                do_scramble = 1;
                        if (td->flags & TD_F_VER_NONE) {
                                populate_verify_io_u(td, io_u);
@@ -1523,7 +1550,7 @@ void io_u_log_error(struct thread_data *td, struct io_u *io_u)
 
 static inline int gtod_reduce(struct thread_data *td)
 {
-       return td->o.disable_clat && td->o.disable_lat && !td->o.disable_slat
+       return td->o.disable_clat && td->o.disable_lat && td->o.disable_slat
                && td->o.disable_bw;
 }
 
@@ -1540,7 +1567,7 @@ static void account_io_completion(struct thread_data *td, struct io_u *io_u,
                unsigned long tusec;
 
                tusec = utime_since(&io_u->start_time, &icd->time);
-               add_lat_sample(td, idx, tusec, bytes);
+               add_lat_sample(td, idx, tusec, bytes, io_u->offset);
 
                if (td->flags & TD_F_PROFILE_OPS) {
                        struct prof_io_ops *ops = &td->prof_io_ops;
@@ -1558,7 +1585,7 @@ static void account_io_completion(struct thread_data *td, struct io_u *io_u,
        }
 
        if (!td->o.disable_clat) {
-               add_clat_sample(td, idx, lusec, bytes);
+               add_clat_sample(td, idx, lusec, bytes, io_u->offset);
                io_u_mark_latency(td, lusec);
        }
 
@@ -1567,9 +1594,6 @@ static void account_io_completion(struct thread_data *td, struct io_u *io_u,
 
        if (!gtod_reduce(td))
                add_iops_sample(td, idx, bytes, &icd->time);
-
-       if (td->o.number_ios && !--td->o.number_ios)
-               td->done = 1;
 }
 
 static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
@@ -1583,10 +1607,12 @@ static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
        return remainder * 1000000 / bps + secs * 1000000;
 }
 
-static void io_completed(struct thread_data *td, struct io_u *io_u,
+static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
                         struct io_completion_data *icd)
 {
-       struct fio_file *f;
+       struct io_u *io_u = *io_u_ptr;
+       enum fio_ddir ddir = io_u->ddir;
+       struct fio_file *f = io_u->file;
 
        dprint_io_u(io_u, "io complete");
 
@@ -1598,15 +1624,21 @@ static void io_completed(struct thread_data *td, struct io_u *io_u,
         * Mark IO ok to verify
         */
        if (io_u->ipo) {
-               io_u->ipo->flags &= ~IP_F_IN_FLIGHT;
-               write_barrier();
+               /*
+                * Remove errored entry from the verification list
+                */
+               if (io_u->error)
+                       unlog_io_piece(td, io_u);
+               else {
+                       io_u->ipo->flags &= ~IP_F_IN_FLIGHT;
+                       write_barrier();
+               }
        }
 
        td_io_u_unlock(td);
 
-       if (ddir_sync(io_u->ddir)) {
+       if (ddir_sync(ddir)) {
                td->last_was_sync = 1;
-               f = io_u->file;
                if (f) {
                        f->first_write = -1ULL;
                        f->last_write = -1ULL;
@@ -1615,52 +1647,51 @@ static void io_completed(struct thread_data *td, struct io_u *io_u,
        }
 
        td->last_was_sync = 0;
-       td->last_ddir = io_u->ddir;
+       td->last_ddir = ddir;
 
-       if (!io_u->error && ddir_rw(io_u->ddir)) {
+       if (!io_u->error && ddir_rw(ddir)) {
                unsigned int bytes = io_u->buflen - io_u->resid;
-               const enum fio_ddir idx = io_u->ddir;
-               const enum fio_ddir odx = io_u->ddir ^ 1;
+               const enum fio_ddir oddir = ddir ^ 1;
                int ret;
 
-               td->io_blocks[idx]++;
-               td->this_io_blocks[idx]++;
-               td->io_bytes[idx] += bytes;
+               td->io_blocks[ddir]++;
+               td->this_io_blocks[ddir]++;
+               td->io_bytes[ddir] += bytes;
 
                if (!(io_u->flags & IO_U_F_VER_LIST))
-                       td->this_io_bytes[idx] += bytes;
-
-               if (idx == DDIR_WRITE) {
-                       f = io_u->file;
-                       if (f) {
-                               if (f->first_write == -1ULL ||
-                                   io_u->offset < f->first_write)
-                                       f->first_write = io_u->offset;
-                               if (f->last_write == -1ULL ||
-                                   ((io_u->offset + bytes) > f->last_write))
-                                       f->last_write = io_u->offset + bytes;
-                       }
+                       td->this_io_bytes[ddir] += bytes;
+
+               if (ddir == DDIR_WRITE && f) {
+                       if (f->first_write == -1ULL ||
+                           io_u->offset < f->first_write)
+                               f->first_write = io_u->offset;
+                       if (f->last_write == -1ULL ||
+                           ((io_u->offset + bytes) > f->last_write))
+                               f->last_write = io_u->offset + bytes;
                }
 
                if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
                                           td->runstate == TD_VERIFYING)) {
-                       account_io_completion(td, io_u, icd, idx, bytes);
+                       account_io_completion(td, io_u, icd, ddir, bytes);
 
-                       if (__should_check_rate(td, idx)) {
-                               td->rate_pending_usleep[idx] =
-                                       (usec_for_io(td, idx) -
+                       if (__should_check_rate(td, ddir)) {
+                               td->rate_pending_usleep[ddir] =
+                                       (usec_for_io(td, ddir) -
                                         utime_since_now(&td->start));
                        }
-                       if (idx != DDIR_TRIM && __should_check_rate(td, odx))
-                               td->rate_pending_usleep[odx] =
-                                       (usec_for_io(td, odx) -
+                       if (ddir != DDIR_TRIM &&
+                           __should_check_rate(td, oddir)) {
+                               td->rate_pending_usleep[oddir] =
+                                       (usec_for_io(td, oddir) -
                                         utime_since_now(&td->start));
+                       }
                }
 
-               icd->bytes_done[idx] += bytes;
+               icd->bytes_done[ddir] += bytes;
 
                if (io_u->end_io) {
-                       ret = io_u->end_io(td, io_u);
+                       ret = io_u->end_io(td, io_u_ptr);
+                       io_u = *io_u_ptr;
                        if (ret && !icd->error)
                                icd->error = ret;
                }
@@ -1669,9 +1700,11 @@ static void io_completed(struct thread_data *td, struct io_u *io_u,
                io_u_log_error(td, io_u);
        }
        if (icd->error) {
-               enum error_type_bit eb = td_error_type(io_u->ddir, icd->error);
+               enum error_type_bit eb = td_error_type(ddir, icd->error);
+
                if (!td_non_fatal_error(td, eb, icd->error))
                        return;
+
                /*
                 * If there is a non_fatal error, then add to the error count
                 * and clear all the errors.
@@ -1679,7 +1712,8 @@ static void io_completed(struct thread_data *td, struct io_u *io_u,
                update_error_count(td, icd->error);
                td_clear_error(td);
                icd->error = 0;
-               io_u->error = 0;
+               if (io_u)
+                       io_u->error = 0;
        }
 }
 
@@ -1707,9 +1741,9 @@ static void ios_completed(struct thread_data *td,
        for (i = 0; i < icd->nr; i++) {
                io_u = td->io_ops->event(td, i);
 
-               io_completed(td, io_u, icd);
+               io_completed(td, &io_u, icd);
 
-               if (!(io_u->flags & IO_U_F_FREE_DEF))
+               if (io_u)
                        put_io_u(td, io_u);
        }
 }
@@ -1723,9 +1757,9 @@ int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
        struct io_completion_data icd;
 
        init_icd(td, &icd, 1);
-       io_completed(td, io_u, &icd);
+       io_completed(td, &io_u, &icd);
 
-       if (!(io_u->flags & IO_U_F_FREE_DEF))
+       if (io_u)
                put_io_u(td, io_u);
 
        if (icd.error) {
@@ -1792,7 +1826,8 @@ void io_u_queued(struct thread_data *td, struct io_u *io_u)
                unsigned long slat_time;
 
                slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
-               add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen);
+               add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen,
+                               io_u->offset);
        }
 }