perf stat: Include PMU name and split uncore events per PMU in metric-only JSON output
PATCH v4 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"}---
v4:
- 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 | 1 + tools/perf/util/stat-display.c | 62 +++++++++++++++---- 2 files changed, 51 insertions(+), 12 deletions(-)
tools/perf/tests/shell/lib/perf_json_output_lint.py +1 -0
@@ -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 +50 -12
129 lines not on this page · raw message
--
2.55.0.571.g244d577d93-goog
Namhyung Kim
maintainerHello,
Quoted 30 lines · Chun-Tse Shao
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_jsonwhenevsel->pmuis a non-core or hybrid PMU inAGGR_NONEmode (-A).
2. Starting a new JSON metric line inAGGR_NONEmode (-A) whenever the underlying hardware PMU instance changes across PMU events.
3. Bypassing line initialization for tool events (e.g. -euser_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. Updatingperf_json_output_lint.pyto 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:
Quoted 8 lines
$ 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"}Can you please fix perf stat JSON output linter test?
---- start ----
...
Checking json output: system wide no aggregation Test failed for input:
{"cpu" : "0", "pmu" : "software", "counter-value" : "3.000000", "unit" : "", "event" : "context-switches", "event-runtime" : 6269136, "pcnt-running" : 100.00, "metric-value" : "478.743700", "metric-unit" : "cs/sec cs_per_second"}
...
check_json_output(expected_items)
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
File "linux/tools/perf/tests/shell/lib/perf_json_output_lint.py", line 89, in check_json_output
raise RuntimeError(f'wrong number of fields. counted {count} expected {expected_items}'
f' in \'{item}\'')
RuntimeError: wrong number of fields. counted 9 expected [6, 8] in '{'cpu': '0', 'pmu': 'software', 'counter-value': '3.000000', 'unit': '', 'event': 'context-switches', 'event-runtime': 6269136, 'pcnt-running': 100.0, 'metric-value': '478.743700', 'metric-unit': 'cs/sec cs_per_second'}' ---- end(-1) ----
119: perf stat JSON output linter : FAILED!
Thanks,
Namhyung