From: Ian Rogers <irogers@google.com>
To: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com,
alice.mei.rogers@gmail.com, james.clark@linaro.org,
linux-perf-users@vger.kernel.org, namhyung@kernel.org
Cc: dapeng1.mi@linux.intel.com, leo.yan@linux.dev,
linux-kernel@vger.kernel.org, mingo@redhat.com,
peterz@infradead.org, tmricht@linux.ibm.com
Subject: [PATCH v1 37/49] perf python: Port check-perf-trace to perf module
Date: Sat, 19 Sep 2026 22:21:29 -0700 [thread overview]
Message-ID: <d9c044b5bbee092583b5bec727b06df44e618d49.1789880842.git.irogers@google.com> (raw)
In-Reply-To: <cover.1789880842.git.irogers@google.com>
Port check-perf-trace.py to a standalone script in tools/perf/python/
using the perf module directly.
Improvements compared to the legacy script:
- Access tracepoint fields directly as attributes on perf.sample_event
instead of per-event dictionaries and legacy Perf-Trace-Util helpers.
- Decode symbolic flag and enum masks for irq:softirq_entry and
kmem:kmalloc directly in Python and add -i/--input CLI support via
argparse.
- Add full type annotations and clean up Python 2 idioms.
Add a shell test (test_check_perf_trace_python.sh) to verify the
standalone script.
Assisted-by: Antigravity:gemini-3.1-pro
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/python/check-perf-trace.py | 213 ++++++++++++++++++
.../shell/test_check_perf_trace_python.sh | 79 +++++++
2 files changed, 292 insertions(+)
create mode 100755 tools/perf/python/check-perf-trace.py
create mode 100755 tools/perf/tests/shell/test_check_perf_trace_python.sh
diff --git a/tools/perf/python/check-perf-trace.py b/tools/perf/python/check-perf-trace.py
new file mode 100755
index 000000000000..19a91c7934b7
--- /dev/null
+++ b/tools/perf/python/check-perf-trace.py
@@ -0,0 +1,213 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+Basic test of Python scripting support for perf.
+Ported from tools/perf/scripts/python/check-perf-trace.py
+"""
+from __future__ import annotations
+
+import argparse
+import collections
+import perf
+
+unhandled: collections.defaultdict[str, int] = collections.defaultdict(int)
+session = None
+
+softirq_vecs = {
+ 0: "HI_SOFTIRQ",
+ 1: "TIMER_SOFTIRQ",
+ 2: "NET_TX_SOFTIRQ",
+ 3: "NET_RX_SOFTIRQ",
+ 4: "BLOCK_SOFTIRQ",
+ 5: "IRQ_POLL_SOFTIRQ",
+ 6: "TASKLET_SOFTIRQ",
+ 7: "SCHED_SOFTIRQ",
+ 8: "HRTIMER_SOFTIRQ",
+ 9: "RCU_SOFTIRQ",
+}
+
+_GFP_DMA = 1 << 0
+_GFP_HIGHMEM = 1 << 1
+_GFP_DMA32 = 1 << 2
+_GFP_MOVABLE = 1 << 3
+_GFP_RECLAIMABLE = 1 << 4
+_GFP_HIGH = 1 << 5
+_GFP_IO = 1 << 6
+_GFP_FS = 1 << 7
+_GFP_ZERO = 1 << 8
+_GFP_DIRECT_RECLAIM = 1 << 10
+_GFP_KSWAPD_RECLAIM = 1 << 11
+_GFP_WRITE = 1 << 12
+_GFP_NOWARN = 1 << 13
+_GFP_RETRY_MAYFAIL = 1 << 14
+_GFP_NOFAIL = 1 << 15
+_GFP_NORETRY = 1 << 16
+_GFP_MEMALLOC = 1 << 17
+_GFP_COMP = 1 << 18
+_GFP_NOMEMALLOC = 1 << 19
+_GFP_HARDWALL = 1 << 20
+_GFP_THISNODE = 1 << 21
+_GFP_ACCOUNT = 1 << 22
+_GFP_ZEROTAGS = 1 << 23
+
+_GFP_RECLAIM = _GFP_DIRECT_RECLAIM | _GFP_KSWAPD_RECLAIM
+_GFP_KERNEL = _GFP_RECLAIM | _GFP_IO | _GFP_FS
+_GFP_USER = _GFP_KERNEL | _GFP_HARDWALL
+_GFP_HIGHUSER = _GFP_USER | _GFP_HIGHMEM
+_GFP_HIGHUSER_MOVABLE = _GFP_HIGHUSER | _GFP_MOVABLE
+_GFP_TRANSHUGE_LIGHT = (
+ _GFP_HIGHUSER_MOVABLE | _GFP_COMP | _GFP_NOMEMALLOC | _GFP_NOWARN
+) & ~_GFP_RECLAIM
+_GFP_TRANSHUGE = _GFP_TRANSHUGE_LIGHT | _GFP_DIRECT_RECLAIM
+
+GFP_FLAG_NAMES = [
+ (_GFP_TRANSHUGE, "GFP_TRANSHUGE"),
+ (_GFP_TRANSHUGE_LIGHT, "GFP_TRANSHUGE_LIGHT"),
+ (_GFP_HIGHUSER_MOVABLE, "GFP_HIGHUSER_MOVABLE"),
+ (_GFP_HIGHUSER, "GFP_HIGHUSER"),
+ (_GFP_USER, "GFP_USER"),
+ (_GFP_KERNEL | _GFP_ACCOUNT, "GFP_KERNEL_ACCOUNT"),
+ (_GFP_KERNEL, "GFP_KERNEL"),
+ (_GFP_RECLAIM | _GFP_IO, "GFP_NOFS"),
+ (_GFP_HIGH | _GFP_KSWAPD_RECLAIM, "GFP_ATOMIC"),
+ (_GFP_RECLAIM, "GFP_NOIO"),
+ (_GFP_KSWAPD_RECLAIM | _GFP_NOWARN, "GFP_NOWAIT"),
+ (_GFP_DMA, "GFP_DMA"),
+ (_GFP_DMA32, "GFP_DMA32"),
+ (_GFP_RECLAIM, "__GFP_RECLAIM"),
+ (_GFP_DMA, "__GFP_DMA"),
+ (_GFP_HIGHMEM, "__GFP_HIGHMEM"),
+ (_GFP_DMA32, "__GFP_DMA32"),
+ (_GFP_MOVABLE, "__GFP_MOVABLE"),
+ (_GFP_RECLAIMABLE, "__GFP_RECLAIMABLE"),
+ (_GFP_HIGH, "__GFP_HIGH"),
+ (_GFP_IO, "__GFP_IO"),
+ (_GFP_FS, "__GFP_FS"),
+ (_GFP_ZERO, "__GFP_ZERO"),
+ (_GFP_DIRECT_RECLAIM, "__GFP_DIRECT_RECLAIM"),
+ (_GFP_KSWAPD_RECLAIM, "__GFP_KSWAPD_RECLAIM"),
+ (_GFP_WRITE, "__GFP_WRITE"),
+ (_GFP_NOWARN, "__GFP_NOWARN"),
+ (_GFP_RETRY_MAYFAIL, "__GFP_RETRY_MAYFAIL"),
+ (_GFP_NOFAIL, "__GFP_NOFAIL"),
+ (_GFP_NORETRY, "__GFP_NORETRY"),
+ (_GFP_MEMALLOC, "__GFP_MEMALLOC"),
+ (_GFP_COMP, "__GFP_COMP"),
+ (_GFP_NOMEMALLOC, "__GFP_NOMEMALLOC"),
+ (_GFP_HARDWALL, "__GFP_HARDWALL"),
+ (_GFP_THISNODE, "__GFP_THISNODE"),
+ (_GFP_ACCOUNT, "__GFP_ACCOUNT"),
+ (_GFP_ZEROTAGS, "__GFP_ZEROTAGS"),
+]
+
+
+def trace_begin() -> None:
+ """Called at the start of trace processing."""
+ print("trace_begin")
+
+def trace_end() -> None:
+ """Called at the end of trace processing."""
+ print_unhandled()
+ print("trace_end")
+
+def symbol_str(event_name: str, field_name: str, value: int) -> str:
+ """Resolves symbol values to strings."""
+ # Note: The standalone Python API currently lacks dynamic libtraceevent
+ # formatting (equivalent to _perf_trace_context.symbol_str())
+ if event_name == "irq__softirq_entry" and field_name == "vec":
+ return softirq_vecs.get(value, str(value))
+ return str(value)
+
+def flag_str(event_name: str, field_name: str, value: int) -> str:
+ """Resolves flag values to strings."""
+ # Note: The standalone Python API currently lacks dynamic libtraceevent
+ # formatting (equivalent to _perf_trace_context.flag_str())
+ if event_name == "kmem__kmalloc" and field_name == "gfp_flags":
+ if value == 0:
+ return "none"
+ names = []
+ rem = value
+ for mask, name in GFP_FLAG_NAMES:
+ if (rem & mask) == mask:
+ names.append(name)
+ rem &= ~mask
+ if rem:
+ names.append(f"0x{rem:x}")
+ return "|".join(names)
+ return str(value)
+
+def print_header(event_name: str, sample: perf.sample_event) -> None:
+ """Prints common header for events."""
+ secs = sample.sample_time // 1000000000
+ nsecs = sample.sample_time % 1000000000
+ comm = "[unknown]"
+ try:
+ if session:
+ thread = session.find_thread(sample.sample_tid)
+ if thread:
+ comm = thread.comm() or "[unknown]"
+ except (TypeError, AttributeError):
+ pass
+ print(f"{event_name:<20} {sample.sample_cpu:5} {secs:05}.{nsecs:09} "
+ f"{sample.sample_tid:8} {comm:<20} ", end=' ')
+
+def print_uncommon(sample: perf.sample_event) -> None:
+ """Prints uncommon fields for tracepoints."""
+ # Fallback to 0 if field not found (e.g. on older kernels or if not tracepoint)
+ pc = getattr(sample, 'common_preempt_count', 0)
+ flags = getattr(sample, 'common_flags', 0)
+ lock_depth = getattr(sample, 'common_lock_depth', 0)
+
+ print(f"common_preempt_count={pc}, common_flags={flags}, "
+ f"common_lock_depth={lock_depth}, ", end='')
+
+def irq__softirq_entry(sample: perf.sample_event) -> None:
+ """Handles irq:softirq_entry events."""
+ print_header("irq__softirq_entry", sample)
+ print_uncommon(sample)
+ print(f"vec={symbol_str('irq__softirq_entry', 'vec', getattr(sample, 'vec', 0))}")
+
+def kmem__kmalloc(sample: perf.sample_event) -> None:
+ """Handles kmem:kmalloc events."""
+ print_header("kmem__kmalloc", sample)
+ print_uncommon(sample)
+
+ print(f"call_site={getattr(sample, 'call_site', 0):#x}, "
+ f"ptr={getattr(sample, 'ptr', 0):#x}, "
+ f"bytes_req={getattr(sample, 'bytes_req', 0):d}, "
+ f"bytes_alloc={getattr(sample, 'bytes_alloc', 0):d}, "
+ f"gfp_flags={flag_str('kmem__kmalloc', 'gfp_flags', getattr(sample, 'gfp_flags', 0))}")
+
+def trace_unhandled(event_name: str) -> None:
+ """Tracks unhandled events."""
+ unhandled[event_name] += 1
+
+def print_unhandled() -> None:
+ """Prints summary of unhandled events."""
+ if not unhandled:
+ return
+ print("\nunhandled events:\n")
+ print(f"{'event':<40} {'count':>10}")
+ print("---------------------------------------- -----------")
+ for event_name, count in unhandled.items():
+ print(f"{event_name:<40} {count:10}")
+
+def process_event(sample: perf.sample_event) -> None:
+ """Callback for processing events."""
+ event_name = str(sample.evsel)
+ if event_name.startswith("evsel(irq:softirq_entry)"):
+ irq__softirq_entry(sample)
+ elif "evsel(kmem:kmalloc)" in event_name:
+ kmem__kmalloc(sample)
+ else:
+ trace_unhandled(event_name)
+
+if __name__ == "__main__":
+ ap = argparse.ArgumentParser()
+ ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+ args = ap.parse_args()
+
+ trace_begin()
+ session = perf.session(perf.data(args.input), sample=process_event)
+ session.process_events()
+ trace_end()
diff --git a/tools/perf/tests/shell/test_check_perf_trace_python.sh b/tools/perf/tests/shell/test_check_perf_trace_python.sh
new file mode 100755
index 000000000000..2c5295134f53
--- /dev/null
+++ b/tools/perf/tests/shell/test_check_perf_trace_python.sh
@@ -0,0 +1,79 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# check-perf-trace python test
+
+set -e
+
+shelldir=$(dirname "$0")
+# shellcheck source=lib/setup_python.sh
+. "${shelldir}"/lib/setup_python.sh
+
+# If we don't have the perf python module, we can't test
+if ! "$PYTHON" -c 'import perf' > /dev/null 2>&1; then
+ echo "Skipping test, perf python module not found"
+ exit 2
+fi
+
+if ! perf check feature -q libtraceevent; then
+ echo "Skipping test, perf built without libtraceevent"
+ exit 2
+fi
+
+script_dir="$(dirname "$0")/../../python"
+script_path="${script_dir}/check-perf-trace.py"
+
+if [ ! -f "$script_path" ]; then
+ echo "Skipping test, check-perf-trace.py not found at $script_path"
+ exit 2
+fi
+
+err=0
+temp_data=""
+
+cleanup() {
+ rm -f "${temp_data}"
+}
+
+trap 'cleanup' EXIT
+trap 'cleanup; exit 1' TERM INT
+
+temp_data=$(mktemp /tmp/perf.data.XXXXXX)
+
+test_file_mode() {
+ echo "Testing check-perf-trace.py..."
+
+ events=""
+ if perf list | grep -q "irq:softirq_entry"; then
+ events="irq:softirq_entry"
+ fi
+ if perf list | grep -q "kmem:kmalloc"; then
+ if [ -n "$events" ]; then
+ events="$events,kmem:kmalloc,kmem:kfree"
+ else
+ events="kmem:kmalloc,kmem:kfree"
+ fi
+ fi
+
+ if [ -z "$events" ]; then
+ echo "Skipping test, no required tracepoints found"
+ exit 2
+ fi
+
+ # Generate events
+ if ! perf record -e "$events" -a -o "${temp_data}" -- sleep 0.5 >/dev/null 2>&1; then
+ echo "Skipping test, perf record failed"
+ exit 2
+ fi
+
+ # Run the script
+ if ! "$PYTHON" "$script_path" -i "${temp_data}" >/dev/null; then
+ echo "File mode test failed."
+ err=1
+ else
+ echo "File mode test passed."
+ fi
+}
+
+test_file_mode
+
+exit $err
--
2.55.0.1082.g2b9226bbc0-goog
next prev parent reply other threads:[~2026-09-20 5:23 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-20 5:20 [PATCH v1 00/49] perf: Complete transition to standalone Python scripts Ian Rogers
2026-09-20 5:20 ` [PATCH v1 01/49] perf python: Update syscall format string to optional positional Ian Rogers
2026-09-20 5:20 ` [PATCH v1 02/49] perf python: Update callchain stubs and session thread lookup Ian Rogers
2026-09-20 5:20 ` [PATCH v1 03/49] perf python: Clean up pylint warnings in ilist.py Ian Rogers
2026-09-20 5:20 ` [PATCH v1 04/49] perf python: Clean up pylint warnings in treport.py Ian Rogers
2026-09-20 5:20 ` [PATCH v1 05/49] perf python: Clean up pylint warnings in tracepoint.py Ian Rogers
2026-09-20 5:20 ` [PATCH v1 06/49] perf python: Clean up pylint warnings in twatch.py Ian Rogers
2026-09-20 5:20 ` [PATCH v1 07/49] perf python: Improve perf script -l descriptions Ian Rogers
2026-09-20 5:21 ` [PATCH v1 08/49] perf python: Expose addr location, transaction, and context_switch Ian Rogers
2026-09-20 5:21 ` [PATCH v1 09/49] perf python: Add Intel PT call_return and itrace capability Ian Rogers
2026-09-20 5:21 ` [PATCH v1 10/49] perf python: Allow KeyboardInterrupt to propagate in LiveSession Ian Rogers
2026-09-20 5:21 ` [PATCH v1 11/49] perf pmu-events: Clean up mypy and pylint issues Ian Rogers
2026-09-20 5:21 ` [PATCH v1 12/49] perf test: Clean up mypy and pylint issues in shell test libraries Ian Rogers
2026-09-20 5:21 ` [PATCH v1 13/49] perf build: Make mypy build test opt-out (NO_MYPY=1) Ian Rogers
2026-09-20 5:21 ` [PATCH v1 14/49] perf build: Make pylint build test opt-out (NO_PYLINT=1) Ian Rogers
2026-09-20 5:21 ` [PATCH v1 15/49] perf Makefile: Install standalone Python scripts during transition Ian Rogers
2026-09-20 5:21 ` [PATCH v1 16/49] perf python: Port stat-cpi to perf module Ian Rogers
2026-09-20 5:21 ` [PATCH v1 17/49] perf python: Port mem-phys-addr " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 18/49] perf python: Port stackcollapse " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 19/49] perf python: Port flamegraph " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 20/49] perf python: Port gecko " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 21/49] perf python: Port event_analyzing_sample " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 22/49] perf python: Port syscall-counts " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 23/49] perf python: Port syscall-counts-by-pid " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 24/49] perf python: Port failed-syscalls-by-pid " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 25/49] perf python: Port failed-syscalls from Perl " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 26/49] perf python: Port sctop " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 27/49] perf python: Port rw-by-file from Perl " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 28/49] perf python: Port rw-by-pid " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 29/49] perf python: Port rwtop " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 30/49] perf python: Port futex-contention " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 31/49] perf python: Port task-analyzer " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 32/49] perf python: Port sched-migration and SchedGui " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 33/49] perf python: Port wakeup-latency from Perl " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 34/49] perf python: Port compaction-times " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 35/49] perf python: Port net_dropmonitor " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 36/49] perf python: Port netdev-times " Ian Rogers
2026-09-20 5:21 ` Ian Rogers [this message]
2026-09-20 5:21 ` [PATCH v1 38/49] perf python: Port arm-cs-trace-disasm " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 39/49] perf python: Port powerpc-hcalls " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 40/49] perf python: Port intel-pt-events and libxed " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 41/49] perf test: Migrate Intel PT virtual LBR test to Python API Ian Rogers
2026-09-20 5:21 ` [PATCH v1 42/49] perf python: Port export-to-sqlite to perf module Ian Rogers
2026-09-20 5:21 ` [PATCH v1 43/49] perf python: Port export-to-postgresql " Ian Rogers
2026-09-20 5:21 ` [PATCH v1 44/49] perf python: Move and clean up exported-sql-viewer.py Ian Rogers
2026-09-20 5:21 ` [PATCH v1 45/49] perf python: Move and clean up parallel-perf.py Ian Rogers
2026-09-20 5:21 ` [PATCH v1 46/49] perf: Remove libpython support and legacy Python scripts Ian Rogers
2026-09-20 5:21 ` [PATCH v1 47/49] perf Makefile: Update Python script installation path Ian Rogers
2026-09-20 5:21 ` [PATCH v1 48/49] perf script: Support standalone scripts and remove embedded scripting Ian Rogers
2026-09-20 5:21 ` [PATCH v1 49/49] perf Documentation: Update for standalone Python scripts Ian Rogers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d9c044b5bbee092583b5bec727b06df44e618d49.1789880842.git.irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alice.mei.rogers@gmail.com \
--cc=dapeng1.mi@linux.intel.com \
--cc=james.clark@linaro.org \
--cc=leo.yan@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=tmricht@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®