Skip to contentSkip to conversation

Fix incorrect counts when count the same uncore event multiple times

PATCH Superseded Chun-Tse Shao 3 participants 10 messages Mar 27, 2025 – May 12, 2025 perf events

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 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 tested hybrid-merge and hwmon, all looks good as well.

Chun-Tse Shao (1):
perf test: Add stat uniquifying test

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 +-
 .../tests/shell/stat+event_uniquifying.sh     |  69 ++++++++
 tools/perf/util/evlist.c                      |  66 +++++---
 tools/perf/util/evlist.h                      |   3 +-
 tools/perf/util/evsel.c                       | 119 ++++++++++++-
 tools/perf/util/evsel.h                       |  11 +-
 tools/perf/util/parse-events.c                |  86 +++++++---
 tools/perf/util/stat-display.c                | 160 ++----------------
 tools/perf/util/stat.c                        |  40 +----
 10 files changed, 329 insertions(+), 239 deletions(-)
 create mode 100755 tools/perf/tests/shell/stat+event_uniquifying.sh

--
v2:
- Fixes for hwmon and --hybrid-merge.
- Add a test for event uniquifying.

v1: lore.kernel.org/[email protected] 2.49.0.472.ge94155a9ec-goog

#1 lore raw

PATCH v2 1/3 perf evlist: Make uniquifying counter names consistent

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

Re: [PATCH v2 1/3] perf evlist: Make uniquifying counter names consistent

Quoted 11 lines · Chun-Tse Shao

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.

Quoted 7 lines

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.

Can you please refresh this series?

Total patches: 3
---
Cover: ./v2_20250327_ctshao_fix_incorrect_counts_when_count_the_same_uncore_event_multiple_times.cover

Base: not specified
git am ./v2_20250327_ctshao_fix_incorrect_counts_when_count_the_same_uncore_event_multiple_times.mbx
⬢ [acme@toolbx perf-tools-next]$ git am ./v2_20250327_ctshao_fix_incorrect_counts_when_count_the_same_uncore_event_multiple_times.mbx
Applying: perf evlist: Make uniquifying counter names consistent
error: patch failed: tools/perf/util/evlist.h:19
error: tools/perf/util/evlist.h: patch does not apply
Patch failed at 0001 perf evlist: Make uniquifying counter names consistent
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
⬢ [acme@toolbx perf-tools-next]$ git am --abort
⬢ [acme@toolbx perf-tools-next]$ patch -p1 < ./v2_20250327_ctshao_fix_incorrect_counts_when_count_the_same_uncore_event_multiple_times.mbx patching file tools/perf/builtin-record.c
Hunk #2 succeeded at 2485 (offset 1 line).
patching file tools/perf/builtin-top.c
patching file tools/perf/util/evlist.c
Hunk #1 succeeded at 2565 (offset 13 lines).
patching file tools/perf/util/evlist.h
Hunk #1 succeeded at 19 with fuzz 2.
Hunk #2 succeeded at 435 with fuzz 1 (offset 1 line).
patching file tools/perf/util/evsel.c
Hunk #1 succeeded at 3954 (offset 15 lines).
patching file tools/perf/util/evsel.h
Hunk #2 succeeded at 549 (offset 6 lines).
patching file tools/perf/util/stat-display.c
Hunk #1 succeeded at 915 (offset -14 lines).
Hunk #2 succeeded at 1005 (offset -9 lines).
Hunk #3 succeeded at 1579 (offset -14 lines).
Hunk #4 succeeded at 1590 (offset -14 lines).
patching file tools/perf/util/evsel.c
Hunk #2 succeeded at 3964 (offset 15 lines).
Hunk #3 succeeded at 3992 (offset 15 lines).
patching file tools/perf/util/evsel.h
patching file tools/perf/util/parse-events.c
Hunk #1 FAILED at 228.
Hunk #2 FAILED at 262.
Hunk #3 succeeded at 318 (offset 31 lines).
Hunk #4 succeeded at 329 (offset 31 lines).
Hunk #5 succeeded at 454 (offset 31 lines).
Hunk #6 FAILED at 433.
Hunk #7 FAILED at 449.
Hunk #8 FAILED at 480.
Hunk #9 succeeded at 1387 (offset 52 lines).
Hunk #10 FAILED at 1363.
Hunk #11 succeeded at 1435 (offset 54 lines).
Hunk #12 succeeded at 1446 (offset 54 lines).
Hunk #13 succeeded at 1478 (offset 54 lines).
Hunk #14 succeeded at 1515 (offset 55 lines).
Hunk #15 FAILED at 1530.
Hunk #16 succeeded at 1623 (offset 60 lines).
Hunk #17 succeeded at 1656 (offset 60 lines).
Hunk #18 succeeded at 1672 (offset 60 lines).
Hunk #19 succeeded at 1709 (offset 60 lines).
Hunk #20 succeeded at 1720 (offset 60 lines).
Hunk #21 succeeded at 1737 (offset 60 lines).
7 out of 21 hunks FAILED -- saving rejects to file tools/perf/util/parse-events.c.rej
patching file tools/perf/util/stat-display.c
Hunk #1 succeeded at 1002 (offset -9 lines).
patching file tools/perf/util/stat.c
patching file tools/perf/tests/shell/stat+event_uniquifying.sh
⬢ [acme@toolbx perf-tools-next]$

#4 lore raw

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

In reply to Chun-Tse Shao

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        | 18 ++++---
 tools/perf/util/evsel.h        |  7 ++-
 tools/perf/util/parse-events.c | 86 ++++++++++++++++++++++++----------
 tools/perf/util/stat-display.c | 11 ++++-
 tools/perf/util/stat.c         | 40 ++--------------
 5 files changed, 90 insertions(+), 72 deletions(-)
tools/perf/util/evsel.c +11 -7
index f00cfff119aa..e360071c870d 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;
@@ -3982,12 +3977,21 @@ bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_stat_con
 		return true;
 	}
 
+	if (counter->first_wildcard_match != NULL) {
+		/*
+		 * If stats are merged then only the first_wildcard_match is
+		 * displayed, there is no need to uniquify this evsel as the
+		 * name won't be shown.
+		 */
+		return false;
+	}
+
 	/*
 	 * Do other non-merged events in the evlist have the same name? If so
 	 * 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 +62 -24

256 lines not on this page · raw message

tools/perf/util/stat-display.c +9 -2
index d427e0ca98a1..91386429115d 100644
@@ -1011,8 +1011,15 @@ static void print_counter_aggrdata(struct perf_stat_config *config,
 	os->evsel = counter;
 
 	/* Skip already merged uncore/hybrid events */
-	if (counter->merged_stat)
-		return;
+	if (config->aggr_mode != AGGR_NONE) {
+		if (evsel__is_hybrid(counter)) {
+			if (config->hybrid_merge && counter->first_wildcard_match != NULL)
+				return;
+		} else {
+			if (counter->first_wildcard_match != NULL)
+				return;
+		}
+	}
 
 	val = aggr->counts.val;
 	ena = aggr->counts.ena;
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
#5 lore raw

PATCH v2 3/3 perf test: Add stat uniquifying test

In reply to Chun-Tse Shao

The stat+uniquify.sh test retrieves all uniquified clockticks events from perf list -v clockticks and check if perf stat -e clockticks -A contains all of them.

---
 .../tests/shell/stat+event_uniquifying.sh     | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100755 tools/perf/tests/shell/stat+event_uniquifying.sh
tools/perf/tests/shell/stat+event_uniquifying.sh new file +69 -0
new file mode 100755
index 000000000000..5ec35c52b7d9
@@ -0,0 +1,69 @@
+#!/bin/bash
+# perf stat events uniquifying
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+stat_output=$(mktemp /tmp/__perf_test.stat_output.XXXXX)
+perf_tool=perf
+err=0
+
+test_event_uniquifying() {
+  # We use `clockticks` to verify the uniquify behavior.
+  event="clockticks"
+
+  # If the `-A` option is added, the event should be uniquified.
+  #
+  # $perf list -v clockticks
+  #
+  # List of pre-defined events (to be used in -e or -M):
+  #
+  #   uncore_imc_0/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_1/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_2/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_3/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_4/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_5/clockticks/                           [Kernel PMU event]
+  #
+  #   ...
+  #
+  # $perf stat -e clockticks -A -- true
+  #
+  #  Performance counter stats for 'system wide':
+  #
+  # CPU0            3,773,018      uncore_imc_0/clockticks/
+  # CPU0            3,609,025      uncore_imc_1/clockticks/
+  # CPU0                    0      uncore_imc_2/clockticks/
+  # CPU0            3,230,009      uncore_imc_3/clockticks/
+  # CPU0            3,049,897      uncore_imc_4/clockticks/
+  # CPU0                    0      uncore_imc_5/clockticks/
+  #
+  #        0.002029828 seconds time elapsed
+
+  echo "stat event uniquifying test"
+  uniquified_event_array=()
+
+  # Check how many uniquified events.
+  while IFS= read -r line; do
+    uniquified_event=$(echo "$line" | awk '{print $1}')
+    uniquified_event_array+=("${uniquified_event}")
+  done < <(${perf_tool} list -v ${event} | grep "\[Kernel PMU event\]")
+
+  perf_command="${perf_tool} stat -e $event -A -o ${stat_output} -- true"
+  $perf_command
+
+  # Check the output contains all uniquified events.
+  for uniquified_event in "${uniquified_event_array[@]}"; do
+    if ! cat "${stat_output}" | grep -q "${uniquified_event}"; then
+      echo "Event is not uniquified [Failed]"
+      echo "${perf_command}"
+      cat "${stat_output}"
+      err=1
+      break
+    fi
+  done
+}
+
+test_event_uniquifying
+rm -f "${stat_output}"
+exit $err
Signature · 1 line
2.49.0.472.ge94155a9ec-goog
reviewer
#6 lore raw

Re: [PATCH v2 3/3] perf test: Add stat uniquifying test

Quoted 20 lines · Chun-Tse Shao

The stat+uniquify.sh test retrieves all uniquified clockticks events from perf list -v clockticks and check if perf stat -e clockticks -A contains all of them.

---
 .../tests/shell/stat+event_uniquifying.sh     | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100755 tools/perf/tests/shell/stat+event_uniquifying.sh
tools/perf/tests/shell/stat+event_uniquifying.sh new file +3 -0
new file mode 100755
index 000000000000..5ec35c52b7d9
@@ -0,0 +1,69 @@
+#!/bin/bash
+# perf stat events uniquifying
+# SPDX-License-Identifier: GPL-2.0

tools/perf/tests/shell/stat+event_uniquifying.sh line 13

Quoted 10 lines
tools/perf/tests/shell/stat+event_uniquifying.sh new file +10 -0
+
+set -e
+
+stat_output=$(mktemp /tmp/__perf_test.stat_output.XXXXX)
+perf_tool=perf
+err=0
+
+test_event_uniquifying() {
+  # We use `clockticks` to verify the uniquify behavior.
+  event="clockticks"

This event is generally only available on Intel, not AMD or ARM, so we will need to skip if it isn't present.

Quoted 26 lines
tools/perf/tests/shell/stat+event_uniquifying.sh new file +26 -0
+  # If the `-A` option is added, the event should be uniquified.
+  #
+  # $perf list -v clockticks
+  #
+  # List of pre-defined events (to be used in -e or -M):
+  #
+  #   uncore_imc_0/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_1/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_2/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_3/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_4/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_5/clockticks/                           [Kernel PMU event]
+  #
+  #   ...
+  #
+  # $perf stat -e clockticks -A -- true
+  #
+  #  Performance counter stats for 'system wide':
+  #
+  # CPU0            3,773,018      uncore_imc_0/clockticks/
+  # CPU0            3,609,025      uncore_imc_1/clockticks/
+  # CPU0                    0      uncore_imc_2/clockticks/
+  # CPU0            3,230,009      uncore_imc_3/clockticks/
+  # CPU0            3,049,897      uncore_imc_4/clockticks/
+  # CPU0                    0      uncore_imc_5/clockticks/
+  #

tools/perf/tests/shell/stat+event_uniquifying.sh line 50

Quoted 10 lines
tools/perf/tests/shell/stat+event_uniquifying.sh new file +10 -0
+  #        0.002029828 seconds time elapsed
+
+  echo "stat event uniquifying test"
+  uniquified_event_array=()
+
+  # Check how many uniquified events.
+  while IFS= read -r line; do
+    uniquified_event=$(echo "$line" | awk '{print $1}')
+    uniquified_event_array+=("${uniquified_event}")
+  done < <(${perf_tool} list -v ${event} | grep "\[Kernel PMU event\]")

Shouldn't the array contain every sysfs event that doesn't have a json component? They may or may not be uniquified so I think the array name is misleading.

tools/perf/tests/shell/stat+event_uniquifying.sh line 57

Quoted 6 lines
tools/perf/tests/shell/stat+event_uniquifying.sh new file +6 -0
+  perf_command="${perf_tool} stat -e $event -A -o ${stat_output} -- true"
+  $perf_command
+
+  # Check the output contains all uniquified events.
+  for uniquified_event in "${uniquified_event_array[@]}"; do
+    if ! cat "${stat_output}" | grep -q "${uniquified_event}"; then

Why not pass the file directly to grep?
Should some of the events not show in the stat output as you only asked for the clockticks event? I'm not sure how this test can pass currently.

Thanks,
Ian

Quoted 15 lines
tools/perf/tests/shell/stat+event_uniquifying.sh new file +12 -1
+      echo "Event is not uniquified [Failed]"
+      echo "${perf_command}"
+      cat "${stat_output}"
+      err=1
+      break
+    fi
+  done
+}
+
+test_event_uniquifying
+rm -f "${stat_output}"
+exit $err
--

2.49.0.472.ge94155a9ec-goog

#7 lore raw

Re: [PATCH v2 3/3] perf test: Add stat uniquifying test

Quoted 84 lines · Ian Rogers
Quoted 20 lines · Chun-Tse Shao

The stat+uniquify.sh test retrieves all uniquified clockticks events from perf list -v clockticks and check if perf stat -e clockticks -A contains all of them.

---
 .../tests/shell/stat+event_uniquifying.sh     | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100755 tools/perf/tests/shell/stat+event_uniquifying.sh
tools/perf/tests/shell/stat+event_uniquifying.sh new file +3 -0
new file mode 100755
index 000000000000..5ec35c52b7d9
@@ -0,0 +1,69 @@
+#!/bin/bash
+# perf stat events uniquifying
+# SPDX-License-Identifier: GPL-2.0

tools/perf/tests/shell/stat+event_uniquifying.sh line 13

Quoted 10 lines
tools/perf/tests/shell/stat+event_uniquifying.sh new file +10 -0
+
+set -e
+
+stat_output=$(mktemp /tmp/__perf_test.stat_output.XXXXX)
+perf_tool=perf
+err=0
+
+test_event_uniquifying() {
+  # We use `clockticks` to verify the uniquify behavior.
+  event="clockticks"

This event is generally only available on Intel, not AMD or ARM, so we will need to skip if it isn't present.

Quoted 26 lines
tools/perf/tests/shell/stat+event_uniquifying.sh new file +26 -0
+  # If the `-A` option is added, the event should be uniquified.
+  #
+  # $perf list -v clockticks
+  #
+  # List of pre-defined events (to be used in -e or -M):
+  #
+  #   uncore_imc_0/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_1/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_2/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_3/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_4/clockticks/                           [Kernel PMU event]
+  #   uncore_imc_5/clockticks/                           [Kernel PMU event]
+  #
+  #   ...
+  #
+  # $perf stat -e clockticks -A -- true
+  #
+  #  Performance counter stats for 'system wide':
+  #
+  # CPU0            3,773,018      uncore_imc_0/clockticks/
+  # CPU0            3,609,025      uncore_imc_1/clockticks/
+  # CPU0                    0      uncore_imc_2/clockticks/
+  # CPU0            3,230,009      uncore_imc_3/clockticks/
+  # CPU0            3,049,897      uncore_imc_4/clockticks/
+  # CPU0                    0      uncore_imc_5/clockticks/
+  #

tools/perf/tests/shell/stat+event_uniquifying.sh line 50

Quoted 10 lines
tools/perf/tests/shell/stat+event_uniquifying.sh new file +10 -0
+  #        0.002029828 seconds time elapsed
+
+  echo "stat event uniquifying test"
+  uniquified_event_array=()
+
+  # Check how many uniquified events.
+  while IFS= read -r line; do
+    uniquified_event=$(echo "$line" | awk '{print $1}')
+    uniquified_event_array+=("${uniquified_event}")
+  done < <(${perf_tool} list -v ${event} | grep "\[Kernel PMU event\]")

Shouldn't the array contain every sysfs event that doesn't have a json component? They may or may not be uniquified so I think the array name is misleading.

tools/perf/tests/shell/stat+event_uniquifying.sh line 57

Quoted 6 lines
tools/perf/tests/shell/stat+event_uniquifying.sh new file +6 -0
+  perf_command="${perf_tool} stat -e $event -A -o ${stat_output} -- true"
+  $perf_command
+
+  # Check the output contains all uniquified events.
+  for uniquified_event in "${uniquified_event_array[@]}"; do
+    if ! cat "${stat_output}" | grep -q "${uniquified_event}"; then
Quoted 8 lines

Why not pass the file directly to grep?
Should some of the events not show in the stat output as you only asked for the clockticks event? I'm not sure how this test can pass currently.

Thanks,
Ian

Thanks for your comment, Ian.

I tried to run perf list -v clockticks and grep for all events which have [Kernel PMU event] tag.
Ideally, that should retrieve all uniquified clockticks events. Then I ran perf stat -e clockticks to check if all events from perf list are showing.

I randomly picked some machines from different arches and it seems work. Let me know if you have a better idea.

-CT

Quoted 15 lines
Quoted 15 lines
tools/perf/tests/shell/stat+event_uniquifying.sh new file +12 -1
+      echo "Event is not uniquified [Failed]"
+      echo "${perf_command}"
+      cat "${stat_output}"
+      err=1
+      break
+    fi
+  done
+}
+
+test_event_uniquifying
+rm -f "${stat_output}"
+exit $err
--

2.49.0.472.ge94155a9ec-goog

reviewer Tested-by on the v2 series
#8 lore raw
In reply to Chun-Tse Shao
Quoted 34 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

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

Ian tested hybrid-merge and hwmon, all looks good as well.

Quoted 7 lines

Chun-Tse Shao (1):
perf test: Add stat uniquifying test

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

There could be minor conflict with this unreviewed series:
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Ian

Quoted 21 lines
 tools/perf/builtin-record.c                   |   7 +-
 tools/perf/builtin-top.c                      |   7 +-
 .../tests/shell/stat+event_uniquifying.sh     |  69 ++++++++
 tools/perf/util/evlist.c                      |  66 +++++---
 tools/perf/util/evlist.h                      |   3 +-
 tools/perf/util/evsel.c                       | 119 ++++++++++++-
 tools/perf/util/evsel.h                       |  11 +-
 tools/perf/util/parse-events.c                |  86 +++++++---
 tools/perf/util/stat-display.c                | 160 ++----------------
 tools/perf/util/stat.c                        |  40 +----
 10 files changed, 329 insertions(+), 239 deletions(-)
 create mode 100755 tools/perf/tests/shell/stat+event_uniquifying.sh

--
v2:
- Fixes for hwmon and --hybrid-merge.
- Add a test for event uniquifying.

v1: lore.kernel.org/[email protected] 2.49.0.472.ge94155a9ec-goog

#9 lore raw

Ping.

Thanks,
CT

Quoted 73 lines · Ian Rogers
Quoted 34 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

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

Ian tested hybrid-merge and hwmon, all looks good as well.

Quoted 7 lines

Chun-Tse Shao (1):
perf test: Add stat uniquifying test

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

There could be minor conflict with this unreviewed series:
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Ian

Quoted 21 lines
 tools/perf/builtin-record.c                   |   7 +-
 tools/perf/builtin-top.c                      |   7 +-
 .../tests/shell/stat+event_uniquifying.sh     |  69 ++++++++
 tools/perf/util/evlist.c                      |  66 +++++---
 tools/perf/util/evlist.h                      |   3 +-
 tools/perf/util/evsel.c                       | 119 ++++++++++++-
 tools/perf/util/evsel.h                       |  11 +-
 tools/perf/util/parse-events.c                |  86 +++++++---
 tools/perf/util/stat-display.c                | 160 ++----------------
 tools/perf/util/stat.c                        |  40 +----
 10 files changed, 329 insertions(+), 239 deletions(-)
 create mode 100755 tools/perf/tests/shell/stat+event_uniquifying.sh

--
v2:
- Fixes for hwmon and --hybrid-merge.
- Add a test for event uniquifying.

v1: lore.kernel.org/[email protected] 2.49.0.472.ge94155a9ec-goog