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

Ian Rogers
2026-04-20T00:07:05Z

Rewrite tools/perf/scripts/python/syscall-counts.py to use the python
module and various style changes. By avoiding the overheads in the
`perf script` execution the performance improves by more than 4x as
shown in the following (with PYTHON_PATH and PERF_EXEC_PATH set as
necessary):

```
$ perf record -e raw_syscalls:sys_enter -a sleep 1
...
$ time perf script tools/perf/scripts/python/syscall-counts.py perf
Install the python-audit package to get syscall names.
For example:
# apt-get install python3-audit (Ubuntu)
# yum install python3-audit (Fedora)
etc.

Press control+C to stop and show the summary
Warning:
1 out of order events recorded.

syscall events for perf:

event count
-------------------------------------- ------------
1 538989
16 32
203 17
3 2
257 1
204 1
15 1
7 1
0 1

real 0m3.887s
user 0m3.578s
sys 0m0.308s
$ time python3 tools/perf/python/syscall-counts.py perf
Warning:
1 out of order events recorded.

syscall events for perf:

event count
-------------------------------------- ------------
write 538989
ioctl 32
sched_setaffinity 17
close 2
openat 1
sched_getaffinity 1
rt_sigreturn 1
poll 1
read 1

real 0m0.953s
user 0m0.905s
sys 0m0.048s
```

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/python/syscall-counts.py | 49 +++++++++++++++++++++++++++++
1 file changed, 49 insertions(+)
create mode 100755 tools/perf/python/syscall-counts.py

diff --git a/tools/perf/python/syscall-counts.py b/tools/perf/python/syscall-counts.py
new file mode 100755
index 000000000000..a2e0994ab736
--- /dev/null
+++ b/tools/perf/python/syscall-counts.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+# Displays system-wide system call totals, broken down by syscall.
+# If a [comm] arg is specified, only syscalls called by [comm] are displayed.
+
+import argparse
+from collections import defaultdict
+import perf
+
+syscalls = defaultdict(int)
+for_comm = None
+session = None
+
+
+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}")
+
+
+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
+ else:
+ return
+ if for_comm and session.process(sample.sample_pid).comm() != for_comm:
+ 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)
+ session.process_events()
+ print_syscall_totals()
--
2.54.0.rc1.513.gad8abe7a5a-goog