perf stat: Include PMU name and split uncore events per PMU in metric-only JSON output
PATCH v5 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 hardware PMU instance changes across PMU events.
3. Bypassing line initialization for tool events (e.g. -e user_time) in metric-only mode so that JSON lines are opened only by hardware PMUs, preventing tool events from corrupting PMU name labels or creating spurious empty {"pmu": "tool"} JSON objects.
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"}---
v5:
- 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.
- Simplify and fix PMU line-split transition tracking when switching between different PMU instances or transitioning from tool events.
- Add bounds check on aggr_map index in print_no_aggr_metric() to prevent out-of-bounds array reads if sysfs CPU topology is incomplete.
v1: lore.kernel.org/[email protected]
.../tests/shell/lib/perf_json_output_lint.py | 5 +- tools/perf/util/stat-display.c | 59 +++++++++++++++---- 2 files changed, 51 insertions(+), 13 deletions(-)
tools/perf/tests/shell/lib/perf_json_output_lint.py +4 -1
@@ -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,
@@ -99,8 +100,10 @@ def check_json_output(expected_items): try: if args.no_args or args.system_wide or args.event: expected_items = [5, 7] - elif args.interval or args.per_thread or args.system_wide_no_aggr: + elif args.interval or args.per_thread: expected_items = [6, 8] + elif args.system_wide_no_aggr: + expected_items = [6, 7, 8, 9] elif args.per_core or args.per_socket or args.per_node or args.per_die or args.per_cluster or args.per_cache: expected_items = [7, 9] elif args.metric_only:
tools/perf/util/stat-display.c +47 -12
125 lines not on this page · raw message
--
2.55.0.679.g6767b8d81c-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Bypassing line initialization and termination for tool events in '--metric-only' mode corrupts…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Bypassing line initialization and termination for tool events in '--metric-only' mode corrupts JSON, CSV, and standard formatting when those events evaluate to metrics. --
--- Patch [1]: [PATCH v5] perf stat: Include PMU name and split uncore events per PMU in metric-only JSON output --- commit f2dfadb52a27c2891ce164d2b06f57924949dd1d
Author: Chun-Tse Shao <[email protected]>
perf stat: Include PMU name and split uncore events per PMU in metric-only JSON output
This commit updates perf stat JSON output to include the PMU name for uncore events in metric-only mode and split output lines when transitioning between PMUs. It bypasses line initialization for tool events to avoid creating spurious empty JSON objects.
Quoted 3 lines
tools/perf/util/stat-display.c +0
-0
[ ... ]
Quoted 8 lines
tools/perf/util/stat-display.c +2
-0@@ -1146,12 +1151,42 @@ static void print_no_aggr_metric(struct perf_stat_config *config,if (config->aggr_map->map[aggr_idx].cpu.cpu == cpu.cpu)break ; } +if (aggr_idx >= config->aggr_map->nr) +continue ; os->evsel = counter;
tools/perf/util/stat-display.c static void print_no_aggr_metric(struct perf_stat_config *config, · line 1164
Quoted 10 lines
tools/perf/util/stat-display.c +6
-3os->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; +bool is_tool =evsel__is_tool (counter); + +if (!is_tool && pmu) {
[Severity: Medium]
Does bypassing line initialization for tool events corrupt the output formatting if the tool event evaluates to a metric?
If a tool event has an associated metric (e.g., from a custom metric using duration_time), it will fail the !is_tool check and skip calling print_metric_begin().
The event still proceeds to printout() down below:
uval = val * counter->scale;
printout(config, os, uval, run, ena, 1.0, aggr_idx);If printout() outputs the metric directly, won't this lack the necessary JSON or CSV preamble (such as { "interval"... ) because print_metric_begin() was skipped?
Furthermore, since line_open remains false, print_metric_end() is also bypassed at the end of the loop, leaving the JSON object unclosed.
Quoted 7 lines
tools/perf/util/stat-display.c +7
-0+bool pmu_changed = false; + +if (line_open && last_pmu) { +bool non_core = !pmu->is_core || +evsel__is_hybrid (counter) || + !last_pmu->is_core || +evsel__is_hybrid (last_evsel);
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1