Trim/discard fixes
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index 53bf492317fedfbe36052c453d6a46021c27b463..a630817f6f41a3b036530affb0a06dce8908b5f0 100644 (file)
--- a/io_u.c
+++ b/io_u.c
@@ -8,6 +8,7 @@
 #include "fio.h"
 #include "hash.h"
 #include "verify.h"
+#include "trim.h"
 #include "lib/rand.h"
 
 struct io_completion_data {
@@ -103,6 +104,8 @@ static unsigned long long last_block(struct thread_data *td, struct fio_file *f,
        unsigned long long max_blocks;
        unsigned long long max_size;
 
+       assert(ddir_rw(ddir));
+
        /*
         * Hmm, should we make sure that ->io_size <= ->real_file_size?
         */
@@ -211,6 +214,8 @@ static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
 static int get_next_seq_block(struct thread_data *td, struct fio_file *f,
                              enum fio_ddir ddir, unsigned long long *b)
 {
+       assert(ddir_rw(ddir));
+
        if (f->last_pos < f->real_file_size) {
                *b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
                return 0;
@@ -225,6 +230,8 @@ static int get_next_block(struct thread_data *td, struct io_u *io_u,
        struct fio_file *f = io_u->file;
        int ret;
 
+       assert(ddir_rw(ddir));
+
        if (rw_seq) {
                if (td_random(td))
                        ret = get_next_rand_block(td, f, ddir, b);
@@ -264,6 +271,8 @@ static int __get_next_offset(struct thread_data *td, struct io_u *io_u)
        enum fio_ddir ddir = io_u->ddir;
        int rw_seq_hit = 0;
 
+       assert(ddir_rw(ddir));
+
        if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
                rw_seq_hit = 1;
                td->ddir_seq_nr = td->o.ddir_seq_nr;
@@ -308,6 +317,8 @@ static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u)
        unsigned int minbs, maxbs;
        long r;
 
+       assert(ddir_rw(ddir));
+
        minbs = td->o.min_bs[ddir];
        maxbs = td->o.max_bs[ddir];
 
@@ -388,6 +399,8 @@ static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
        struct timeval t;
        long usec;
 
+       assert(ddir_rw(ddir));
+
        if (td->rate_pending_usleep[ddir] <= 0)
                return ddir;
 
@@ -531,7 +544,7 @@ void requeue_io_u(struct thread_data *td, struct io_u **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))
+       if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(__io_u->ddir))
                td->io_issues[__io_u->ddir]--;
 
        __io_u->flags &= ~IO_U_F_FLIGHT;
@@ -551,9 +564,9 @@ static int fill_io_u(struct thread_data *td, struct io_u *io_u)
        io_u->ddir = get_rw_ddir(td);
 
        /*
-        * fsync() or fdatasync(), we are done
+        * fsync() or fdatasync() or trim etc, we are done
         */
-       if (ddir_sync(io_u->ddir))
+       if (!ddir_rw(io_u->ddir))
                goto out;
 
        /*
@@ -951,6 +964,7 @@ again:
        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->error = 0;
                flist_del(&io_u->list);
@@ -970,21 +984,31 @@ again:
        return io_u;
 }
 
-/*
- * Return an io_u to be processed. Gets a buflen and offset, sets direction,
- * etc. The returned io_u is fully ready to be prepped and submitted.
- */
-struct io_u *get_io_u(struct thread_data *td)
+static int check_get_trim(struct thread_data *td, struct io_u *io_u)
 {
-       struct fio_file *f;
-       struct io_u *io_u;
+       if (td->o.trim_backlog && td->trim_entries) {
+               int get_trim = 0;
 
-       io_u = __get_io_u(td);
-       if (!io_u) {
-               dprint(FD_IO, "__get_io_u failed\n");
-               return NULL;
+               if (td->trim_batch) {
+                       td->trim_batch--;
+                       get_trim = 1;
+               } else if (!(td->io_hist_len % td->o.trim_backlog) &&
+                        td->last_ddir != DDIR_READ) {
+                       td->trim_batch = td->o.trim_batch;
+                       if (!td->trim_batch)
+                               td->trim_batch = td->o.trim_backlog;
+                       get_trim = 1;
+               }
+
+               if (get_trim && !get_next_trim(td, io_u))
+                       return 1;
        }
 
+       return 0;
+}
+
+static int check_get_verify(struct thread_data *td, struct io_u *io_u)
+{
        if (td->o.verify_backlog && td->io_hist_len) {
                int get_verify = 0;
 
@@ -1000,9 +1024,32 @@ struct io_u *get_io_u(struct thread_data *td)
                }
 
                if (get_verify && !get_next_verify(td, io_u))
-                       goto out;
+                       return 1;
+       }
+
+       return 0;
+}
+
+/*
+ * Return an io_u to be processed. Gets a buflen and offset, sets direction,
+ * etc. The returned io_u is fully ready to be prepped and submitted.
+ */
+struct io_u *get_io_u(struct thread_data *td)
+{
+       struct fio_file *f;
+       struct io_u *io_u;
+
+       io_u = __get_io_u(td);
+       if (!io_u) {
+               dprint(FD_IO, "__get_io_u failed\n");
+               return NULL;
        }
 
+       if (check_get_verify(td, io_u))
+               goto out;
+       if (check_get_trim(td, io_u))
+               goto out;
+
        /*
         * from a requeue, io_u already setup
         */
@@ -1023,7 +1070,7 @@ struct io_u *get_io_u(struct thread_data *td)
        f = io_u->file;
        assert(fio_file_open(f));
 
-       if (!ddir_sync(io_u->ddir)) {
+       if (ddir_rw(io_u->ddir)) {
                if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
                        dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
                        goto err_put;
@@ -1052,6 +1099,7 @@ struct io_u *get_io_u(struct thread_data *td)
        io_u->xfer_buflen = io_u->buflen;
 
 out:
+       assert(io_u->file);
        if (!td_io_prep(td, io_u)) {
                if (!td->o.disable_slat)
                        fio_gettime(&io_u->start_time, NULL);
@@ -1114,7 +1162,7 @@ static void io_completed(struct thread_data *td, struct io_u *io_u,
        td->last_was_sync = 0;
        td->last_ddir = io_u->ddir;
 
-       if (!io_u->error) {
+       if (!io_u->error && ddir_rw(io_u->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;
@@ -1180,7 +1228,7 @@ static void io_completed(struct thread_data *td, struct io_u *io_u,
                        if (ret && !icd->error)
                                icd->error = ret;
                }
-       } else {
+       } else if (io_u->error) {
                icd->error = io_u->error;
                io_u_log_error(td, io_u);
        }