From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 83EE63E5EC2 for ; Fri, 24 Apr 2026 16:48:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777049317; cv=none; b=VLsbpZ+PxbS26YmNbfWN6dY0ojEG0dXJbwmiQl38TrL7gUlwBtf+K0hL7OR/xvwuBX/xnflw9j6odHQCxRfMXINYj6V781LzzRcQ8hd1/2dN50WnA5/sZT3kklN6nkgGYRTmrwgguy+r2DsT0v3jOZjyGa1EV0fAtDXERfVQ5Tg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777049317; c=relaxed/simple; bh=zDAi6+BP11nSSIhghkCbe/DAtdl7FJlUT7QQCt3+JnI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GCXmnlFbS/7u4DM9ZFy7Nh0lqyGHHiEew8J48y4ywsHFZpIPQjzn6LnQeFISta+e6QnZHJsKCYOeU1Vfb4VQdPvlVSFrSKO+7AQ22kftZlrsp7Y2bHUTgWg+SsyUQHjzkMXFXsiZhudmS7eUPj38gCrMBf6jIjgFPPzP745xlH0= 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=XrgC9Cra; arc=none smtp.client-ip=209.85.214.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="XrgC9Cra" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2b2ead99f5dso101230725ad.0 for ; Fri, 24 Apr 2026 09:48:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777049315; x=1777654115; 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=2j+/G/gLHQ7NZrTXlwU5UyQ2EY4+32u+JcYzqEO/fuc=; b=XrgC9CraMQNOxxkhfCnYqnUBOxZUxurJ+v/M1yUTD3Ym+VwdMFGewYFYD2VHBwo7wk YF9+TCbB8NFXgu1f03d0An4gKX7jfAFXJIqfZzVk1QpwK1wVwSesKfXSfuhjMq2iICNf ziPjQzY1q/kWbx6ExOPoXl9WR3MMLonAn0ggZCkDTsN0OsmGoS8BTeRtmp6/5Ebk18Y3 Cnkhb+iT14MoohqS6JChFZkwyp7XdpE0/abc2c1PHlwZdjr0k5NMLu6O9FXbcjEIKwt0 8pRhC9bLkeuQ91AXr7lsDBB8+zY3aCRxYJR0hOO1cYI0U06TtaagBedg0dAoU+2LMiJ5 /8MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777049315; x=1777654115; 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=2j+/G/gLHQ7NZrTXlwU5UyQ2EY4+32u+JcYzqEO/fuc=; b=n8oH7PMhrhKYSl1w9y4MhnfOd/Gv5SG7Y7PWWk3TaSV8PR7ZZ6N25lI5wmFwK9fCDE Dl5+rUmk9BHn6EuyhgY0TDVbbsjsdJNR9TqqRBxAUObgrw0VpF3BY+21xWkOdblH8b34 vhUWLXAMwW8AFGRZkCt1stHRnnrsCGSOfWyfoeZu7/zqzRqUmOzQ9tPMSDFzKzh413Yv qQ4vDUWSvPjWtRU38ymI3+RjFa0hNupZ1+d6XO/Pk6mvdn2m9/GC18fOL3S09zOYdROs TUWWAVn2ukz7RnqAiFMuivxc/H462/vUm09QtYkOFCNZrI9ktEq+Mlni4k90yUP/2OH1 hbfg== X-Forwarded-Encrypted: i=1; AFNElJ/41KaZrodjGggAXDYUJpq6BuXQ+rPTyA27jUCmTQEhmkijCMYa8i036vTEAhDEDt6yH9DKb1lJyLD6JxI=@vger.kernel.org X-Gm-Message-State: AOJu0Yxw1Pw5u7PV+iepgnzNCnbLtpVkZqWmqkeRw+YUgk4lcHHxiqgx tRg8w2dqvZoBgHwJGJo2V0Cq1Gbi7+fc1YTYbo3vg8GM8NFsHdauOZ4E3RFp4CTI/zm7AXPC+Lj oNaboRYOZsw== X-Received: from plsu12.prod.google.com ([2002:a17:902:bf4c:b0:2b0:5ad5:b5af]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:37c4:b0:2b7:abc0:3bd7 with SMTP id d9443c01a7336-2b7abc0448fmr82802385ad.9.1777049314589; Fri, 24 Apr 2026 09:48:34 -0700 (PDT) Date: Fri, 24 Apr 2026 09:46:45 -0700 In-Reply-To: <20260424164721.2229025-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: <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-24-irogers@google.com> Subject: [PATCH v5 23/58] perf python: Add LiveSession helper 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" Add LiveSession class in tools/perf/python/perf_live.py to support live event collection using perf.evlist and perf.parse_events, avoiding the need to fork a separate perf record process. Signed-off-by: Ian Rogers Assisted-by: Gemini:gemini-3.1-pro-preview --- v2: 1. Fixed File Descriptor Leak: I moved self.evlist.mmap() inside the try block so that if it raises an exception, the finally block will still be executed and call self.evlist.close() , preventing file descriptor leaks. 2. Handled InterruptedError in poll() : I wrapped the poll() call in a try-except block to catch InterruptedError and continue the loop. This prevents the live session from crashing on non-fatal signals like SIGWINCH . 3. Added evlist.config() : I added a call to self.evlist.config() in the constructor after parse_events() . This applies the default record options to the events, enabling sampling and setting up PERF_SAMPLE_* fields so that the kernel will actually generate PERF_RECORD_SAMPLE events. 4. Enable the evlist and be robust to exceptions from reading unsupported events like mmap2. --- tools/perf/python/perf_live.py | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 tools/perf/python/perf_live.py diff --git a/tools/perf/python/perf_live.py b/tools/perf/python/perf_live.py new file mode 100755 index 000000000000..d1dcbab1150b --- /dev/null +++ b/tools/perf/python/perf_live.py @@ -0,0 +1,48 @@ +# SPDX-License-Identifier: GPL-2.0 +""" +Live event session helper using perf.evlist. + +This module provides a LiveSession class that allows running a callback +for each event collected live from the system, similar to perf.session +but without requiring a perf.data file. +""" + +import perf + + +class LiveSession: + """Represents a live event collection session.""" + + def __init__(self, event_string: str, sample_callback): + self.event_string = event_string + self.sample_callback = sample_callback + # Create a cpu map for all online CPUs + self.cpus = perf.cpu_map() + # Parse events and set maps + self.evlist = perf.parse_events(self.event_string, self.cpus) + self.evlist.config() + + def run(self): + """Run the live session.""" + self.evlist.open() + try: + self.evlist.mmap() + self.evlist.enable() + + while True: + # Poll for events with 100ms timeout + try: + self.evlist.poll(100) + except InterruptedError: + continue + for cpu in self.cpus: + try: + event = self.evlist.read_on_cpu(cpu) + if event and event.type == perf.RECORD_SAMPLE: + self.sample_callback(event) + except Exception: + pass + except KeyboardInterrupt: + pass + finally: + self.evlist.close() -- 2.54.0.545.g6539524ca2-goog