[PATCH v13 12/19] perf test: Exercise the annotate stats output

From: Arnaldo Carvalho de Melo

Date: Wed Nov 25 2026 - 14:24:21 EST


From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>

Add a shell test for the --insn-stat and --type-stat output and for the
warning when --type-stat is used without --data-type. The
--code-with-type stats are resolved while printing, so they are
expected once, after the annotations; the plain --data-type stats are
resolved in the histogram resort, so they are expected once, before
them. The instruction stats run with one and with two events and are
checked against the per-instruction rows, so a total that counts only
the first event, or does not match the rows, fails; the data type total
is checked against its counters. A repeated section also fails.

Operand extraction is implemented only for x86, arm64 and powerpc, so
other architectures skip rather than fail on the zero totals it would
leave. The bad counters check compares numerically: when every access
resolves no counter line is printed, and awks that follow POSIX would
then compare "0" to the unset sum as strings. The warning is matched
as a substring, as some disassembly errors are printed without a
trailing newline and would join it into one line.

Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/perf/tests/shell/annotate_stats.sh | 186 +++++++++++++++++++++++
1 file changed, 186 insertions(+)
create mode 100755 tools/perf/tests/shell/annotate_stats.sh

diff --git a/tools/perf/tests/shell/annotate_stats.sh b/tools/perf/tests/shell/annotate_stats.sh
new file mode 100755
index 0000000000000000..085dde4ddecff18f
--- /dev/null
+++ b/tools/perf/tests/shell/annotate_stats.sh
@@ -0,0 +1,186 @@
+#!/bin/bash
+# perf annotate instruction stats test
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+shelldir=$(dirname "$0")
+. "${shelldir}"/lib/perf_has_symbol.sh
+
+skip_test_missing_symbol datasym
+
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+perfone=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+perftwo=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
+perfout2=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
+perferr=$(mktemp /tmp/__perf_test.perf.err.XXXXX)
+
+cleanup()
+{
+ rm -f "$perfdata" "$perfone" "$perftwo" "$perfout" "$perfout2" "$perferr"
+ trap - EXIT TERM INT
+}
+
+trap_cleanup()
+{
+ echo "Unexpected signal in ${FUNCNAME[1]}"
+ cleanup
+ exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+if ! perf check feature -q dwarf; then
+ echo "Skip: no DWARF (libdw) support"
+ cleanup
+ exit 2
+fi
+
+# The instruction stats are counted by the data type resolver, whose
+# operand extraction is implemented only for x86, arm64 and powerpc;
+# elsewhere the totals are zero by design, not because of a bug.
+case "$(uname -m)" in
+ x86_64 | i[3456]86 | aarch64 | ppc64 | ppc64le) ;;
+ *)
+ echo "Skip: no instruction operand extraction for $(uname -m)"
+ cleanup
+ exit 2
+ ;;
+esac
+
+if ! perf record -o "$perfone" -e cpu-clock -- perf test -w datasym \
+ > /dev/null 2>&1 ||
+ ! perf record -o "$perftwo" -e cpu-clock -e task-clock -- \
+ perf test -w datasym > /dev/null 2>&1; then
+ echo "FAIL: perf record"
+ cleanup
+ exit 1
+fi
+
+if ! perf annotate --code-with-type --insn-stat -i "$perfone" --stdio \
+ --percent-limit 1 datasym > "$perfout"; then
+ echo "FAIL: perf annotate"
+ cleanup
+ exit 1
+fi
+
+if ! perf annotate --code-with-type --insn-stat -i "$perftwo" --stdio \
+ --percent-limit 1 datasym > "$perfout2"; then
+ echo "FAIL: perf annotate"
+ cleanup
+ exit 1
+fi
+
+# The instruction stats are accounted while the instructions are
+# displayed; printing them before the display loop shows zeros. A single
+# positive total would also accept a stale first event's total, so check
+# that the section is printed once, after the annotations.
+if ! awk '
+ /^Annotate Instruction stats/ { sections++ }
+ sections && /^[[:space:]]*[0-9]+\.[0-9]+ : / { late = 1 }
+ END { exit !(sections == 1 && !late) }
+' "$perfout2"; then
+ echo "FAIL: instruction stats not printed once after the annotations"
+ tail -n 30 "$perfout2"
+ cleanup
+ exit 1
+fi
+
+# Each event annotates the same instructions, so the two-event total is
+# twice the one-event total.
+one_total=$(awk '/^total / { t = $2; sub(/,/, "", t); print t; exit }' \
+ "$perfout")
+two_total=$(awk '/^total / { t = $2; sub(/,/, "", t); print t; exit }' \
+ "$perfout2")
+if [ -z "$one_total" ] || [ -z "$two_total" ] ||
+ [ "$two_total" -le 0 ] || [ "$two_total" -ne $((2 * one_total)) ]; then
+ echo "FAIL: instruction stats do not count every event" \
+ "(one event: ${one_total:-none}, two events: ${two_total:-none})"
+ tail -n 30 "$perfout2"
+ cleanup
+ exit 1
+fi
+
+# The totals are the sums of the per-instruction rows.
+if ! awk '
+ /^total / {
+ t = $2; o = $4; b = $7
+ sub(/,/, "", t); sub(/,/, "", o); sub(/,/, "", b)
+ total = t; ok = o; bad = b; next
+ }
+ /^ .*: +[0-9]+ +[0-9]+$/ { good += $(NF - 1); bads += $NF }
+ END {
+ exit !(total > 0 && total == good + bads &&
+ ok == good && bad == bads)
+ }
+' "$perfout2"; then
+ echo "FAIL: instruction stats do not add up"
+ tail -n 30 "$perfout2"
+ cleanup
+ exit 1
+fi
+
+# No point in recording the data type samples if the PMU cannot do memory
+# events.
+if ! perf mem record -o /dev/null -- true 2>/dev/null; then
+ echo "Skip: cannot record memory events on this PMU"
+ cleanup
+ exit 2
+fi
+
+if ! perf mem record -o "$perfdata" -- perf test -w datasym > /dev/null 2>&1; then
+ echo "FAIL: perf mem record"
+ cleanup
+ exit 1
+fi
+
+# The data type stats are accounted in the histogram resort, before any
+# annotation is printed, and must add up like the instruction stats above.
+if ! perf annotate --data-type --type-stat -i "$perfdata" --stdio \
+ > "$perfout"; then
+ echo "FAIL: perf annotate --data-type --type-stat"
+ cleanup
+ exit 1
+fi
+
+if ! awk '
+ /^Annotate data type stats:/ { sections++; after = 1; next }
+ after && /^Annotate type: / { annotated++ }
+ END { exit !(sections == 1 && annotated) }
+' "$perfout"; then
+ echo "FAIL: data type stats not printed once before the annotations"
+ tail -n 30 "$perfout"
+ cleanup
+ exit 1
+fi
+
+# The total is the sum of the bad counters and of the good ones.
+if ! awk '
+ /^Annotate data type stats:/ { stats = 1; next }
+ !stats { next }
+ /^total / {
+ t = $2; o = $4; b = $7
+ sub(/,/, "", t); sub(/,/, "", o); sub(/,/, "", b)
+ total = t; ok = o; bad = b; next
+ }
+ /^[[:space:]]*[0-9]+ : / && $3 != "insn_track" { fields += $1 }
+ END { exit !(total > 0 && total == ok + bad && bad + 0 == fields + 0) }
+' "$perfout"; then
+ echo "FAIL: data type stats do not add up"
+ tail -n 30 "$perfout"
+ cleanup
+ exit 1
+fi
+
+# -F without -x: some disassembly errors print without a trailing
+# newline and would join the warning into one line.
+if ! perf annotate --type-stat -i "$perfdata" --stdio > /dev/null 2> "$perferr" ||
+ ! grep -Fq "Warning: --type-stat needs --data-type" "$perferr"; then
+ echo "FAIL: missing --type-stat warning"
+ tail -n 30 "$perferr"
+ cleanup
+ exit 1
+fi
+
+echo "instruction stats accounted"
+cleanup
--
2.56.0