2 # Check branch stack sampling
4 # SPDX-License-Identifier: GPL-2.0
7 # we need a C compiler to build the test programs
8 # so bail if none is found
9 if ! [ -x "$(command -v cc)" ]; then
10 echo "failed: no compiler, install gcc"
14 # skip the test if the hardware doesn't support branch stack sampling
15 perf record -b -o- -B true > /dev/null 2>&1 || exit 2
17 TMPDIR=$(mktemp -d /tmp/__perf_test.program.XXXXX)
23 trap cleanup exit term int
26 # generate test program
28 #define BENCH_RUNS 999999
36 void (*foo_ind)(void) = foo;
37 if ((cnt++) % 3) /* branch (cond) */
40 foo_ind(); /* call (ind) */
46 if ((cnt++) > BENCH_RUNS)
49 } /* branch (uncond) */
55 test_user_branches() {
56 echo "Testing user branch stack sampling"
58 gen_test_program "$TEMPDIR/program.c"
59 cc -fno-inline -g "$TEMPDIR/program.c" -o $TMPDIR/a.out
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
64 # example of branch entries:
65 # foo+0x14/bar+0x40/P/-/-/0/CALL
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
78 # some branch types are still not being tested:
79 # IND COND_CALL COND_RET SYSCALL SYSRET IRQ SERROR NO_TX
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
88 echo "Testing branch stack filtering permutation ($filter,$expect)"
90 gen_test_program "$TEMPDIR/program.c"
91 cc -fno-inline -g "$TEMPDIR/program.c" -o $TMPDIR/a.out
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
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
107 test_filter "any_call" "CALL|IND_CALL|COND_CALL|SYSCALL|IRQ"
108 test_filter "call" "CALL|SYSCALL"
109 test_filter "cond" "COND"
110 test_filter "any_ret" "RET|COND_RET|SYSRET|ERET"
112 test_filter "call,cond" "CALL|SYSCALL|COND"
113 test_filter "any_call,cond" "CALL|IND_CALL|COND_CALL|IRQ|SYSCALL|COND"
114 test_filter "cond,any_call,any_ret" "COND|CALL|IND_CALL|COND_CALL|SYSCALL|IRQ|RET|COND_RET|SYSRET|ERET"