]> Git Repo - linux.git/blob - tools/perf/tests/shell/test_brstack.sh
kasan: move tests to mm/kasan/
[linux.git] / tools / perf / tests / shell / test_brstack.sh
1 #!/bin/sh
2 # Check branch stack sampling
3
4 # SPDX-License-Identifier: GPL-2.0
5 # German Gomez <[email protected]>, 2022
6
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"
11         exit 2
12 fi
13
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
16
17 TMPDIR=$(mktemp -d /tmp/__perf_test.program.XXXXX)
18
19 cleanup() {
20         rm -rf $TMPDIR
21 }
22
23 trap cleanup exit term int
24
25 gen_test_program() {
26         # generate test program
27         cat << EOF > $1
28 #define BENCH_RUNS 999999
29 int cnt;
30 void bar(void) {
31 }                       /* return */
32 void foo(void) {
33         bar();          /* call */
34 }                       /* return */
35 void 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 }
42 int 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 }
52 EOF
53 }
54
55 test_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
84 test_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
103 set -e
104
105 test_user_branches
106
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"
111
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"
This page took 0.049124 seconds and 4 git commands to generate.