Fix bandwidth logging for mixed read/write workloads.
[fio.git] / stat.c
diff --git a/stat.c b/stat.c
index 851cc2c0ccf7188da3755e80deeaf8b95e34d83d..68744281a92de519cbb1f350fb459dfcca517cfd 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -440,12 +440,14 @@ static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
                                        ts->percentile_list);
        }
        if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
-               double p_of_agg;
+               double p_of_agg = 100.0;
                const char *bw_str = "KB";
 
-               p_of_agg = mean * 100 / (double) rs->agg[ddir];
-               if (p_of_agg > 100.0)
-                       p_of_agg = 100.0;
+               if (rs->agg[ddir]) {
+                       p_of_agg = mean * 100 / (double) rs->agg[ddir];
+                       if (p_of_agg > 100.0)
+                               p_of_agg = 100.0;
+               }
 
                if (mean > 999999.9) {
                        min /= 1000.0;
@@ -653,9 +655,14 @@ static void show_ddir_status_terse(struct thread_stat *ts,
                free(ovals);
 
        if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
-               double p_of_agg;
+               double p_of_agg = 100.0;
+
+               if (rs->agg[ddir]) {
+                       p_of_agg = mean * 100 / (double) rs->agg[ddir];
+                       if (p_of_agg > 100.0)
+                               p_of_agg = 100.0;
+               }
 
-               p_of_agg = mean * 100 / (double) rs->agg[ddir];
                log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
        } else
                log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
@@ -1284,16 +1291,27 @@ void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
        spent = mtime_since(&td->bw_sample_time, t);
        if (spent < td->o.bw_avg_time)
                return;
+       
+       /* 
+        * Compute both read and write rates for the interval.
+        */
+       for (ddir = DDIR_READ; ddir <= DDIR_WRITE; ddir++) {
+               uint64_t delta;
 
-       rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) *
-                       1000 / spent / 1024;
-       add_stat_sample(&ts->bw_stat[ddir], rate);
+               delta = td->this_io_bytes[ddir] - td->stat_io_bytes[ddir];
+               if (!delta)
+                       continue; /* No entries for interval */
 
-       if (td->bw_log)
-               add_log_sample(td, td->bw_log, rate, ddir, bs);
+               rate = delta * 1000 / spent / 1024;
+               add_stat_sample(&ts->bw_stat[ddir], rate);
+
+               if (td->bw_log)
+                       add_log_sample(td, td->bw_log, rate, ddir, bs);
+
+               td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
+       }
 
        fio_gettime(&td->bw_sample_time, NULL);
-       td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
 }
 
 void add_iops_sample(struct thread_data *td, enum fio_ddir ddir,