[PATCH v1 52/58] perf test: Migrate Intel PT virtual LBR test to use Python API
From: Ian Rogers
Date: Sun Apr 19 2026 - 20:16:28 EST
The Intel PT virtual LBR test used an ad-hoc Python script written on
the fly to parse branch stacks. This change migrates it to use the
newly added `brstack` iterator API in the `perf` Python module.
Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
.../perf/tests/shell/lib/perf_brstack_max.py | 43 +++++++++++++++++++
tools/perf/tests/shell/test_intel_pt.sh | 35 +++++----------
2 files changed, 53 insertions(+), 25 deletions(-)
create mode 100644 tools/perf/tests/shell/lib/perf_brstack_max.py
diff --git a/tools/perf/tests/shell/lib/perf_brstack_max.py b/tools/perf/tests/shell/lib/perf_brstack_max.py
new file mode 100644
index 000000000000..c826e14160d6
--- /dev/null
+++ b/tools/perf/tests/shell/lib/perf_brstack_max.py
@@ -0,0 +1,43 @@
+#!/usr/bin/python
+# SPDX-License-Identifier: GPL-2.0
+# Determine the maximum size of branch stacks in a perf.data file.
+
+import argparse
+import sys
+
+import os
+
+script_dir = os.path.dirname(os.path.abspath(__file__))
+python_dir = os.path.abspath(os.path.join(script_dir, "../../../python"))
+sys.path.insert(0, python_dir)
+
+import perf
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+ args = ap.parse_args()
+
+ bmax = 0
+
+ def process_event(sample):
+ nonlocal bmax
+ try:
+ brstack = sample.brstack
+ if brstack:
+ n = sum(1 for _ in brstack)
+ if n > bmax:
+ bmax = n
+ except AttributeError:
+ pass
+
+ try:
+ session = perf.session(perf.data(args.input), sample=process_event)
+ session.process_events()
+ print("max brstack", bmax)
+ except Exception as e:
+ print(f"Error processing events: {e}", file=sys.stderr)
+ sys.exit(1)
+
+if __name__ == "__main__":
+ main()
diff --git a/tools/perf/tests/shell/test_intel_pt.sh b/tools/perf/tests/shell/test_intel_pt.sh
index 8ee761f03c38..d711ecdf5be0 100755
--- a/tools/perf/tests/shell/test_intel_pt.sh
+++ b/tools/perf/tests/shell/test_intel_pt.sh
@@ -24,7 +24,6 @@ errfile="${temp_dir}/test-err.txt"
workload="${temp_dir}/workload"
awkscript="${temp_dir}/awkscript"
jitdump_workload="${temp_dir}/jitdump_workload"
-maxbrstack="${temp_dir}/maxbrstack.py"
cleanup()
{
@@ -539,34 +538,20 @@ test_kernel_trace()
test_virtual_lbr()
{
echo "--- Test virtual LBR ---"
- # Check if python script is supported
- libpython=$(perf version --build-options | grep python | grep -cv OFF)
- if [ "${libpython}" != "1" ] ; then
- echo "SKIP: python scripting is not supported"
+ # shellcheck source=lib/setup_python.sh
+ . "$(dirname "$0")"/lib/setup_python.sh
+
+ if [ -z "$PYTHON" ] ; then
+ echo "SKIP: Python not found"
return 2
fi
- # Python script to determine the maximum size of branch stacks
- cat << "_end_of_file_" > "${maxbrstack}"
-from __future__ import print_function
-
-bmax = 0
-
-def process_event(param_dict):
- if "brstack" in param_dict:
- brstack = param_dict["brstack"]
- n = len(brstack)
- global bmax
- if n > bmax:
- bmax = n
-
-def trace_end():
- print("max brstack", bmax)
-_end_of_file_
-
# Check if virtual lbr is working
- perf_record_no_bpf -o "${perfdatafile}" --aux-sample -e '{intel_pt//,cycles}:u' uname
- times_val=$(perf script -i "${perfdatafile}" --itrace=L -s "${maxbrstack}" 2>/dev/null | grep "max brstack " | cut -d " " -f 3)
+ perf_record_no_bpf -o "${tmpfile}" --aux-sample -e '{intel_pt//,cycles}:u' perf test -w brstack
+ perf inject --itrace=L -i "${tmpfile}" -o "${perfdatafile}"
+ output=$($PYTHON "$(dirname "$0")"/lib/perf_brstack_max.py -i "${perfdatafile}")
+ echo "Debug: perf_brstack_max.py output: $output"
+ times_val=$(echo "$output" | grep "max brstack " | cut -d " " -f 3)
case "${times_val}" in
[0-9]*) ;;
*) times_val=0;;
--
2.54.0.rc1.513.gad8abe7a5a-goog