fio: fix aio trim completion latencies
[fio.git] / stat.c
diff --git a/stat.c b/stat.c
index ec75de241be623756c88c1b88edb69473a0dfb97..bf87917c2956eb60729fa9ea95ac93b1cbe1ed50 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -139,7 +139,6 @@ unsigned int calc_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
 {
        unsigned long long sum = 0;
        unsigned int len, i, j = 0;
-       unsigned int oval_len = 0;
        unsigned long long *ovals = NULL;
        bool is_last;
 
@@ -161,20 +160,19 @@ unsigned int calc_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
        if (len > 1)
                qsort((void *)plist, len, sizeof(plist[0]), double_cmp);
 
+       ovals = malloc(len * sizeof(*ovals));
+       if (!ovals)
+               return 0;
+
        /*
         * Calculate bucket values, note down max and min values
         */
        is_last = false;
        for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
                sum += io_u_plat[i];
-               while (sum >= (plist[j].u.f / 100.0 * nr)) {
+               while (sum >= ((long double) plist[j].u.f / 100.0 * nr)) {
                        assert(plist[j].u.f <= 100.0);
 
-                       if (j == oval_len) {
-                               oval_len += 100;
-                               ovals = realloc(ovals, oval_len * sizeof(*ovals));
-                       }
-
                        ovals[j] = plat_idx_to_val(i);
                        if (ovals[j] < *minv)
                                *minv = ovals[j];
@@ -189,6 +187,9 @@ unsigned int calc_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
                }
        }
 
+       if (!is_last)
+               log_err("fio: error calculating latency percentiles\n");
+
        *output = ovals;
        return len;
 }
@@ -1090,7 +1091,8 @@ static void add_ddir_status_json(struct thread_stat *ts,
                len = 0;
 
        percentile_object = json_create_object();
-       json_object_add_value_object(tmp_object, "percentile", percentile_object);
+       if (ts->clat_percentiles)
+               json_object_add_value_object(tmp_object, "percentile", percentile_object);
        for (i = 0; i < len; i++) {
                snprintf(buf, sizeof(buf), "%f", ts->percentile_list[i].u.f);
                json_object_add_value_int(percentile_object, (const char *)buf, ovals[i]);
@@ -1129,6 +1131,8 @@ static void add_ddir_status_json(struct thread_stat *ts,
        json_object_add_value_int(tmp_object, "max", max);
        json_object_add_value_float(tmp_object, "mean", mean);
        json_object_add_value_float(tmp_object, "stddev", dev);
+       if (ts->lat_percentiles)
+               json_object_add_value_object(tmp_object, "percentile", percentile_object);
        if (output_format & FIO_OUTPUT_JSON_PLUS && ts->lat_percentiles)
                json_object_add_value_object(tmp_object, "bins", clat_bins_object);
 
@@ -1240,12 +1244,13 @@ static void show_thread_status_terse_all(struct thread_stat *ts,
        /* Additional output if continue_on_error set - default off*/
        if (ts->continue_on_error)
                log_buf(out, ";%llu;%d", (unsigned long long) ts->total_err_count, ts->first_error);
-       if (ver == 2)
-               log_buf(out, "\n");
 
        /* Additional output if description is set */
-       if (strlen(ts->description))
+       if (strlen(ts->description)) {
+               if (ver == 2)
+                       log_buf(out, "\n");
                log_buf(out, ";%s", ts->description);
+       }
 
        log_buf(out, "\n");
 }
@@ -1678,11 +1683,14 @@ void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src,
                dst->io_u_submit[k] += src->io_u_submit[k];
                dst->io_u_complete[k] += src->io_u_complete[k];
        }
-       for (k = 0; k < FIO_IO_U_LAT_N_NR; k++) {
+
+       for (k = 0; k < FIO_IO_U_LAT_N_NR; k++)
                dst->io_u_lat_n[k] += src->io_u_lat_n[k];
+       for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
                dst->io_u_lat_u[k] += src->io_u_lat_u[k];
+       for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
                dst->io_u_lat_m[k] += src->io_u_lat_m[k];
-       }
+
        for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
                dst->io_u_sync_plat[k] += src->io_u_sync_plat[k];
 
@@ -2351,7 +2359,8 @@ static void __add_log_sample(struct io_log *iolog, union io_sample_data data,
 
 static inline void reset_io_stat(struct io_stat *ios)
 {
-       ios->max_val = ios->min_val = ios->samples = 0;
+       ios->min_val = -1ULL;
+       ios->max_val = ios->samples = 0;
        ios->mean.u.f = ios->S.u.f = 0;
 }