stat: Fix another memory leak in add_ddir_status_json()
[fio.git] / stat.c
diff --git a/stat.c b/stat.c
index ec75de241be623756c88c1b88edb69473a0dfb97..4d3b728c3815dac1614eef6bee10475e0a617bd1 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -15,6 +15,7 @@
 #include "helper_thread.h"
 #include "smalloc.h"
 #include "zbd.h"
+#include "oslib/asprintf.h"
 
 #define LOG_MSEC_SLACK 1
 
@@ -139,7 +140,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;
 
@@ -159,7 +159,11 @@ unsigned int calc_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
         * isn't a worry. Also note that this does not work for NaN values.
         */
        if (len > 1)
-               qsort((void *)plist, len, sizeof(plist[0]), double_cmp);
+               qsort(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
@@ -167,14 +171,9 @@ unsigned int calc_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
        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 +188,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;
 }
@@ -257,8 +259,7 @@ static void show_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
        }
 
 out:
-       if (ovals)
-               free(ovals);
+       free(ovals);
 }
 
 bool calc_lat(struct io_stat *is, unsigned long long *min,
@@ -682,7 +683,7 @@ static int calc_block_percentiles(int nr_block_infos, uint32_t *block_infos,
         * isn't a worry. Also note that this does not work for NaN values.
         */
        if (len > 1)
-               qsort((void *)plist, len, sizeof(plist[0]), double_cmp);
+               qsort(plist, len, sizeof(plist[0]), double_cmp);
 
        /* Start only after the uninit entries end */
        for (nr_uninit = 0;
@@ -784,6 +785,218 @@ static void show_ss_normal(struct thread_stat *ts, struct buf_output *out)
        free(p2);
 }
 
+static void show_agg_stats(struct disk_util_agg *agg, int terse,
+                          struct buf_output *out)
+{
+       if (!agg->slavecount)
+               return;
+
+       if (!terse) {
+               log_buf(out, ", aggrios=%llu/%llu, aggrmerge=%llu/%llu, "
+                        "aggrticks=%llu/%llu, aggrin_queue=%llu, "
+                        "aggrutil=%3.2f%%",
+                       (unsigned long long) agg->ios[0] / agg->slavecount,
+                       (unsigned long long) agg->ios[1] / agg->slavecount,
+                       (unsigned long long) agg->merges[0] / agg->slavecount,
+                       (unsigned long long) agg->merges[1] / agg->slavecount,
+                       (unsigned long long) agg->ticks[0] / agg->slavecount,
+                       (unsigned long long) agg->ticks[1] / agg->slavecount,
+                       (unsigned long long) agg->time_in_queue / agg->slavecount,
+                       agg->max_util.u.f);
+       } else {
+               log_buf(out, ";slaves;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
+                       (unsigned long long) agg->ios[0] / agg->slavecount,
+                       (unsigned long long) agg->ios[1] / agg->slavecount,
+                       (unsigned long long) agg->merges[0] / agg->slavecount,
+                       (unsigned long long) agg->merges[1] / agg->slavecount,
+                       (unsigned long long) agg->ticks[0] / agg->slavecount,
+                       (unsigned long long) agg->ticks[1] / agg->slavecount,
+                       (unsigned long long) agg->time_in_queue / agg->slavecount,
+                       agg->max_util.u.f);
+       }
+}
+
+static void aggregate_slaves_stats(struct disk_util *masterdu)
+{
+       struct disk_util_agg *agg = &masterdu->agg;
+       struct disk_util_stat *dus;
+       struct flist_head *entry;
+       struct disk_util *slavedu;
+       double util;
+
+       flist_for_each(entry, &masterdu->slaves) {
+               slavedu = flist_entry(entry, struct disk_util, slavelist);
+               dus = &slavedu->dus;
+               agg->ios[0] += dus->s.ios[0];
+               agg->ios[1] += dus->s.ios[1];
+               agg->merges[0] += dus->s.merges[0];
+               agg->merges[1] += dus->s.merges[1];
+               agg->sectors[0] += dus->s.sectors[0];
+               agg->sectors[1] += dus->s.sectors[1];
+               agg->ticks[0] += dus->s.ticks[0];
+               agg->ticks[1] += dus->s.ticks[1];
+               agg->time_in_queue += dus->s.time_in_queue;
+               agg->slavecount++;
+
+               util = (double) (100 * dus->s.io_ticks / (double) slavedu->dus.s.msec);
+               /* System utilization is the utilization of the
+                * component with the highest utilization.
+                */
+               if (util > agg->max_util.u.f)
+                       agg->max_util.u.f = util;
+
+       }
+
+       if (agg->max_util.u.f > 100.0)
+               agg->max_util.u.f = 100.0;
+}
+
+void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
+                    int terse, struct buf_output *out)
+{
+       double util = 0;
+
+       if (dus->s.msec)
+               util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
+       if (util > 100.0)
+               util = 100.0;
+
+       if (!terse) {
+               if (agg->slavecount)
+                       log_buf(out, "  ");
+
+               log_buf(out, "  %s: ios=%llu/%llu, merge=%llu/%llu, "
+                        "ticks=%llu/%llu, in_queue=%llu, util=%3.2f%%",
+                               dus->name,
+                               (unsigned long long) dus->s.ios[0],
+                               (unsigned long long) dus->s.ios[1],
+                               (unsigned long long) dus->s.merges[0],
+                               (unsigned long long) dus->s.merges[1],
+                               (unsigned long long) dus->s.ticks[0],
+                               (unsigned long long) dus->s.ticks[1],
+                               (unsigned long long) dus->s.time_in_queue,
+                               util);
+       } else {
+               log_buf(out, ";%s;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
+                               dus->name,
+                               (unsigned long long) dus->s.ios[0],
+                               (unsigned long long) dus->s.ios[1],
+                               (unsigned long long) dus->s.merges[0],
+                               (unsigned long long) dus->s.merges[1],
+                               (unsigned long long) dus->s.ticks[0],
+                               (unsigned long long) dus->s.ticks[1],
+                               (unsigned long long) dus->s.time_in_queue,
+                               util);
+       }
+
+       /*
+        * If the device has slaves, aggregate the stats for
+        * those slave devices also.
+        */
+       show_agg_stats(agg, terse, out);
+
+       if (!terse)
+               log_buf(out, "\n");
+}
+
+void json_array_add_disk_util(struct disk_util_stat *dus,
+               struct disk_util_agg *agg, struct json_array *array)
+{
+       struct json_object *obj;
+       double util = 0;
+
+       if (dus->s.msec)
+               util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
+       if (util > 100.0)
+               util = 100.0;
+
+       obj = json_create_object();
+       json_array_add_value_object(array, obj);
+
+       json_object_add_value_string(obj, "name", dus->name);
+       json_object_add_value_int(obj, "read_ios", dus->s.ios[0]);
+       json_object_add_value_int(obj, "write_ios", dus->s.ios[1]);
+       json_object_add_value_int(obj, "read_merges", dus->s.merges[0]);
+       json_object_add_value_int(obj, "write_merges", dus->s.merges[1]);
+       json_object_add_value_int(obj, "read_ticks", dus->s.ticks[0]);
+       json_object_add_value_int(obj, "write_ticks", dus->s.ticks[1]);
+       json_object_add_value_int(obj, "in_queue", dus->s.time_in_queue);
+       json_object_add_value_float(obj, "util", util);
+
+       /*
+        * If the device has slaves, aggregate the stats for
+        * those slave devices also.
+        */
+       if (!agg->slavecount)
+               return;
+       json_object_add_value_int(obj, "aggr_read_ios",
+                               agg->ios[0] / agg->slavecount);
+       json_object_add_value_int(obj, "aggr_write_ios",
+                               agg->ios[1] / agg->slavecount);
+       json_object_add_value_int(obj, "aggr_read_merges",
+                               agg->merges[0] / agg->slavecount);
+       json_object_add_value_int(obj, "aggr_write_merge",
+                               agg->merges[1] / agg->slavecount);
+       json_object_add_value_int(obj, "aggr_read_ticks",
+                               agg->ticks[0] / agg->slavecount);
+       json_object_add_value_int(obj, "aggr_write_ticks",
+                               agg->ticks[1] / agg->slavecount);
+       json_object_add_value_int(obj, "aggr_in_queue",
+                               agg->time_in_queue / agg->slavecount);
+       json_object_add_value_float(obj, "aggr_util", agg->max_util.u.f);
+}
+
+static void json_object_add_disk_utils(struct json_object *obj,
+                                      struct flist_head *head)
+{
+       struct json_array *array = json_create_array();
+       struct flist_head *entry;
+       struct disk_util *du;
+
+       json_object_add_value_array(obj, "disk_util", array);
+
+       flist_for_each(entry, head) {
+               du = flist_entry(entry, struct disk_util, list);
+
+               aggregate_slaves_stats(du);
+               json_array_add_disk_util(&du->dus, &du->agg, array);
+       }
+}
+
+void show_disk_util(int terse, struct json_object *parent,
+                   struct buf_output *out)
+{
+       struct flist_head *entry;
+       struct disk_util *du;
+       bool do_json;
+
+       if (!is_running_backend())
+               return;
+
+       if (flist_empty(&disk_list)) {
+               return;
+       }
+
+       if ((output_format & FIO_OUTPUT_JSON) && parent)
+               do_json = true;
+       else
+               do_json = false;
+
+       if (!terse && !do_json)
+               log_buf(out, "\nDisk stats (read/write):\n");
+
+       if (do_json)
+               json_object_add_disk_utils(parent, &disk_list);
+       else if (output_format & ~(FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS)) {
+               flist_for_each(entry, &disk_list) {
+                       du = flist_entry(entry, struct disk_util, list);
+
+                       aggregate_slaves_stats(du);
+                       print_disk_util(&du->dus, &du->agg, terse, out);
+               }
+       }
+}
+
 static void show_thread_status_normal(struct thread_stat *ts,
                                      struct group_run_stats *rs,
                                      struct buf_output *out)
@@ -954,8 +1167,7 @@ static void show_ddir_status_terse(struct thread_stat *ts,
        else
                log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
 
-       if (ovals)
-               free(ovals);
+       free(ovals);
 
        bw_stat = calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev);
        if (bw_stat) {
@@ -994,7 +1206,8 @@ static void add_ddir_status_json(struct thread_stat *ts,
        double mean, dev, iops;
        unsigned int len;
        int i;
-       struct json_object *dir_object, *tmp_object, *percentile_object, *clat_bins_object = NULL;
+       struct json_object *dir_object, *tmp_object, *percentile_object = NULL,
+               *clat_bins_object = NULL;
        char buf[120];
        double p_of_agg = 100.0;
 
@@ -1089,28 +1302,34 @@ static void add_ddir_status_json(struct thread_stat *ts,
        } else
                len = 0;
 
-       percentile_object = json_create_object();
-       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]);
+       if (ts->clat_percentiles) {
+               percentile_object = json_create_object();
+               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, buf,
+                                                 ovals[i]);
+               }
        }
 
-       if (output_format & FIO_OUTPUT_JSON_PLUS) {
+       free(ovals);
+
+       if (output_format & FIO_OUTPUT_JSON_PLUS && ts->clat_percentiles) {
                clat_bins_object = json_create_object();
-               if (ts->clat_percentiles)
-                       json_object_add_value_object(tmp_object, "bins", clat_bins_object);
+               json_object_add_value_object(tmp_object, "bins",
+                                            clat_bins_object);
 
                for(i = 0; i < FIO_IO_U_PLAT_NR; i++) {
                        if (ddir_rw(ddir)) {
                                if (ts->io_u_plat[ddir][i]) {
                                        snprintf(buf, sizeof(buf), "%llu", plat_idx_to_val(i));
-                                       json_object_add_value_int(clat_bins_object, (const char *)buf, ts->io_u_plat[ddir][i]);
+                                       json_object_add_value_int(clat_bins_object, buf, ts->io_u_plat[ddir][i]);
                                }
                        } else {
                                if (ts->io_u_sync_plat[i]) {
                                        snprintf(buf, sizeof(buf), "%llu", plat_idx_to_val(i));
-                                       json_object_add_value_int(clat_bins_object, (const char *)buf, ts->io_u_sync_plat[i]);
+                                       json_object_add_value_int(clat_bins_object, buf, ts->io_u_sync_plat[i]);
                                }
                        }
                }
@@ -1129,12 +1348,11 @@ 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);
 
-       if (ovals)
-               free(ovals);
-
        if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
                if (rs->agg[ddir]) {
                        p_of_agg = mean * 100 / (double) (rs->agg[ddir] / 1024);
@@ -1240,12 +1458,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");
 }
@@ -1443,7 +1662,7 @@ static struct json_object *show_thread_status_json(struct thread_stat *ts,
                                snprintf(buf, sizeof(buf), "%f",
                                         ts->percentile_list[i].u.f);
                                json_object_add_value_int(percentile_object,
-                                                         (const char *)buf,
+                                                         buf,
                                                          percentiles[i]);
                        }
 
@@ -1678,11 +1897,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];
 
@@ -1820,10 +2042,11 @@ void __show_run_stats(void)
                        /*
                         * These are per-group shared already
                         */
-                       strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE - 1);
+                       snprintf(ts->name, sizeof(ts->name), "%s", td->o.name);
                        if (td->o.description)
-                               strncpy(ts->description, td->o.description,
-                                               FIO_JOBDESC_SIZE - 1);
+                               snprintf(ts->description,
+                                        sizeof(ts->description), "%s",
+                                        td->o.description);
                        else
                                memset(ts->description, 0, FIO_JOBDESC_SIZE);
 
@@ -1860,12 +2083,12 @@ void __show_run_stats(void)
                        if (!td->error && td->o.continue_on_error &&
                            td->first_error) {
                                ts->error = td->first_error;
-                               ts->verror[sizeof(ts->verror) - 1] = '\0';
-                               strncpy(ts->verror, td->verror, sizeof(ts->verror) - 1);
+                               snprintf(ts->verror, sizeof(ts->verror), "%s",
+                                        td->verror);
                        } else  if (td->error) {
                                ts->error = td->error;
-                               ts->verror[sizeof(ts->verror) - 1] = '\0';
-                               strncpy(ts->verror, td->verror, sizeof(ts->verror) - 1);
+                               snprintf(ts->verror, sizeof(ts->verror), "%s",
+                                        td->verror);
                        }
                }
 
@@ -2114,6 +2337,9 @@ static int check_status_file(void)
        }
        if (temp_dir == NULL)
                temp_dir = "/tmp";
+#ifdef __COVERITY__
+       __coverity_tainted_data_sanitize__(temp_dir);
+#endif
 
        snprintf(fio_status_file_path, sizeof(fio_status_file_path), "%s/%s", temp_dir, FIO_STATUS_FILE);
 
@@ -2351,7 +2577,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;
 }
 
@@ -2570,7 +2797,7 @@ void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
                        io_u_plat = (uint64_t *) td->ts.io_u_plat[ddir];
                        dst = malloc(sizeof(struct io_u_plat_entry));
                        memcpy(&(dst->io_u_plat), io_u_plat,
-                               FIO_IO_U_PLAT_NR * sizeof(unsigned int));
+                               FIO_IO_U_PLAT_NR * sizeof(uint64_t));
                        flist_add(&dst->list, &hw->list);
                        __add_log_sample(iolog, sample_plat(dst), ddir, bs,
                                                elapsed, offset);