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

sashiko-bot
2026-04-20T00:41:47Z

Sashiko AI review found 4 potential issue(s):
- [High] The script crashes with a TypeError when a thread lookup fails during 'for_comm' filtering.
- [High] The script crashes with a TypeError if 'perf.syscall_name(id)' encounters an unmapped syscall ID.
- [High] The script crashes with an AttributeError on older kernels where the tracepoint field is 'nr' instead of '__syscall_nr'.
- [Medium] The standalone script hardcodes the input trace file to 'perf.data', breaking the ability to analyze arbitrarily named trace files.
--

commit 6f15d3f04b68eebb0fdc02fb0dd260ac9c6fdf6a
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 execution performance.

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

[ ... ]

> +def print_syscall_totals():
> + if for_comm is not None:
> + print(f"\nsyscall events for {for_comm}:\n")
> + else:
> + print("\nsyscall events:\n")
> +
> + print(f"{'event':<40} {'count':>10}")
> + print("---------------------------------------- -----------")
> +
> + for id, val in sorted(syscalls.items(),
> + key=lambda kv: (kv[1], kv[0]), reverse=True):
> + print(f"{perf.syscall_name(id):<40} {val:>10}")

What happens if perf.syscall_name(id) returns None for an unmapped syscall
ID?

Applying a width specifier (<40) to a NoneType object throws a TypeError,
which will abort the output. Should there be a fallback mechanism, such as
using the numeric ID when the name is unknown?

> +def process_event(sample):
> + event_name = str(sample.evsel)
> + if event_name == "evsel(raw_syscalls:sys_enter)":
> + id = sample.id
> + elif event_name.startswith("evsel(syscalls:sys_enter_"):
> + id = sample.__syscall_nr

On some older kernels and architectures, the tracepoint format field for
syscalls is named nr instead of __syscall_nr.

Will accessing sample.__syscall_nr raise an AttributeError in those
environments? Is it possible to check for nr as a fallback?

> + else:
> + return
> + if for_comm and session.process(sample.sample_pid).comm() != for_comm:

If a thread lookup fails (for example, for short-lived processes or samples
recorded before procfs synthesis completes), session.process() returns NULL
and sets a Python TypeError.

Can this uncaught exception bubble up and cause the script to crash?

> + return
> + syscalls[id] += 1
> +
> +
> +if __name__ == "__main__":
> + ap = argparse.ArgumentParser()
> + ap.add_argument("comm", nargs="?", help="Only report syscalls for comm")
> + args = ap.parse_args()
> + for_comm = args.comm
> + session = perf.session(perf.data("perf.data"), sample=process_event)

The previous implementation running under perf script automatically supported
standard options like -i my_trace.data to override the input file.

Since the file path is now hardcoded to perf.data, does this introduce a
regression in usability by preventing the analysis of arbitrarily named
trace files?

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