Skip to contentSkip to conversation

perf stat: Include PMU name and split uncore events per PMU in metric-only JSON output

PATCH Superseded Chun-Tse Shao 2 participants 2 messages Jul 14 perf events

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf stat: Include PMU name and split uncore events per PMU in metric-only JSON output

When running perf stat with -A (--no-aggr) and --metric-only in JSON output mode (-j), perf stat evaluates metric expressions across all matching PMUs (including uncore PMUs like uncore_iio_0, uncore_iio_1, etc.).

However, perf stat previously formatted JSON output by printing only "cpu" : "<id>" and grouping all metric values on a single line per CPU without identifying which PMU instance evaluated each metric. As a result, when an uncore event spans multiple PMU boxes, perf stat printed repeated, ambiguous metric keys without PMU names.

Fix this by:
1. Including "pmu" : "<pmu_name>" in print_aggr_id_json when evsel->pmu is a non-core or hybrid PMU in AGGR_NONE mode (-A).
2. Starting a new JSON metric line in AGGR_NONE mode (-A) whenever the underlying PMU instance changes across PMU events.
3. Updating perf_json_output_lint.py to recognize the new "pmu" key in the JSON test suite.

Example output:

  $ perf stat -M iio_bandwidth_read -a -A --metric-only -j -I 1000
  {"interval" : 1.000314908, "cpu" : "0", "pmu" : "uncore_iio_0", "MB/s  iio_bandwidth_read" : "0.0"}
  {"interval" : 1.000314908, "cpu" : "0", "pmu" : "uncore_iio_1", "MB/s  iio_bandwidth_read" : "0.1"}
  {"interval" : 1.000314908, "cpu" : "0", "pmu" : "uncore_iio_11", "MB/s  iio_bandwidth_read" : "0.0"}
  ...
  {"interval" : 1.000314908, "cpu" : "56", "pmu" : "uncore_iio_0", "MB/s  iio_bandwidth_read" : "0.0"}
  {"interval" : 1.000314908, "cpu" : "56", "pmu" : "uncore_iio_1", "MB/s  iio_bandwidth_read" : "0.0"}
  {"interval" : 1.000314908, "cpu" : "56", "pmu" : "uncore_iio_11", "MB/s  iio_bandwidth_read" : "0.0"}
Assisted-by Gemini:gemini-3.1-pro-preview
---
 .../tests/shell/lib/perf_json_output_lint.py  |  1 +
 tools/perf/util/stat-display.c                | 41 +++++++++++++------
 2 files changed, 30 insertions(+), 12 deletions(-)
tools/perf/tests/shell/lib/perf_json_output_lint.py +1 -0
index dafbde56cc76..36767905fcdb 100644
@@ -64,6 +64,7 @@ def check_json_output(expected_items):
       'metric-threshold': lambda x: x in ['unknown', 'good', 'less good', 'nearly bad', 'bad'],
       'metricgroup': lambda x: True,
       'node': lambda x: True,
+      'pmu': lambda x: True,
       'pcnt-running': lambda x: isfloat(x),
       'socket': lambda x: True,
       'thread': lambda x: True,
tools/perf/util/stat-display.c +29 -12

105 lines not on this page · raw message

Signature · 1 line
2.55.0.141.g00534a21ce-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Medium] Bypassing print_metric_begin for tool events produces invalid, corrupted JSON and CSV output in --metric-only mode.

Pre-existing issues:
- [High] Missing bounds check on aggr_idx after searching config->aggr_map results in an out-of-bounds array read. --

--- Patch [1]: [PATCH] perf stat: Include PMU name and split uncore events per PMU in metric-only JSON output --- commit 418eb65331465e855519851f0438c3a1fe047563
Author: Chun-Tse Shao <[email protected]>

perf stat: Include PMU name and split uncore events per PMU in metric-only JSON output

This commit modifies perf stat to include the PMU name in the metric-only JSON output when running in no-aggr mode. It ensures that when an uncore event spans multiple PMU instances, each instance is printed on a separate line with the correct PMU identifier. The JSON linting script is also updated to recognize the new PMU key.

Quoted 4 lines
tools/perf/util/stat-display.c +0 -0
index f94f1324d24ac..d7f1feb638784 100644

[ ... ]

tools/perf/util/stat-display.c static void print_no_aggr_metric(struct perf_stat_config *config, · line 1156

Quoted 12 lines
tools/perf/util/stat-display.c +5 -3
@@ -1146,10 +1149,24 @@ static void print_no_aggr_metric(struct perf_stat_config *config,
 
 			os->evsel = counter;
 			os->id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
-			if (first) {
-				print_metric_begin(config, evlist, os, aggr_idx);
-				first = false;
+
+			if (config->metric_only) {
+				struct perf_pmu *pmu = counter->pmu;
+
+				if (!evsel__is_tool(counter)) {

[Severity: Medium]
Does bypassing the print_metric_begin() initialization for tool events cause invalid JSON output?

If a metric uses a tool event like duration_time as the metric leader, skipping print_metric_begin() means the opening bracket, timestamp, and CPU ID are not printed. This can leave the metric key-value pair as a bare string, followed by a mid-stream bracket when a subsequent hardware PMU event is processed.

Quoted 4 lines
tools/perf/util/stat-display.c +4 -0
+					if (config->json_output && line_open && pmu != last_pmu) {
+						print_metric_end(config, os);
+						line_open = false;
+					}

tools/perf/util/stat-display.c static void print_no_aggr_metric(struct perf_stat_config *config, · line 1170

Quoted 10 lines
tools/perf/util/stat-display.c +8 -0
+					if (!line_open) {
+						print_metric_begin(config, evlist, os,
+								   aggr_idx, counter);
+						line_open = true;
+						last_pmu = pmu;
+					}
+				}
 			}
+
 			val = ps->aggr[aggr_idx].counts.val;

[Severity: High]
This is a pre-existing issue, but does this code read past the end of the ps->aggr array if the CPU is not found in the aggregation map?

In print_no_aggr_metric(), there is a loop right before this block that searches for the CPU:

cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
	if (config->aggr_map->map[aggr_idx].cpu.cpu == cpu.cpu)
		break;
}

If a system lacks complete sysfs CPU topology, aggr_cpu_id__cpu() might return an empty ID, and the CPU may not be found in config->aggr_map.

Could the loop terminate with aggr_idx == config->aggr_map->nr, causing this line to unconditionally read out of bounds?

Quoted 2 lines
tools/perf/util/stat-display.c +0 -0
 			ena = ps->aggr[aggr_idx].counts.ena;
 			run = ps->aggr[aggr_idx].counts.run;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1