Re: [PATCH v5 27/58] perf syscall-counts: Port syscall-counts to use python module

sashiko-bot
2026-04-24T17:13:38Z

Sashiko AI review found 3 potential issue(s):
- [High] The script is vulnerable to crashes from unhandled `OverflowError` and `SystemError` exceptions when processing malformed or edge-case trace data.
- [Medium] The script uses strict string equality for event names, causing it to drop events recorded with modifiers (e.g., :k or :u).
- [Medium] The script drops support for the generic `syscalls:sys_enter` fallback event, breaking compatibility with older environments.
--

commit a62a8fdbc0cd35bc8b0e51cb5929e23578afa7d5
Author: Ian Rogers <irogers@google.com>

perf syscall-counts: Port syscall-counts to use python module

This commit rewrites the tools/perf/scripts/python/syscall-counts.py script
to use the Python module directly. This avoids the overhead of executing via
perf script, improving performance significantly.

> diff --git a/tools/perf/python/syscall-counts.py b/tools/perf/python/syscall-counts.py
> new file mode 100755
> index 00000000000000..cdeae6c1e9f912
> --- /dev/null
> +++ b/tools/perf/python/syscall-counts.py

[ ... ]

> +def print_syscall_totals():

[ ... ]

> + for sc_id, val in sorted(syscalls.items(),
> + key=lambda kv: (kv[1], kv[0]), reverse=True):
> + name = perf.syscall_name(sc_id) or str(sc_id)

Can perf.syscall_name() raise an OverflowError if tracepoint parsing fails
and sc_id ends up holding the 64-bit generic perf event ID from the
getattr(sample, "id", -1) call?

The C extension expects a 32-bit integer, and an uncaught exception would
crash the script.

> + print(f"{name:<40} {val:>10}")
> +
> +
> +def process_event(sample):
> + """Process a single sample event."""
> + event_name = str(sample.evsel)
> + if event_name == "evsel(raw_syscalls:sys_enter)":

Is it possible for events recorded with modifiers (like
perf record -e raw_syscalls:sys_enter:k) to be missed here?

The exact string match would evaluate to false for
evsel(raw_syscalls:sys_enter:k) and silently ignore valid events.

> + sc_id = getattr(sample, "id", -1)
> + elif event_name.startswith("evsel(syscalls:sys_enter_"):

Does this condition break compatibility with older environments that use the
generic syscalls:sys_enter fallback event?

The trailing underscore in the startswith() check prevents matching the base
event evsel(syscalls:sys_enter), causing these fallback events to be
dropped.

> + sc_id = getattr(sample, "__syscall_nr", None)
> + if sc_id is None:
> + sc_id = getattr(sample, "nr", -1)
> + else:
> + return
> +
> + if sc_id == -1:
> + return
> +
> + comm = "unknown"
> + try:
> + if session:
> + proc = session.process(sample.sample_pid)
> + if proc:
> + comm = proc.comm()
> + except (TypeError, AttributeError):
> + pass

Could proc.comm() trigger a SystemError if a thread lacks a resolved command
string?

If thread__comm_str() returns null, passing it to PyUnicode_FromString()
in the C extension natively raises a SystemError in CPython, which is not
caught by the current except block.

> +
> + if for_comm and comm != for_comm:
> + return

[ ... ]

--
Sashiko AI review · https://sashiko.dev/#/patchset/20260424164721.2229025-1-irogers@google.com?part=27