Re: [PATCH v7 23/59] perf python: Add LiveSession helper

sashiko-bot
2026-04-25T23:25:40Z

Sashiko AI review found 2 potential issue(s):
- [Medium] The over-broad `except Exception:` block silently catches and ignores all exceptions raised by the user-provided `sample_callback`.
- [Medium] The event collection loop fails to drain the ring buffer, reading only a single event per CPU per poll iteration.
--

commit e2eca1dd7f39c083ae56a20fe3f81a6dda3a902c
Author: Ian Rogers <irogers@google.com>

perf python: Add LiveSession helper

This commit introduces the LiveSession class in the python perf module to
support live event collection using perf.evlist and perf.parse_events,
avoiding the need to fork a separate perf record process.

> 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

[ ... ]

> + 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)

Does this code need to fully drain the ring buffer on each wakeup?

Currently, it reads only a single event per CPU and then goes back to
poll(). Under moderate to high event rates, processing them one by one
per poll iteration might cause the utility to fall behind and drop events.

Could this loop until read_on_cpu() returns None before moving to the
next CPU or polling again?

> + if event and event.type == perf.RECORD_SAMPLE:
> + self.sample_callback(event)
> + except Exception:
> + pass

Since this except block catches all exceptions, could it accidentally swallow
exceptions raised by the user-provided sample_callback?

If the user's callback contains a logic error or raises a ValueError, catching
and ignoring it silently here might make it very difficult for them to debug
their scripts.

> + except KeyboardInterrupt:
> + pass
> + finally:
> + self.evlist.close()

--
Sashiko AI review · https://sashiko.dev/#/patchset/20260425224951.174663-1-irogers@google.com?part=23