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 11857FF885A for ; Sat, 25 Apr 2026 22:52:09 +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=VNEC1d+XQU6xcwJqo0QpL4AVBd93i989y6KjyJp07QI=; b=0RNFF9iI9AGzBPlKpnf108stGN PZ8spdZSQ2MoEALxKA/4CFl/Gz+0I0SCmeaxrciEeDwdCCAOtdxPXXUo4q/jrNJRZXjLPm5RjNDWl scrox6Z1T6lr3RI3jL//MZLr8n0d4tfhXiCcuHJhmYQKztpYiv+aw04JlrgDoqfc1iHJH4p01t0Oi Vorp8n3btyE6ahqnPZ1B67C9HoWH5K8m8c9Rr3s/IYJWtT3tBP838OyiLVYuM7RW8p6sQ29RC7KG9 tVfSLXaaT8gcMkl9lOHWDg0LUXpD3/b2CBrJ/Wj/5BS985bhyGT0MMI8hff96mTw8iWGmzPFMtHX/ /77RPPSQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1wGlqu-0000000EzbB-2MIY; Sat, 25 Apr 2026 22:51:56 +0000 Received: from mail-pl1-f202.google.com ([209.85.214.202]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1wGlq7-0000000Eyf0-48rk for linux-arm-kernel@lists.infradead.org; Sat, 25 Apr 2026 22:51:23 +0000 Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2b2ecc96a9aso79068505ad.1 for ; Sat, 25 Apr 2026 15:51:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777157465; x=1777762265; 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=VNEC1d+XQU6xcwJqo0QpL4AVBd93i989y6KjyJp07QI=; b=aLQZvW4A2Atw5dkh+xw+KXXLsNJbI8MHoGd27KDCspZj/BbjjUYoDhJWI4cGsM3BWz sJkg/xLwOhvlammeM7rsVyh27uV1f7Rbtlz7wMqKgjVViW6akuAMrjVoWbdXzrH5CHbR 16IFkQUCtgv/d/xrFcPi1i4LV5G3uN9rRm1lVdrVZHtn/6YI+II+Sk2KBVUd1LWjHYa5 jUDXv3ptnlYZ8aZ++OBYsJxSu0U3ChnLY7hjzVAXIdN92DoZtOANkwjZ7e7n35PBpVlO RgzX+SNGQkfmFYOfP+XV/aXYaDsRLBitYyzIR1eTRn5VY4MejIFGWQTc8aFi4hVsztpr Oq9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777157465; x=1777762265; 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=VNEC1d+XQU6xcwJqo0QpL4AVBd93i989y6KjyJp07QI=; b=so+NLAZGfgoJhvNpbzkY/UBunQgb2LbsurCkTAY+vNZmg6wVP2wy+aEdpxxBZgse2W +FdPX65FEiFD4OdMQfWpyCc6MMVis8q5LAoJt4ow+b13RrSZDhSQgVij+cXqXeR33SnU LZ+5AD5rYL6q0NJQzHbJPW7Dbfag9WcUjbmAJHL5AfYBkKyTsLP8ehZgrNQRBHoC3PEI m6MxiouX5LrPMOww7Dafh0/I0Rc9JiyVk8Mcz3w1jaHzN23ZuSPjdCBoWsynFc2xaN07 cLtKezh+wFjUHbvTJOCSMykjdh1+ZkEyuWs2NXTtENh1oDoDLznfc2O9R63K1yNaF7Vu yiPA== X-Forwarded-Encrypted: i=1; AFNElJ+pcdO5BYE7c1V5phoXW2eYmxe7g6qXj8KoxZqf7VNJFGmt4c9qwKd5vy66ZUC91Chsq+7x+R6hUj5vwhdQ1NAS@lists.infradead.org X-Gm-Message-State: AOJu0Yzj4G+giFxjX94UllAz3rS+PbGmdTETxAp5lJlYQ2yUQm14V7D/ ppl2qY7czHGBlxAe67yaQyg22tslMZGx4LoIfUN1o9GdH5q44RR2aHhlieeAyOgxUMMmNJqpHDs xMaRe0qjqZA== X-Received: from plfh1.prod.google.com ([2002:a17:902:f541:b0:2b7:f9d8:3d1b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1ac6:b0:2b2:5840:808e with SMTP id d9443c01a7336-2b5f9ee9487mr368769035ad.10.1777157465041; Sat, 25 Apr 2026 15:51:05 -0700 (PDT) Date: Sat, 25 Apr 2026 15:49:17 -0700 In-Reply-To: <20260425224951.174663-1-irogers@google.com> 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-26-irogers@google.com> Subject: [PATCH v7 25/59] perf stat-cpi: Port stat-cpi 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_155108_161888_DEB64D14 X-CRM114-Status: GOOD ( 17.41 ) 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 Port stat-cpi.py from the legacy framework to a standalone script. Support both file processing mode (using perf.session) and live mode (reading counters directly via perf.parse_events and evsel.read). Use argparse for command line options handling. Calculate and display CPI (Cycles Per Instruction) per interval per CPU/thread. Assisted-by: Gemini:gemini-3.1-pro-preview Signed-off-by: Ian Rogers --- v2: 1. Accurate CPI Calculation (Multiplexing Support): - Before: The get() method returned the raw counter value directly, ignoring whether the counter ran for the full interval. - After: The get() method now scales the raw value by the ratio of enabled time to running time ( val * (ena / float(run)) ) when run > 0 . This handles cases where PMU counters are overcommitted and multiplexed. 2. Per-Interval CPI in File Mode: - Before: store() saved absolute counter values as read from PERF_RECORD_STAT . Since these are cumulative from the start of the trace, and data.clear() was called every round, the script computed cumulative CPI rather than per-interval CPI. - After: store() now computes the delta between the current absolute value and the value from the previous interval. It saves this delta in self.data and retains the absolute value in self. prev_data for the next delta computation. 3. Prevention of Dummy Output (Cartesian Product Fix): - Before: self.cpus and self.threads lists accumulated all unique CPUs and threads seen independently. The nested loops in print_interval() then created a Cartesian product of all seen CPUs and threads, querying data for combinations that might never have occurred. - After: Replaced lists with a self.recorded_pairs set that stores (cpu, thread) tuples only when a sample actually records them. The output loop now iterates strictly over these verified pairs. --- tools/perf/python/stat-cpi.py | 151 ++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100755 tools/perf/python/stat-cpi.py diff --git a/tools/perf/python/stat-cpi.py b/tools/perf/python/stat-cpi.py new file mode 100755 index 000000000000..4b1f1f69c94a --- /dev/null +++ b/tools/perf/python/stat-cpi.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 +"""Calculate CPI from perf stat data or live.""" + +import argparse +import sys +import time +from typing import Any, Optional +import perf + +class StatCpiAnalyzer: + """Accumulates cycles and instructions and calculates CPI.""" + + def __init__(self, args: argparse.Namespace) -> None: + self.args = args + self.data: dict[str, tuple[int, int, int]] = {} + self.prev_data: dict[str, tuple[int, int, int]] = {} + self.recorded_pairs: set[tuple[int, int]] = set() + + def get_key(self, event: str, cpu: int, thread: int) -> str: + """Get key for data dictionary.""" + return f"{event}-{cpu}-{thread}" + + def store_key(self, cpu: int, thread: int) -> None: + """Store CPU and thread IDs.""" + self.recorded_pairs.add((cpu, thread)) + + def store(self, event: str, cpu: int, thread: int, counts: tuple[int, int, int]) -> None: + """Store counter values, computing difference from previous absolute values.""" + self.store_key(cpu, thread) + key = self.get_key(event, cpu, thread) + + val, ena, run = counts + if key in self.prev_data: + prev_val, prev_ena, prev_run = self.prev_data[key] + cur_val = val - prev_val + cur_ena = ena - prev_ena + cur_run = run - prev_run + else: + cur_val = val + cur_ena = ena + cur_run = run + + self.data[key] = (cur_val, cur_ena, cur_run) + self.prev_data[key] = counts # Store absolute value for next time + + def get(self, event: str, cpu: int, thread: int) -> float: + """Get scaled counter value.""" + key = self.get_key(event, cpu, thread) + if key not in self.data: + return 0.0 + val, ena, run = self.data[key] + if run > 0: + return val * (ena / float(run)) + return float(val) + + def process_stat_event(self, event: Any, name: Optional[str] = None) -> None: + """Process PERF_RECORD_STAT and PERF_RECORD_STAT_ROUND events.""" + if event.type == perf.RECORD_STAT: + if name: + if "cycles" in name: + event_name = "cycles" + elif "instructions" in name: + event_name = "instructions" + else: + return + self.store(event_name, event.cpu, event.thread, (event.val, event.ena, event.run)) + elif event.type == perf.RECORD_STAT_ROUND: + timestamp = getattr(event, "time", 0) + self.print_interval(timestamp) + self.data.clear() + self.recorded_pairs.clear() + + def print_interval(self, timestamp: int) -> None: + """Print CPI for the current interval.""" + for cpu, thread in sorted(self.recorded_pairs): + cyc = self.get("cycles", cpu, thread) + ins = self.get("instructions", cpu, thread) + cpi = 0.0 + if ins != 0: + cpi = cyc / float(ins) + t_sec = timestamp / 1000000000.0 + print(f"{t_sec:15f}: cpu {cpu}, thread {thread} -> cpi {cpi:f} ({cyc:.0f}/{ins:.0f})") + + def read_counters(self, evlist: Any) -> None: + """Read counters live.""" + for evsel in evlist: + name = str(evsel) + if "cycles" in name: + event_name = "cycles" + elif "instructions" in name: + event_name = "instructions" + else: + continue + + for cpu in evsel.cpus(): + for thread in evsel.threads(): + try: + counts = evsel.read(cpu, thread) + self.store(event_name, cpu, thread, + (counts.val, counts.ena, counts.run)) + except OSError: + pass + + def run_file(self) -> None: + """Process events from file.""" + session = perf.session(perf.data(self.args.input), stat=self.process_stat_event) + session.process_events() + + def run_live(self) -> None: + """Read counters live.""" + evlist = perf.parse_events("cycles,instructions") + if not evlist: + print("Failed to parse events", file=sys.stderr) + return + try: + evlist.open() + except OSError as e: + print(f"Failed to open events: {e}", file=sys.stderr) + return + + print("Live mode started. Press Ctrl+C to stop.") + try: + while True: + time.sleep(self.args.interval) + timestamp = time.time_ns() + self.read_counters(evlist) + self.print_interval(timestamp) + self.data.clear() + self.recorded_pairs.clear() + except KeyboardInterrupt: + print("\nStopped.") + finally: + evlist.close() + +def main() -> None: + """Main function.""" + ap = argparse.ArgumentParser(description="Calculate CPI from perf stat data or live") + ap.add_argument("-i", "--input", help="Input file name (enables file mode)") + ap.add_argument("-I", "--interval", type=float, default=1.0, + help="Interval in seconds for live mode") + args = ap.parse_args() + + analyzer = StatCpiAnalyzer(args) + if args.input: + analyzer.run_file() + else: + analyzer.run_live() + +if __name__ == "__main__": + main() -- 2.54.0.545.g6539524ca2-goog