File Index
Files and directories
Sign in
FILE

record_lbr.sh

File preview with syntax highlighting.

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