Add support for cpus_allowed_policy
[fio.git] / backend.c
index 8787cce6d7e093c0f52733b3e5d24cbdb94ef543..12c76d8545ef53a02b95223ca8d53237b3175ef4 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -52,6 +52,7 @@
 #include "server.h"
 #include "lib/getrusage.h"
 #include "idletime.h"
+#include "err.h"
 
 static pthread_t disk_util_thread;
 static struct fio_mutex *disk_thread_mutex;
@@ -238,8 +239,6 @@ static int check_min_rate(struct thread_data *td, struct timeval *now,
  */
 static void cleanup_pending_aio(struct thread_data *td)
 {
-       struct flist_head *entry, *n;
-       struct io_u *io_u;
        int r;
 
        /*
@@ -253,18 +252,11 @@ static void cleanup_pending_aio(struct thread_data *td)
         * now cancel remaining active events
         */
        if (td->io_ops->cancel) {
-               flist_for_each_safe(entry, n, &td->io_u_busylist) {
-                       io_u = flist_entry(entry, struct io_u, list);
+               struct io_u *io_u;
+               int i;
 
-                       /*
-                        * if the io_u isn't in flight, then that generally
-                        * means someone leaked an io_u. complain but fix
-                        * it up, so we don't stall here.
-                        */
-                       if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
-                               log_err("fio: non-busy IO on busy list\n");
-                               put_io_u(td, io_u);
-                       } else {
+               io_u_qiter(&td->io_u_all, io_u, i) {
+                       if (io_u->flags & IO_U_F_FLIGHT) {
                                r = td->io_ops->cancel(td, io_u);
                                if (!r)
                                        put_io_u(td, io_u);
@@ -354,7 +346,7 @@ static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
                return 0;
        if (!td->o.timeout)
                return 0;
-       if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
+       if (utime_since(&td->epoch, t) >= td->o.timeout)
                return 1;
 
        return 0;
@@ -487,6 +479,12 @@ static void do_verify(struct thread_data *td, uint64_t verify_bytes)
                                break;
 
                        while ((io_u = get_io_u(td)) != NULL) {
+                               if (IS_ERR(io_u)) {
+                                       io_u = NULL;
+                                       ret = FIO_Q_BUSY;
+                                       goto reap;
+                               }
+
                                /*
                                 * We are only interested in the places where
                                 * we wrote or trimmed IOs. Turn those into
@@ -583,6 +581,7 @@ sync_done:
                 * completed io_u's first. Note that we can get BUSY even
                 * without IO queued, if the system is resource starved.
                 */
+reap:
                full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
                if (full || !td->o.iodepth_batch_complete) {
                        min_events = min(td->o.iodepth_batch_complete,
@@ -651,12 +650,25 @@ static uint64_t do_io(struct thread_data *td)
        uint64_t bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
        unsigned int i;
        int ret = 0;
+       uint64_t total_bytes, bytes_issued = 0;
 
        if (in_ramp_time(td))
                td_set_runstate(td, TD_RAMP);
        else
                td_set_runstate(td, TD_RUNNING);
 
+       lat_target_init(td);
+
+       /*
+        * If verify_backlog is enabled, we'll run the verify in this
+        * handler as well. For that case, we may need up to twice the
+        * amount of bytes.
+        */
+       total_bytes = td->o.size;
+       if (td->o.verify != VERIFY_NONE &&
+          (td_write(td) && td->o.verify_backlog))
+               total_bytes += td->o.size;
+
        while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
                (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td) ||
                td->o.time_based) {
@@ -684,9 +696,22 @@ static uint64_t do_io(struct thread_data *td)
                if (flow_threshold_exceeded(td))
                        continue;
 
+               if (bytes_issued >= total_bytes)
+                       break;
+
                io_u = get_io_u(td);
-               if (!io_u)
+               if (IS_ERR_OR_NULL(io_u)) {
+                       int err = PTR_ERR(io_u);
+
+                       io_u = NULL;
+                       if (err == -EBUSY) {
+                               ret = FIO_Q_BUSY;
+                               goto reap;
+                       }
+                       if (td->o.latency_target)
+                               goto reap;
                        break;
+               }
 
                ddir = io_u->ddir;
 
@@ -697,6 +722,13 @@ static uint64_t do_io(struct thread_data *td)
                 */
                if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
                    ((io_u->flags & IO_U_F_VER_LIST) || !td_rw(td))) {
+
+                       if (!td->o.verify_pattern_bytes) {
+                               io_u->rand_seed = __rand(&td->__verify_state);
+                               if (sizeof(int) != sizeof(long *))
+                                       io_u->rand_seed *= __rand(&td->__verify_state);
+                       }
+
                        if (td->o.verify_async)
                                io_u->end_io = verify_io_u_async;
                        else
@@ -707,6 +739,17 @@ static uint64_t do_io(struct thread_data *td)
                else
                        td_set_runstate(td, TD_RUNNING);
 
+               /*
+                * Always log IO before it's issued, so we know the specific
+                * order of it. The logged unit will track when the IO has
+                * completed.
+                */
+               if (td_write(td) && io_u->ddir == DDIR_WRITE &&
+                   td->o.do_verify &&
+                   td->o.verify != VERIFY_NONE &&
+                   !td->o.experimental_verify)
+                       log_io_piece(td, io_u);
+
                ret = td_io_queue(td, io_u);
                switch (ret) {
                case FIO_Q_COMPLETED:
@@ -717,6 +760,7 @@ static uint64_t do_io(struct thread_data *td)
                                int bytes = io_u->xfer_buflen - io_u->resid;
                                struct fio_file *f = io_u->file;
 
+                               bytes_issued += bytes;
                                /*
                                 * zero read, fail
                                 */
@@ -747,6 +791,7 @@ sync_done:
                                ret = io_u_sync_complete(td, io_u, bytes_done);
                                if (ret < 0)
                                        break;
+                               bytes_issued += io_u->xfer_buflen;
                        }
                        break;
                case FIO_Q_QUEUED:
@@ -757,6 +802,7 @@ sync_done:
                         */
                        if (td->io_ops->commit == NULL)
                                io_u_queued(td, io_u);
+                       bytes_issued += io_u->xfer_buflen;
                        break;
                case FIO_Q_BUSY:
                        requeue_io_u(td, &io_u);
@@ -778,6 +824,7 @@ sync_done:
                 * can get BUSY even without IO queued, if the system is
                 * resource starved.
                 */
+reap:
                full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
                if (full || !td->o.iodepth_batch_complete) {
                        min_evts = min(td->o.iodepth_batch_complete,
@@ -814,6 +861,8 @@ sync_done:
                                break;
                        }
                }
+               if (!in_ramp_time(td) && td->o.latency_target)
+                       lat_target_check(td);
 
                if (td->o.thinktime) {
                        unsigned long long b;
@@ -878,13 +927,9 @@ sync_done:
 
 static void cleanup_io_u(struct thread_data *td)
 {
-       struct flist_head *entry, *n;
        struct io_u *io_u;
 
-       flist_for_each_safe(entry, n, &td->io_u_freelist) {
-               io_u = flist_entry(entry, struct io_u, list);
-
-               flist_del(&io_u->list);
+       while ((io_u = io_u_qpop(&td->io_u_freelist)) != NULL) {
 
                if (td->io_ops->io_u_free)
                        td->io_ops->io_u_free(td, io_u);
@@ -893,6 +938,10 @@ static void cleanup_io_u(struct thread_data *td)
        }
 
        free_io_mem(td);
+
+       io_u_rexit(&td->io_u_requeues);
+       io_u_qexit(&td->io_u_freelist);
+       io_u_qexit(&td->io_u_all);
 }
 
 static int init_io_u(struct thread_data *td)
@@ -900,7 +949,7 @@ static int init_io_u(struct thread_data *td)
        struct io_u *io_u;
        unsigned int max_bs, min_write;
        int cl_align, i, max_units;
-       int data_xfer = 1;
+       int data_xfer = 1, err;
        char *p;
 
        max_units = td->o.iodepth;
@@ -912,13 +961,24 @@ static int init_io_u(struct thread_data *td)
        if ((td->io_ops->flags & FIO_NOIO) || !(td_read(td) || td_write(td)))
                data_xfer = 0;
 
+       err = 0;
+       err += io_u_rinit(&td->io_u_requeues, td->o.iodepth);
+       err += io_u_qinit(&td->io_u_freelist, td->o.iodepth);
+       err += io_u_qinit(&td->io_u_all, td->o.iodepth);
+
+       if (err) {
+               log_err("fio: failed setting up IO queues\n");
+               return 1;
+       }
+
        /*
         * if we may later need to do address alignment, then add any
         * possible adjustment here so that we don't cause a buffer
         * overflow later. this adjustment may be too much if we get
         * lucky and the allocator gives us an aligned address.
         */
-       if (td->o.odirect || td->o.mem_align || (td->io_ops->flags & FIO_RAWIO))
+       if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
+           (td->io_ops->flags & FIO_RAWIO))
                td->orig_buffer_size += page_mask + td->o.mem_align;
 
        if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
@@ -936,7 +996,7 @@ static int init_io_u(struct thread_data *td)
        if (data_xfer && allocate_io_mem(td))
                return 1;
 
-       if (td->o.odirect || td->o.mem_align ||
+       if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
            (td->io_ops->flags & FIO_RAWIO))
                p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
        else
@@ -958,7 +1018,7 @@ static int init_io_u(struct thread_data *td)
 
                io_u = ptr;
                memset(io_u, 0, sizeof(*io_u));
-               INIT_FLIST_HEAD(&io_u->list);
+               INIT_FLIST_HEAD(&io_u->verify_list);
                dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
 
                if (data_xfer) {
@@ -972,13 +1032,19 @@ static int init_io_u(struct thread_data *td)
                                 * Fill the buffer with the pattern if we are
                                 * going to be doing writes.
                                 */
-                               fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
+                               fill_verify_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
                        }
                }
 
                io_u->index = i;
                io_u->flags = IO_U_F_FREE;
-               flist_add(&io_u->list, &td->io_u_freelist);
+               io_u_qpush(&td->io_u_freelist, io_u);
+
+               /*
+                * io_u never leaves this stack, used for iteration of all
+                * io_u buffers.
+                */
+               io_u_qpush(&td->io_u_all, io_u);
 
                if (td->io_ops->io_u_init) {
                        int ret = td->io_ops->io_u_init(td, io_u);
@@ -1073,20 +1139,24 @@ static int keep_running(struct thread_data *td)
                if (diff < td_max_bs(td))
                        return 0;
 
+               if (fio_files_done(td))
+                       return 0;
+
                return 1;
        }
 
        return 0;
 }
 
-static int exec_string(const char *string)
+static int exec_string(struct thread_options *o, const char *string, const char *mode)
 {
-       int ret, newlen = strlen(string) + 1 + 8;
+       int ret, newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1;
        char *str;
 
        str = malloc(newlen);
-       sprintf(str, "sh -c %s", string);
+       sprintf(str, "%s &> %s.%s.txt", string, o->name, mode);
 
+       log_info("%s : Saving output of %s in %s.%s.txt\n",o->name, mode, o->name, mode);
        ret = system(str);
        if (ret == -1)
                log_err("fio: exec of cmd <%s> failed\n", str);
@@ -1095,6 +1165,44 @@ static int exec_string(const char *string)
        return ret;
 }
 
+/*
+ * Dry run to compute correct state of numberio for verification.
+ */
+static uint64_t do_dry_run(struct thread_data *td)
+{
+       uint64_t bytes_done[DDIR_RWDIR_CNT] = { 0, 0, 0 };
+
+       td_set_runstate(td, TD_RUNNING);
+
+       while ((td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
+               (!flist_empty(&td->trim_list)) || !io_bytes_exceeded(td)) {
+               struct io_u *io_u;
+               int ret;
+
+               if (td->terminate || td->done)
+                       break;
+
+               io_u = get_io_u(td);
+               if (!io_u)
+                       break;
+
+               io_u->flags |= IO_U_F_FLIGHT;
+               io_u->error = 0;
+               io_u->resid = 0;
+               if (ddir_rw(acct_ddir(io_u)))
+                       td->io_issues[acct_ddir(io_u)]++;
+               if (ddir_rw(io_u->ddir)) {
+                       io_u_mark_depth(td, 1);
+                       td->ts.total_io_u[io_u->ddir]++;
+               }
+
+               ret = io_u_sync_complete(td, io_u, bytes_done);
+               (void) ret;
+       }
+
+       return bytes_done[DDIR_WRITE] + bytes_done[DDIR_TRIM];
+}
+
 /*
  * Entry point for the thread based jobs. The process based jobs end up
  * here as well, after a little setup.
@@ -1114,6 +1222,11 @@ static void *thread_main(void *data)
        } else
                td->pid = gettid();
 
+       /*
+        * fio_time_init() may not have been called yet if running as a server
+        */
+       fio_time_init();
+
        fio_local_clock_init(o->use_thread);
 
        dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
@@ -1121,9 +1234,6 @@ static void *thread_main(void *data)
        if (is_backend)
                fio_server_send_start(td);
 
-       INIT_FLIST_HEAD(&td->io_u_freelist);
-       INIT_FLIST_HEAD(&td->io_u_busylist);
-       INIT_FLIST_HEAD(&td->io_u_requeues);
        INIT_FLIST_HEAD(&td->io_log_list);
        INIT_FLIST_HEAD(&td->io_hist_list);
        INIT_FLIST_HEAD(&td->verify_list);
@@ -1143,13 +1253,6 @@ static void *thread_main(void *data)
        fio_mutex_down(td->mutex);
        dprint(FD_MUTEX, "done waiting on td->mutex\n");
 
-       /*
-        * the ->mutex mutex is now no longer used, close it to avoid
-        * eating a file descriptor
-        */
-       fio_mutex_remove(td->mutex);
-       td->mutex = NULL;
-
        /*
         * A new gid requires privilege, so we need to do this before setting
         * the uid.
@@ -1175,6 +1278,15 @@ static void *thread_main(void *data)
         * allocations.
         */
        if (o->cpumask_set) {
+               if (o->cpus_allowed_policy == FIO_CPUS_SPLIT) {
+                       ret = fio_cpus_split(&o->cpumask, td->thread_number);
+                       if (!ret) {
+                               log_err("fio: no CPUs set\n");
+                               log_err("fio: Try increasing number of available CPUs\n");
+                               td_verror(td, EINVAL, "cpus_split");
+                               goto err;
+                       }
+               }
                ret = fio_setaffinity(td->pid, o->cpumask);
                if (ret == -1) {
                        td_verror(td, errno, "cpu_set_affinity");
@@ -1270,7 +1382,7 @@ static void *thread_main(void *data)
        if (init_random_map(td))
                goto err;
 
-       if (o->exec_prerun && exec_string(o->exec_prerun))
+       if (o->exec_prerun && exec_string(o, o->exec_prerun, (const char *)"prerun"))
                goto err;
 
        if (o->pre_read) {
@@ -1306,7 +1418,10 @@ static void *thread_main(void *data)
 
                prune_io_piece_log(td);
 
-               verify_bytes = do_io(td);
+               if (td->o.verify_only && (td_write(td) || td_rw(td)))
+                       verify_bytes = do_dry_run(td);
+               else
+                       verify_bytes = do_io(td);
 
                clear_state = 1;
 
@@ -1355,6 +1470,7 @@ static void *thread_main(void *data)
        fio_unpin_memory(td);
 
        fio_mutex_down(writeout_mutex);
+       finalize_logs(td);
        if (td->bw_log) {
                if (o->bw_log_file) {
                        finish_log_named(td, td->bw_log,
@@ -1393,7 +1509,7 @@ static void *thread_main(void *data)
 
        fio_mutex_up(writeout_mutex);
        if (o->exec_postrun)
-               exec_string(o->exec_postrun);
+               exec_string(o, o->exec_postrun, (const char *)"postrun");
 
        if (exitall_on_terminate)
                fio_terminate_threads(td->groupid);
@@ -1426,6 +1542,9 @@ err:
        fio_mutex_remove(td->rusage_sem);
        td->rusage_sem = NULL;
 
+       fio_mutex_remove(td->mutex);
+       td->mutex = NULL;
+
        td_set_runstate(td, TD_EXITED);
        return (void *) (uintptr_t) td->error;
 }
@@ -1573,12 +1692,12 @@ static void do_usleep(unsigned int usecs)
 static void run_threads(void)
 {
        struct thread_data *td;
-       unsigned long spent;
        unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
+       uint64_t spent;
 
        if (fio_gtod_offload && fio_start_gtod_thread())
                return;
-       
+
        fio_idle_prof_init();
 
        set_sig_handlers();
@@ -1672,9 +1791,9 @@ static void run_threads(void)
                        }
 
                        if (td->o.start_delay) {
-                               spent = mtime_since_genesis();
+                               spent = utime_since_genesis();
 
-                               if (td->o.start_delay * 1000 > spent)
+                               if (td->o.start_delay > spent)
                                        continue;
                        }