Improve rate usec calculation
[fio.git] / fio.c
diff --git a/fio.c b/fio.c
index 0f72fd92c15c883e75603a19b2a4fc89e915ba6f..58e03f9a6b94687011d720235decc238ffaf106b 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -98,7 +98,6 @@ static int check_min_rate(struct thread_data *td, struct timeval *now)
 {
        unsigned long spent;
        unsigned long rate;
-       int ddir = td->ddir;
 
        /*
         * allow a 2 second settle period in the beginning
@@ -110,18 +109,30 @@ static int check_min_rate(struct thread_data *td, struct timeval *now)
         * if rate blocks is set, sample is running
         */
        if (td->rate_bytes) {
+               unsigned long long bytes = 0;
+
                spent = mtime_since(&td->lastrate, now);
                if (spent < td->ratecycle)
                        return 0;
 
-               rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
-               if (rate < td->ratemin) {
-                       fprintf(f_out, "%s: min rate %u not met, got %luKiB/sec\n", td->name, td->ratemin, rate);
+               if (td_read(td))
+                       bytes += td->this_io_bytes[DDIR_READ];
+               if (td_write(td))
+                       bytes += td->this_io_bytes[DDIR_WRITE];
+
+               if (bytes < td->rate_bytes) {
+                       fprintf(f_out, "%s: min rate %u not met\n", td->name, td->ratemin);
                        return 1;
+               } else {
+                       rate = (bytes - td->rate_bytes) / spent;
+                       if (rate < td->ratemin || bytes < td->rate_bytes) {
+                               fprintf(f_out, "%s: min rate %u not met, got %luKiB/sec\n", td->name, td->ratemin, rate);
+                               return 1;
+                       }
                }
+               td->rate_bytes = bytes;
        }
 
-       td->rate_bytes = td->this_io_bytes[ddir];
        memcpy(&td->lastrate, now, sizeof(*now));
        return 0;
 }
@@ -203,7 +214,7 @@ static int fio_io_sync(struct thread_data *td, struct fio_file *f)
 requeue:
        ret = td_io_queue(td, io_u);
        if (ret < 0) {
-               td_verror(td, io_u->error);
+               td_verror(td, io_u->error, "td_io_queue");
                put_io_u(td, io_u);
                return 1;
        } else if (ret == FIO_Q_QUEUED) {
@@ -211,7 +222,7 @@ requeue:
                        return 1;
        } else if (ret == FIO_Q_COMPLETED) {
                if (io_u->error) {
-                       td_verror(td, io_u->error);
+                       td_verror(td, io_u->error, "td_io_queue");
                        return 1;
                }
 
@@ -298,7 +309,7 @@ requeue:
                        break;
                default:
                        assert(ret < 0);
-                       td_verror(td, -ret);
+                       td_verror(td, -ret, "td_io_queue");
                        break;
                }
 
@@ -458,12 +469,12 @@ requeue:
                 */
                usec = utime_since(&s, &comp_time);
 
-               rate_throttle(td, usec, bytes_done, td->ddir);
+               rate_throttle(td, usec, bytes_done);
 
                if (check_min_rate(td, &comp_time)) {
                        if (exitall_on_terminate)
                                terminate_threads(td->groupid, 0);
-                       td_verror(td, ENODATA);
+                       td_verror(td, ENODATA, "check_min_rate");
                        break;
                }
 
@@ -585,7 +596,7 @@ static int switch_ioscheduler(struct thread_data *td)
 
        f = fopen(tmp, "r+");
        if (!f) {
-               td_verror(td, errno);
+               td_verror(td, errno, "fopen");
                return 1;
        }
 
@@ -594,7 +605,7 @@ static int switch_ioscheduler(struct thread_data *td)
         */
        ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
        if (ferror(f) || ret != 1) {
-               td_verror(td, errno);
+               td_verror(td, errno, "fwrite");
                fclose(f);
                return 1;
        }
@@ -606,7 +617,7 @@ static int switch_ioscheduler(struct thread_data *td)
         */
        ret = fread(tmp, 1, sizeof(tmp), f);
        if (ferror(f) || ret < 0) {
-               td_verror(td, errno);
+               td_verror(td, errno, "fread");
                fclose(f);
                return 1;
        }
@@ -614,7 +625,7 @@ static int switch_ioscheduler(struct thread_data *td)
        sprintf(tmp2, "[%s]", td->ioscheduler);
        if (!strstr(tmp, tmp2)) {
                log_err("fio: io scheduler %s not found\n", td->ioscheduler);
-               td_verror(td, EINVAL);
+               td_verror(td, EINVAL, "iosched_switch");
                fclose(f);
                return 1;
        }
@@ -670,7 +681,7 @@ static void *thread_main(void *data)
                goto err;
 
        if (fio_setaffinity(td) == -1) {
-               td_verror(td, errno);
+               td_verror(td, errno, "cpu_set_affinity");
                goto err;
        }
 
@@ -679,13 +690,13 @@ static void *thread_main(void *data)
 
        if (td->ioprio) {
                if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
-                       td_verror(td, errno);
+                       td_verror(td, errno, "ioprio_set");
                        goto err;
                }
        }
 
        if (nice(td->nice) == -1) {
-               td_verror(td, errno);
+               td_verror(td, errno, "nice");
                goto err;
        }
 
@@ -736,10 +747,11 @@ static void *thread_main(void *data)
                else
                        do_io(td);
 
-               runtime[td->ddir] += utime_since_now(&td->start);
-               if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
-                       runtime[td->ddir ^ 1] = runtime[td->ddir];
-
+               if (td_read(td) && td->io_bytes[DDIR_READ])
+                       runtime[DDIR_READ] += utime_since_now(&td->start);
+               if (td_write(td) && td->io_bytes[DDIR_WRITE])
+                       runtime[DDIR_WRITE] += utime_since_now(&td->start);
+               
                if (td->error || td->terminate)
                        break;
 
@@ -824,7 +836,7 @@ static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
         */
        pending = cputhreads = 0;
        for_each_td(td, i) {
-               int flags;
+               int flags = 0;
 
                /*
                 * ->io_ops is NULL for a thread that has closed its
@@ -835,6 +847,13 @@ static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
 
                if (!td->pid || td->runstate == TD_REAPED)
                        continue;
+               if (td->use_thread) {
+                       if (td->runstate == TD_EXITED) {
+                               td_set_runstate(td, TD_REAPED);
+                               goto reaped;
+                       }
+                       continue;
+               }
 
                flags = WNOHANG;
                if (td->runstate == TD_EXITED)
@@ -862,12 +881,7 @@ static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
                        if (WIFEXITED(status)) {
                                if (WEXITSTATUS(status) && !td->error)
                                        td->error = WEXITSTATUS(status);
-                               if (td->use_thread) {
-                                       long ret;
 
-                                       if (pthread_join(td->thread, (void *) &ret))
-                                               perror("pthread_join");
-                               }
                                td_set_runstate(td, TD_REAPED);
                                goto reaped;
                        }
@@ -878,6 +892,13 @@ static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
                 */
                continue;
 reaped:
+               if (td->use_thread) {
+                       long ret;
+
+                       if (pthread_join(td->thread, (void *) &ret))
+                               perror("pthread_join");
+               }
+
                (*nr_running)--;
                (*m_rate) -= td->ratemin;
                (*t_rate) -= td->rate;