[PATCH v1 23/58] perf python: Add LiveSession helper

From: Ian Rogers

Date: Sun Apr 19 2026 - 20:05:43 EST


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 <irogers@xxxxxxxxxx>
Assisted-by: Gemini:gemini-3.1-pro-preview
---
tools/perf/python/perf_live.py | 42 ++++++++++++++++++++++++++++++++++
1 file changed, 42 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..81d92f720b58
--- /dev/null
+++ b/tools/perf/python/perf_live.py
@@ -0,0 +1,42 @@
+# 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)
+
+ def run(self):
+ """Run the live session."""
+ self.evlist.open()
+ self.evlist.mmap()
+
+ try:
+ while True:
+ # Poll for events with 100ms timeout
+ self.evlist.poll(100)
+ for cpu in self.cpus:
+ event = self.evlist.read_on_cpu(cpu)
+ while event:
+ if event.type == perf.RECORD_SAMPLE:
+ self.sample_callback(event)
+ event = self.evlist.read_on_cpu(cpu)
+ except KeyboardInterrupt:
+ pass
+ finally:
+ self.evlist.close()
--
2.54.0.rc1.513.gad8abe7a5a-goog