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 D9705FF8862 for ; Sat, 25 Apr 2026 17:50:50 +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=FIUPQnj9Sm31hxCjwi2GzFaWFVwRkKGchfSFKojE+/g=; b=hPy6w4VdfVgDGTWd7cwJyCuaHc 8EfBlKaR7zzHfazL970eu52GNU/sEydfK1gQ+79mZK4M6i7+IPvgDhE5nJ3zgS+0VR4o+UwNch84u 7AAoEDRZln+TxP9Kx53UYU4ChjQQ6K6CClz3oZ9/aSOzXQTIKq94kM80kCtAutIFlTvgoua5kJsvH Ee19p/aU2ncypemTopOBSeQQmr8xiL3qJQrI1R2GxMOzPBqY6VK8xM9iOwRHeI2TASWpdfxd5n1kT kFvB9Fn4Uq95QWwnXSrQrV3qw9dQayrcw48fNinZQADxgNjKGlhLXm/QPQwDmdvwnH9U1P8L8CSPO 9Nb9A9wg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1wGh9E-0000000Edal-1Pnv; Sat, 25 Apr 2026 17:50:32 +0000 Received: from mail-dl1-x1249.google.com ([2607:f8b0:4864:20::1249]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1wGh8g-0000000Ecwr-15Nv for linux-arm-kernel@lists.infradead.org; Sat, 25 Apr 2026 17:50:11 +0000 Received: by mail-dl1-x1249.google.com with SMTP id a92af1059eb24-12db218e265so14120339c88.0 for ; Sat, 25 Apr 2026 10:49:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777139397; x=1777744197; 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=FIUPQnj9Sm31hxCjwi2GzFaWFVwRkKGchfSFKojE+/g=; b=j/aNTbLWd99KEOQWlNw3UXjzs8uw3+ZBdjZeufaIY7S0m/unYiA6v92oGlEaN35/C3 nmLOhgJ0lWfYeeb/dFJn25yFnDRLlysw4R99ZppnVSHX85cM5fqhFNEcMeLEcBwTYqks y7yE7QIrvElDw/ihmQ9kEWw/LWxEbAb5zV+fbsQ9J2v5nDzXm+au5qmexMRMqF9VQA1y ViHYfqngbp1EQlRZnlCQXNc2hOWDmslGC7mGkz123xqL6P5Qth2MlK/G38iHmdlFpVH7 9c2+sopOhg70noO6Zrkbpo+p0hqVY1N/Ebm99z98B4tRyNquA1cfnEhGXaE5O4xD+TQK 0sxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777139397; x=1777744197; 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=FIUPQnj9Sm31hxCjwi2GzFaWFVwRkKGchfSFKojE+/g=; b=aG8E/W2b1/dsDBj3GX5NRH3vuCC0GM8Z+ERJd9Z62GJWxygDO9y2+onbf+eOrAd9FF zfCCAJNa2SKtz2VswupcxrVwHVU2bE/ta3Q8+UCaIgVkuVi9wu9MKD9JXr3QHF9haeCq XHpfABJ5EpR/nQyc4HufFhixEXd37F7WU1lnG4GdWBM2hg33uf+P40PPu7vBPAw8Ymqg /hb0T5LDuv0Tx07FEiXSV7VsHUpfl8lXR1YbEfli2dKGieSS5IyxzZr/FWf9HnOauPW4 dI4WkxxrrxskL3dvpHwINpRXY8P65foiYiEDIkjFzYgbnMvSjb2TO/xfXeIMfhQQ2Yii sokQ== X-Forwarded-Encrypted: i=1; AFNElJ9h5F8tNL34srpbenyAMrAR82SQySJri/BfigveRbyApn/DVmbBIVEJhucxHGxEwTl4pNtA7V3aPXgk5PKJ01As@lists.infradead.org X-Gm-Message-State: AOJu0YwqnYuVx4bDbvAg+YcYSmr53X9LSU5UYBTr6FuK7O+/lGOUJNL0 ComlYkgEatCSD051U/4GlibFyRTsD1xYuuYzoxp4QVmnMuM8F10gJFadctNrCN6PfGfAbHZA8ge 6Jdqzt0RJCA== X-Received: from dlbpt10.prod.google.com ([2002:a05:7022:e80a:b0:12d:b1c5:1596]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:2512:b0:12d:c039:65d1 with SMTP id a92af1059eb24-12dc03968d7mr4725972c88.1.1777139396731; Sat, 25 Apr 2026 10:49:56 -0700 (PDT) Date: Sat, 25 Apr 2026 10:48:26 -0700 In-Reply-To: <20260425174858.3922152-1-irogers@google.com> Mime-Version: 1.0 References: <20260424164721.2229025-1-irogers@google.com> <20260425174858.3922152-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260425174858.3922152-29-irogers@google.com> Subject: [PATCH v6 28/59] 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-20260425_104958_431582_24EA8429 X-CRM114-Status: GOOD ( 18.06 ) 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..ff962334a143 --- /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.find_thread(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