perf jevents: Check the JSON keys that jevents.py never reads
PATCH 1/1 perf jevents: Check the JSON keys that jevents.py never reads
jevents.py reads the fields it wants out of every JSON object by name:jd.get('BriefDescription'), 'EventCode' in jd, the event_fields table. A key spelled differently from the name it looks for is never read, so the object is built without whatever the field held, and nothing says so.
"BriefDescript6ion" in the three AMD tables (amdzen4/cache.json, amdzen5/load-store.json, amdzen6/load-store.json) is the case that loses
something: those three events carry no BriefDescription at all, so the generated table has an empty text field for them. "PublicDescrition" in AmpereOneX's mmu.json is the same mistake caught before it cost anything -- the value repeats BriefDescription, and jevents.py drops the long description when the two are equal, so that table comes out the same either way.
JsonEvent now remembers which keys of the object it was built from were never looked at. jevents.py --check reads the tables under <dir> and reports, for each of those keys that is a near miss for a name jevents.py reads, the likely spelling and the files carrying it:
jevents: error: 19 object(s) in 4 file(s) carry a key that looks like a misspelling:
'BriefDescript6ion', did you mean 'BriefDescription'? (3 object(s) in 3
file(s)):
x86/amdzen4/cache.json
x86/amdzen5/load-store.json
x86/amdzen6/load-store.json
'PublicDescrition', did you mean 'PublicDescription'? (16 object(s)
in 1 file(s)):
arm64/ampere/ampereonex/mmu.jsonThe architecture and the model are the ones the build takes, and all for both reads every table; the model only picks the tables a build needs, so a check ignores it. Naming an architecture reads that directory and the common and test tables every build reads, and skips the rest.
Keys that are not near misses are left alone. The Intel tables carry Counter, CountersNumFixed, CountersNumGeneric and Experimental, none of which jevents.py reads, and Experimental is read by metric.py; a vendor may add metadata for another reader tomorrow, and an allow list in jevents.py would mean asking every one of them to edit perf's Python. So the set of names jevents.py reads is the only list, and a name below the similarity cutoff is not reported. Over this tree the tables carry 52 distinct key names and jevents.py reads 46 of them; the two misspellings score 0.97 against the name each was meant to be, and the highest score among the other unread names is Counter, at 0.78 against CounterMask -- so any cutoff in that gap reports the same two names, and 0.8 is what this uses. A key whose words are only reordered ('DescriptionBrief') falls below it too, and so does a misspelling that lands on another name the file already reads.
The exit status is 0 when the tree is clean, 1 when something was reported and 2 when a table could not be read; a bad command line is 2 as well. make turns either failure into its own status, so a script that has to tell them apart should run pmu-events/jevents.py directly; for a person it is
make -C tools/perf jevents-check
which reads the whole tree, and JEVENTS_CHECK_ARCH=x86 reads one architecture. That target fails on this tree until the misspellings above are fixed, which is why they are posted separately and should go in first.
It reads every architecture, not just the one being built: a typo is cheapest to fix by whoever reads that table next, and that reader may be building something else. It is deliberately not part of the build, where a report about a table the build does not use would fail make for everyone, and it costs nothing when it does not run, because the tracking is off unless the check asked for it. It reads the 973 json files under arch/ that jevents.py parses, and takes seconds of CPU to do it.
It is not a schema: it checks nothing about types, required fields or value ranges, and it looks at key names only, so a misspelled description in a value is out of scope. It also does not read mapfile.csv, which has no keys, or metricgroups.json, whose keys are group names.
Outside --check this changes two things, both on the failure path. A table that does not parse is now reported on stderr as "jevents: error: Exception processing <path>" and the traceback ends in "Failure processing '<path>'", where the path used to go to stdout and the traceback came straight from the pool. And the output file is now opened after the tables are read, so a build that fails leaves it as it was instead of truncated to the header.
Written with Hermes Agent, an LLM assistant, which found the misspellings and wrote this check and its changelog. I ran the check over the whole tree, compared the generated tables with those from an unpatched tree on x86, arm64 and all architectures at once, and ran mypy on jevents.py.
--- tools/perf/Makefile | 14 +- tools/perf/Makefile.perf | 1 + tools/perf/pmu-events/jevents.py | 348 ++++++++++++++++++++++++++----- 3 files changed, 314 insertions(+), 49 deletions(-)
tools/perf/Makefile +13 -1
@@ -140,6 +140,18 @@ check-headers: sync-headers: @./scripts/sync-headers.sh $(SYNC_HEADERS_ARGS) +# The pmu-events tables are data, so a key nothing reads is invisible until a +# description goes missing. Named in the wrapper like check-headers: this is +# a data check, not a build, so it needs neither config nor fixdep and reads +# the source tree whatever O= says. JEVENTS_CHECK_ARCH=x86 reads x86 and the +# common and test tables every build reads. +# Makefile.perf picks the interpreter for the build; the wrapper does not +# include it, so default here and let PYTHON= win when it is given. +JEVENTS_CHECK_ARCH ?= all +PYTHON ?= python3 +jevents-check: + @$(PYTHON) ./pmu-events/jevents.py --check '$(JEVENTS_CHECK_ARCH)' all pmu-events/arch + # # All other targets get passed through: #
@@ -147,4 +159,4 @@ sync-headers: $(print_msg) $(make) -.PHONY: all clean tags TAGS FORCE Makefile build-test build-test-tarball check-headers sync-headers +.PHONY: all clean tags TAGS FORCE Makefile build-test build-test-tarball check-headers sync-headers jevents-check
tools/perf/Makefile.perf +1 -0
@@ -871,6 +871,7 @@ help: @echo 'Perf maintainer targets:' @echo ' clean - clean all binary objects and build output' @echo ' check-headers - check which header copies are out of sync with the kernel' + @echo ' jevents-check - report the pmu-events json keys that jevents.py never reads' @echo ' sync-headers - sync those header copies with the kernel, creating commits' @echo ' (SYNC_HEADERS_ARGS="..." passes options to the script)'
tools/perf/pmu-events/jevents.py +300 -48
436 lines not on this page · raw message
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] perf jevents: crash in _read_json_events_impl() when MetricExpr is mistyped - [Low] perf jevents…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] perf jevents: crash in _read_json_events_impl() when MetricExpr is mistyped
- [Low] perf jevents: preprocess_arch_std_files() drops parse error in --check mode
--
commit b85ceb11b01de607fd5251f8da36bf8900130730
Author: TAKANO Mitsuhiro <[email protected]>
perf jevents: Check the JSON keys that jevents.py never reads
This commit adds a --check mode to jevents.py (and a jevents-check make target) that tracks unread JSON keys in JsonEvent and reports keys that closely match field names read by jevents.py.
Quoted 4 lines
tools/perf/pmu-events/jevents.py +0
-0
[ ... ]
tools/perf/pmu-events/jevents.py def preprocess_arch_std_files(archpath: str) -> None: · line 659
Quoted 9 lines
tools/perf/pmu-events/jevents.py +4
-1@@ -493,7 +653,10 @@ def preprocess_arch_std_files(archpath: str) -> None: if event.metric_name: _arch_std_events[event.metric_name.lower()] = event except Exception as e: - raise RuntimeError(f'Failure processing \'{item.name}\' in \'{archpath}\'') from e + failure = RuntimeError(f'Failure processing \'{item.name}\' in \'{archpath}\'') + if failures is None: + raise failure from e + failures.append((item.path, failure))
[Severity: Low]
Does appending failure instead of e here drop the underlying parse error when --check reports failures from preprocess_arch_std_files()?
In main(), each entry in failed is printed with str(error), which does not follow __cause__ and only prints "Failure processing '<name>' in '<archpath>'" alongside the relative path, hiding why the JSON file failed to load.
[ ... ]
Quoted 1 line
tools/perf/pmu-events/jevents.py +0
-0@@ -1490,12 +1660,111 @@ such as "arm/cortex-a34".''',
[ ... ]
tools/perf/pmu-events/jevents.py such as "arm/cortex-a34".''', · line 1735
Quoted 10 lines
tools/perf/pmu-events/jevents.py +10
-0+ with concurrent.futures.ProcessPoolExecutor(max_workers=_parallel_worker_count(len(tasks)), + initializer=_init_worker, + initargs=(_arch_std_events, _args.check)) as executor: + for path, topic, events in executor.map(_parallel_read_json_events, tasks): + if isinstance(events, Exception): + if not _args.check: + raise RuntimeError(f'Failure processing \'{path}\'') from events + failed.append((path, events)) + else: + _json_cache[(path, topic)] = events
[Severity: Low]
What happens in --check mode when a metric object has a valid MetricName (without '-') but misspells MetricExpr (for example as MetricExp)?
JsonEvent.__init__() leaves self.metric_expr as None and records the mistyped key in self.mistyped, but _read_json_events_impl() then passes that None expression into metric.RewriteMetricsInTermsOfOthers():
tools/perf/pmu-events/jevents.py:_read_json_events_impl() {
...
for event in events:
event.topic = topic
if event.metric_name and '-' not in event.metric_name:
metrics.append((event.pmu, event.metric_name, event.metric_expr))
updates = metric.RewriteMetricsInTermsOfOthers(metrics)
...
}RewriteMetricsInTermsOfOthers() raises an AttributeError on None before _read_json_events_impl() can return events, so main() records the file in failed instead of _json_cache and --check reports an AttributeError (exit status 2) instead of reporting the misspelled MetricExpr key. Checking that event.metric_expr is not None before appending to metrics would let --check report the misspelling.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1