From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.201]) (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 94D9F3ACF14 for ; Sat, 25 Apr 2026 22:51:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777157508; cv=none; b=H7BAABcG+v0pT+EG3zzaZx5/K7W3BNC3+7fUXGG+6LVD/sfmBDV0seXaGIPn4goCL79rMv110e8E572QS0unSj51RpAdFAMFVTnnkuFvZsgcrshxdbMsxBbq7MlK+e6WMic3K0W0kPutzKJkXYn7jyXlwiH5KFWyTcNJRhvW3m4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777157508; c=relaxed/simple; bh=3OzyHhDflAGnhfcPd/Cdhu5Dl9jWi1PgdqDYN/G82GU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MWTJxP0/SeYxBYp8ozYIAmHLIBuKrVV4XZOBBoCE50xEZUBk7K8v/4I3AYrGKn0al14oRwNdmRn/SxO0SxfyhJAUsuNET+4qsI8xapXUr/2Rp/j6bw7L3ytJxB61E/7diiO49T+ayBpMITpSbmwUHcky0aLxhxIAU7//vsUikrc= 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=Dv8jfiT8; arc=none smtp.client-ip=74.125.82.201 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="Dv8jfiT8" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-2eaed3d96d7so860849eec.0 for ; Sat, 25 Apr 2026 15:51:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777157506; x=1777762306; 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=Dv8jfiT8+LbHfLL/IIFcA9BFTflcvBNy+ZEyg2KMdTvDoLsxskionuWzlU3NHpuoAw itmWZKlmv0wnMmg89pC8Mm7iPJg00/ZsY5VUWePbGgmaN0U4MGJox14XB+V1rTHt/Lw+ MCJL28YXk/EFX7r/sr9rsucK/DqQrrfVPni55qA9l9HgsiN4mSWc65XUcjblUEdQ7b6q IbarwKG+5j+2B+SCcCOW631XRJhOO9+QmtvS/Qudc7CQitvpQ5I2JNZS1DjinfTiGAxd 8h92HYN3CaCjnlFUdltBqZkxGXHGNt9Jkwqw1Kxaqxx/7LLoqtzo8B6rXZbG80jLDZS+ 6JOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777157506; x=1777762306; 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=jkeNufNEBZTAGQe8K9RARI+4pXLFmtAvj8bDsKlCTp/G3RUHDtYncSf/OaCXrgkWiv YoF+q7tlBn7tniwdw2A87f4lhN2bq56biIJOgNlKehDki6najBSHjT3XlqP591cMoqrZ pXcw/M84Vj0y5sLrb+A7i3/iW6m1Xv/dJV1PfPyc1+X1uU0kzBO11OIg3Q+mqFTduyGg J7IS1svbPcy1Z0Tzv72wYJOU14CdmXMC6w/b5U3Vk49aF5curSKsTZIQyGQr37Gg0qcL Faq38NK4MBjR9zL39WiAkkpms34hkBpnnc/v1IQH8TnvoUPwuye/yQbLuSqgHYvIzHdW DA3w== X-Forwarded-Encrypted: i=1; AFNElJ84ktRZEXoHjLDcA6cUr/3nHPxKDaOzCkjkHuMCPBcRgMbqbEYWAk/NjoS2BlehDwR0XemDP3VgRpFv7mhC4TQK@vger.kernel.org X-Gm-Message-State: AOJu0YzZwtUhOJGCTPlDRGPDreEFTNP66KvmkJXrA1nXcKEic+XucuWT TX6ghMB+wZ8l+8vc/pHksu/1wBdVYHPppdNKI7Gz6p9rMH9ok8sFR62LUQoehUeMQjtXdxrdUxI 1YifMGuV+Uw== X-Received: from dycnr11-n1.prod.google.com ([2002:a05:7300:e9cb:10b0:2e5:fa99:f6e4]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:2c84:b0:2d9:2b54:ea9c with SMTP id 5a478bee46e88-2e477c9bc5bmr22454427eec.18.1777157505556; Sat, 25 Apr 2026 15:51:45 -0700 (PDT) Date: Sat, 25 Apr 2026 15:49:36 -0700 In-Reply-To: <20260425224951.174663-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: <20260425174858.3922152-1-irogers@google.com> <20260425224951.174663-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260425224951.174663-45-irogers@google.com> Subject: [PATCH v7 44/59] perf sctop: Port sctop 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" 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