Fixup bw/iops logging for short runs
[fio.git] / backend.c
index c58294bb7558f3c4ef8c2b5773d753735d954b90..0a42da3ec440673d741da6ce56e235e997caf1b6 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -55,6 +55,7 @@
 #include "err.h"
 #include "lib/tp.h"
 #include "workqueue.h"
+#include "lib/mountcheck.h"
 
 static pthread_t helper_thread;
 static pthread_mutex_t helper_lock;
@@ -99,7 +100,7 @@ static void sig_int(int sig)
        }
 }
 
-static void sig_show_status(int sig)
+void sig_show_status(int sig)
 {
        show_running_run_stats();
 }
@@ -178,7 +179,7 @@ static int __check_min_rate(struct thread_data *td, struct timeval *now,
                if (spent < td->o.ratecycle)
                        return 0;
 
-               if (td->o.rate[ddir]) {
+               if (td->o.rate[ddir] || td->o.ratemin[ddir]) {
                        /*
                         * check bandwidth specified rate
                         */
@@ -219,6 +220,7 @@ static int __check_min_rate(struct thread_data *td, struct timeval *now,
                                        log_err("%s: min iops rate %u not met,"
                                                " got %lu\n", td->o.name,
                                                        rate_iops_min, rate);
+                                       return 1;
                                }
                        }
                }
@@ -372,6 +374,9 @@ static inline void update_runtime(struct thread_data *td,
                                  unsigned long long *elapsed_us,
                                  const enum fio_ddir ddir)
 {
+       if (ddir == DDIR_WRITE && td_write(td) && td->o.verify_only)
+               return;
+
        td->ts.runtime[ddir] -= (elapsed_us[ddir] + 999) / 1000;
        elapsed_us[ddir] += utime_since_now(&td->start);
        td->ts.runtime[ddir] += (elapsed_us[ddir] + 999) / 1000;
@@ -441,8 +446,8 @@ static int wait_for_completions(struct thread_data *td, struct timeval *time)
        /*
         * if the queue is full, we MUST reap at least 1 event
         */
-       min_evts = min(td->o.iodepth_batch_complete, td->cur_depth);
-    if ((full && !min_evts) || !td->o.iodepth_batch_complete)
+       min_evts = min(td->o.iodepth_batch_complete_min, td->cur_depth);
+    if ((full && !min_evts) || !td->o.iodepth_batch_complete_min)
                min_evts = 1;
 
        if (time && (__should_check_rate(td, DDIR_READ) ||
@@ -535,7 +540,7 @@ sync_done:
                        *ret = ret2;
                break;
        default:
-               assert(ret < 0);
+               assert(*ret < 0);
                td_verror(td, -(*ret), "td_io_queue");
                break;
        }
@@ -546,6 +551,12 @@ sync_done:
        return 0;
 }
 
+static inline int io_in_polling(struct thread_data *td)
+{
+       return !td->o.iodepth_batch_complete_min &&
+                  !td->o.iodepth_batch_complete_max;
+}
+
 /*
  * The main verify engine. Runs over the writes we previously submitted,
  * reads the blocks back in, and checks the crc/md5 of the data.
@@ -679,7 +690,7 @@ static void do_verify(struct thread_data *td, uint64_t verify_bytes)
                 */
 reap:
                full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
-               if (full || !td->o.iodepth_batch_complete)
+               if (full || io_in_polling(td))
                        ret = wait_for_completions(td, NULL);
 
                if (ret < 0)
@@ -758,6 +769,25 @@ static int io_complete_bytes_exceeded(struct thread_data *td)
        return bytes >= limit || exceeds_number_ios(td);
 }
 
+/*
+ * used to calculate the next io time for rate control
+ *
+ */
+static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
+{
+       uint64_t secs, remainder, bps, bytes;
+
+       assert(!(td->flags & TD_F_CHILD));
+       bytes = td->rate_io_issue_bytes[ddir];
+       bps = td->rate_bps[ddir];
+       if (bps) {
+               secs = bytes / bps;
+               remainder = bytes % bps;
+               return remainder * 1000000 / bps + secs * 1000000;
+       } else
+               return 0;
+}
+
 /*
  * Main IO worker function. It retrieves io_u's to process and queues
  * and reaps them, checking for rate and errors along the way.
@@ -887,10 +917,17 @@ static uint64_t do_io(struct thread_data *td)
                        if (td->error)
                                break;
                        ret = workqueue_enqueue(&td->io_wq, io_u);
+
+                       if (should_check_rate(td))
+                               td->rate_next_io_time[ddir] = usec_for_io(td, ddir);
+
                } else {
                        ret = td_io_queue(td, io_u);
 
-                       if (io_queue_event(td, io_u, &ret, ddir, &bytes_issued, 1, &comp_time))
+                       if (should_check_rate(td))
+                               td->rate_next_io_time[ddir] = usec_for_io(td, ddir);
+
+                       if (io_queue_event(td, io_u, &ret, ddir, &bytes_issued, 0, &comp_time))
                                break;
 
                        /*
@@ -901,7 +938,7 @@ static uint64_t do_io(struct thread_data *td)
 reap:
                        full = queue_full(td) ||
                                (ret == FIO_Q_BUSY && td->cur_depth);
-                       if (full || !td->o.iodepth_batch_complete)
+                       if (full || io_in_polling(td))
                                ret = wait_for_completions(td, &comp_time);
                }
                if (ret < 0)
@@ -1171,13 +1208,17 @@ static int switch_ioscheduler(struct thread_data *td)
        /*
         * Read back and check that the selected scheduler is now the default.
         */
+       memset(tmp, 0, sizeof(tmp));
        ret = fread(tmp, sizeof(tmp), 1, f);
        if (ferror(f) || ret < 0) {
                td_verror(td, errno, "fread");
                fclose(f);
                return 1;
        }
-       tmp[sizeof(tmp) - 1] = '\0';
+       /*
+        * either a list of io schedulers or "none\n" is expected.
+        */
+       tmp[strlen(tmp) - 1] = '\0';
 
 
        sprintf(tmp2, "[%s]", td->o.ioscheduler);
@@ -1234,7 +1275,8 @@ static int keep_running(struct thread_data *td)
 
 static int exec_string(struct thread_options *o, const char *string, const char *mode)
 {
-       int ret, newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1;
+       size_t newlen = strlen(string) + strlen(o->name) + strlen(mode) + 9 + 1;
+       int ret;
        char *str;
 
        str = malloc(newlen);
@@ -1517,27 +1559,28 @@ static void *thread_main(void *data)
 
        fio_gettime(&td->epoch, NULL);
        fio_getrusage(&td->ru_start);
+       memcpy(&td->bw_sample_time, &td->epoch, sizeof(td->epoch));
+       memcpy(&td->iops_sample_time, &td->epoch, sizeof(td->epoch));
+
+       if (o->ratemin[DDIR_READ] || o->ratemin[DDIR_WRITE] ||
+                       o->ratemin[DDIR_TRIM]) {
+               memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
+                                       sizeof(td->bw_sample_time));
+               memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
+                                       sizeof(td->bw_sample_time));
+               memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
+                                       sizeof(td->bw_sample_time));
+       }
+
        clear_state = 0;
        while (keep_running(td)) {
                uint64_t verify_bytes;
 
                fio_gettime(&td->start, NULL);
-               memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
-               memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
                memcpy(&td->tv_cache, &td->start, sizeof(td->start));
 
-               if (o->ratemin[DDIR_READ] || o->ratemin[DDIR_WRITE] ||
-                               o->ratemin[DDIR_TRIM]) {
-                       memcpy(&td->lastrate[DDIR_READ], &td->bw_sample_time,
-                                               sizeof(td->bw_sample_time));
-                       memcpy(&td->lastrate[DDIR_WRITE], &td->bw_sample_time,
-                                               sizeof(td->bw_sample_time));
-                       memcpy(&td->lastrate[DDIR_TRIM], &td->bw_sample_time,
-                                               sizeof(td->bw_sample_time));
-               }
-
                if (clear_state)
-                       clear_io_state(td);
+                       clear_io_state(td, 0);
 
                prune_io_piece_log(td);
 
@@ -1575,7 +1618,7 @@ static void *thread_main(void *data)
                    (td->io_ops->flags & FIO_UNIDIR))
                        continue;
 
-               clear_io_state(td);
+               clear_io_state(td, 0);
 
                fio_gettime(&td->start, NULL);
 
@@ -1602,16 +1645,8 @@ static void *thread_main(void *data)
        td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
 
        if (td->o.verify_state_save && !(td->flags & TD_F_VSTATE_SAVED) &&
-           (td->o.verify != VERIFY_NONE && td_write(td))) {
-               struct all_io_list *state;
-               size_t sz;
-
-               state = get_all_io_list(td->thread_number, &sz);
-               if (state) {
-                       __verify_save_state(state, "local");
-                       free(state);
-               }
-       }
+           (td->o.verify != VERIFY_NONE && td_write(td)))
+               verify_save_state(td->thread_number);
 
        fio_unpin_memory(td);
 
@@ -1861,7 +1896,7 @@ void check_trigger_file(void)
                if (nr_clients)
                        fio_clients_send_trigger(trigger_remote_cmd);
                else {
-                       verify_save_state();
+                       verify_save_state(IO_LIST_ALL);
                        fio_terminate_threads(TERMINATE_ALL);
                        exec_trigger(trigger_cmd);
                }
@@ -1877,11 +1912,12 @@ static int fio_verify_load_state(struct thread_data *td)
 
        if (is_backend) {
                void *data;
+               int ver;
 
                ret = fio_server_get_verify_state(td->o.name,
-                                       td->thread_number - 1, &data);
+                                       td->thread_number - 1, &data, &ver);
                if (!ret)
-                       verify_convert_assign_state(td, data);
+                       verify_convert_assign_state(td, data, ver);
        } else
                ret = verify_load_state(td, "local");
 
@@ -1895,6 +1931,27 @@ static void do_usleep(unsigned int usecs)
        usleep(usecs);
 }
 
+static int check_mount_writes(struct thread_data *td)
+{
+       struct fio_file *f;
+       unsigned int i;
+
+       if (!td_write(td) || td->o.allow_mounted_write)
+               return 0;
+
+       for_each_file(td, f, i) {
+               if (f->filetype != FIO_TYPE_BD)
+                       continue;
+               if (device_is_mounted(f->file_name))
+                       goto mounted;
+       }
+
+       return 0;
+mounted:
+       log_err("fio: %s appears mounted, and 'allow_mounted_write' isn't set. Aborting.", f->file_name);
+       return 1;
+}
+
 /*
  * Main function for kicking off and reaping jobs, as needed.
  */
@@ -1913,13 +1970,15 @@ static void run_threads(void)
 
        nr_thread = nr_process = 0;
        for_each_td(td, i) {
+               if (check_mount_writes(td))
+                       return;
                if (td->o.use_thread)
                        nr_thread++;
                else
                        nr_process++;
        }
 
-       if (output_format == FIO_OUTPUT_NORMAL) {
+       if (output_format & FIO_OUTPUT_NORMAL) {
                log_info("Starting ");
                if (nr_thread)
                        log_info("%d thread%s", nr_thread,