[PATCH v2 30/30] perf test: Add stat record metrics test
From: Ian Rogers
Date: Fri Nov 13 2026 - 15:09:43 EST
Test that metrics measured by perf stat record are displayed by perf
stat report and perf script, and that events recorded with -e only
display matching JSON metrics in perf stat report and perf script when
--all-metrics, stat.all-metrics=true, or -M is used.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/tests/shell/stat.sh | 133 +++++++++++++++++++++++++++++++++
1 file changed, 133 insertions(+)
diff --git a/tools/perf/tests/shell/stat.sh b/tools/perf/tests/shell/stat.sh
index ac9e031831da..2e1d5978d55a 100755
--- a/tools/perf/tests/shell/stat.sh
+++ b/tools/perf/tests/shell/stat.sh
@@ -6,9 +6,25 @@ set -e
err=0
stat_output=$(mktemp /tmp/perf-stat-test-output.XXXXX)
+stat_data=""
+stat_config=""
+
+stat_record_metrics_cleanup() {
+ if [ -n "${stat_data}" ]
+ then
+ rm -f "${stat_data}" "${stat_data}.old"
+ stat_data=""
+ fi
+ if [ -n "${stat_config}" ]
+ then
+ rm -f "${stat_config}"
+ stat_config=""
+ fi
+}
cleanup() {
rm -f "${stat_output}"
+ stat_record_metrics_cleanup
trap - EXIT TERM INT
}
@@ -92,6 +108,122 @@ test_stat_record_script() {
echo "stat record and script test [Success]"
}
+test_stat_record_metrics() {
+ echo "stat record metrics test"
+ if ! perf stat -M insn_per_cycle true 2>&1 | grep -E -q "[0-9]+.*[[:space:]]+insn_per_cycle"
+ then
+ echo "stat record metrics test [Skipped insn_per_cycle metric unsupported]"
+ return
+ fi
+ stat_data=$(mktemp /tmp/perf-stat-test-data.XXXXX)
+ if ! perf stat record -o "${stat_data}" -M insn_per_cycle true > /dev/null 2>&1
+ then
+ echo "stat record metrics test [Failed record]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if ! perf stat report -i "${stat_data}" 2>&1 | grep -q "insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed missing metric in perf stat report]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if ! perf script -i "${stat_data}" 2>&1 | grep -E -q "metric:.*insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed missing metric in perf script]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if ! perf stat record -o "${stat_data}" -e instructions,cycles true > /dev/null 2>&1
+ then
+ echo "stat record metrics test [Failed record with events]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if perf stat report -i "${stat_data}" 2>&1 | grep -q "insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed unexpected metric in perf stat report]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if ! perf stat report --all-metrics -i "${stat_data}" 2>&1 | grep -q "insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed missing metric in perf stat report --all-metrics]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if perf script -i "${stat_data}" 2>&1 | grep -E -q "metric:.*insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed unexpected metric in perf script]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if ! perf script --all-metrics -i "${stat_data}" 2>&1 | grep -E -q "metric:.*insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed missing metric in perf script --all-metrics]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ stat_config=$(mktemp /tmp/perf-stat-test-config.XXXXX)
+ printf "[stat]\nall-metrics = true\n" > "${stat_config}"
+ if ! PERF_CONFIG="${stat_config}" perf stat report -i "${stat_data}" 2>&1 | \
+ grep -q "insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed stat report stat.all-metrics=true]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if ! PERF_CONFIG="${stat_config}" perf script -i "${stat_data}" 2>&1 | \
+ grep -E -q "metric:.*insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed perf script stat.all-metrics=true]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ rm -f "${stat_config}"
+ stat_config=""
+ if ! perf stat report -M insn_per_cycle -i "${stat_data}" 2>&1 | grep -q "insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed missing metric in perf stat report -M insn_per_cycle]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if ! perf stat record -o "${stat_data}" true > /dev/null 2>&1
+ then
+ echo "stat record metrics test [Failed default record]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if ! perf stat report -i "${stat_data}" 2>&1 | grep -q "insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed missing default metric in perf stat report]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ if ! perf script -i "${stat_data}" 2>&1 | grep -E -q "metric:.*insn_per_cycle"
+ then
+ echo "stat record metrics test [Failed missing default metric in perf script]"
+ stat_record_metrics_cleanup
+ err=1
+ return
+ fi
+ stat_record_metrics_cleanup
+ echo "stat record metrics test [Success]"
+}
+
test_stat_repeat_weak_groups() {
echo "stat repeat weak groups test"
if ! perf stat -e '{cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles}' \
@@ -601,6 +733,7 @@ test_null_stat
test_offline_cpu_stat
test_stat_record_report
test_stat_record_script
+test_stat_record_metrics
test_stat_repeat_weak_groups
test_topdown_groups
test_topdown_weak_groups
--
2.56.1.703.gf848ed807c-goog