iolog: fix use-after-free of iolog_flush_data
[fio.git] / stat.c
diff --git a/stat.c b/stat.c
index 3070cef9dc895f220ef48107d130839fe746285e..6d8d4d06748291601056269f8d14832f42e76de5 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -1580,6 +1580,8 @@ void __show_run_stats(void)
                unsigned long long bw;
 
                ts = &threadstats[i];
+               if (ts->groupid == -1)
+                       continue;
                rs = &runstats[ts->groupid];
                rs->kb_base = ts->kb_base;
                rs->unit_base = ts->unit_base;
@@ -1942,35 +1944,35 @@ void reset_io_stats(struct thread_data *td)
        }
 }
 
-static void _add_stat_to_log(struct io_log *iolog, unsigned long elapsed)
+static void __add_stat_to_log(struct io_log *iolog, enum fio_ddir ddir,
+                             unsigned long elapsed, bool log_max)
 {
        /*
         * Note an entry in the log. Use the mean from the logged samples,
         * making sure to properly round up. Only write a log entry if we
         * had actual samples done.
         */
-       if (iolog->avg_window[DDIR_READ].samples) {
-               unsigned long mr;
+       if (iolog->avg_window[ddir].samples) {
+               unsigned long val;
 
-               mr = iolog->avg_window[DDIR_READ].mean.u.f + 0.50;
-               __add_log_sample(iolog, mr, DDIR_READ, 0, elapsed, 0);
-       }
-       if (iolog->avg_window[DDIR_WRITE].samples) {
-               unsigned long mw;
+               if (log_max)
+                       val = iolog->avg_window[ddir].max_val;
+               else
+                       val = iolog->avg_window[ddir].mean.u.f + 0.50;
 
-               mw = iolog->avg_window[DDIR_WRITE].mean.u.f + 0.50;
-               __add_log_sample(iolog, mw, DDIR_WRITE, 0, elapsed, 0);
+               __add_log_sample(iolog, val, ddir, 0, elapsed, 0);
        }
-       if (iolog->avg_window[DDIR_TRIM].samples) {
-               unsigned long mw;
 
-               mw = iolog->avg_window[DDIR_TRIM].mean.u.f + 0.50;
-               __add_log_sample(iolog, mw, DDIR_TRIM, 0, elapsed, 0);
-       }
+       reset_io_stat(&iolog->avg_window[ddir]);
+}
+
+static void _add_stat_to_log(struct io_log *iolog, unsigned long elapsed,
+                            bool log_max)
+{
+       int ddir;
 
-       reset_io_stat(&iolog->avg_window[DDIR_READ]);
-       reset_io_stat(&iolog->avg_window[DDIR_WRITE]);
-       reset_io_stat(&iolog->avg_window[DDIR_TRIM]);
+       for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
+               __add_stat_to_log(iolog, ddir, elapsed, log_max);
 }
 
 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
@@ -2006,7 +2008,7 @@ static void add_log_sample(struct thread_data *td, struct io_log *iolog,
        if (this_window < iolog->avg_msec)
                return;
 
-       _add_stat_to_log(iolog, elapsed);
+       _add_stat_to_log(iolog, elapsed, td->o.log_max != 0);
 
        iolog->avg_last = elapsed;
 }
@@ -2018,15 +2020,15 @@ void finalize_logs(struct thread_data *td)
        elapsed = mtime_since_now(&td->epoch);
 
        if (td->clat_log)
-               _add_stat_to_log(td->clat_log, elapsed);
+               _add_stat_to_log(td->clat_log, elapsed, td->o.log_max != 0);
        if (td->slat_log)
-               _add_stat_to_log(td->slat_log, elapsed);
+               _add_stat_to_log(td->slat_log, elapsed, td->o.log_max != 0);
        if (td->lat_log)
-               _add_stat_to_log(td->lat_log, elapsed);
+               _add_stat_to_log(td->lat_log, elapsed, td->o.log_max != 0);
        if (td->bw_log)
-               _add_stat_to_log(td->bw_log, elapsed);
+               _add_stat_to_log(td->bw_log, elapsed, td->o.log_max != 0);
        if (td->iops_log)
-               _add_stat_to_log(td->iops_log, elapsed);
+               _add_stat_to_log(td->iops_log, elapsed, td->o.log_max != 0);
 }
 
 void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)