The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Namhyung Kim <namhyung@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	James Clark <james.clark@linaro.org>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Clark Williams <williams@redhat.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH v11 12/19] perf test: Exercise the annotate stats output
Date: Sun, 22 Nov 2026 18:40:18 -0300	[thread overview]
Message-ID: <20261122214025.16769-13-acme@kernel.org> (raw)
In-Reply-To: <20261122214025.16769-1-acme@kernel.org>

From: Arnaldo Carvalho de Melo <acme@redhat.com>

Add a shell test for the --insn-stat and --type-stat output printed
once, after the annotations, and for the warning when --type-stat is
used without --data-type.  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.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/shell/annotate_stats.sh | 172 +++++++++++++++++++++++
 1 file changed, 172 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..5d8a687e85ecb526
--- /dev/null
+++ b/tools/perf/tests/shell/annotate_stats.sh
@@ -0,0 +1,172 @@
+#!/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
+
+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 only printed with --data-type, and must be
+# accounted 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++ }
+	sections && /^Annotate type: / { late = 1 }
+	END { exit !(sections == 1 && !late) }
+' "$perfout"; then
+	echo "FAIL: data type stats not printed once after 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 == fields) }
+' "$perfout"; then
+	echo "FAIL: data type stats do not add up"
+	tail -n 30 "$perfout"
+	cleanup
+	exit 1
+fi
+
+if ! perf annotate --type-stat -i "$perfdata" --stdio > /dev/null 2> "$perferr" ||
+   ! grep -Fxq "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.1


  parent reply	other threads:[~2026-11-22 21:41 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 21:40 [PATCH v11 00/19] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 01/19] perf build: Link the GTK UI with EXTRA_CFLAGS Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 02/19] perf annotate: Handle absolute addressing in data type profiling Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 03/19] perf annotate: Print the stats after the annotations Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 04/19] perf build: Record the features checked outside FEATURE_TESTS Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 05/19] perf annotate-x86: Record immediates in a 64-bit field Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 06/19] perf annotate-x86: Parse movabs like a mov Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 07/19] perf annotate-data: Count flexible array accesses for the whole struct Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 08/19] perf test: Exercise the x86 operand parser Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 09/19] perf test: Exercise DW_OP_addrx global variables Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 10/19] perf test: Exercise the flexible array member path Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 11/19] perf test: Exercise data type resolution from a register Arnaldo Carvalho de Melo
2026-11-22 21:40 ` Arnaldo Carvalho de Melo [this message]
2026-11-22 21:40 ` [PATCH v11 13/19] perf test: Fix the Intel PT virtual LBR check Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 14/19] perf test: Drop samples without an IP in the dlfilter test Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 15/19] perf test: Skip the LBR inline unwinding check when unsupported Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 16/19] perf test: Skip the trace summary BPF tests without kernel BTF Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 17/19] perf test: Avoid online CPUs in the offline stat test Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 18/19] perf scripts: Add a coverage harness for perf Arnaldo Carvalho de Melo
2026-11-22 21:40 ` [PATCH v11 19/19] perf scripts: Add a coverage table Arnaldo Carvalho de Melo

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=20261122214025.16769-13-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=adrian.hunter@intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=williams@redhat.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