Blame 0001-perf-stat-Show-average-value-on-multiple-runs.patch

Michel Lind a02e37
From c90a5d6209fa9a6e6cf25456828a5966c6e09206 Mon Sep 17 00:00:00 2001
Michel Lind a02e37
From: Namhyung Kim <namhyung@kernel.org>
Michel Lind a02e37
Date: Fri, 16 Jun 2023 00:32:11 -0700
Michel Lind a02e37
Subject: [PATCH] perf stat: Show average value on multiple runs
Michel Lind a02e37
Michel Lind a02e37
When -r option is used, perf stat runs the command multiple times and
Michel Lind a02e37
update stats in the evsel->stats.res_stats for global aggregation.  But
Michel Lind a02e37
the value is never used and the value it prints at the end is just the
Michel Lind a02e37
value from the last run.  I think we should print the average number of
Michel Lind a02e37
multiple runs.
Michel Lind a02e37
Michel Lind a02e37
Add evlist__copy_res_stats() to update the aggr counter (for display)
Michel Lind a02e37
using the values in the evsel->stats.res_stats.
Michel Lind a02e37
Michel Lind a02e37
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Michel Lind a02e37
Cc: Adrian Hunter <adrian.hunter@intel.com>
Michel Lind a02e37
Cc: Andi Kleen <ak@linux.intel.com>
Michel Lind a02e37
Cc: Ian Rogers <irogers@google.com>
Michel Lind a02e37
Cc: Ingo Molnar <mingo@kernel.org>
Michel Lind a02e37
Cc: Jiri Olsa <jolsa@kernel.org>
Michel Lind a02e37
Cc: Kan Liang <kan.liang@linux.intel.com>
Michel Lind a02e37
Cc: Peter Zijlstra <peterz@infradead.org>
Michel Lind a02e37
Link: https://lore.kernel.org/r/20230616073211.1057936-2-namhyung@kernel.org
Michel Lind a02e37
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Michel Lind a02e37
---
Michel Lind a02e37
 tools/perf/builtin-stat.c |  5 ++++-
Michel Lind a02e37
 tools/perf/util/stat.c    | 22 ++++++++++++++++++++++
Michel Lind a02e37
 tools/perf/util/stat.h    |  1 +
Michel Lind a02e37
 3 files changed, 27 insertions(+), 1 deletion(-)
Michel Lind a02e37
Michel Lind a02e37
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
Michel Lind a02e37
index 463643cda0d5..fc8bc04ee1f2 100644
Michel Lind a02e37
--- a/tools/perf/builtin-stat.c
Michel Lind a02e37
+++ b/tools/perf/builtin-stat.c
Michel Lind a02e37
@@ -2569,8 +2569,11 @@ int cmd_stat(int argc, const char **argv)
Michel Lind a02e37
 		}
Michel Lind a02e37
 	}
Michel Lind a02e37
 
Michel Lind a02e37
-	if (!forever && status != -1 && (!interval || stat_config.summary))
Michel Lind a02e37
+	if (!forever && status != -1 && (!interval || stat_config.summary)) {
Michel Lind a02e37
+		if (stat_config.run_count > 1)
Michel Lind a02e37
+			evlist__copy_res_stats(&stat_config, evsel_list);
Michel Lind a02e37
 		print_counters(NULL, argc, argv);
Michel Lind a02e37
+	}
Michel Lind a02e37
 
Michel Lind a02e37
 	evlist__finalize_ctlfd(evsel_list);
Michel Lind a02e37
 
Michel Lind a02e37
diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c
Michel Lind a02e37
index 0f7b8a8cdea6..967e583392c7 100644
Michel Lind a02e37
--- a/tools/perf/util/stat.c
Michel Lind a02e37
+++ b/tools/perf/util/stat.c
Michel Lind a02e37
@@ -264,6 +264,28 @@ void evlist__copy_prev_raw_counts(struct evlist *evlist)
Michel Lind a02e37
 		evsel__copy_prev_raw_counts(evsel);
Michel Lind a02e37
 }
Michel Lind a02e37
 
Michel Lind a02e37
+static void evsel__copy_res_stats(struct evsel *evsel)
Michel Lind a02e37
+{
Michel Lind a02e37
+	struct perf_stat_evsel *ps = evsel->stats;
Michel Lind a02e37
+
Michel Lind a02e37
+	/*
Michel Lind a02e37
+	 * For GLOBAL aggregation mode, it updates the counts for each run
Michel Lind a02e37
+	 * in the evsel->stats.res_stats.  See perf_stat_process_counter().
Michel Lind a02e37
+	 */
Michel Lind a02e37
+	*ps->aggr[0].counts.values = avg_stats(&ps->res_stats);
Michel Lind a02e37
+}
Michel Lind a02e37
+
Michel Lind a02e37
+void evlist__copy_res_stats(struct perf_stat_config *config, struct evlist *evlist)
Michel Lind a02e37
+{
Michel Lind a02e37
+	struct evsel *evsel;
Michel Lind a02e37
+
Michel Lind a02e37
+	if (config->aggr_mode != AGGR_GLOBAL)
Michel Lind a02e37
+		return;
Michel Lind a02e37
+
Michel Lind a02e37
+	evlist__for_each_entry(evlist, evsel)
Michel Lind a02e37
+		evsel__copy_res_stats(evsel);
Michel Lind a02e37
+}
Michel Lind a02e37
+
Michel Lind a02e37
 static size_t pkg_id_hash(long __key, void *ctx __maybe_unused)
Michel Lind a02e37
 {
Michel Lind a02e37
 	uint64_t *key = (uint64_t *) __key;
Michel Lind a02e37
diff --git a/tools/perf/util/stat.h b/tools/perf/util/stat.h
Michel Lind a02e37
index e35e188237c8..ff15bb3cbe19 100644
Michel Lind a02e37
--- a/tools/perf/util/stat.h
Michel Lind a02e37
+++ b/tools/perf/util/stat.h
Michel Lind a02e37
@@ -180,6 +180,7 @@ void evlist__save_aggr_prev_raw_counts(struct evlist *evlist);
Michel Lind a02e37
 
Michel Lind a02e37
 int evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr);
Michel Lind a02e37
 void evlist__reset_aggr_stats(struct evlist *evlist);
Michel Lind a02e37
+void evlist__copy_res_stats(struct perf_stat_config *config, struct evlist *evlist);
Michel Lind a02e37
 
Michel Lind a02e37
 int perf_stat_process_counter(struct perf_stat_config *config,
Michel Lind a02e37
 			      struct evsel *counter);
Michel Lind a02e37
-- 
Michel Lind a02e37
2.41.0
Michel Lind a02e37