From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f202.google.com (mail-dy1-f202.google.com [74.125.82.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6565E3F7A8C for ; Tue, 28 Apr 2026 07:20:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777360850; cv=none; b=o0L5rHq6CIV1Coz7Vhln/UjSWBFTVTqmnmr8AVKltsHzkX1o/5FWXSRBjNmGdw2aKCu1a/dvEF8DdFxFFHqU29+3VTZruCMvBfoNGEmXEhkpI5fRVDkjaQvfur7KIA0TKqJ9BhzND0WnpEMDMZpKSQ0CTtn9t+9mmJicm3eLIJo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777360850; c=relaxed/simple; bh=3OzyHhDflAGnhfcPd/Cdhu5Dl9jWi1PgdqDYN/G82GU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Msu5WDPw7xQMIZS5vrneLgywiQYN5HVmEsgxufjHiCDTVy7m+jPY3ROGBfRt5lXb0/2i7GKucCPDLisUmE2Kc7rN7QlpiBedut3OXYDrFr97tKScna8JJX206e2CG3FEgwFcSxERJWXn7j3D+YivCr2LLFd8hI2tzNqMAnckEwQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cJQ39Yq7; arc=none smtp.client-ip=74.125.82.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cJQ39Yq7" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-2ddd8ef5343so10996771eec.1 for ; Tue, 28 Apr 2026 00:20:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777360848; x=1777965648; darn=vger.kernel.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=C1LwLJ/S5+yYNXf1JRm+e96X+YoT2PYO4quJ+4E0ZMc=; b=cJQ39Yq7Mftpo41n/qyAa6nq+7J5Pfb1BHQ8jRhI0dq/pE7yAtq2Z5qE5vR/S3Cbg+ NaDj4iw+bI4HLbbdAs6KwWcgkSoV/JrKLPAnBPYAuyryhU+a54WgcWlFtbuzABpaRuCe 0gph3oBmIoIVVCehYIQz0HLqv4+POzB24gPubzwWAj/Rt9TsUOUK9Uk8sC0rs9E69JHQ h2wAItBPFbdS8CKWHBWdJA85pRfdZZESf2ivfeEvSJ+thRYIBKhyL0ZvuqP2bPRaejqX Npg3mPdjuNIk52vRXpvLSF2a1VpZr1V4RtAwM9WorurC31k0FOBNtKwl6yZbn5KM58wD wtxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777360848; x=1777965648; 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=C1LwLJ/S5+yYNXf1JRm+e96X+YoT2PYO4quJ+4E0ZMc=; b=SNtjtzER502hTs6t5e15sCYPERqa33+eKFcIaGUN8q157/AwIm4J1Y2IWADFHC6qpF DrRL9HqalmIcdWIvcdfoOJZbXhj+E1MkVwSnHGiJQO4XZC6cBS3xFyZ3M5YKXxB93x3C bJPPllr1Rw93OOORc5Q40XQxZFL1emMu1YMpLNPBCe31ATE6SSkV3m1Tfqt0NJprApcG D3YJ2MfsaejQOqymTh3x/+JWXKzgTXytr76KhPIgxBxqTOHW0sNCbkX271A1G+F3dPHq k4d7sMyEBV++hG0DcklcuB6xLFBH596gQiMDKGNvtCj3ltbJAnTSZobBd2RyxEKhQnue PEpw== X-Forwarded-Encrypted: i=1; AFNElJ/oBUBjx/AGplDC7Bh+ws0nHN3Z+o9rO1pdoDivZYKRMYtZxTLN3M1DGdFdaVTb68h0WvJpwEPIm1hvu83OMfVh@vger.kernel.org X-Gm-Message-State: AOJu0Yw41SSSrPLdL9xUbLD2R8PAIp/ayS+J7xZbzJlW8qLaqzgNJ9ag GHGWxMmEKg3r488pdVYNP2XQxfqmpzg7mYmtnWLHRJH0TerDaH2QxFzjLecvqGHnfcATU0xb6VD fjfsjuogNew== X-Received: from dycui24.prod.google.com ([2002:a05:693c:6018:b0:2de:e63d:7daa]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:6ba1:b0:12d:de3f:f3dc with SMTP id a92af1059eb24-12dde3ff91emr365882c88.38.1777360847456; Tue, 28 Apr 2026 00:20:47 -0700 (PDT) Date: Tue, 28 Apr 2026 00:18:49 -0700 In-Reply-To: <20260428071903.1886173-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260425224951.174663-1-irogers@google.com> <20260428071903.1886173-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260428071903.1886173-45-irogers@google.com> Subject: [PATCH v8 44/58] perf sctop: Port sctop to use python module From: Ian Rogers To: acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, alice.mei.rogers@gmail.com, dapeng1.mi@linux.intel.com, james.clark@linaro.org, leo.yan@linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, tmricht@linux.ibm.com, Ian Rogers Content-Type: text/plain; charset="UTF-8" This commit ports sctop.py from tools/perf/scripts/python/ to tools/perf/python/ using a class-based structure. It also adds live mode support using the LiveSession helper with a fallback strategy for tracepoint names. Assisted-by: Gemini:gemini-3.1-pro-preview Signed-off-by: Ian Rogers --- v5: 1. Fix Fallback Logic: Check `__syscall_nr` and `nr` fields for syscall ID if `id` is missing on fallback tracepoints. 2. Fix Thread Lookup Crash: Added try-except block around `session.process()` to handle missing PIDs gracefully. --- tools/perf/python/sctop.py | 186 +++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100755 tools/perf/python/sctop.py diff --git a/tools/perf/python/sctop.py b/tools/perf/python/sctop.py new file mode 100755 index 000000000000..b94f66a8307d --- /dev/null +++ b/tools/perf/python/sctop.py @@ -0,0 +1,186 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 +""" +System call top + +Periodically displays system-wide system call totals, broken down by +syscall. If a [comm] arg is specified, only syscalls called by +[comm] are displayed. If an [interval] arg is specified, the display +will be refreshed every [interval] seconds. The default interval is +3 seconds. + +Ported from tools/perf/scripts/python/sctop.py +""" + +import argparse +from collections import defaultdict +import sys +import threading +from typing import Optional +import perf +from perf_live import LiveSession + + + + +class SCTopAnalyzer: + """Periodically displays system-wide system call totals.""" + + def __init__(self, for_comm: str | None, interval: int, offline: bool = False): + self.for_comm = for_comm + self.interval = interval + self.syscalls: dict[int, int] = defaultdict(int) + self.lock = threading.Lock() + self.stop_event = threading.Event() + self.thread = threading.Thread(target=self.print_syscall_totals) + self.offline = offline + self.last_print_time: int | None = None + self.session: Optional[perf.session] = None + + def syscall_name(self, syscall_id: int) -> str: + """Lookup syscall name by ID.""" + try: + return perf.syscall_name(syscall_id) + except Exception: # pylint: disable=broad-exception-caught + pass + return str(syscall_id) + + def process_event(self, sample: perf.sample_event) -> None: + """Collect syscall events.""" + name = str(sample.evsel) + syscall_id = getattr(sample, "id", -1) + if syscall_id == -1: + syscall_id = getattr(sample, "__syscall_nr", -1) + if syscall_id == -1: + syscall_id = getattr(sample, "nr", -1) + + if syscall_id == -1: + return + + comm = "Unknown" + if hasattr(self, 'session') and self.session: + try: + proc = self.session.find_thread(sample.sample_pid) + if proc: + comm = proc.comm() + except (TypeError, AttributeError): + pass + else: + comm = getattr(sample, "comm", "Unknown") + + if name in ("evsel(raw_syscalls:sys_enter)", "evsel(syscalls:sys_enter)"): + if self.for_comm is not None and comm != self.for_comm: + return + with self.lock: + self.syscalls[syscall_id] += 1 + + if self.offline and hasattr(sample, "time"): + interval_ns = self.interval * (10 ** 9) + if self.last_print_time is None: + self.last_print_time = sample.time + elif sample.time - self.last_print_time >= interval_ns: + self.print_current_totals() + self.last_print_time = sample.time + + def print_current_totals(self): + """Print current syscall totals.""" + # Clear terminal + print("\x1b[2J\x1b[H", end="") + + if self.for_comm is not None: + print(f"\nsyscall events for {self.for_comm}:\n") + else: + print("\nsyscall events:\n") + + print(f"{'event':40s} {'count':10s}") + print(f"{'-' * 40:40s} {'-' * 10:10s}") + + with self.lock: + current_syscalls = list(self.syscalls.items()) + self.syscalls.clear() + + current_syscalls.sort(key=lambda kv: (kv[1], kv[0]), reverse=True) + + for syscall_id, val in current_syscalls: + print(f"{self.syscall_name(syscall_id):<40s} {val:10d}") + + def print_syscall_totals(self): + """Periodically print syscall totals.""" + while not self.stop_event.is_set(): + self.print_current_totals() + self.stop_event.wait(self.interval) + # Print final batch + self.print_current_totals() + + def start(self): + """Start the background thread.""" + self.thread.start() + + def stop(self): + """Stop the background thread.""" + self.stop_event.set() + self.thread.join() + + +def main(): + """Main function.""" + ap = argparse.ArgumentParser(description="System call top") + ap.add_argument("args", nargs="*", help="[comm] [interval] or [interval]") + ap.add_argument("-i", "--input", help="Input file name") + args = ap.parse_args() + + for_comm = None + default_interval = 3 + interval = default_interval + + if len(args.args) > 2: + print("Usage: perf script -s sctop.py [comm] [interval]") + sys.exit(1) + + if len(args.args) > 1: + for_comm = args.args[0] + try: + interval = int(args.args[1]) + except ValueError: + print(f"Invalid interval: {args.args[1]}") + sys.exit(1) + elif len(args.args) > 0: + try: + interval = int(args.args[0]) + except ValueError: + for_comm = args.args[0] + interval = default_interval + + analyzer = SCTopAnalyzer(for_comm, interval, offline=bool(args.input)) + + if not args.input: + analyzer.start() + + try: + if args.input: + session = perf.session(perf.data(args.input), sample=analyzer.process_event) + analyzer.session = session + session.process_events() + else: + try: + live_session = LiveSession( + "raw_syscalls:sys_enter", sample_callback=analyzer.process_event + ) + except OSError: + live_session = LiveSession( + "syscalls:sys_enter", sample_callback=analyzer.process_event + ) + live_session.run() + except KeyboardInterrupt: + pass + except IOError as e: + print(f"Error: {e}") + finally: + if args.input: + analyzer.print_current_totals() + else: + analyzer.stop() + + +if __name__ == "__main__": + main() -- 2.54.0.545.g6539524ca2-goog