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 63A123AEF4E for ; Sat, 25 Apr 2026 22:51:54 +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=1777157515; cv=none; b=G5X5I/dAN843ShbOhRqjNTaxNtG9uR0245I2sIFn/wVYlZhcscTvJDnJKt0S0iP1s4Xqpwm1nc3bLd2RtRYc3B6ynR3ZZd+ro80t5MNbFRmz/e4e+OGbm8I3kfWubMGlJFQUoAvbCpK1MWBYJS2tkfk3ZZsbsbHLDSDU7nshbi8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777157515; c=relaxed/simple; bh=j+1DPJ7hHqMkCRtiNL7ZTTuDpfoCTqlYLmFWD1lm5jM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mFWqQWBKOJg4DFyr3IIn84P/o87t+NlbEa/m9/fESWXwEeg27JQhTi6sBaUW034dGviBaVkQFs+DfoFrZ83c9HG2kI61RRwHFpp3MPvSKKjWmqbNdx0lWvAjV/PePMB/ZCDCcZBquFNZpVqX5+tYAhJ0y+slamXKiFfTLNs1dnI= 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=lCDrn245; 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="lCDrn245" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-2ba9a744f7dso11750590eec.0 for ; Sat, 25 Apr 2026 15:51:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777157514; x=1777762314; 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=Z8uDU0iX99Tgv3o+fYqZ9yp1w2za7K0HbBrM8LxB5IA=; b=lCDrn245Gj2kRjUOv+H7fTT4nNqmrNXzzLG0LDp1CHqMeZ84n+VyvvMKGxj1peSHmo JJCeTBH9O6njQUXHUYUK8/YlbS0qQmqcNRITAYJ30Y22SW4JBiWRmkjUIk2KM8plAgQz da3V2s3Kn9rayQOIKQo7nSi9CRccQ80LqIZvq2p66fx2jT1mTvkK5yILCEIxsoXNlLqb z6z8virhZ5rISpTxCqdMYSCi5vRgckm8Jv6iUhKkMSKQeaRjqJWX9gf031N+cYSuqOHN AQG8tSYCasSpYo7wjNZsakE8GDHQ+gbvez/pkgSZIC/9RAnKJk6+q0/jUQ9Un/2Udb+q 0ZmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777157514; x=1777762314; 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=Z8uDU0iX99Tgv3o+fYqZ9yp1w2za7K0HbBrM8LxB5IA=; b=AyPZlDJzSwD3feEYUxAAzIaLHxTu1rC1oLECtARfvHuzxlToPWzZMIrka9Fp7QTWNG xGJWVF+Ek3TSf9hnp2dNDN+JqVmVU8yPROqxCmU1qRAJTvTq/VWry9ksmNrpxAQ1bEVD CGLFyjuirHAbr77yLf80FzEjt9foQYBrqGCFDkDURJgwJHIZjkpm09mMJl8jDBjQ+kDM tsFJCgpzCU4DkpVmVVr0jqjJXbatosq5nnTLgNq4flGApZvMkfttH6wfBkgx0eyesidz 4lRmXh69SXvJIDfqj5sjzo+gH28Vle/CtI0TV8WrqrRwgrLrFc8/dS6j8cO+fzQqdXe0 s9ag== X-Forwarded-Encrypted: i=1; AFNElJ/ERha5yUy4Fmo4rzQZJIzr/6EgdR21bxBR9lsQdkbaqWuOEsqexKbxpCCFngMveE5bqVgVxWBG3033lJ8=@vger.kernel.org X-Gm-Message-State: AOJu0YwAVSiTyxjM2WDcxKYv4XNnzz8LGANT1vANtrt68rjUC4c0dIMm +FewqOLZKPe9CVwjRlNbP8zTAknodG9z6ZTjUUx0joFs+saI1TphVu2hwZI5hp+lPTB//ZpS0q/ 3Gd3ZK9Eu+w== X-Received: from dyjf14.prod.google.com ([2002:a05:7300:680e:b0:2d9:9fa3:5aa6]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:2b08:b0:2c1:7afc:df06 with SMTP id 5a478bee46e88-2e464ea7057mr18321401eec.5.1777157513342; Sat, 25 Apr 2026 15:51:53 -0700 (PDT) Date: Sat, 25 Apr 2026 15:49:40 -0700 In-Reply-To: <20260425224951.174663-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@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-49-irogers@google.com> Subject: [PATCH v7 48/59] perf rw-by-file: Port rw-by-file 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" Port the legacy Perl script rw-by-file.pl to a python script using the perf module in tools/perf/python. The new script uses a class-based architecture and leverages the perf.session API for event processing. It tracks read and write activity by file descriptor for a given program name, aggregating bytes requested/written and total counts. Complications: - Had to split long lines in __init__ to satisfy pylint. - pylint warns about the module name not being snake_case, but it is kept for consistency with the original script name. Assisted-by: Gemini:gemini-3.1-pro-preview Signed-off-by: Ian Rogers --- v2: - Fixed Substring Matching: Replaced if "sys_enter_read" in event_name: with an exact match against syscalls:sys_enter_read and raw_syscalls:sys_enter_read using sample.evsel.name . This prevents variants like readv or readlink from incorrectly triggering the read logic. Similar fixes were applied for write events. - Fixed Silent Error Dropping: Instead of silently returning when expected fields are missing (causing AttributeError ), the script now increments the self.unhandled counter for that event. This ensures that missing data or unexpected event variants are reported to the user instead of quietly skewing the results. v6: - Fixed `AttributeError` by using `str(sample.evsel)` to get event name. --- tools/perf/python/rw-by-file.py | 103 ++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100755 tools/perf/python/rw-by-file.py diff --git a/tools/perf/python/rw-by-file.py b/tools/perf/python/rw-by-file.py new file mode 100755 index 000000000000..2103ac0412bb --- /dev/null +++ b/tools/perf/python/rw-by-file.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Display r/w activity for files read/written to for a given program.""" + +import argparse +from collections import defaultdict +import sys +from typing import Optional, Dict +import perf + +class RwByFile: + """Tracks and displays read/write activity by file descriptor.""" + def __init__(self, comm: str) -> None: + self.for_comm = comm + self.reads: Dict[int, Dict[str, int]] = defaultdict( + lambda: {"bytes_requested": 0, "total_reads": 0} + ) + self.writes: Dict[int, Dict[str, int]] = defaultdict( + lambda: {"bytes_written": 0, "total_writes": 0} + ) + self.unhandled: Dict[str, int] = defaultdict(int) + self.session: Optional[perf.session] = None + + def process_event(self, sample: perf.sample_event) -> None: + """Process events.""" + event_name = str(sample.evsel)[6:-1] + + pid = sample.sample_pid + assert self.session is not None + try: + comm = self.session.find_thread(pid).comm() + except Exception: # pylint: disable=broad-except + comm = "unknown" + + if comm != self.for_comm: + return + + if event_name in ("syscalls:sys_enter_read", "raw_syscalls:sys_enter_read"): + try: + fd = sample.fd + count = sample.count + self.reads[fd]["bytes_requested"] += count + self.reads[fd]["total_reads"] += 1 + except AttributeError: + self.unhandled[event_name] += 1 + elif event_name in ("syscalls:sys_enter_write", "raw_syscalls:sys_enter_write"): + try: + fd = sample.fd + count = sample.count + self.writes[fd]["bytes_written"] += count + self.writes[fd]["total_writes"] += 1 + except AttributeError: + self.unhandled[event_name] += 1 + else: + self.unhandled[event_name] += 1 + + def print_totals(self) -> None: + """Print summary tables.""" + print(f"file read counts for {self.for_comm}:\n") + print(f"{'fd':>6s} {'# reads':>10s} {'bytes_requested':>15s}") + print(f"{'-'*6} {'-'*10} {'-'*15}") + + for fd, data in sorted(self.reads.items(), + key=lambda kv: kv[1]["bytes_requested"], reverse=True): + print(f"{fd:6d} {data['total_reads']:10d} {data['bytes_requested']:15d}") + + print(f"\nfile write counts for {self.for_comm}:\n") + print(f"{'fd':>6s} {'# writes':>10s} {'bytes_written':>15s}") + print(f"{'-'*6} {'-'*10} {'-'*15}") + + for fd, data in sorted(self.writes.items(), + key=lambda kv: kv[1]["bytes_written"], reverse=True): + print(f"{fd:6d} {data['total_writes']:10d} {data['bytes_written']:15d}") + + if self.unhandled: + print("\nunhandled events:\n") + print(f"{'event':<40s} {'count':>10s}") + print(f"{'-'*40} {'-'*10}") + for event_name, count in self.unhandled.items(): + print(f"{event_name:<40s} {count:10d}") + + def run(self, input_file: str) -> None: + """Run the session.""" + self.session = perf.session(perf.data(input_file), sample=self.process_event) + self.session.process_events() + self.print_totals() + +def main() -> None: + """Main function.""" + parser = argparse.ArgumentParser(description="Trace r/w activity by file") + parser.add_argument("comm", help="Filter by command name") + parser.add_argument("-i", "--input", default="perf.data", help="Input file") + args = parser.parse_args() + + analyzer = RwByFile(args.comm) + try: + analyzer.run(args.input) + except IOError as e: + print(e, file=sys.stderr) + sys.exit(1) + +if __name__ == "__main__": + main() -- 2.54.0.545.g6539524ca2-goog