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 BCD70FED3F0 for ; Fri, 24 Apr 2026 16:50:59 +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=4vO51mkUv8czx4QMNQNbvlHwQFmUDbpKAVfeUaPjKEk=; b=rxKzcSZG2N8RAvpcj9v6ttg9Cc HS7St+z1gat1btczqy2TGTS3SQwmiIF9bi/ol30Koj9J5xFKkDw9eVYMoWAwh4WHxwX/nyxPcz5+7 Aqcd/c7McldMSd8949O0GEMnv/aPESVd4/C5TpvYGZV0jKRYM54px6w/dYY2TL2Y1HWIUiLcOzptN yJAmhLe29bx7YHUoJPZMluzBz+ReRR28WqKOjtn45Bn7PnoydBsvKF07c9Ebd3xr4wijx376YspU/ lk362kgGHGqAo/jIx1FqLM6PFI1S8CHBZMgwHh0C1PTi5OBQBveIBdPcf9Jx475+d4DJXCsFUz1Fc dvwEluPA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1wGJjq-0000000DWBH-2nSq; Fri, 24 Apr 2026 16:50:46 +0000 Received: from mail-dy1-x1349.google.com ([2607:f8b0:4864:20::1349]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1wGJih-0000000DUtI-1RXa for linux-arm-kernel@lists.infradead.org; Fri, 24 Apr 2026 16:49:45 +0000 Received: by mail-dy1-x1349.google.com with SMTP id 5a478bee46e88-2d8a677cdfaso8557432eec.1 for ; Fri, 24 Apr 2026 09:49:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777049373; x=1777654173; 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=4vO51mkUv8czx4QMNQNbvlHwQFmUDbpKAVfeUaPjKEk=; b=eg14L3foY4jjMZce4Cusgc0osB/kxUdt9HPE1GdFHB61Pmwn5TFniXBXby0Lw1oqI2 oWvyvo+ZGV8jSwaj5WzfrwEMDCUPzZTG9Qs7Il8JoaEuKT5XtLtTjdnisWmeqNhxOpzy p0mqMfMsaehdFhdOGtYBzxVuJrD7LFZsUR6DSc/xBqI7GjQpYe3gCJ/taodm7npotyoA EQ5/tkdrKm3kuDNEJjWXiQUkLGqtjyphMDJEx4JYG5KjS6DwNL06gClkMmRPzMJ7hXui LFESxBLCeCbPgugD+LQ5r/0jbC9XStoqh39xw8Ej8E9Qnbwp8noU6/ZOc5V39Ly3OVaX Xsmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777049373; x=1777654173; 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=4vO51mkUv8czx4QMNQNbvlHwQFmUDbpKAVfeUaPjKEk=; b=HxtT50qYUkNBJTimUBZU0z/9lOVeMuYM7SrJA4p+qlVADPK0SdLLIFR6ZgsrvuEUfk cShEUQEimodPXK3tOrinXCRT0NXpZvbg1qR8SQu55vSiWP4JKkagTmhkT+ZJCcEUmdL5 A/6VuPf1rPWZ9URTSj0Jf7vg7YCJzRTyaYRPQMqnw6N7aI850/8IGbYkfqyPn5a4iGV1 oxVU1C2CXXSUSd0TXQMfg6sMzbzCPk91Y9iSUjeuvF6LDvIyc6ySFOuVB/R2j+PYdZiI pV20c4WJ+NwXq7Hm9X1ELY9faPlv8ZbfvdEXpBNMtOOKC5XTInDjeIqkT8APibUKBcnf sv9Q== X-Forwarded-Encrypted: i=1; AFNElJ/LSdmMDJUiiNifvSQP+ca5/DFQL1tLWPJAhKfWolqKfhU84k9SoLFn3OHKf0gcbEtgtIOfIhe7N9fBnPnvXXW3@lists.infradead.org X-Gm-Message-State: AOJu0Yy++mZztdzO1PCeQgGRsqYLTg4b/SaSZHhLBOrjwh+LXutuzQF0 v41/GEucyCIL4xfPDmObsYm/ZQWMmTTUW8/hIKmCMLHVUgcktzZsS0eHJtPCWllCEAey0mQ/5Lh 5VNfjWQrlxw== X-Received: from dycoy3.prod.google.com ([2002:a05:7301:fc03:b0:2e0:fe68:96c7]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:5ba1:b0:2dd:8ac2:9f7a with SMTP id 5a478bee46e88-2eada6d6539mr2779618eec.11.1777049372661; Fri, 24 Apr 2026 09:49:32 -0700 (PDT) Date: Fri, 24 Apr 2026 09:47:13 -0700 In-Reply-To: <20260424164721.2229025-1-irogers@google.com> Mime-Version: 1.0 References: <20260423163406.1779809-1-irogers@google.com> <20260424164721.2229025-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260424164721.2229025-52-irogers@google.com> Subject: [PATCH v5 51/58] perf wakeup-latency: Port wakeup-latency 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-20260424_094935_539242_A2B893BC X-CRM114-Status: GOOD ( 15.98 ) 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 the legacy Perl script wakeup-latency.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 measures wakeup latency by tracking timestamps of sched:sched_wakeup and sched:sched_switch events. Complications: - Used min() and max() built-in functions instead of if blocks to satisfy pylint recommendations. - 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 Wakeup Latency Logic: Modified the script to track wakeup timestamps per task (using sample.pid as the key) instead of per CPU. This ensures that context switches are correctly paired with the specific task that was woken up, even if multiple tasks are woken up on the same CPU or if a task is migrated to a different CPU before running. - Prevented Memory Growth: Added del self.last_wakeup[next_pid] after successful latency calculation to prevent the dictionary from growing unbounded over time. - Added Error Tracking: Added try-except blocks around tracepoint field access in process_event() and tracked missing fields in self. unhandled instead of ignoring them. --- tools/perf/python/wakeup-latency.py | 88 +++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 tools/perf/python/wakeup-latency.py diff --git a/tools/perf/python/wakeup-latency.py b/tools/perf/python/wakeup-latency.py new file mode 100755 index 000000000000..1b0db115abcf --- /dev/null +++ b/tools/perf/python/wakeup-latency.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Display avg/min/max wakeup latency.""" + +import argparse +from collections import defaultdict +import sys +from typing import Optional, Dict +import perf + +class WakeupLatency: + """Tracks and displays wakeup latency statistics.""" + def __init__(self) -> None: + self.last_wakeup: Dict[int, int] = defaultdict(int) + self.max_wakeup_latency = 0 + self.min_wakeup_latency = 1000000000 + self.total_wakeup_latency = 0 + self.total_wakeups = 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) + sample_time = sample.sample_time + + if "sched:sched_wakeup" in event_name: + try: + pid = sample.pid + self.last_wakeup[pid] = sample_time + except AttributeError: + self.unhandled[event_name] += 1 + elif "sched:sched_switch" in event_name: + try: + next_pid = sample.next_pid + wakeup_ts = self.last_wakeup.get(next_pid, 0) + if wakeup_ts: + latency = sample_time - wakeup_ts + self.max_wakeup_latency = max(self.max_wakeup_latency, latency) + self.min_wakeup_latency = min(self.min_wakeup_latency, latency) + self.total_wakeup_latency += latency + self.total_wakeups += 1 + del self.last_wakeup[next_pid] + except AttributeError: + self.unhandled[event_name] += 1 + else: + self.unhandled[event_name] += 1 + + def print_totals(self) -> None: + """Print summary statistics.""" + print("wakeup_latency stats:\n") + print(f"total_wakeups: {self.total_wakeups}") + if self.total_wakeups: + avg = self.total_wakeup_latency // self.total_wakeups + print(f"avg_wakeup_latency (ns): {avg}") + else: + print("avg_wakeup_latency (ns): N/A") + print(f"min_wakeup_latency (ns): {self.min_wakeup_latency}") + print(f"max_wakeup_latency (ns): {self.max_wakeup_latency}") + + 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 wakeup latency") + parser.add_argument("-i", "--input", default="perf.data", help="Input file") + args = parser.parse_args() + + analyzer = WakeupLatency() + 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