Skip to content

Commit f3061d5

Browse files
captain5050acmel
authored andcommitted
perf tests: Harden branch stack sampling test
On continuous testing the perf script output can be empty, or nearly empty, causing tr/grep to exit and due to "set -e" the test traps and fails. Add some empty file handling that sets the test to skip and make grep and other text rewriting failures non-fatal by adding "|| true". Committer testing: root@number:~# grep -m1 "model name" /proc/cpuinfo model name : AMD Ryzen 9 9950X3D 16-Core Processor root@number:~# perf test "Check branch stack sampling" 104: Check branch stack sampling : Ok root@number:~# root@number:~# perf test -vvvvvvv "Check branch stack sampling" 104: Check branch stack sampling: --- start --- test child forked, pid 396047 142d22-142da0 l brstack_bench perf does have symbol 'brstack_bench' Testing user branch stack sampling Testing branch stack filtering permutation (any_call,CALL|IND_CALL|COND_CALL|SYSCALL|IRQ) Testing branch stack filtering permutation (call,CALL|SYSCALL) Testing branch stack filtering permutation (cond,COND) Testing branch stack filtering permutation (any_ret,RET|COND_RET|SYSRET|ERET) Testing branch stack filtering permutation (call,cond,CALL|SYSCALL|COND) Testing branch stack filtering permutation (any_call,cond,CALL|IND_CALL|COND_CALL|IRQ|SYSCALL|COND) Testing branch stack filtering permutation (cond,any_call,any_ret,COND|CALL|IND_CALL|COND_CALL|SYSCALL|IRQ|RET|COND_RET|SYSRET|ERET) ---- end(0) ---- 104: Check branch stack sampling : Ok root@number:~# Signed-off-by: Ian Rogers <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Anshuman Khandual <[email protected]> Cc: German Gomez <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 255f5b6 commit f3061d5

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

tools/perf/tests/shell/test_brstack.sh

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
# Check branch stack sampling
33

44
# SPDX-License-Identifier: GPL-2.0
@@ -17,35 +17,50 @@ fi
1717

1818
skip_test_missing_symbol brstack_bench
1919

20+
err=0
2021
TMPDIR=$(mktemp -d /tmp/__perf_test.program.XXXXX)
2122
TESTPROG="perf test -w brstack"
2223

2324
cleanup() {
2425
rm -rf $TMPDIR
26+
trap - EXIT TERM INT
2527
}
2628

27-
trap cleanup EXIT TERM INT
29+
trap_cleanup() {
30+
set +e
31+
echo "Unexpected signal in ${FUNCNAME[1]}"
32+
cleanup
33+
exit 1
34+
}
35+
trap trap_cleanup EXIT TERM INT
2836

2937
test_user_branches() {
3038
echo "Testing user branch stack sampling"
3139

32-
perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u -- ${TESTPROG} > /dev/null 2>&1
33-
perf script -i $TMPDIR/perf.data --fields brstacksym | tr -s ' ' '\n' > $TMPDIR/perf.script
40+
perf record -o "$TMPDIR/perf.data" --branch-filter any,save_type,u -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
41+
perf script -i "$TMPDIR/perf.data" --fields brstacksym > "$TMPDIR/perf.script"
3442

3543
# example of branch entries:
3644
# brstack_foo+0x14/brstack_bar+0x40/P/-/-/0/CALL
3745

38-
set -x
39-
grep -E -m1 "^brstack_bench\+[^ ]*/brstack_foo\+[^ ]*/IND_CALL/.*$" $TMPDIR/perf.script
40-
grep -E -m1 "^brstack_foo\+[^ ]*/brstack_bar\+[^ ]*/CALL/.*$" $TMPDIR/perf.script
41-
grep -E -m1 "^brstack_bench\+[^ ]*/brstack_foo\+[^ ]*/CALL/.*$" $TMPDIR/perf.script
42-
grep -E -m1 "^brstack_bench\+[^ ]*/brstack_bar\+[^ ]*/CALL/.*$" $TMPDIR/perf.script
43-
grep -E -m1 "^brstack_bar\+[^ ]*/brstack_foo\+[^ ]*/RET/.*$" $TMPDIR/perf.script
44-
grep -E -m1 "^brstack_foo\+[^ ]*/brstack_bench\+[^ ]*/RET/.*$" $TMPDIR/perf.script
45-
grep -E -m1 "^brstack_bench\+[^ ]*/brstack_bench\+[^ ]*/COND/.*$" $TMPDIR/perf.script
46-
grep -E -m1 "^brstack\+[^ ]*/brstack\+[^ ]*/UNCOND/.*$" $TMPDIR/perf.script
47-
set +x
48-
46+
expected=(
47+
"^brstack_bench\+[^ ]*/brstack_foo\+[^ ]*/IND_CALL/.*$"
48+
"^brstack_foo\+[^ ]*/brstack_bar\+[^ ]*/CALL/.*$"
49+
"^brstack_bench\+[^ ]*/brstack_foo\+[^ ]*/CALL/.*$"
50+
"^brstack_bench\+[^ ]*/brstack_bar\+[^ ]*/CALL/.*$"
51+
"^brstack_bar\+[^ ]*/brstack_foo\+[^ ]*/RET/.*$"
52+
"^brstack_foo\+[^ ]*/brstack_bench\+[^ ]*/RET/.*$"
53+
"^brstack_bench\+[^ ]*/brstack_bench\+[^ ]*/COND/.*$"
54+
"^brstack\+[^ ]*/brstack\+[^ ]*/UNCOND/.*$"
55+
)
56+
for x in "${expected[@]}"
57+
do
58+
if ! tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep -E -m1 -q "$x"
59+
then
60+
echo "Branches missing $x"
61+
err=1
62+
fi
63+
done
4964
# some branch types are still not being tested:
5065
# IND COND_CALL COND_RET SYSCALL SYSRET IRQ SERROR NO_TX
5166
}
@@ -57,14 +72,28 @@ test_filter() {
5772
test_filter_expect=$2
5873

5974
echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
60-
61-
perf record -o $TMPDIR/perf.data --branch-filter $test_filter_filter,save_type,u -- ${TESTPROG} > /dev/null 2>&1
62-
perf script -i $TMPDIR/perf.data --fields brstack | tr -s ' ' '\n' | grep '.' > $TMPDIR/perf.script
75+
perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
76+
perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.script"
6377

6478
# fail if we find any branch type that doesn't match any of the expected ones
6579
# also consider UNKNOWN branch types (-)
66-
if grep -E -vm1 "^[^ ]*/($test_filter_expect|-|( *))/.*$" $TMPDIR/perf.script; then
67-
return 1
80+
if [ ! -s "$TMPDIR/perf.script" ]
81+
then
82+
echo "Empty script output"
83+
err=1
84+
return
85+
fi
86+
# Look for lines not matching test_filter_expect ignoring issues caused
87+
# by empty output
88+
tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep '.' | \
89+
grep -E -vm1 "^[^ ]*/($test_filter_expect|-|( *))/.*$" \
90+
> "$TMPDIR/perf.script-filtered" || true
91+
if [ -s "$TMPDIR/perf.script-filtered" ]
92+
then
93+
echo "Unexpected branch filter in script output"
94+
cat "$TMPDIR/perf.script"
95+
err=1
96+
return
6897
fi
6998
}
7099

@@ -80,3 +109,6 @@ test_filter "any_ret" "RET|COND_RET|SYSRET|ERET"
80109
test_filter "call,cond" "CALL|SYSCALL|COND"
81110
test_filter "any_call,cond" "CALL|IND_CALL|COND_CALL|IRQ|SYSCALL|COND"
82111
test_filter "cond,any_call,any_ret" "COND|CALL|IND_CALL|COND_CALL|SYSCALL|IRQ|RET|COND_RET|SYSRET|ERET"
112+
113+
cleanup
114+
exit $err

0 commit comments

Comments
 (0)