Patch to make per-thread IOPS more accurate
[fio.git] / stat.c
diff --git a/stat.c b/stat.c
index 536d5a9c000449675caa36f8a4d998b79f58bb82..85bd728d4aaa748ae97a50b7861b43f853b2338e 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -14,7 +14,7 @@
 #include "lib/getrusage.h"
 #include "idletime.h"
 
-static struct fio_mutex *stat_mutex;
+struct fio_mutex *stat_mutex;
 
 void update_rusage_stat(struct thread_data *td)
 {
@@ -263,7 +263,7 @@ int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
 void show_group_stats(struct group_run_stats *rs)
 {
        char *p1, *p2, *p3, *p4;
-       const char *ddir_str[] = { "   READ", "  WRITE" , "   TRIM"};
+       const char *str[] = { "   READ", "  WRITE" , "   TRIM"};
        int i;
 
        log_info("\nRun status group %d (all jobs):\n", rs->groupid);
@@ -281,7 +281,7 @@ void show_group_stats(struct group_run_stats *rs)
 
                log_info("%s: io=%s, aggrb=%s/s, minb=%s/s, maxb=%s/s,"
                         " mint=%llumsec, maxt=%llumsec\n",
-                               rs->unified_rw_rep ? "  MIXED" : ddir_str[i],
+                               rs->unified_rw_rep ? "  MIXED" : str[i],
                                p1, p2, p3, p4,
                                (unsigned long long) rs->min_run[i],
                                (unsigned long long) rs->max_run[i]);
@@ -363,7 +363,7 @@ static void display_lat(const char *name, unsigned long min, unsigned long max,
 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
                             int ddir)
 {
-       const char *ddir_str[] = { "read ", "write", "trim" };
+       const char *str[] = { "read ", "write", "trim" };
        unsigned long min, max, runt;
        unsigned long long bw, iops;
        double mean, dev;
@@ -386,7 +386,7 @@ static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
        iops_p = num2str(iops, 6, 1, 0, 0);
 
        log_info("  %s: io=%s, bw=%s/s, iops=%s, runt=%6llumsec\n",
-                               rs->unified_rw_rep ? "mixed" : ddir_str[ddir],
+                               rs->unified_rw_rep ? "mixed" : str[ddir],
                                io_p, bw_p, iops_p,
                                (unsigned long long) ts->runtime[ddir]);
 
@@ -504,11 +504,9 @@ static void show_thread_status_normal(struct thread_stat *ts,
        unsigned long runtime;
        double io_u_dist[FIO_IO_U_MAP_NR];
        time_t time_p;
-       char time_buf[64];
+       char time_buf[32];
 
-       if (!(ts->io_bytes[DDIR_READ] + ts->io_bytes[DDIR_WRITE] +
-           ts->io_bytes[DDIR_TRIM]) && !(ts->total_io_u[DDIR_READ] +
-           ts->total_io_u[DDIR_WRITE] + ts->total_io_u[DDIR_TRIM]))
+       if (!ddir_rw_sum(ts->io_bytes) && !ddir_rw_sum(ts->total_io_u))
                return;
 
        time(&time_p);
@@ -676,9 +674,9 @@ static void add_ddir_status_json(struct thread_stat *ts,
                struct group_run_stats *rs, int ddir, struct json_object *parent)
 {
        unsigned long min, max;
-       unsigned long long bw, iops;
+       unsigned long long bw;
        unsigned int *ovals = NULL;
-       double mean, dev;
+       double mean, dev, iops;
        unsigned int len, minv, maxv;
        int i;
        const char *ddirname[] = {"read", "write", "trim"};
@@ -695,18 +693,22 @@ static void add_ddir_status_json(struct thread_stat *ts,
        json_object_add_value_object(parent,
                ts->unified_rw_rep ? "mixed" : ddirname[ddir], dir_object);
 
-       iops = bw = 0;
+       bw = 0;
+       iops = 0.0;
        if (ts->runtime[ddir]) {
                uint64_t runt = ts->runtime[ddir];
 
                bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
-               iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
+               iops = (1000.0 * (uint64_t) ts->total_io_u[ddir]) / runt;
        }
 
        json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir] >> 10);
        json_object_add_value_int(dir_object, "bw", bw);
-       json_object_add_value_int(dir_object, "iops", iops);
+       json_object_add_value_float(dir_object, "iops", iops);
        json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
+       json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[ddir]);
+       json_object_add_value_int(dir_object, "short_ios", ts->short_io_u[ddir]);
+       json_object_add_value_int(dir_object, "drop_ios", ts->drop_io_u[ddir]);
 
        if (!calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
                min = max = 0;
@@ -895,8 +897,7 @@ static void show_thread_status_terse_v3_v4(struct thread_stat *ts,
                log_info(";%3.2f%%", io_u_lat_m[i]);
 
        /* disk util stats, if any */
-       if (is_backend)
-               show_disk_util(1, NULL);
+       show_disk_util(1, NULL);
 
        /* Additional output if continue_on_error set - default off*/
        if (ts->continue_on_error)
@@ -1076,6 +1077,10 @@ void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
                dst->agg[i] += src->agg[i];
        }
 
+       if (!dst->kb_base)
+               dst->kb_base = src->kb_base;
+       if (!dst->unit_base)
+               dst->unit_base = src->unit_base;
 }
 
 void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
@@ -1185,7 +1190,6 @@ void __show_run_stats(void)
        int unit_base_warned = 0;
        struct json_object *root = NULL;
        struct json_array *array = NULL;
-
        runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
 
        for (i = 0; i < groupid + 1; i++)
@@ -1347,8 +1351,18 @@ void __show_run_stats(void)
        if (output_format == FIO_OUTPUT_NORMAL)
                log_info("\n");
        else if (output_format == FIO_OUTPUT_JSON) {
+               char time_buf[32];
+               time_t time_p;
+
+               time(&time_p);
+               os_ctime_r((const time_t *) &time_p, time_buf,
+                               sizeof(time_buf));
+               time_buf[strlen(time_buf) - 1] = '\0';
+
                root = json_create_object();
                json_object_add_value_string(root, "fio version", fio_version_string);
+               json_object_add_value_int(root, "timestamp", time_p);
+               json_object_add_value_string(root, "time", time_buf);
                array = json_create_array();
                json_object_add_value_array(root, "jobs", array);
        }
@@ -1417,7 +1431,7 @@ void show_run_stats(void)
        fio_mutex_up(stat_mutex);
 }
 
-static void *__show_running_run_stats(void *arg)
+void __show_running_run_stats(void)
 {
        struct thread_data *td;
        unsigned long long *rt;
@@ -1446,6 +1460,8 @@ static void *__show_running_run_stats(void *arg)
        }
 
        for_each_td(td, i) {
+               if (td->runstate >= TD_EXITED)
+                       continue;
                if (td->rusage_sem) {
                        td->update_rusage = 1;
                        fio_mutex_down(td->rusage_sem);
@@ -1466,34 +1482,6 @@ static void *__show_running_run_stats(void *arg)
 
        free(rt);
        fio_mutex_up(stat_mutex);
-       free(arg);
-       return NULL;
-}
-
-/*
- * Called from signal handler. It _should_ be safe to just run this inline
- * in the sig handler, but we should be disturbing the system less by just
- * creating a thread to do it.
- */
-void show_running_run_stats(void)
-{
-       pthread_t *thread;
-
-       thread = calloc(1, sizeof(*thread));
-       if (!thread)
-               return;
-
-       if (!pthread_create(thread, NULL, __show_running_run_stats, thread)) {
-               int err;
-
-               err = pthread_detach(*thread);
-               if (err)
-                       log_err("fio: DU thread detach failed: %s\n", strerror(err));
-
-               return;
-       }
-
-       free(thread);
 }
 
 static int status_interval_init;
@@ -1912,3 +1900,12 @@ void stat_exit(void)
        fio_mutex_down(stat_mutex);
        fio_mutex_remove(stat_mutex);
 }
+
+/*
+ * Called from signal handler. Wake up status thread.
+ */
+void show_running_run_stats(void)
+{
+       helper_do_stat = 1;
+       pthread_cond_signal(&helper_cond);
+}