1#!/bin/bash 2# perf record LBR tests (exclusive) 3# SPDX-License-Identifier: GPL-2.0 4 5set -e 6 7if [ ! -f /sys/bus/event_source/devices/cpu/caps/branches ] && 8 [ ! -f /sys/bus/event_source/devices/cpu_core/caps/branches ] 9then 10 echo "Skip: only x86 CPUs support LBR" 11 exit 2 12fi 13 14err=0 15perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX) 16 17cleanup() { 18 rm -rf "${perfdata}" 19 rm -rf "${perfdata}".old 20 rm -rf "${perfdata}".txt 21 22 trap - EXIT TERM INT 23} 24 25trap_cleanup() { 26 cleanup 27 exit 1 28} 29trap trap_cleanup EXIT TERM INT 30 31 32lbr_callgraph_test() { 33 test="LBR callgraph" 34 35 echo "$test" 36 if ! perf record -e cycles --call-graph lbr -o "${perfdata}" perf test -w thloop 37 then 38 echo "$test [Failed support missing]" 39 if [ $err -eq 0 ] 40 then 41 err=2 42 fi 43 return 44 fi 45 46 if ! perf report --stitch-lbr -i "${perfdata}" > "${perfdata}".txt 47 then 48 cat "${perfdata}".txt 49 echo "$test [Failed in perf report]" 50 err=1 51 return 52 fi 53 54 echo "$test [Success]" 55} 56 57lbr_test() { 58 local branch_flags=$1 59 local test="LBR $2 test" 60 local threshold=$3 61 local out 62 local sam_nr 63 local bs_nr 64 local zero_nr 65 local r 66 67 echo "$test" 68 if ! perf record -e cycles $branch_flags -o "${perfdata}" perf test -w thloop 69 then 70 echo "$test [Failed support missing]" 71 perf record -e cycles $branch_flags -o "${perfdata}" perf test -w thloop || true 72 if [ $err -eq 0 ] 73 then 74 err=2 75 fi 76 return 77 fi 78 79 out=$(perf report -D -i "${perfdata}" 2> /dev/null | grep -A1 'PERF_RECORD_SAMPLE') 80 sam_nr=$(echo "$out" | grep -c 'PERF_RECORD_SAMPLE' || true) 81 if [ $sam_nr -eq 0 ] 82 then 83 echo "$test [Failed no samples captured]" 84 err=1 85 return 86 fi 87 echo "$test: $sam_nr samples" 88 89 bs_nr=$(echo "$out" | grep -c 'branch stack: nr:' || true) 90 if [ $sam_nr -ne $bs_nr ] 91 then 92 echo "$test [Failed samples missing branch stacks]" 93 err=1 94 return 95 fi 96 97 zero_nr=$(echo "$out" | grep -A3 'branch stack: nr:0' | grep thread | grep -cv swapper || true) 98 r=$(($zero_nr * 100 / $bs_nr)) 99 if [ $r -gt $threshold ]; then 100 echo "$test [Failed empty br stack ratio exceed $threshold%: $r%]" 101 err=1 102 return 103 fi 104 105 echo "$test [Success]" 106} 107 108parallel_lbr_test() { 109 err=0 110 perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX) 111 lbr_test "$1" "$2" "$3" 112 cleanup 113 exit $err 114} 115 116lbr_callgraph_test 117 118# Sequential 119lbr_test "-b" "any branch" 2 120lbr_test "-j any_call" "any call" 2 121lbr_test "-j any_ret" "any ret" 2 122lbr_test "-j ind_call" "any indirect call" 2 123lbr_test "-j ind_jmp" "any indirect jump" 100 124lbr_test "-j call" "direct calls" 2 125lbr_test "-j ind_call,u" "any indirect user call" 100 126lbr_test "-a -b" "system wide any branch" 2 127lbr_test "-a -j any_call" "system wide any call" 2 128 129# Parallel 130parallel_lbr_test "-b" "parallel any branch" 100 & 131pid1=$! 132parallel_lbr_test "-j any_call" "parallel any call" 100 & 133pid2=$! 134parallel_lbr_test "-j any_ret" "parallel any ret" 100 & 135pid3=$! 136parallel_lbr_test "-j ind_call" "parallel any indirect call" 100 & 137pid4=$! 138parallel_lbr_test "-j ind_jmp" "parallel any indirect jump" 100 & 139pid5=$! 140parallel_lbr_test "-j call" "parallel direct calls" 100 & 141pid6=$! 142parallel_lbr_test "-j ind_call,u" "parallel any indirect user call" 100 & 143pid7=$! 144parallel_lbr_test "-a -b" "parallel system wide any branch" 100 & 145pid8=$! 146parallel_lbr_test "-a -j any_call" "parallel system wide any call" 100 & 147pid9=$! 148 149for pid in $pid1 $pid2 $pid3 $pid4 $pid5 $pid6 $pid7 $pid8 $pid9 150do 151 set +e 152 wait $pid 153 child_err=$? 154 set -e 155 if ([ $err -eq 2 ] && [ $child_err -eq 1 ]) || [ $err -eq 0 ] 156 then 157 err=$child_err 158 fi 159done 160 161cleanup 162exit $err 163