Skip to contentSkip to conversation

perf stat: Merge CPU maps when merging uncore aliases

PATCH In review Chun-Tse Shao 3 participants 5 messages Apr 16 – May 1 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf stat: Merge CPU maps when merging uncore aliases

Sockets with event zero counts are hidden in perf stat --per-socket because alias merging aggregates counts but not CPU maps. This causes the display logic to incorrectly skip sockets not present in the leader instance's map.

For example, with AMD_UMC:

  $ cat /sys/bus/event_source/devices/amd_umc*/cpumask
  0
  ...
  128
  ...
  $ perf stat -e amd_umc/config=0xff/ --per-socket -a -- sleep 1

Performance counter stats for 'system wide':

S0 12 0 amd_umc/config=0xff/

         1.000777829 seconds time elapsed

Merge CPU maps in evsel__merge_aggr_counters to ensure the aggregated event correctly reflects all underlying PMU instances.

After fix:

  $ perf stat -e amd_umc/config=0xff/ --per-socket -a -- sleep 1

Performance counter stats for 'system wide':

  S0       12                  0      amd_umc/config=0xff/
  S1       12                  0      amd_umc/config=0xff/
         1.000666681 seconds time elapsed
---
 tools/perf/util/stat.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/util/stat.c +6 -0
index 14d169e22e8f..d696d217f98d 100644
@@ -535,6 +535,12 @@ static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias)
 		aggr_counts_a->run += aggr_counts_b->run;
 	}
 
+	/*
+	 * Merge the CPU maps so that the display logic (e.g. should_skip_zero_counter)
+	 * knows this merged event covers all CPUs from both aliases.
+	 */
+	perf_cpu_map__merge(&evsel->core.cpus, alias->core.cpus);
+
 	return 0;
 }
 
Signature · 1 line
2.54.0.rc1.555.g9c883467ad-goog
reviewer
#1 lore raw
Quoted 44 lines · Chun-Tse Shao

Sockets with event zero counts are hidden in perf stat --per-socket because alias merging aggregates counts but not CPU maps. This causes the display logic to incorrectly skip sockets not present in the leader instance's map.

For example, with AMD_UMC:

  $ cat /sys/bus/event_source/devices/amd_umc*/cpumask
  0
  ...
  128
  ...
  $ perf stat -e amd_umc/config=0xff/ --per-socket -a -- sleep 1

Performance counter stats for 'system wide':

S0 12 0 amd_umc/config=0xff/

         1.000777829 seconds time elapsed

Merge CPU maps in evsel__merge_aggr_counters to ensure the aggregated event correctly reflects all underlying PMU instances.

After fix:

  $ perf stat -e amd_umc/config=0xff/ --per-socket -a -- sleep 1

Performance counter stats for 'system wide':

  S0       12                  0      amd_umc/config=0xff/
  S1       12                  0      amd_umc/config=0xff/
         1.000666681 seconds time elapsed
---
 tools/perf/util/stat.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/util/stat.c +0 -0
index 14d169e22e8f..d696d217f98d 100644

tools/perf/util/stat.c static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias) · line 543

Quoted 9 lines
tools/perf/util/stat.c +6 -0
@@ -535,6 +535,12 @@ static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias)
                aggr_counts_a->run += aggr_counts_b->run;
        }
 
+       /*
+        * Merge the CPU maps so that the display logic (e.g. should_skip_zero_counter)
+        * knows this merged event covers all CPUs from both aliases.
+        */
+       perf_cpu_map__merge(&evsel->core.cpus, alias->core.cpus);
+

Once we have counts, mutating the cpumaps seems wrong; it could impact other things such as the evlist copies of unioned together cpumaps. I'm confused by the bug, we should have two aggregate ids one for each socket. In should_skip_zero_counter it should only skip the zero count if the counter is not applicable for the counter currently trying to be printed. Does the display code fail to iterate through all the counters?

Thanks,
Ian

Quoted 6 lines
tools/perf/util/stat.c +0 -1
        return 0;
 }
 
--

2.54.0.rc1.555.g9c883467ad-goog

#2 lore raw

Instead, I submitted another fix to tweak Merge CPU maps when merging uncore aliases. Please take a look lore.kernel.org/[email protected]

Thanks,
CT

Quoted 73 lines · Ian Rogers
Quoted 44 lines · Chun-Tse Shao

Sockets with event zero counts are hidden in perf stat --per-socket because alias merging aggregates counts but not CPU maps. This causes the display logic to incorrectly skip sockets not present in the leader instance's map.

For example, with AMD_UMC:

  $ cat /sys/bus/event_source/devices/amd_umc*/cpumask
  0
  ...
  128
  ...
  $ perf stat -e amd_umc/config=0xff/ --per-socket -a -- sleep 1

Performance counter stats for 'system wide':

S0 12 0 amd_umc/config=0xff/

         1.000777829 seconds time elapsed

Merge CPU maps in evsel__merge_aggr_counters to ensure the aggregated event correctly reflects all underlying PMU instances.

After fix:

  $ perf stat -e amd_umc/config=0xff/ --per-socket -a -- sleep 1

Performance counter stats for 'system wide':

  S0       12                  0      amd_umc/config=0xff/
  S1       12                  0      amd_umc/config=0xff/
         1.000666681 seconds time elapsed
---
 tools/perf/util/stat.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/util/stat.c +0 -0
index 14d169e22e8f..d696d217f98d 100644

tools/perf/util/stat.c static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias) · line 543

Quoted 9 lines
tools/perf/util/stat.c +6 -0
@@ -535,6 +535,12 @@ static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias)
                aggr_counts_a->run += aggr_counts_b->run;
        }
 
+       /*
+        * Merge the CPU maps so that the display logic (e.g. should_skip_zero_counter)
+        * knows this merged event covers all CPUs from both aliases.
+        */
+       perf_cpu_map__merge(&evsel->core.cpus, alias->core.cpus);
+

Once we have counts, mutating the cpumaps seems wrong; it could impact other things such as the evlist copies of unioned together cpumaps. I'm confused by the bug, we should have two aggregate ids one for each socket. In should_skip_zero_counter it should only skip the zero count if the counter is not applicable for the counter currently trying to be printed. Does the display code fail to iterate through all the counters?

Thanks,
Ian

Quoted 6 lines
tools/perf/util/stat.c +0 -1
        return 0;
 }
 
--

2.54.0.rc1.555.g9c883467ad-goog

#3 lore raw

My fault, what I mean was "tweak should_skip_zero_counter"

Quoted 83 lines · Chun-Tse Shao

Instead, I submitted another fix to tweak Merge CPU maps when merging uncore aliases. Please take a look lore.kernel.org/[email protected]

Thanks,
CT

Quoted 73 lines · Ian Rogers
Quoted 44 lines · Chun-Tse Shao

Sockets with event zero counts are hidden in perf stat --per-socket because alias merging aggregates counts but not CPU maps. This causes the display logic to incorrectly skip sockets not present in the leader instance's map.

For example, with AMD_UMC:

  $ cat /sys/bus/event_source/devices/amd_umc*/cpumask
  0
  ...
  128
  ...
  $ perf stat -e amd_umc/config=0xff/ --per-socket -a -- sleep 1

Performance counter stats for 'system wide':

S0 12 0 amd_umc/config=0xff/

         1.000777829 seconds time elapsed

Merge CPU maps in evsel__merge_aggr_counters to ensure the aggregated event correctly reflects all underlying PMU instances.

After fix:

  $ perf stat -e amd_umc/config=0xff/ --per-socket -a -- sleep 1

Performance counter stats for 'system wide':

  S0       12                  0      amd_umc/config=0xff/
  S1       12                  0      amd_umc/config=0xff/
         1.000666681 seconds time elapsed
---
 tools/perf/util/stat.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/util/stat.c +0 -0
index 14d169e22e8f..d696d217f98d 100644

tools/perf/util/stat.c static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias) · line 543

Quoted 9 lines
tools/perf/util/stat.c +6 -0
@@ -535,6 +535,12 @@ static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias)
                aggr_counts_a->run += aggr_counts_b->run;
        }
 
+       /*
+        * Merge the CPU maps so that the display logic (e.g. should_skip_zero_counter)
+        * knows this merged event covers all CPUs from both aliases.
+        */
+       perf_cpu_map__merge(&evsel->core.cpus, alias->core.cpus);
+

Once we have counts, mutating the cpumaps seems wrong; it could impact other things such as the evlist copies of unioned together cpumaps. I'm confused by the bug, we should have two aggregate ids one for each socket. In should_skip_zero_counter it should only skip the zero count if the counter is not applicable for the counter currently trying to be printed. Does the display code fail to iterate through all the counters?

Thanks,
Ian

Quoted 6 lines
tools/perf/util/stat.c +0 -1
        return 0;
 }
 
--

2.54.0.rc1.555.g9c883467ad-goog

#4 lore raw
In reply to Chun-Tse Shao
Sashiko AI review found 2 potential issue(s): - [Medium] Expanding `evsel->core.cpus` without reallocating parallel arrays causes a deterministic NULL pointer dereference and out-of-bounds access.…

Sashiko AI review found 2 potential issue(s):
- [Medium] Expanding evsel->core.cpus without reallocating parallel arrays causes a deterministic NULL pointer dereference and out-of-bounds access.
- [Medium] Missing error handling for perf_cpu_map__merge(), masking memory allocation failures.
--

commit ca357f612c76d14c9a719708c9f0cdce0f262717
Author: Chun-Tse Shao <[email protected]>

perf stat: Merge CPU maps when merging uncore aliases

This commit merges CPU maps for aliases in evsel__merge_aggr_counters() to prevent the display logic from hiding sockets with zero counts. It aims to ensure the aggregated event correctly reflects all underlying PMU instances.

Quoted 4 lines
tools/perf/util/stat.c +0 -0
index 14d169e22e8f5..d696d217f98d6 100644

[ ... ]

tools/perf/util/stat.c static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias) · line 542

Quoted 9 lines
tools/perf/util/stat.c +5 -0
@@ -535,6 +535,12 @@ static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias)
 		aggr_counts_a->run += aggr_counts_b->run;
 	}
 
+	/*
+	 * Merge the CPU maps so that the display logic (e.g. should_skip_zero_counter)
+	 * knows this merged event covers all CPUs from both aliases.
+	 */
+	perf_cpu_map__merge(&evsel->core.cpus, alias->core.cpus);

perf_cpu_map__merge() can fail and return an error code like -ENOMEM. Should we check the return value here and propagate the error, instead of ignoring it?

Additionally, does expanding the CPU map in-place here cause out-of-bounds memory accesses?

Arrays like evsel->counts and evsel->prev_raw_counts are allocated earlier based on the original size of evsel->core.cpus:

tools/perf/util/stat.c:evsel__alloc_prev_raw_counts() {
	int cpu_map_nr = evsel__nr_cpus(evsel);
	int nthreads = perf_thread_map__nr(evsel->core.threads);
	struct perf_counts *counts;

	counts = perf_counts__new(cpu_map_nr, nthreads);
	...
}

If perf_cpu_map__merge() increases the number of CPUs in the map, later iterations over the CPU map (such as in interval mode) will access these parallel xyarrays out of bounds. When the loop index exceeds the original array size, the access returns NULL and causes a segmentation fault when unconditionally dereferenced.

Do we need to reallocate evsel->counts and evsel->prev_raw_counts when merging these CPU maps?

Quoted 3 lines
tools/perf/util/stat.c +1 -0
+
 	return 0;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1