Separate io_u from ioengine [2/3] - move io_u functions
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index b6d530f2a23a542d28673587ababf8a0e091f3e9..363bfe10283f5043aa8565cd0d5c1236adf5d532 100644 (file)
--- a/io_u.c
+++ b/io_u.c
@@ -62,6 +62,7 @@ static uint64_t last_block(struct thread_data *td, struct fio_file *f,
 
        /*
         * Hmm, should we make sure that ->io_size <= ->real_file_size?
+        * -> not for now since there is code assuming it could go either.
         */
        max_size = f->io_size;
        if (max_size > f->real_file_size)
@@ -531,8 +532,8 @@ static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
        int ddir = io_u->ddir;
        unsigned int buflen = 0;
        unsigned int minbs, maxbs;
-       uint64_t frand_max;
-       unsigned long r;
+       uint64_t frand_max, r;
+       bool power_2;
 
        assert(ddir_rw(ddir));
 
@@ -561,7 +562,7 @@ static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
                        if (buflen < minbs)
                                buflen = minbs;
                } else {
-                       long perc = 0;
+                       long long perc = 0;
                        unsigned int i;
 
                        for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
@@ -569,19 +570,19 @@ static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
 
                                buflen = bsp->bs;
                                perc += bsp->perc;
-                               if ((r * 100UL <= frand_max * perc) &&
+                               if (!perc)
+                                       break;
+                               if ((r / perc <= frand_max / 100ULL) &&
                                    io_u_fits(td, io_u, buflen))
                                        break;
                        }
                }
 
-               if (td->o.verify != VERIFY_NONE)
-                       buflen = (buflen + td->o.verify_interval - 1) &
-                               ~(td->o.verify_interval - 1);
-
-               if (!td->o.bs_unaligned && is_power_of_2(minbs))
+               power_2 = is_power_of_2(minbs);
+               if (!td->o.bs_unaligned && power_2)
                        buflen &= ~(minbs - 1);
-
+               else if (!td->o.bs_unaligned && !power_2) 
+                       buflen -= buflen % minbs; 
        } while (!io_u_fits(td, io_u, buflen));
 
        return buflen;
@@ -645,20 +646,24 @@ int io_u_quiesce(struct thread_data *td)
        }
 
        while (td->io_u_in_flight) {
-               int fio_unused ret;
+               int ret;
 
                ret = io_u_queued_complete(td, 1);
                if (ret > 0)
                        completed += ret;
        }
 
+       if (td->flags & TD_F_REGROW_LOGS)
+               regrow_logs(td);
+
        return completed;
 }
 
 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
 {
        enum fio_ddir odir = ddir ^ 1;
-       long usec, now;
+       long usec;
+       uint64_t now;
 
        assert(ddir_rw(ddir));
        now = utime_since_now(&td->start);
@@ -712,28 +717,22 @@ static enum fio_ddir get_rw_ddir(struct thread_data *td)
        enum fio_ddir ddir;
 
        /*
-        * see if it's time to fsync
-        */
-       if (td->o.fsync_blocks &&
-          !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
-            td->io_issues[DDIR_WRITE] && should_fsync(td))
-               return DDIR_SYNC;
-
-       /*
-        * see if it's time to fdatasync
-        */
-       if (td->o.fdatasync_blocks &&
-          !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
-            td->io_issues[DDIR_WRITE] && should_fsync(td))
-               return DDIR_DATASYNC;
-
-       /*
-        * see if it's time to sync_file_range
+        * See if it's time to fsync/fdatasync/sync_file_range first,
+        * and if not then move on to check regular I/Os.
         */
-       if (td->sync_file_range_nr &&
-          !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
-            td->io_issues[DDIR_WRITE] && should_fsync(td))
-               return DDIR_SYNC_FILE_RANGE;
+       if (should_fsync(td)) {
+               if (td->o.fsync_blocks && td->io_issues[DDIR_WRITE] &&
+                   !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks))
+                       return DDIR_SYNC;
+
+               if (td->o.fdatasync_blocks && td->io_issues[DDIR_WRITE] &&
+                   !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks))
+                       return DDIR_DATASYNC;
+
+               if (td->sync_file_range_nr && td->io_issues[DDIR_WRITE] &&
+                   !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr))
+                       return DDIR_SYNC_FILE_RANGE;
+       }
 
        if (td_rw(td)) {
                /*
@@ -757,8 +756,10 @@ static enum fio_ddir get_rw_ddir(struct thread_data *td)
                ddir = DDIR_READ;
        else if (td_write(td))
                ddir = DDIR_WRITE;
-       else
+       else if (td_trim(td))
                ddir = DDIR_TRIM;
+       else
+               ddir = DDIR_INVAL;
 
        td->rwmix_ddir = rate_ddir(td, ddir);
        return td->rwmix_ddir;
@@ -1730,6 +1731,9 @@ static void account_io_completion(struct thread_data *td, struct io_u *io_u,
        if (td->parent)
                td = td->parent;
 
+       if (!td->o.stats)
+               return;
+
        if (no_reduce)
                lusec = utime_since(&io_u->issue_time, &icd->time);
 
@@ -1901,7 +1905,7 @@ static void init_icd(struct thread_data *td, struct io_completion_data *icd,
        icd->nr = nr;
 
        icd->error = 0;
-       for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
+       for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
                icd->bytes_done[ddir] = 0;
 }
 
@@ -1940,7 +1944,7 @@ int io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
                return -1;
        }
 
-       for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
+       for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
                td->bytes_done[ddir] += icd.bytes_done[ddir];
 
        return 0;
@@ -1956,7 +1960,7 @@ int io_u_queued_complete(struct thread_data *td, int min_evts)
        int ret, ddir;
        struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
 
-       dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
+       dprint(FD_IO, "io_u_queued_complete: min=%d\n", min_evts);
 
        if (!min_evts)
                tvp = &ts;
@@ -1979,7 +1983,7 @@ int io_u_queued_complete(struct thread_data *td, int min_evts)
                return -1;
        }
 
-       for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
+       for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
                td->bytes_done[ddir] += icd.bytes_done[ddir];
 
        return ret;
@@ -1990,7 +1994,7 @@ int io_u_queued_complete(struct thread_data *td, int min_evts)
  */
 void io_u_queued(struct thread_data *td, struct io_u *io_u)
 {
-       if (!td->o.disable_slat) {
+       if (!td->o.disable_slat && ramp_time_over(td) && td->o.stats) {
                unsigned long slat_time;
 
                slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
@@ -2083,3 +2087,61 @@ void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
        io_u->buf_filled_len = 0;
        fill_io_buffer(td, io_u->buf, min_write, max_bs);
 }
+
+static int do_sync_file_range(const struct thread_data *td,
+                             struct fio_file *f)
+{
+       off64_t offset, nbytes;
+
+       offset = f->first_write;
+       nbytes = f->last_write - f->first_write;
+
+       if (!nbytes)
+               return 0;
+
+       return sync_file_range(f->fd, offset, nbytes, td->o.sync_file_range);
+}
+
+int do_io_u_sync(const struct thread_data *td, struct io_u *io_u)
+{
+       int ret;
+
+       if (io_u->ddir == DDIR_SYNC) {
+               ret = fsync(io_u->file->fd);
+       } else if (io_u->ddir == DDIR_DATASYNC) {
+#ifdef CONFIG_FDATASYNC
+               ret = fdatasync(io_u->file->fd);
+#else
+               ret = io_u->xfer_buflen;
+               io_u->error = EINVAL;
+#endif
+       } else if (io_u->ddir == DDIR_SYNC_FILE_RANGE)
+               ret = do_sync_file_range(td, io_u->file);
+       else {
+               ret = io_u->xfer_buflen;
+               io_u->error = EINVAL;
+       }
+
+       if (ret < 0)
+               io_u->error = errno;
+
+       return ret;
+}
+
+int do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
+{
+#ifndef FIO_HAVE_TRIM
+       io_u->error = EINVAL;
+       return 0;
+#else
+       struct fio_file *f = io_u->file;
+       int ret;
+
+       ret = os_trim(f->fd, io_u->offset, io_u->xfer_buflen);
+       if (!ret)
+               return io_u->xfer_buflen;
+
+       io_u->error = ret;
+       return 0;
+#endif
+}