Skip to contentSkip to conversation

Fix incorrect counts when count the same uncore event multiple times

PATCH Superseded Chun-Tse Shao 2 participants 4 messages Mar 26, 2025 – Mar 27, 2025 perf events

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Fix incorrect counts when count the same uncore event multiple times

Let's take a look an example, the machine is SKX with 6 IMC devices.

perf stat -e clockticks,clockticks -I 1000
# time counts unit events

       1.001127430      6,901,503,174      uncore_imc_0/clockticks/
       1.001127430      3,940,896,301      uncore_imc_0/clockticks/
       2.002649722        988,376,876      uncore_imc_0/clockticks/
       2.002649722        988,376,141      uncore_imc_0/clockticks/
       3.004071319      1,000,292,675      uncore_imc_0/clockticks/
       3.004071319      1,000,294,160      uncore_imc_0/clockticks/

1) The events name should not be uniquified.
2) The initial count for the first clockticks is doubled.
3) Subsequent count only report for the first IMC device.

The first patch fixes 1) and 3), and the second patch fixes 2).

After these fix:

perf stat -e clockticks,clockticks -I 1000
# time counts unit events

       1.001127586      4,126,938,857      clockticks
       1.001127586      4,121,564,277      clockticks
       2.001686014      3,953,806,350      clockticks
       2.001686014      3,953,809,541      clockticks
       3.003121403      4,137,750,252      clockticks
       3.003121403      4,137,749,048      clockticks

I also tested -A, --per-socket, --per-die and --per-core, all looks good.

Ian Rogers (2):
perf evlist: Make uniquifying counter names consistent perf parse-events: Use wildcard processing to set an event to merge
into

 tools/perf/builtin-record.c    |   7 +-
 tools/perf/builtin-top.c       |   7 +-
 tools/perf/util/evlist.c       |  66 +++++++++-----
 tools/perf/util/evlist.h       |   3 +-
 tools/perf/util/evsel.c        | 116 ++++++++++++++++++++++++-
 tools/perf/util/evsel.h        |  11 ++-
 tools/perf/util/parse-events.c |  45 ++++++----
 tools/perf/util/stat-display.c | 151 +--------------------------------
 tools/perf/util/stat.c         |  40 +--------
 9 files changed, 214 insertions(+), 232 deletions(-)

--
2.49.0.472.ge94155a9ec-goog

#1 lore raw

PATCH 1/2 perf evlist: Make uniquifying counter names consistent

From: Ian Rogers <[email protected]>

From: Ian Rogers <[email protected]>

perf stat has different uniquification logic to perf record and perf top. In the case of perf record and perf top all hybrid event names are uniquified. Perf stat is more disciplined respecting name config terms, libpfm4 events, etc. Perf stat will uniquify hybrid events and the non-core PMU cases shouldn't apply to perf record or perf top. For consistency, remove the uniquification for perf record and perf top and reuse the perf stat uniquification, making the code more globally visible for this.

Fix the detection of cross-PMU for disabling uniquify by correctly setting last_pmu. When setting uniquify on an evsel, make sure the PMUs between the 2 considered events differ otherwise the uniquify isn't adding value.

---
 tools/perf/builtin-record.c    |   7 +-
 tools/perf/builtin-top.c       |   7 +-
 tools/perf/util/evlist.c       |  66 ++++++++++-----
 tools/perf/util/evlist.h       |   3 +-
 tools/perf/util/evsel.c        | 113 +++++++++++++++++++++++++
 tools/perf/util/evsel.h        |   4 +
 tools/perf/util/stat-display.c | 149 +--------------------------------
 7 files changed, 176 insertions(+), 173 deletions(-)
tools/perf/builtin-record.c +6 -1
index ba20bf7c011d..83f8f9728e12 100644
@@ -26,6 +26,7 @@
 #include "util/target.h"
 #include "util/session.h"
 #include "util/tool.h"
+#include "util/stat.h"
 #include "util/symbol.h"
 #include "util/record.h"
 #include "util/cpumap.h"
@@ -2483,7 +2484,11 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 		pr_warning("WARNING: --timestamp-filename option is not available in pipe mode.\n");
 	}
 
-	evlist__uniquify_name(rec->evlist);
+	/*
+	 * Use global stat_config that is zero meaning aggr_mode is AGGR_NONE
+	 * and hybrid_merge is false.
+	 */
+	evlist__uniquify_evsel_names(rec->evlist, &stat_config);
 
 	evlist__config(rec->evlist, opts, &callchain_param);
 
tools/perf/builtin-top.c +6 -1
index 1061f4eebc3f..b7c8ecafa2f2 100644
@@ -35,6 +35,7 @@
 #include "util/mmap.h"
 #include "util/session.h"
 #include "util/thread.h"
+#include "util/stat.h"
 #include "util/symbol.h"
 #include "util/synthetic-events.h"
 #include "util/top.h"
@@ -1309,7 +1310,11 @@ static int __cmd_top(struct perf_top *top)
 		}
 	}
 
-	evlist__uniquify_name(top->evlist);
+	/*
+	 * Use global stat_config that is zero meaning aggr_mode is AGGR_NONE
+	 * and hybrid_merge is false.
+	 */
+	evlist__uniquify_evsel_names(top->evlist, &stat_config);
 	ret = perf_top__start_counters(top);
 	if (ret)
 		return ret;
tools/perf/util/evlist.c +44 -22

78 lines not on this page · raw message

tools/perf/util/evlist.h +2 -1
index edcbf1c10e92..7cfdaf543214 100644
@@ -19,6 +19,7 @@
 struct pollfd;
 struct thread_map;
 struct perf_cpu_map;
+struct perf_stat_config;
 struct record_opts;
 struct target;
 
@@ -433,7 +434,7 @@ struct evsel *evlist__find_evsel(struct evlist *evlist, int idx);
 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf);
 void evlist__check_mem_load_aux(struct evlist *evlist);
 void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list);
-void evlist__uniquify_name(struct evlist *evlist);
+void evlist__uniquify_evsel_names(struct evlist *evlist, const struct perf_stat_config *config);
 bool evlist__has_bpf_output(struct evlist *evlist);
 bool evlist__needs_bpf_sb_event(struct evlist *evlist);
 
tools/perf/util/evsel.c +113 -0

116 lines not on this page · raw message

tools/perf/util/evsel.h +4 -0
index aae431d63d64..76ccb7c7e8c2 100644
@@ -16,6 +16,7 @@
 struct bpf_object;
 struct cgroup;
 struct perf_counts;
+struct perf_stat_config;
 struct perf_stat_evsel;
 union perf_event;
 struct bpf_counter_ops;
@@ -542,6 +543,9 @@ void evsel__remove_from_group(struct evsel *evsel, struct evsel *leader);
 
 bool arch_evsel__must_be_in_group(const struct evsel *evsel);
 
+bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_stat_config *config);
+void evsel__uniquify_counter(struct evsel *counter);
+
 /*
  * Macro to swap the bit-field postition and size.
  * Used when,
tools/perf/util/stat-display.c +1 -148

173 lines not on this page · raw message

Signature · 1 line
2.49.0.472.ge94155a9ec-goog
#2 lore raw

PATCH 2/2 perf parse-events: Use wildcard processing to set an event to merge into

In reply to Chun-Tse Shao

From: Ian Rogers <[email protected]>

From: Ian Rogers <[email protected]>

The merge stat code fails for uncore events if they are repeated twice, for example perf stat -e clockticks,clockticks -I 1000 as the counts of the second set of uncore events will be merged into the first counter. Reimplement the logic to have a first_wildcard_match so that merged later events correctly merge into the first wildcard event that they will be aggregated into.

---
 tools/perf/util/evsel.c        | 15 ++++++------
 tools/perf/util/evsel.h        |  7 ++++--
 tools/perf/util/parse-events.c | 45 ++++++++++++++++++++--------------
 tools/perf/util/stat-display.c |  2 +-
 tools/perf/util/stat.c         | 40 +++---------------------------
 5 files changed, 44 insertions(+), 65 deletions(-)
tools/perf/util/evsel.c +8 -7
index f00cfff119aa..84e56691c49f 100644
@@ -552,11 +552,11 @@ struct evsel *evsel__clone(struct evsel *dest, struct evsel *orig)
 
 	evsel->exclude_GH = orig->exclude_GH;
 	evsel->sample_read = orig->sample_read;
-	evsel->auto_merge_stats = orig->auto_merge_stats;
 	evsel->collect_stat = orig->collect_stat;
 	evsel->weak_group = orig->weak_group;
 	evsel->use_config_name = orig->use_config_name;
 	evsel->pmu = orig->pmu;
+	evsel->first_wildcard_match = orig->first_wildcard_match;
 
 	if (evsel__copy_config_terms(evsel, orig) < 0)
 		goto out_err;
@@ -3949,11 +3949,6 @@ bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_stat_con
 		return true;
 	}
 
-	if (counter->merged_stat) {
-		/* Counter won't be shown. */
-		return false;
-	}
-
 	if (counter->use_config_name || counter->is_libpfm_event) {
 		/* Original name will be used. */
 		return false;
@@ -3980,6 +3975,12 @@ bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_stat_con
 		/* Always unique with no aggregation. */
 		counter->needs_uniquify = true;
 		return true;
+	} else if (counter->first_wildcard_match != NULL) {
+		/*
+		 * It is under aggregation mode and the counter won't be shown (the counts should be
+		 * aggreated to the `first_wildcard_match` evsel)
+		 */
+		return false;
 	}
 
 	/*
@@ -3987,7 +3988,7 @@ bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_stat_con
 	 * uniquify is necessary.
 	 */
 	evlist__for_each_entry(counter->evlist, evsel) {
-		if (evsel == counter || evsel->merged_stat || evsel->pmu == counter->pmu)
+		if (evsel == counter || evsel->first_wildcard_match || evsel->pmu == counter->pmu)
 			continue;
 
 		if (evsel__name_is(counter, evsel__name(evsel))) {
tools/perf/util/evsel.h +5 -2
index 76ccb7c7e8c2..2cc046894e30 100644
@@ -70,6 +70,11 @@ struct evsel {
 		const char		*unit;
 		struct cgroup		*cgrp;
 		const char		*metric_id;
+		/*
+		 * This point to the first evsel with the same name, intended to store the
+		 * aggregated counts in aggregation mode.
+		 */
+		struct evsel		*first_wildcard_match;
 		/* parse modifier helper */
 		int			exclude_GH;
 		int			sample_read;
@@ -78,7 +83,6 @@ struct evsel {
 		bool			percore;
 		bool			precise_max;
 		bool			is_libpfm_event;
-		bool			auto_merge_stats;
 		bool			collect_stat;
 		bool			weak_group;
 		bool			bpf_counter;
@@ -115,7 +119,6 @@ struct evsel {
 	bool			ignore_missing_thread;
 	bool			forced_leader;
 	bool			cmdline_group_boundary;
-	bool			merged_stat;
 	bool			reset_group;
 	bool			errored;
 	bool			needs_auxtrace_mmap;
tools/perf/util/parse-events.c +27 -18

167 lines not on this page · raw message

tools/perf/util/stat-display.c +1 -1
index d427e0ca98a1..7e2ec33e8c99 100644
@@ -1011,7 +1011,7 @@ static void print_counter_aggrdata(struct perf_stat_config *config,
 	os->evsel = counter;
 
 	/* Skip already merged uncore/hybrid events */
-	if (counter->merged_stat)
+	if (config->aggr_mode != AGGR_NONE && counter->first_wildcard_match)
 		return;
 
 	val = aggr->counts.val;
tools/perf/util/stat.c +3 -37
index 1f7abd8754c7..355a7d5c8ab8 100644
@@ -535,35 +535,6 @@ static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias)
 
 	return 0;
 }
-/*
- * Events should have the same name, scale, unit, cgroup but on different core
- * PMUs or on different but matching uncore PMUs.
- */
-static bool evsel__is_alias(struct evsel *evsel_a, struct evsel *evsel_b)
-{
-	if (strcmp(evsel__name(evsel_a), evsel__name(evsel_b)))
-		return false;
-
-	if (evsel_a->scale != evsel_b->scale)
-		return false;
-
-	if (evsel_a->cgrp != evsel_b->cgrp)
-		return false;
-
-	if (strcmp(evsel_a->unit, evsel_b->unit))
-		return false;
-
-	if (evsel__is_clock(evsel_a) != evsel__is_clock(evsel_b))
-		return false;
-
-	if (evsel_a->pmu == evsel_b->pmu || evsel_a->pmu == NULL || evsel_b->pmu == NULL)
-		return false;
-
-	if (evsel_a->pmu->is_core)
-		return evsel_b->pmu->is_core;
-
-	return perf_pmu__name_no_suffix_match(evsel_a->pmu, evsel_b->pmu->name);
-}
 
 static void evsel__merge_aliases(struct evsel *evsel)
 {
@@ -572,10 +543,9 @@ static void evsel__merge_aliases(struct evsel *evsel)
 
 	alias = list_prepare_entry(evsel, &(evlist->core.entries), core.node);
 	list_for_each_entry_continue(alias, &evlist->core.entries, core.node) {
-		/* Merge the same events on different PMUs. */
-		if (evsel__is_alias(evsel, alias)) {
+		if (alias->first_wildcard_match == evsel) {
+			/* Merge the same events on different PMUs. */
 			evsel__merge_aggr_counters(evsel, alias);
-			alias->merged_stat = true;
 		}
 	}
 }
@@ -588,11 +558,7 @@ static bool evsel__should_merge_hybrid(const struct evsel *evsel,
 
 static void evsel__merge_stats(struct evsel *evsel, struct perf_stat_config *config)
 {
-	/* this evsel is already merged */
-	if (evsel->merged_stat)
-		return;
-
-	if (evsel->auto_merge_stats || evsel__should_merge_hybrid(evsel, config))
+	if (!evsel->pmu || !evsel->pmu->is_core || evsel__should_merge_hybrid(evsel, config))
 		evsel__merge_aliases(evsel);
 }
 
Signature · 1 line
2.49.0.472.ge94155a9ec-goog
reviewer
#3 lore raw
In reply to Chun-Tse Shao
Quoted 29 lines · Chun-Tse Shao

Let's take a look an example, the machine is SKX with 6 IMC devices.

perf stat -e clockticks,clockticks -I 1000
# time counts unit events

       1.001127430      6,901,503,174      uncore_imc_0/clockticks/
       1.001127430      3,940,896,301      uncore_imc_0/clockticks/
       2.002649722        988,376,876      uncore_imc_0/clockticks/
       2.002649722        988,376,141      uncore_imc_0/clockticks/
       3.004071319      1,000,292,675      uncore_imc_0/clockticks/
       3.004071319      1,000,294,160      uncore_imc_0/clockticks/

1) The events name should not be uniquified.
2) The initial count for the first clockticks is doubled.
3) Subsequent count only report for the first IMC device.

The first patch fixes 1) and 3), and the second patch fixes 2).

After these fix:

perf stat -e clockticks,clockticks -I 1000
# time counts unit events

       1.001127586      4,126,938,857      clockticks
       1.001127586      4,121,564,277      clockticks
       2.001686014      3,953,806,350      clockticks
       2.001686014      3,953,809,541      clockticks
       3.003121403      4,137,750,252      clockticks
       3.003121403      4,137,749,048      clockticks
Quoted 2 lines

I also tested -A, --per-socket, --per-die and --per-core, all looks good.

Thanks CT, I tested on hybrid and it looks good. I did notice a regression with hwmon:

Before:

```
$ perf stat -e data_read,temp1 -a sleep 0.1

Performance counter stats for 'system wide':

           212.12 MiB  data_read
18,446,744,073,709,284.00 'C   hwmon_acpitz/temp1/
            46.00 'C   hwmon_coretemp/temp1/
            32.00 'C   hwmon_iwlwifi_1/temp1/
            40.85 'C   hwmon_nvme/temp1/
            47.00 'C   hwmon_spd5118/temp1/
```

After:

```
$  perf stat -e data_read,temp1 -a sleep 0.1

Performance counter stats for 'system wide':

           213.08 MiB  data_read
18,446,744,073,709,448.00 'C   temp1
```

So we're not uniquifying the hwmon events any more, I'll look into a fix. I also need to open a bug to use my acptiz device to solve the world's energy problems as it is currently running with 5% of the surface temperature of the Sun.

Thanks,
Ian

Quoted 19 lines

Ian Rogers (2):
perf evlist: Make uniquifying counter names consistent perf parse-events: Use wildcard processing to set an event to merge
into

 tools/perf/builtin-record.c    |   7 +-
 tools/perf/builtin-top.c       |   7 +-
 tools/perf/util/evlist.c       |  66 +++++++++-----
 tools/perf/util/evlist.h       |   3 +-
 tools/perf/util/evsel.c        | 116 ++++++++++++++++++++++++-
 tools/perf/util/evsel.h        |  11 ++-
 tools/perf/util/parse-events.c |  45 ++++++----
 tools/perf/util/stat-display.c | 151 +--------------------------------
 tools/perf/util/stat.c         |  40 +--------
 9 files changed, 214 insertions(+), 232 deletions(-)

--
2.49.0.472.ge94155a9ec-goog