perf stat: Include PMU name and split uncore events per PMU in metric-only JSON output
PATCH v8 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 in metric-only AGGR_NONE mode (-A) when evsel->pmu is a non-core hardware or hybrid PMU. Software and tool events don't belong to a hardware PMU instance and so are not labeled.
2. Starting a new JSON metric line in AGGR_NONE mode (-A) whenever the PMU name changes, so that a line only has metrics from the PMU it is labeled with.
3. Opening a metric line only for events that have a metric, as other events print nothing in metric-only mode. This avoids spurious lines like {"cpu" : "0", "pmu" : "tool"} for events like user_time, while metrics on tool events, like tma_info_system_time (duration_time), are still printed with the line prefix.
4. Updating perf_json_output_lint.py to recognize the new "pmu" key in the JSON test suite.
Before the fix:
$ perf stat -M iio_bandwidth_read -a -A --metric-only -j -I 1000
{"interval" : 1.001017947, "cpu" : "0", "MB/s iio_bandwidth_read" : "0.0", "MB/s iio_bandwidth_read" : "0.0", "MB/s iio_bandwidth_read" : "22.5", "MB/s iio_bandwidth_read" : "0.0", "MB/s iio_bandwidth_read" : "0.2", "MB/s iio_bandwidth_read" : "0.0", "MB/s iio_bandwidth_read" : "0.0", "MB/s iio_bandwidth_read" : "0.0", "MB/s iio_bandwidth_read" : "0.0", "MB/s iio_bandwidth_read" : "0.0"}There is no way to determine which uncore device generated the metrics.
After:
$ 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"}---
v8:
- Gate "pmu" output in print_aggr_id_json() on metric-only mode and skip software PMUs via perf_pmu__is_software(), keeping plain perf stat -a -A -j unchanged and avoiding splitting default metrics onto a separate "software" line. (Hang Zhengyang)
- Rebase onto the latest perf-tools-next.
v7: lore.kernel.org/[email protected]
- Pass the evsel to print_metric_begin() explicitly from all callers instead of falling back to evlist__first(). (Ian)
v6: lore.kernel.org/[email protected]
- Open metric lines for events that have a metric instead of skipping tool events, so metrics on tool events (e.g. tma_info_system_time) keep the line prefix and are terminated properly. (Sashiko)
- Split JSON lines only when the shown PMU name changes. Tool event metrics are no longer mislabeled with an uncore PMU name, and the STD and CSV output are no longer split.
- Rebase onto the latest perf-tools-next.
v5: lore.kernel.org/[email protected]
- Fix perf test "perf stat JSON output linter" failure.
v4: lore.kernel.org/[email protected]
- Fix the line splitting issue, causing spurious empty {"pmu": "tool"} objects and order-dependent PMU mislabeling.
v3: lore.kernel.org/[email protected]
- Add before the fix output.
v2: lore.kernel.org/[email protected]
- Fix print_metric_begin() initialization in print_no_aggr_metric() to ensure tool events always open lines cleanly.
.../tests/shell/lib/perf_json_output_lint.py | 1 + tools/perf/util/stat-display.c | 79 ++++++++++++++++--- 2 files changed, 68 insertions(+), 12 deletions(-)
tools/perf/tests/shell/lib/perf_json_output_lint.py +1 -0
@@ -65,6 +65,7 @@ def check_json_output(expected_count): 'metricgroup': lambda x: True, 'node': lambda x: True, 'pcnt-running': isfloat, + 'pmu': lambda x: True, 'socket': lambda x: True, 'thread': lambda x: True, 'unit': lambda x: True,
tools/perf/util/stat-display.c +67 -12
168 lines not on this page · raw message
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
Quoted 2 lines · Chun-Tse Shao
- Gate "pmu" output in
print_aggr_id_json()on metric-only mode and skip software PMUs viaperf_pmu__is_software(), [...]
Thanks, tested again on AMD Zen 3; both issues I mentioned are fixed.
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 19 lines
When running
perf statwith-A(--no-aggr) and--metric-onlyin JSON output mode (-j),perf statevaluates metric expressions across all matching PMUs (including uncore PMUs likeuncore_iio_0,uncore_iio_1, etc.).However,
perf statpreviously 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 statprinted repeated, ambiguous metric keys without PMU names.Fix this by:
1. Including "pmu" : "<pmu_name>" inprint_aggr_id_jsonin metric-only
AGGR_NONEmode (-A) whenevsel->pmuis a non-core hardware or hybrid
PMU. Software and tool events don't belong to a hardware PMU instance
[ ... ]
{"interval" : 1.000314908, "cpu" : "56", "pmu" : "uncore_iio_1", "MB/siio_bandwidth_read" : "0.0"} {"interval" : 1.000314908, "cpu" : "56", "pmu" : "uncore_iio_11", "MB/siio_bandwidth_read" : "0.0"}
Quoted 2 lines
Assisted-by Gemini:gemini-3.1-pro-previewSigned-off-by Chun-Tse Shao <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1