1#!/bin/sh
2# Check branch stack sampling
3
4# SPDX-License-Identifier: GPL-2.0
5# German Gomez <german.gomez@arm.com>, 2022
6
7# we need a C compiler to build the test programs
8# so bail if none is found
9if ! [ -x "$(command -v cc)" ]; then
10	echo "failed: no compiler, install gcc"
11	exit 2
12fi
13
14# skip the test if the hardware doesn't support branch stack sampling
15perf record -b -o- -B true > /dev/null 2>&1 || exit 2
16
17TMPDIR=$(mktemp -d /tmp/__perf_test.program.XXXXX)
18
19cleanup() {
20	rm -rf $TMPDIR
21}
22
23trap cleanup exit term int
24
25gen_test_program() {
26	# generate test program
27	cat << EOF > $1
28#define BENCH_RUNS 999999
29int cnt;
30void bar(void) {
31}			/* return */
32void foo(void) {
33	bar();		/* call */
34}			/* return */
35void bench(void) {
36  void (*foo_ind)(void) = foo;
37  if ((cnt++) % 3)	/* branch (cond) */
38    foo();		/* call */
39  bar();		/* call */
40  foo_ind();		/* call (ind) */
41}
42int main(void)
43{
44  int cnt = 0;
45  while (1) {
46    if ((cnt++) > BENCH_RUNS)
47      break;
48    bench();		/* call */
49  }			/* branch (uncond) */
50  return 0;
51}
52EOF
53}
54
55test_user_branches() {
56	echo "Testing user branch stack sampling"
57
58	gen_test_program "$TEMPDIR/program.c"
59	cc -fno-inline -g "$TEMPDIR/program.c" -o $TMPDIR/a.out
60
61	perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u -- $TMPDIR/a.out > /dev/null 2>&1
62	perf script -i $TMPDIR/perf.data --fields brstacksym | xargs -n1 > $TMPDIR/perf.script
63
64	# example of branch entries:
65	# 	foo+0x14/bar+0x40/P/-/-/0/CALL
66
67	set -x
68	egrep -m1 "^bench\+[^ ]*/foo\+[^ ]*/IND_CALL$"	$TMPDIR/perf.script
69	egrep -m1 "^foo\+[^ ]*/bar\+[^ ]*/CALL$"	$TMPDIR/perf.script
70	egrep -m1 "^bench\+[^ ]*/foo\+[^ ]*/CALL$"	$TMPDIR/perf.script
71	egrep -m1 "^bench\+[^ ]*/bar\+[^ ]*/CALL$"	$TMPDIR/perf.script
72	egrep -m1 "^bar\+[^ ]*/foo\+[^ ]*/RET$"		$TMPDIR/perf.script
73	egrep -m1 "^foo\+[^ ]*/bench\+[^ ]*/RET$"	$TMPDIR/perf.script
74	egrep -m1 "^bench\+[^ ]*/bench\+[^ ]*/COND$"	$TMPDIR/perf.script
75	egrep -m1 "^main\+[^ ]*/main\+[^ ]*/UNCOND$"	$TMPDIR/perf.script
76	set +x
77
78	# some branch types are still not being tested:
79	# IND COND_CALL COND_RET SYSCALL SYSRET IRQ SERROR NO_TX
80}
81
82# first argument <arg0> is the argument passed to "--branch-stack <arg0>,save_type,u"
83# second argument are the expected branch types for the given filter
84test_filter() {
85	local filter=$1
86	local expect=$2
87
88	echo "Testing branch stack filtering permutation ($filter,$expect)"
89
90	gen_test_program "$TEMPDIR/program.c"
91	cc -fno-inline -g "$TEMPDIR/program.c" -o $TMPDIR/a.out
92
93	perf record -o $TMPDIR/perf.data --branch-filter $filter,save_type,u -- $TMPDIR/a.out > /dev/null 2>&1
94	perf script -i $TMPDIR/perf.data --fields brstack | xargs -n1 > $TMPDIR/perf.script
95
96	# fail if we find any branch type that doesn't match any of the expected ones
97	# also consider UNKNOWN branch types (-)
98	if egrep -vm1 "^[^ ]*/($expect|-|( *))$" $TMPDIR/perf.script; then
99		return 1
100	fi
101}
102
103set -e
104
105test_user_branches
106
107test_filter "any_call"	"CALL|IND_CALL|COND_CALL|SYSCALL|IRQ"
108test_filter "call"	"CALL|SYSCALL"
109test_filter "cond"	"COND"
110test_filter "any_ret"	"RET|COND_RET|SYSRET|ERET"
111
112test_filter "call,cond"		"CALL|SYSCALL|COND"
113test_filter "any_call,cond"		"CALL|IND_CALL|COND_CALL|IRQ|SYSCALL|COND"
114test_filter "cond,any_call,any_ret"	"COND|CALL|IND_CALL|COND_CALL|SYSCALL|IRQ|RET|COND_RET|SYSRET|ERET"
115