Test fsync/fdatasync/sync_file_range for the next i/o only if should_fsync(td)
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index 34acc5698e37cc4b7f1e8572ffa55ecd49d342bb..c6d814bf6d8765904f74b9ac01c0dfeaf48cb77a 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)
@@ -362,8 +363,12 @@ static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
        if (f->last_pos[ddir] < f->real_file_size) {
                uint64_t pos;
 
-               if (f->last_pos[ddir] == f->file_offset && o->ddir_seq_add < 0)
-                       f->last_pos[ddir] = f->real_file_size;
+               if (f->last_pos[ddir] == f->file_offset && o->ddir_seq_add < 0) {
+                       if (f->real_file_size > f->io_size)
+                               f->last_pos[ddir] = f->io_size;
+                       else
+                               f->last_pos[ddir] = f->real_file_size;
+               }
 
                pos = f->last_pos[ddir] - f->file_offset;
                if (pos && o->ddir_seq_add) {
@@ -378,8 +383,14 @@ static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
                        if (pos >= f->real_file_size) {
                                if (o->ddir_seq_add > 0)
                                        pos = f->file_offset;
-                               else
-                                       pos = f->real_file_size + o->ddir_seq_add;
+                               else {
+                                       if (f->real_file_size > f->io_size)
+                                               pos = f->io_size;
+                                       else
+                                               pos = f->real_file_size;
+
+                                       pos += o->ddir_seq_add;
+                               }
                        }
                }
 
@@ -521,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));
 
@@ -551,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++) {
@@ -559,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;
@@ -635,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);
@@ -702,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
+        * 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->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
-        */
-       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)) {
                /*
@@ -747,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;
@@ -768,7 +779,7 @@ static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
 
        io_u->ddir = io_u->acct_ddir = ddir;
 
-       if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
+       if (io_u->ddir == DDIR_WRITE && td_ioengine_flagged(td, FIO_BARRIER) &&
            td->o.barrier_blocks &&
           !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
             td->io_issues[DDIR_WRITE])
@@ -843,7 +854,7 @@ static int fill_io_u(struct thread_data *td, struct io_u *io_u)
 {
        unsigned int is_random;
 
-       if (td->io_ops->flags & FIO_NOIO)
+       if (td_ioengine_flagged(td, FIO_NOIO))
                goto out;
 
        set_rw_ddir(td, io_u);
@@ -1500,7 +1511,7 @@ static bool check_get_trim(struct thread_data *td, struct io_u *io_u)
                        get_trim = 1;
                }
 
-               if (get_trim && !get_next_trim(td, io_u))
+               if (get_trim && get_next_trim(td, io_u))
                        return true;
        }
 
@@ -1622,7 +1633,7 @@ struct io_u *get_io_u(struct thread_data *td)
        assert(fio_file_open(f));
 
        if (ddir_rw(io_u->ddir)) {
-               if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
+               if (!io_u->buflen && !td_ioengine_flagged(td, FIO_NOIO)) {
                        dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
                        goto err_put;
                }
@@ -1720,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);
 
@@ -1891,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;
 }
 
@@ -1930,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;
@@ -1946,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;
@@ -1969,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;
@@ -1980,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);