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