From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 58399FED3F1 for ; Fri, 24 Apr 2026 16:49:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=pFZnICrUGM5HdiGtclFFSrRR5EMO67SGzR+2zeCgTNs=; b=Z7H5JW3+WjovuynV+YBn231fED teMFbKFvosUdBQpk9Zq+hYADG3o1QBGJWQVkfYFBRKHV+0A+gsd9DtrvD5uLMIGczXwkZY1Lf93fQ jcvmuqapDDj7/d3qbnLYz00Q1GnEIzYZD98xi0qsxhG1B493UCQeIsHGAniuVYDnQUDVj2vTn+1yx 78nfg4evpS8vaqd8J8U1yfScT8fUsHfHRzH+9ps2jiXVA+R9g7GrUN2hPA8gzaB0DyeMYj7zLgyTf rl3vbjr9GzWiaRMI8hGPlduFdp9PgzuSOFQ9j+EoqJssqLFN1Sms9LldpIgEmliPyqfHq/4gQxDVo R254+t4Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1wGJiN-0000000DUZ7-2SAa; Fri, 24 Apr 2026 16:49:15 +0000 Received: from mail-dy1-x134a.google.com ([2607:f8b0:4864:20::134a]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1wGJhu-0000000DU2S-1VRQ for linux-arm-kernel@lists.infradead.org; Fri, 24 Apr 2026 16:48:52 +0000 Received: by mail-dy1-x134a.google.com with SMTP id 5a478bee46e88-2ba9a744f7dso10531403eec.0 for ; Fri, 24 Apr 2026 09:48:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777049325; x=1777654125; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=pFZnICrUGM5HdiGtclFFSrRR5EMO67SGzR+2zeCgTNs=; b=lX5KMguLYIouQjCME7acLeXQJxY88iouxFG+Rk/S1Q+aohK+3+v4EZDc+/il5FduwE bpZVJoK0Kt+vF4kqfeiUSpK7Y0UcWVsSZ+bMlEZUva+5Br3Eti4ElqTOq4jBoyYKBMwd iM6sBWECDu761gsCZNpoxjkdmZFtvCsA2vk6mjO3Ore6ZvEa59qdmeOLUoXrNFx5Dubq W1IaSVyqFLqo90SZ97bSMdLHjslncpUnPR2Rplkz/xMQ+d+SUEivQOblTT0UuOd8ahxF XlTW+PnyJ00u3zH0erf/hiBx7Q1lhJ3V4PpxdI0UWMrMC9g4fTD6MBthepwvLXKcF6dE oI8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777049325; x=1777654125; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pFZnICrUGM5HdiGtclFFSrRR5EMO67SGzR+2zeCgTNs=; b=lIF4h47Wk9J/FkWa3KoIQX1VLn27SlT078ho3RbVkJdFhbo6l5CvvcKo7pC8OGYBHM OiFrv/tbkTPyPZAR7wJEnZ+8e7QnRtvFn7SqwP5AFBMPWjBiqkAkgWajy6q2k7RC0vxE seq+mFZ4JLAqM1mq7ntwe3RJUTGgLtIJfm6en0fMyFlFRRk8gypL8DvTNDyYktFv4SKe Jw1NQ5B6M3ShMPhHh0HduIcOkPtfpPrS2pPXgwkatc7GDdkQnFFuC9do6Z3Tc1T39zf/ DFVRaHbU9fdCKqkBS93IjmgW2YBEa/WQqS57rpEjkItAVorVVgVYrFZNF4oXlD4cpMls xTqw== X-Forwarded-Encrypted: i=1; AFNElJ/Ea5CPXxxF1LRSJiMjNK/362PKBc2zVKlIQ/kG5fvtNohrG3RgOUNc8cIVfD5+HjjO2ydK0tkrNZaYyUdAm/1I@lists.infradead.org X-Gm-Message-State: AOJu0YzoxdeUUTcEMWQlOyoMYsAmX4RiQukdbdsN19DE13xz+YATKeg2 B4I8VlL8x5Ryoege9xdpxYffFyeCG4c2wkTmbvSvMsZdV8ickj+RPdGsqelL4u2ZP9C8g5Mni4i I+3LsVkWrQg== X-Received: from dyer22.prod.google.com ([2002:a05:7300:2316:b0:2d9:e328:e0db]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:1292:b0:2c0:c415:cfd6 with SMTP id 5a478bee46e88-2e47873bca2mr17943223eec.13.1777049324782; Fri, 24 Apr 2026 09:48:44 -0700 (PDT) Date: Fri, 24 Apr 2026 09:46:50 -0700 In-Reply-To: <20260424164721.2229025-1-irogers@google.com> Mime-Version: 1.0 References: <20260423163406.1779809-1-irogers@google.com> <20260424164721.2229025-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260424164721.2229025-29-irogers@google.com> Subject: [PATCH v5 28/58] perf syscall-counts-by-pid: Port syscall-counts-by-pid to use python module From: Ian Rogers To: acme@kernel.org, adrian.hunter@intel.com, james.clark@linaro.org, leo.yan@linux.dev, namhyung@kernel.org, tmricht@linux.ibm.com Cc: alice.mei.rogers@gmail.com, dapeng1.mi@linux.intel.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260424_094846_562031_F70A1157 X-CRM114-Status: GOOD ( 18.02 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Rewrite tools/perf/scripts/python/syscall-counts-by-pid.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 3.8x 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-by-pid.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: comm [pid]/syscalls count --------------------------------------- ---------- perf [3886080] 1 538989 16 32 203 17 3 2 257 1 204 1 15 1 0 1 perf [3886082] 7 1 real 0m3.852s user 0m3.512s sys 0m0.336s $ time python3 tools/perf/python/syscall-counts-by-pid.py perf Warning: 1 out of order events recorded. syscall events for perf: comm [pid]/syscalls count --------------------------------------- ----------- perf [3886080] write 538989 ioctl 32 sched_setaffinity 17 close 2 openat 1 sched_getaffinity 1 rt_sigreturn 1 read 1 perf [3886082] poll 1 real 0m1.011s user 0m0.963s sys 0m0.048s ``` Assisted-by: Gemini:gemini-3.1-pro-preview Signed-off-by: Ian Rogers --- v2: 1. Removed Unused Variable: Removed id_keys which was assigned but never read. 2. Fallback for Unknown Syscalls: If perf.syscall_name() returns None for an unmapped ID, it now falls back to the numeric ID string to prevent TypeError crashes during string formatting. 3. Fallback for Syscall Number Attribute: It now checks for __syscall_nr first, and if missing, falls back to checking for nr . 4. Robust Process Resolution: Added a try-except block around session.process(sample.pid).comm() to handle untracked PIDs gracefully instead of crashing on a TypeError . 5. Restored PID Filtering: The script now attempts to parse the positional argument as an integer to filter by Process ID. If that fails, it treats it as a command name (COMM) string to filter by, restoring behavior from the original legacy script. 6. Support for Custom Input Files: Added a -i / --input command-line argument to support arbitrarily named trace files, removing the hardcoded "perf.data" restriction. --- tools/perf/python/syscall-counts-by-pid.py | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 tools/perf/python/syscall-counts-by-pid.py diff --git a/tools/perf/python/syscall-counts-by-pid.py b/tools/perf/python/syscall-counts-by-pid.py new file mode 100755 index 000000000000..45a98e6e8e01 --- /dev/null +++ b/tools/perf/python/syscall-counts-by-pid.py @@ -0,0 +1,88 @@ +#!/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: dict[tuple[str, int, int], int] = defaultdict(int) +for_comm = None +for_pid = None +session = None + + +def print_syscall_totals(): + """Print aggregated statistics.""" + if for_comm is not None: + print(f"\nsyscall events for {for_comm}:\n") + elif for_pid is not None: + print(f"\nsyscall events for PID {for_pid}:\n") + else: + print("\nsyscall events:\n") + + print(f"{'comm [pid]/syscalls':<40} {'count':>10}") + print("---------------------------------------- -----------") + + sorted_keys = sorted(syscalls.keys(), key=lambda k: (k[0], k[1], k[2])) + current_comm_pid = None + for comm, pid, sc_id in sorted_keys: + if current_comm_pid != (comm, pid): + print(f"\n{comm} [{pid}]") + current_comm_pid = (comm, pid) + name = perf.syscall_name(sc_id) or str(sc_id) + print(f" {name:<38} {syscalls[(comm, pid, sc_id)]:>10}") + + +def process_event(sample): + """Process a single sample event.""" + event_name = str(sample.evsel) + if event_name == "evsel(raw_syscalls:sys_enter)": + sc_id = getattr(sample, "id", -1) + elif event_name.startswith("evsel(syscalls:sys_enter_"): + 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 + + pid = sample.sample_pid + + if for_pid and pid != for_pid: + return + + comm = "unknown" + try: + if session: + proc = session.process(pid) + if proc: + comm = proc.comm() + except (TypeError, AttributeError): + pass + + if for_comm and comm != for_comm: + return + syscalls[(comm, pid, sc_id)] += 1 + + +if __name__ == "__main__": + ap = argparse.ArgumentParser() + ap.add_argument("filter", nargs="?", help="COMM or PID to filter by") + ap.add_argument("-i", "--input", default="perf.data", help="Input file name") + args = ap.parse_args() + + if args.filter: + try: + for_pid = int(args.filter) + except ValueError: + for_comm = args.filter + + session = perf.session(perf.data(args.input), sample=process_event) + session.process_events() + print_syscall_totals() -- 2.54.0.545.g6539524ca2-goog