Handle normal output wrapping of the percentile list
[fio.git] / stat.c
diff --git a/stat.c b/stat.c
index 4d3e97475d78e5de156df14c9307c62e206a908f..28acc233870fea2fa9e29abd8d5664b2101e7b89 100644 (file)
--- a/stat.c
+++ b/stat.c
 #include "diskutil.h"
 #include "lib/ieee754.h"
 #include "json.h"
+#include "lib/getrusage.h"
+#include "idletime.h"
 
 void update_rusage_stat(struct thread_data *td)
 {
        struct thread_stat *ts = &td->ts;
 
-       getrusage(RUSAGE_SELF, &td->ru_end);
-
+       fio_getrusage(&td->ru_end);
        ts->usr_time += mtime_since(&td->ru_start.ru_utime,
                                        &td->ru_end.ru_utime);
        ts->sys_time += mtime_since(&td->ru_start.ru_stime,
@@ -181,11 +182,12 @@ static unsigned int calc_clat_percentiles(unsigned int *io_u_plat,
  * Find and display the p-th percentile of clat
  */
 static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
-                                 fio_fp64_t *plist)
+                                 fio_fp64_t *plist, uint64_t precision)
 {
        unsigned int len, j = 0, minv, maxv;
        unsigned int *ovals;
-       int is_last, scale_down;
+       int is_last, per_line, scale_down;
+       char fmt[32];
 
        len = calc_clat_percentiles(io_u_plat, nr, plist, &ovals, &maxv, &minv);
        if (!len)
@@ -203,20 +205,23 @@ static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
                log_info("    clat percentiles (usec):\n     |");
        }
 
+       snprintf(fmt, sizeof(fmt), "%%1.%luf", precision);
+       per_line = (80 - 7) / (precision + 14);
+
        for (j = 0; j < len; j++) {
-               char fbuf[8];
+               char fbuf[16], *ptr = fbuf;
 
                /* for formatting */
-               if (j != 0 && (j % 4) == 0)
+               if (j != 0 && (j % per_line) == 0)
                        log_info("     |");
 
                /* end of the list */
                is_last = (j == len - 1);
 
                if (plist[j].u.f < 10.0)
-                       sprintf(fbuf, " %2.2f", plist[j].u.f);
-               else
-                       sprintf(fbuf, "%2.2f", plist[j].u.f);
+                       ptr += sprintf(fbuf, " ");
+
+               snprintf(ptr, sizeof(fbuf), fmt, plist[j].u.f);
 
                if (scale_down)
                        ovals[j] = (ovals[j] + 999) / 1000;
@@ -226,7 +231,7 @@ static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
                if (is_last)
                        break;
 
-               if (j % 4 == 3) /* for formatting */
+               if ((j % per_line) == per_line - 1)     /* for formatting */
                        log_info("\n");
        }
 
@@ -277,9 +282,9 @@ void show_group_stats(struct group_run_stats *rs)
                p4 = num2str(rs->max_bw[i], 6, rs->kb_base, i2p);
 
                log_info("%s: io=%sB, aggrb=%sB/s, minb=%sB/s, maxb=%sB/s,"
-                        " mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2,
-                                               p3, p4, rs->min_run[i],
-                                               rs->max_run[i]);
+                        " mint=%llumsec, maxt=%llumsec\n",
+                               rs->unified_rw_rep ? "  MIXED" : ddir_str[i],
+                               p1, p2, p3, p4, rs->min_run[i], rs->max_run[i]);
 
                free(p1);
                free(p2);
@@ -381,8 +386,8 @@ static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
        iops_p = num2str(iops, 6, 1, 0);
 
        log_info("  %s: io=%sB, bw=%sB/s, iops=%s, runt=%6llumsec\n",
-                                       ddir_str[ddir], io_p, bw_p, iops_p,
-                                       ts->runtime[ddir]);
+                               rs->unified_rw_rep ? "mixed" : ddir_str[ddir],
+                               io_p, bw_p, iops_p, ts->runtime[ddir]);
 
        free(io_p);
        free(bw_p);
@@ -439,7 +444,8 @@ static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
        if (ts->clat_percentiles) {
                show_clat_percentiles(ts->io_u_plat[ddir],
                                        ts->clat_stat[ddir].samples,
-                                       ts->percentile_list);
+                                       ts->percentile_list,
+                                       ts->percentile_precision);
        }
        if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
                double p_of_agg = 100.0;
@@ -534,7 +540,7 @@ void show_thread_status(struct thread_stat *ts, struct group_run_stats *rs)
                return;
 
        time(&time_p);
-       ctime_r((const time_t *) &time_p, time_buf);
+       os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
 
        if (!ts->error) {
                log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
@@ -654,7 +660,7 @@ static void show_ddir_status_terse(struct thread_stat *ts,
                        log_info(";0%%=0");
                        continue;
                }
-               log_info(";%2.2f%%=%u", ts->percentile_list[i].u.f, ovals[i]);
+               log_info(";%f%%=%u", ts->percentile_list[i].u.f, ovals[i]);
        }
 
        if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
@@ -695,8 +701,12 @@ static void add_ddir_status_json(struct thread_stat *ts,
 
        assert(ddir_rw(ddir));
 
+       if (ts->unified_rw_rep && ddir != DDIR_READ)
+               return;
+
        dir_object = json_create_object();
-       json_object_add_value_object(parent, ddirname[ddir], dir_object);
+       json_object_add_value_object(parent,
+               ts->unified_rw_rep ? "mixed" : ddirname[ddir], dir_object);
 
        iops = bw = 0;
        if (ts->runtime[ddir]) {
@@ -748,7 +758,7 @@ static void add_ddir_status_json(struct thread_stat *ts,
                        json_object_add_value_int(percentile_object, "0.00", 0);
                        continue;
                }
-               snprintf(buf, sizeof(buf) - 1, "%2.2f", ts->percentile_list[i].u.f);
+               snprintf(buf, sizeof(buf), "%f", ts->percentile_list[i].u.f);
                json_object_add_value_int(percentile_object, (const char *)buf, ovals[i]);
        }
 
@@ -852,7 +862,7 @@ static void show_thread_status_terse_v3_v4(struct thread_stat *ts,
        int i;
 
        /* General Info */
-       log_info("%s;%s;%s;%d;%d", ver, fio_version_string,
+       log_info("%d;%s;%s;%d;%d", ver, fio_version_string,
                                        ts->name, ts->groupid, ts->error);
        /* Log Read Status */
        show_ddir_status_terse(ts, rs, DDIR_READ);
@@ -924,7 +934,7 @@ static struct json_object *show_thread_status_json(struct thread_stat *ts,
 
        add_ddir_status_json(ts, rs, DDIR_READ, root);
        add_ddir_status_json(ts, rs, DDIR_WRITE, root);
-//     add_ddir_status_json(ts, rs, DDIR_TRIM, root);
+       add_ddir_status_json(ts, rs, DDIR_TRIM, root);
 
        /* CPU Usage */
        if (ts->total_run_time) {
@@ -954,9 +964,9 @@ static struct json_object *show_thread_status_json(struct thread_stat *ts,
        for (i = 0; i < 7; i++) {
                char name[20];
                if (i < 6)
-                       snprintf(name, 19, "%d", 1 << i);
+                       snprintf(name, 20, "%d", 1 << i);
                else
-                       snprintf(name, 19, ">=%d", 1 << i);
+                       snprintf(name, 20, ">=%d", 1 << i);
                json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
        }
 
@@ -1062,15 +1072,27 @@ void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
        int l, k;
 
        for (l = 0; l < DDIR_RWDIR_CNT; l++) {
-               sum_stat(&dst->clat_stat[l], &src->clat_stat[l], nr);
-               sum_stat(&dst->slat_stat[l], &src->slat_stat[l], nr);
-               sum_stat(&dst->lat_stat[l], &src->lat_stat[l], nr);
-               sum_stat(&dst->bw_stat[l], &src->bw_stat[l], nr);
-
-               dst->io_bytes[l] += src->io_bytes[l];
-
-               if (dst->runtime[l] < src->runtime[l])
-                       dst->runtime[l] = src->runtime[l];
+               if (!dst->unified_rw_rep) {
+                       sum_stat(&dst->clat_stat[l], &src->clat_stat[l], nr);
+                       sum_stat(&dst->slat_stat[l], &src->slat_stat[l], nr);
+                       sum_stat(&dst->lat_stat[l], &src->lat_stat[l], nr);
+                       sum_stat(&dst->bw_stat[l], &src->bw_stat[l], nr);
+
+                       dst->io_bytes[l] += src->io_bytes[l];
+
+                       if (dst->runtime[l] < src->runtime[l])
+                               dst->runtime[l] = src->runtime[l];
+               } else {
+                       sum_stat(&dst->clat_stat[0], &src->clat_stat[l], nr);
+                       sum_stat(&dst->slat_stat[0], &src->slat_stat[l], nr);
+                       sum_stat(&dst->lat_stat[0], &src->lat_stat[l], nr);
+                       sum_stat(&dst->bw_stat[0], &src->bw_stat[l], nr);
+
+                       dst->io_bytes[0] += src->io_bytes[l];
+
+                       if (dst->runtime[0] < src->runtime[l])
+                               dst->runtime[0] = src->runtime[l];
+               }
        }
 
        dst->usr_time += src->usr_time;
@@ -1091,14 +1113,24 @@ void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
                dst->io_u_lat_m[k] += src->io_u_lat_m[k];
 
        for (k = 0; k < DDIR_RWDIR_CNT; k++) {
-               dst->total_io_u[k] += src->total_io_u[k];
-               dst->short_io_u[k] += src->short_io_u[k];
+               if (!dst->unified_rw_rep) {
+                       dst->total_io_u[k] += src->total_io_u[k];
+                       dst->short_io_u[k] += src->short_io_u[k];
+               } else {
+                       dst->total_io_u[0] += src->total_io_u[k];
+                       dst->short_io_u[0] += src->short_io_u[k];
+               }
        }
 
        for (k = 0; k < DDIR_RWDIR_CNT; k++) {
                int m;
-               for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
-                       dst->io_u_plat[k][m] += src->io_u_plat[k][m];
+
+               for (m = 0; m < FIO_IO_U_PLAT_NR; m++) {
+                       if (!dst->unified_rw_rep)
+                               dst->io_u_plat[k][m] += src->io_u_plat[k][m];
+                       else
+                               dst->io_u_plat[0][m] += src->io_u_plat[k][m];
+               }
        }
 
        dst->total_run_time += src->total_run_time;
@@ -1183,10 +1215,8 @@ void show_run_stats(void)
                ts = &threadstats[j];
 
                ts->clat_percentiles = td->o.clat_percentiles;
-               if (td->o.overwrite_plist)
-                       memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
-               else
-                       memcpy(ts->percentile_list, def_percentile_list, sizeof(def_percentile_list));
+               ts->percentile_precision = td->o.percentile_precision;
+               memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
 
                idx++;
                ts->members++;
@@ -1210,6 +1240,7 @@ void show_run_stats(void)
                        ts->pid = td->pid;
 
                        ts->kb_base = td->o.kb_base;
+                       ts->unified_rw_rep = td->o.unified_rw_rep;
                } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
                        log_info("fio: kb_base differs for jobs in group, using"
                                 " %u as the base\n", ts->kb_base);
@@ -1239,6 +1270,7 @@ void show_run_stats(void)
                ts = &threadstats[i];
                rs = &runstats[ts->groupid];
                rs->kb_base = ts->kb_base;
+               rs->unified_rw_rep += ts->unified_rw_rep;
 
                for (j = 0; j < DDIR_RWDIR_CNT; j++) {
                        if (!ts->runtime[j])
@@ -1280,10 +1312,9 @@ void show_run_stats(void)
        /*
         * don't overwrite last signal output
         */
-       if (!terse_output)
+       if (output_format == FIO_OUTPUT_NORMAL)
                log_info("\n");
-
-       if (terse_output && terse_version == 4) {
+       else if (output_format == FIO_OUTPUT_JSON) {
                root = json_create_object();
                json_object_add_value_string(root, "fio version", fio_version_string);
                array = json_create_array();
@@ -1296,21 +1327,20 @@ void show_run_stats(void)
 
                if (is_backend)
                        fio_server_send_ts(ts, rs);
-               else if (terse_output) {
-                       if (terse_version != 4)
-                               show_thread_status_terse(ts, rs);
-                       else {
-                               struct json_object *tmp = show_thread_status_json(ts,
-                                       rs);
-                               json_array_add_value_object(array, tmp);
-                       }
+               else if (output_format == FIO_OUTPUT_TERSE)
+                       show_thread_status_terse(ts, rs);
+               else if (output_format == FIO_OUTPUT_JSON) {
+                       struct json_object *tmp = show_thread_status_json(ts, rs);
+                       json_array_add_value_object(array, tmp);
                } else
                        show_thread_status(ts, rs);
        }
-       if (terse_output && terse_version == 4) {
+       if (output_format == FIO_OUTPUT_JSON) {
                /* disk util stats, if any */
                show_disk_util(1, root);
 
+               show_idle_prof_stats(FIO_OUTPUT_JSON, root);
+
                json_print_object(root);
                log_info("\n");
                json_free_object(root);
@@ -1322,15 +1352,17 @@ void show_run_stats(void)
                rs->groupid = i;
                if (is_backend)
                        fio_server_send_gs(rs);
-               else if (!terse_output)
+               else if (output_format == FIO_OUTPUT_NORMAL)
                        show_group_stats(rs);
        }
 
        if (is_backend)
                fio_server_send_du();
-       else if (!terse_output)
+       else if (output_format == FIO_OUTPUT_NORMAL)
                show_disk_util(0, NULL);
 
+       show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL);
+
        free(runstats);
        free(threadstats);
 }