From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f74.google.com (mail-dl1-f74.google.com [74.125.82.74]) (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 543F1401495 for ; Fri, 12 Jun 2026 22:13:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781302396; cv=none; b=oo6wq2hWgG8mePR0WYKlisCgsGQB1ivFvYsz0dyflA6JLBOeobpgkcuupO17O3X8snhjn9aNKi6/Da+ZISt4PrZBLQhwG6Q3DOuoJxetvIgNFUVLr24a3P5ZAvl7thiaHkrB8hbp71PUx1xs++mkMfOCxzn1S1Dx7VWvpvJIgSY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781302396; c=relaxed/simple; bh=qOf1Wd7zgs8S1zE2z0Om6/yIsdiCHCh+jtIDOS7g64M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VpouG6yTB5vktKn2h+eXl9t3XHVx0SgluDgdCxOD14j6x6EJVUL+3H2FGaJjfmsrGiFvOxdDc2NKWY64qvBtqybalFovFTvJ4kpDRQbdZKxu36DVkbYtjrpp9hHTxU0+s17Yo2xIfICL+6mWmlqfVvzgSrJihgGcSEmV52ngchM= 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=so7vBzCl; arc=none smtp.client-ip=74.125.82.74 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="so7vBzCl" Received: by mail-dl1-f74.google.com with SMTP id a92af1059eb24-1383678095cso5746902c88.0 for ; Fri, 12 Jun 2026 15:13:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781302393; x=1781907193; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=zOKcWEflCtB2iI3mAyKxrWp4CrQAheZW6CDWW+PkO+0=; b=so7vBzClPKSHbpggOP7elwFzdDiWRINxZ3B0nyh9fTnNltPkkpyRvKl/pX+DiKYgNT pFfMXDFJ3SkZzP/ioarfxW9a3kiuID11c/PesPODtDegw0HYt1wzTCUOVcNP1zsXD/yR CF40upM39MnDdo7xVEQBiJfy8QZSiIpUUqY30FvxkddT85chwTF5jVcW36oRBUVlUBRz GhJnD/+rOePh75KxwIS4b/P+bY3cq4NvY+RECPlde0+KzDHtxlarz2ecKcdSY6nI+PbY RzjFwfw/dfVzVlT30K87d+F8ayksk8UwG5IkRZHJSwd73gC2X/Gq3KBshkdi3hhhCBCA fY9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781302393; x=1781907193; h=content-transfer-encoding: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=zOKcWEflCtB2iI3mAyKxrWp4CrQAheZW6CDWW+PkO+0=; b=TYcUuKoC+PxbCrB8M3gjHhPJz8Wqb22w89VMLAe7iFGUqWNpsRpVZm3Q0shmDmEraJ mQ8tY0tx8f3JKycynQjMnb2ZPfmTcWyN1/E+C1/AzS1m7LK3XLc5VyQkkN951nC+y+i4 4wAAMulPg8e33Veauh/FQ9MOugDeSyRscfJKudwFl+ni3RL7UXsHOPSb/tB+bDd4ZrOl Jq3cBF92uIlbxIkl+SIXk0yMLPloYedtSba/vvo5KGLowDlQcWOP4tr46CRSC7P/RbWR bFd51KxDJ9gFOGxVaFuuYqIpasmKwoO+4rLHP4K+A6ubbFWzQBjeKY08aLz3YV25vXKN Bz2g== X-Forwarded-Encrypted: i=1; AFNElJ/Ej3udJhC3x+Ad49V2ou887EYoibafSdZHBh2qStNpcdoO+zNr8DzLQ6ZrhGxxbETH1yW7Y1OEpBmrWyc=@vger.kernel.org X-Gm-Message-State: AOJu0Yyewv4R+q4n8/ygOTOtXHkLAUIpD8NC74JC1mdnk9oqnRYPoiX6 1rd34r1pLuiJQyMIW2ERFpY/cUxIYiBy/+DOyGPNTVDdewTJgh1zeLDrX2cj9sQf+ISw3srEMRy ZCQMQ6aZNug== X-Received: from dlbsi8.prod.google.com ([2002:a05:7022:b888:b0:138:5e8:1b36]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:6089:b0:138:407c:1d20 with SMTP id a92af1059eb24-1384bb9446cmr2301133c88.35.1781302392754; Fri, 12 Jun 2026 15:13:12 -0700 (PDT) Date: Fri, 12 Jun 2026 15:12:11 -0700 In-Reply-To: <20260612221211.1221305-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: <20260612180956.1105352-1-irogers@google.com> <20260612221211.1221305-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1136.gdb2ca164c4-goog Message-ID: <20260612221211.1221305-20-irogers@google.com> Subject: [PATCH v15 19/19] perf python: Add LiveSession helper From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, alice.mei.rogers@gmail.com, dapeng1.mi@linux.intel.com, james.clark@linaro.org, leo.yan@linux.dev, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, tmricht@linux.ibm.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 --- v12: - Replaced `except TypeError` with `continue` to ignore unmapped/offline C= PUs instead of breaking the poll loop. Replaced hardcoded `-11` with `e.err= no =3D=3D errno.EAGAIN` in `LiveSession.run()`. Fixed `setup_python.sh` `PY= THONPATH` prepend logic to cleanly append to existing paths. 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. v8: - Drain all events from a CPU before moving to the next. --- tools/perf/python/perf_live.py | 55 ++++++++++++++++++++++ tools/perf/tests/shell/lib/setup_python.sh | 13 +++++ 2 files changed, 68 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.p= y new file mode 100755 index 000000000000..fd049933a052 --- /dev/null +++ b/tools/perf/python/perf_live.py @@ -0,0 +1,55 @@ +# 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 errno +import perf + + +class LiveSession: + """Represents a live event collection session.""" + + def __init__(self, event_string: str, sample_callback): + self.event_string =3D event_string + self.sample_callback =3D sample_callback + # Create a cpu map for all online CPUs + self.cpus =3D perf.cpu_map() + # Parse events and set maps + self.evlist =3D perf.parse_events(self.event_string, self.cpus) + self.evlist.config() + + def run(self): + """Run the live session.""" + try: + self.evlist.open() + 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: + for _ in range(1000): # Limit to 1000 events per CPU p= er poll to prevent starvation + try: + event =3D self.evlist.read_on_cpu(cpu) + except TypeError: + # CPU might be unmapped or offline, wait for m= map event + break + + if event is None: + break + + if event.type =3D=3D perf.RECORD_SAMPLE: + self.sample_callback(event) + except KeyboardInterrupt: + pass + finally: + self.evlist.close() diff --git a/tools/perf/tests/shell/lib/setup_python.sh b/tools/perf/tests/= shell/lib/setup_python.sh index a58e5536f2ed..2173215a0517 100644 --- a/tools/perf/tests/shell/lib/setup_python.sh +++ b/tools/perf/tests/shell/lib/setup_python.sh @@ -14,3 +14,16 @@ then echo Skipping test, python not detected please set environment variable = PYTHON. exit 2 fi + +# Set PYTHONPATH to find the in-tree built perf.so first, avoiding system-= wide perf.so +if [ -n "$PERF_EXEC_PATH" ] && [ -d "$PERF_EXEC_PATH/python" ]; then + PYTHONPATH_DIR=3D"$PERF_EXEC_PATH/python" +elif [ -d "$(dirname "$0")/../../python" ]; then + PYTHONPATH_DIR=3D"$(dirname "$0")/../../python" +elif [ -d "$(dirname "$0")/../python" ]; then + PYTHONPATH_DIR=3D"$(dirname "$0")/../python" +fi + +if [ -n "$PYTHONPATH_DIR" ]; then + export PYTHONPATH=3D"$PYTHONPATH_DIR${PYTHONPATH:+:$PYTHONPATH}" +fi --=20 2.54.0.1136.gdb2ca164c4-goog