[PATCH 0/4] perf python: Add a script to open profiles in the Perfetto UI
From: Lalit Maganti
Date: Sat Nov 14 2026 - 19:39:54 EST
This series adds a perf script that converts the samples in a perf.data
file to a Perfetto trace and opens it in the Perfetto UI
(https://ui.perfetto.dev):
$ perf record -g -- ./workload
$ perf script perfetto
Unlike a flame graph, the trace keeps the time of every sample, so you
can see what each thread was running over time, get a flame graph for
any time range, thread or process, and query the samples with SQL.
The samples are written as Perfetto StackSample packets [1]. The few
protobuf messages needed are encoded by hand, so the script only needs
the Python standard library.
- Patches 1 and 2 add what the script needs from the perf module:
whether a frame is in the kernel, and the clock from perf record -k.
- Patch 3 adds the script and a shell test.
- Patch 4 compresses the trace (zstd, or gzip on Python < 3.14), which
makes it 3-4x smaller.
I compared the output with perf report and perf script --no-inline
(the perf module doesn't expand inlined frames) on x86 and arm64. The
samples, their periods and their callchains match for:
- cpu-clock, cycles and {cycles,instructions} groups
- recordings with and without callchains
- perf record -k CLOCK_MONOTONIC and CLOCK_BOOTTIME
- pipe mode (perf record -o - | perf script perfetto -i -)
- system-wide recording on arm64
DWARF callchains need a separate fix to the perf module, "perf python:
Accept events larger than union perf_event" [2]. Without it, the script
exits with an error on --call-graph dwarf recordings.
[1] https://github.com/google/perfetto/blob/main/protos/perfetto/trace/profiling/stack_sample.proto
[2] https://lore.kernel.org/linux-perf-users/20261018074528.974953-1-lalitmaganti@xxxxxxxxx/
Lalit Maganti (4):
perf python: Add kernel attribute to callchain_node
perf python: Add clockid attribute to session
perf python: Add perfetto script to open profiles in the Perfetto UI
perf python: Compress the traces of the perfetto script
tools/perf/python/perf.pyi | 2 +
tools/perf/python/perfetto.py | 675 ++++++++++++++++++++++
tools/perf/tests/shell/script_perfetto.sh | 98 ++++
tools/perf/util/python.c | 17 +
4 files changed, 792 insertions(+)
create mode 100755 tools/perf/python/perfetto.py
create mode 100755 tools/perf/tests/shell/script_perfetto.sh
base-commit: a1c3b14c28ebfa79dd56e2743c2fbad0c02a7146
--
2.56.0