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