162 lines · bash
1#!/bin/bash2# perf record LBR tests3# SPDX-License-Identifier: GPL-2.04 5set -e6 7if [ ! -f /sys/devices/cpu/caps/branches ] && [ ! -f /sys/devices/cpu_core/caps/branches ]8then9 echo "Skip: only x86 CPUs support LBR"10 exit 211fi12 13err=014perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)15 16cleanup() {17 rm -rf "${perfdata}"18 rm -rf "${perfdata}".old19 rm -rf "${perfdata}".txt20 21 trap - EXIT TERM INT22}23 24trap_cleanup() {25 cleanup26 exit 127}28trap trap_cleanup EXIT TERM INT29 30 31lbr_callgraph_test() {32 test="LBR callgraph"33 34 echo "$test"35 if ! perf record -e cycles --call-graph lbr -o "${perfdata}" perf test -w thloop36 then37 echo "$test [Failed support missing]"38 if [ $err -eq 0 ]39 then40 err=241 fi42 return43 fi44 45 if ! perf report --stitch-lbr -i "${perfdata}" > "${perfdata}".txt46 then47 cat "${perfdata}".txt48 echo "$test [Failed in perf report]"49 err=150 return51 fi52 53 echo "$test [Success]"54}55 56lbr_test() {57 local branch_flags=$158 local test="LBR $2 test"59 local threshold=$360 local out61 local sam_nr62 local bs_nr63 local zero_nr64 local r65 66 echo "$test"67 if ! perf record -e cycles $branch_flags -o "${perfdata}" perf test -w thloop68 then69 echo "$test [Failed support missing]"70 perf record -e cycles $branch_flags -o "${perfdata}" perf test -w thloop || true71 if [ $err -eq 0 ]72 then73 err=274 fi75 return76 fi77 78 out=$(perf report -D -i "${perfdata}" 2> /dev/null | grep -A1 'PERF_RECORD_SAMPLE')79 sam_nr=$(echo "$out" | grep -c 'PERF_RECORD_SAMPLE' || true)80 if [ $sam_nr -eq 0 ]81 then82 echo "$test [Failed no samples captured]"83 err=184 return85 fi86 echo "$test: $sam_nr samples"87 88 bs_nr=$(echo "$out" | grep -c 'branch stack: nr:' || true)89 if [ $sam_nr -ne $bs_nr ]90 then91 echo "$test [Failed samples missing branch stacks]"92 err=193 return94 fi95 96 zero_nr=$(echo "$out" | grep -c 'branch stack: nr:0' || true)97 r=$(($zero_nr * 100 / $bs_nr))98 if [ $r -gt $threshold ]; then99 echo "$test [Failed empty br stack ratio exceed $threshold%: $r%]"100 err=1101 return102 fi103 104 echo "$test [Success]"105}106 107parallel_lbr_test() {108 err=0109 perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)110 lbr_test "$1" "$2" "$3"111 cleanup112 exit $err113}114 115lbr_callgraph_test116 117# Sequential118lbr_test "-b" "any branch" 2119lbr_test "-j any_call" "any call" 2120lbr_test "-j any_ret" "any ret" 2121lbr_test "-j ind_call" "any indirect call" 2122lbr_test "-j ind_jmp" "any indirect jump" 100123lbr_test "-j call" "direct calls" 2124lbr_test "-j ind_call,u" "any indirect user call" 100125lbr_test "-a -b" "system wide any branch" 2126lbr_test "-a -j any_call" "system wide any call" 2127 128# Parallel129parallel_lbr_test "-b" "parallel any branch" 100 &130pid1=$!131parallel_lbr_test "-j any_call" "parallel any call" 100 &132pid2=$!133parallel_lbr_test "-j any_ret" "parallel any ret" 100 &134pid3=$!135parallel_lbr_test "-j ind_call" "parallel any indirect call" 100 &136pid4=$!137parallel_lbr_test "-j ind_jmp" "parallel any indirect jump" 100 &138pid5=$!139parallel_lbr_test "-j call" "parallel direct calls" 100 &140pid6=$!141parallel_lbr_test "-j ind_call,u" "parallel any indirect user call" 100 &142pid7=$!143parallel_lbr_test "-a -b" "parallel system wide any branch" 100 &144pid8=$!145parallel_lbr_test "-a -j any_call" "parallel system wide any call" 100 &146pid9=$!147 148for pid in $pid1 $pid2 $pid3 $pid4 $pid5 $pid6 $pid7 $pid8 $pid9149do150 set +e151 wait $pid152 child_err=$?153 set -e154 if ([ $err -eq 2 ] && [ $child_err -eq 1 ]) || [ $err -eq 0 ]155 then156 err=$child_err157 fi158done159 160cleanup161exit $err162