From 5c0abd5e21df3b0ca7ee0833eb0ddf3f87ad4374 Mon Sep 17 00:00:00 2001 From: sribs Date: Fri, 24 Jul 2020 22:23:50 +0530 Subject: [PATCH] stat: stop triggerring division by zero on bandwidth lower than 1KBps Before the integer arithmetic is performed to obtain percentage of aggregation this conversion of rs->agg[ddir] into KBytes could potentially turn out to be 0. Adjust stat.c to no longer trigger division by zero on bandwidths greater than 0 but less than 1kilobyte a second. The fact that this is performed on multiple occasions, a function is preferred Signed-off-by: sribs --- stat.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/stat.c b/stat.c index b3951199..23657cee 100644 --- a/stat.c +++ b/stat.c @@ -414,6 +414,18 @@ static void display_lat(const char *name, unsigned long long min, free(maxp); } +static double convert_agg_kbytes_percent(struct group_run_stats *rs, int ddir, int mean) +{ + double p_of_agg = 100.0; + if (rs && rs->agg[ddir] > 1024) { + p_of_agg = mean * 100 / (double) (rs->agg[ddir] / 1024.0); + + if (p_of_agg > 100.0) + p_of_agg = 100.0; + } + return p_of_agg; +} + static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts, int ddir, struct buf_output *out) { @@ -551,11 +563,7 @@ static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts, else bw_str = "kB"; - if (rs->agg[ddir]) { - p_of_agg = mean * 100 / (double) (rs->agg[ddir] / 1024); - if (p_of_agg > 100.0) - p_of_agg = 100.0; - } + p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean); if (rs->unit_base == 1) { min *= 8.0; @@ -1376,11 +1384,7 @@ static void add_ddir_status_json(struct thread_stat *ts, } 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); - if (p_of_agg > 100.0) - p_of_agg = 100.0; - } + p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean); } else { min = max = 0; p_of_agg = mean = dev = 0.0; @@ -3130,3 +3134,4 @@ uint32_t *io_u_block_info(struct thread_data *td, struct io_u *io_u) assert(idx < td->ts.nr_block_infos); return info; } + -- 2.25.1