stat: move unified=both mixed allocation and calculation to new helper
authorNiklas Cassel <niklas.cassel@wdc.com>
Mon, 17 Jan 2022 15:50:54 +0000 (15:50 +0000)
committerJens Axboe <axboe@kernel.dk>
Mon, 17 Jan 2022 23:21:36 +0000 (16:21 -0700)
When using unified_rw_reporting=both, we need to print both the
per ddir stats, as well as the mixed stats.

In order to print both, the regular printing functions are responsible
for printing the per ddir stats from the unmodified struct thread_stat,
and show_mixed_ddir_status(), show_mixed_ddir_status_terse()
or add_mixed_ddir_status_json() is responsible for calculating and
printing the mixed stats.

In order to keep the original struct thread_stat intact, these three
functions have to allocate a new local thread_stat, where the mixed ddir
result can be stored before printing.

Move the allocation and calculation of this new struct thread_stat to a
new helper function, so that the code is easier to follow.

Signed-off-by: Niklas Cassel <niklas.cassel@wdc.com>
Link: https://lore.kernel.org/r/20220117155045.311453-3-Niklas.Cassel@wdc.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
stat.c

diff --git a/stat.c b/stat.c
index 42be600dc268a3161bba7e47727f46dd6a22e934..b08d2f25df0e7353222d3346a80b21a3100bca39 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -462,6 +462,35 @@ static void display_lat(const char *name, unsigned long long min,
        free(maxp);
 }
 
+static struct thread_stat *gen_mixed_ddir_stats_from_ts(struct thread_stat *ts)
+{
+       struct thread_stat *ts_lcl;
+
+       /*
+        * Handle aggregation of Reads (ddir = 0), Writes (ddir = 1), and
+        * Trims (ddir = 2)
+        */
+       ts_lcl = malloc(sizeof(struct thread_stat));
+       if (!ts_lcl) {
+               log_err("fio: failed to allocate local thread stat\n");
+               return NULL;
+       }
+
+       init_thread_stat(ts_lcl);
+
+       /* calculate mixed stats  */
+       ts_lcl->unified_rw_rep = UNIFIED_MIXED;
+       ts_lcl->lat_percentiles = ts->lat_percentiles;
+       ts_lcl->clat_percentiles = ts->clat_percentiles;
+       ts_lcl->slat_percentiles = ts->slat_percentiles;
+       ts_lcl->percentile_precision = ts->percentile_precision;
+       memcpy(ts_lcl->percentile_list, ts->percentile_list, sizeof(ts->percentile_list));
+
+       sum_thread_stats(ts_lcl, ts);
+
+       return ts_lcl;
+}
+
 static double convert_agg_kbytes_percent(struct group_run_stats *rs, int ddir, int mean)
 {
        double p_of_agg = 100.0;
@@ -644,26 +673,11 @@ static void show_mixed_ddir_status(struct group_run_stats *rs,
                                   struct thread_stat *ts,
                                   struct buf_output *out)
 {
-       struct thread_stat *ts_lcl;
+       struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
 
-       /*
-        * Handle aggregation of Reads (ddir = 0), Writes (ddir = 1), and
-        * Trims (ddir = 2)
-        */
-       ts_lcl = malloc(sizeof(struct thread_stat));
-       memset((void *)ts_lcl, 0, sizeof(struct thread_stat));
-       /* calculate mixed stats  */
-       ts_lcl->unified_rw_rep = UNIFIED_MIXED;
-       init_thread_stat_min_vals(ts_lcl);
-       ts_lcl->lat_percentiles = ts->lat_percentiles;
-       ts_lcl->clat_percentiles = ts->clat_percentiles;
-       ts_lcl->slat_percentiles = ts->slat_percentiles;
-       ts_lcl->percentile_precision = ts->percentile_precision;
-       memcpy(ts_lcl->percentile_list, ts->percentile_list, sizeof(ts->percentile_list));
-
-       sum_thread_stats(ts_lcl, ts);
+       if (ts_lcl)
+               show_ddir_status(rs, ts_lcl, DDIR_READ, out);
 
-       show_ddir_status(rs, ts_lcl, DDIR_READ, out);
        free(ts_lcl);
 }
 
@@ -1332,27 +1346,11 @@ static void show_mixed_ddir_status_terse(struct thread_stat *ts,
                                   struct group_run_stats *rs,
                                   int ver, struct buf_output *out)
 {
-       struct thread_stat *ts_lcl;
+       struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
 
-       /*
-        * Handle aggregation of Reads (ddir = 0), Writes (ddir = 1), and
-        * Trims (ddir = 2)
-        */
-       ts_lcl = malloc(sizeof(struct thread_stat));
-       memset((void *)ts_lcl, 0, sizeof(struct thread_stat));
-       /* calculate mixed stats  */
-       ts_lcl->unified_rw_rep = UNIFIED_MIXED;
-       init_thread_stat_min_vals(ts_lcl);
-       ts_lcl->lat_percentiles = ts->lat_percentiles;
-       ts_lcl->clat_percentiles = ts->clat_percentiles;
-       ts_lcl->slat_percentiles = ts->slat_percentiles;
-       ts_lcl->percentile_precision = ts->percentile_precision;
-       memcpy(ts_lcl->percentile_list, ts->percentile_list, sizeof(ts->percentile_list));
-       
-       sum_thread_stats(ts_lcl, ts);
+       if (ts_lcl)
+               show_ddir_status_terse(ts_lcl, rs, DDIR_READ, ver, out);
 
-       /* add the aggregated stats to json parent */
-       show_ddir_status_terse(ts_lcl, rs, DDIR_READ, ver, out);
        free(ts_lcl);
 }
 
@@ -1530,27 +1528,12 @@ static void add_ddir_status_json(struct thread_stat *ts,
 static void add_mixed_ddir_status_json(struct thread_stat *ts,
                struct group_run_stats *rs, struct json_object *parent)
 {
-       struct thread_stat *ts_lcl;
-
-       /*
-        * Handle aggregation of Reads (ddir = 0), Writes (ddir = 1), and
-        * Trims (ddir = 2)
-        */
-       ts_lcl = malloc(sizeof(struct thread_stat));
-       memset((void *)ts_lcl, 0, sizeof(struct thread_stat));
-       /* calculate mixed stats  */
-       ts_lcl->unified_rw_rep = UNIFIED_MIXED;
-       init_thread_stat_min_vals(ts_lcl);
-       ts_lcl->lat_percentiles = ts->lat_percentiles;
-       ts_lcl->clat_percentiles = ts->clat_percentiles;
-       ts_lcl->slat_percentiles = ts->slat_percentiles;
-       ts_lcl->percentile_precision = ts->percentile_precision;
-       memcpy(ts_lcl->percentile_list, ts->percentile_list, sizeof(ts->percentile_list));
-
-       sum_thread_stats(ts_lcl, ts);
+       struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
 
        /* add the aggregated stats to json parent */
-       add_ddir_status_json(ts_lcl, rs, DDIR_READ, parent);
+       if (ts_lcl)
+               add_ddir_status_json(ts_lcl, rs, DDIR_READ, parent);
+
        free(ts_lcl);
 }