test_brstack.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/bin/bash
  2. # Check branch stack sampling
  3. # SPDX-License-Identifier: GPL-2.0
  4. # German Gomez <german.gomez@arm.com>, 2022
  5. shelldir=$(dirname "$0")
  6. # shellcheck source=lib/perf_has_symbol.sh
  7. . "${shelldir}"/lib/perf_has_symbol.sh
  8. # skip the test if the hardware doesn't support branch stack sampling
  9. # and if the architecture doesn't support filter types: any,save_type,u
  10. if ! perf record -o- --no-buildid --branch-filter any,save_type,u -- true > /dev/null 2>&1 ; then
  11. echo "skip: system doesn't support filter types: any,save_type,u"
  12. exit 2
  13. fi
  14. skip_test_missing_symbol brstack_bench
  15. err=0
  16. TMPDIR=$(mktemp -d /tmp/__perf_test.program.XXXXX)
  17. TESTPROG="perf test -w brstack"
  18. cleanup() {
  19. rm -rf $TMPDIR
  20. trap - EXIT TERM INT
  21. }
  22. trap_cleanup() {
  23. set +e
  24. echo "Unexpected signal in ${FUNCNAME[1]}"
  25. cleanup
  26. exit 1
  27. }
  28. trap trap_cleanup EXIT TERM INT
  29. is_arm64() {
  30. [ "$(uname -m)" = "aarch64" ];
  31. }
  32. check_branches() {
  33. if ! tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep -E -m1 -q "$1"; then
  34. echo "Branches missing $1"
  35. err=1
  36. fi
  37. }
  38. test_user_branches() {
  39. echo "Testing user branch stack sampling"
  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"
  42. # example of branch entries:
  43. # brstack_foo+0x14/brstack_bar+0x40/P/-/-/0/CALL
  44. expected=(
  45. "^brstack_bench\+[^ ]*/brstack_foo\+[^ ]*/IND_CALL/.*$"
  46. "^brstack_foo\+[^ ]*/brstack_bar\+[^ ]*/CALL/.*$"
  47. "^brstack_bench\+[^ ]*/brstack_foo\+[^ ]*/CALL/.*$"
  48. "^brstack_bench\+[^ ]*/brstack_bar\+[^ ]*/CALL/.*$"
  49. "^brstack_bar\+[^ ]*/brstack_foo\+[^ ]*/RET/.*$"
  50. "^brstack_foo\+[^ ]*/brstack_bench\+[^ ]*/RET/.*$"
  51. "^brstack_bench\+[^ ]*/brstack_bench\+[^ ]*/COND/.*$"
  52. "^brstack\+[^ ]*/brstack\+[^ ]*/UNCOND/.*$"
  53. )
  54. for x in "${expected[@]}"
  55. do
  56. check_branches "$x"
  57. done
  58. # Dump addresses only this time
  59. perf script -i "$TMPDIR/perf.data" --fields brstack | \
  60. tr ' ' '\n' > "$TMPDIR/perf.script"
  61. # There should be no kernel addresses with the u option, in either
  62. # source or target addresses.
  63. if grep -E -m1 "0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
  64. echo "ERROR: Kernel address found in user mode"
  65. err=1
  66. fi
  67. # some branch types are still not being tested:
  68. # IND COND_CALL COND_RET SYSRET SERROR NO_TX
  69. }
  70. test_trap_eret_branches() {
  71. echo "Testing trap & eret branches"
  72. if ! is_arm64; then
  73. echo "skip: not arm64"
  74. else
  75. perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u,k -- \
  76. perf test -w traploop 1000
  77. perf script -i $TMPDIR/perf.data --fields brstacksym | \
  78. tr ' ' '\n' > $TMPDIR/perf.script
  79. # BRBINF<n>.TYPE == TRAP are mapped to PERF_BR_IRQ by the BRBE driver
  80. check_branches "^trap_bench\+[^ ]+/[^ ]/IRQ/"
  81. check_branches "^[^ ]+/trap_bench\+[^ ]+/ERET/"
  82. fi
  83. }
  84. test_kernel_branches() {
  85. echo "Testing that k option only includes kernel source addresses"
  86. if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
  87. echo "skip: not enough privileges"
  88. else
  89. perf record -o $TMPDIR/perf.data --branch-filter any,k -- \
  90. perf bench syscall basic --loop 1000
  91. perf script -i $TMPDIR/perf.data --fields brstack | \
  92. tr ' ' '\n' > $TMPDIR/perf.script
  93. # Example of branch entries:
  94. # "0xffffffff93bda241/0xffffffff93bda20f/M/-/-/..."
  95. # Source addresses come first and target address can be either
  96. # userspace or kernel even with k option, as long as the source
  97. # is in kernel.
  98. #Look for source addresses with top bit set
  99. if ! grep -E -m1 "^0x[89a-f][0-9a-f]{15}" $TMPDIR/perf.script; then
  100. echo "ERROR: Kernel branches missing"
  101. err=1
  102. fi
  103. # Look for no source addresses without top bit set
  104. if grep -E -m1 "^0x[0-7][0-9a-f]{0,15}" $TMPDIR/perf.script; then
  105. echo "ERROR: User branches found with kernel filter"
  106. err=1
  107. fi
  108. fi
  109. }
  110. # first argument <arg0> is the argument passed to "--branch-stack <arg0>,save_type,u"
  111. # second argument are the expected branch types for the given filter
  112. test_filter() {
  113. test_filter_filter=$1
  114. test_filter_expect=$2
  115. echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
  116. perf record -o "$TMPDIR/perf.data" --branch-filter "$test_filter_filter,save_type,u" -- ${TESTPROG} > "$TMPDIR/record.txt" 2>&1
  117. perf script -i "$TMPDIR/perf.data" --fields brstack > "$TMPDIR/perf.script"
  118. # fail if we find any branch type that doesn't match any of the expected ones
  119. # also consider UNKNOWN branch types (-)
  120. if [ ! -s "$TMPDIR/perf.script" ]
  121. then
  122. echo "Empty script output"
  123. err=1
  124. return
  125. fi
  126. # Look for lines not matching test_filter_expect ignoring issues caused
  127. # by empty output
  128. tr -s ' ' '\n' < "$TMPDIR/perf.script" | grep '.' | \
  129. grep -E -vm1 "^[^ ]*/($test_filter_expect|-|( *))/.*$" \
  130. > "$TMPDIR/perf.script-filtered" || true
  131. if [ -s "$TMPDIR/perf.script-filtered" ]
  132. then
  133. echo "Unexpected branch filter in script output"
  134. cat "$TMPDIR/perf.script"
  135. err=1
  136. return
  137. fi
  138. }
  139. test_syscall() {
  140. echo "Testing syscalls"
  141. # skip if perf doesn't have enough privileges
  142. if ! perf record --branch-filter any,k -o- -- true > /dev/null; then
  143. echo "skip: not enough privileges"
  144. else
  145. perf record -o $TMPDIR/perf.data --branch-filter \
  146. any_call,save_type,u,k -c 10000 -- \
  147. perf bench syscall basic --loop 1000
  148. perf script -i $TMPDIR/perf.data --fields brstacksym | \
  149. tr ' ' '\n' > $TMPDIR/perf.script
  150. check_branches "getppid[^ ]*/SYSCALL/"
  151. fi
  152. }
  153. set -e
  154. test_user_branches
  155. test_syscall
  156. test_kernel_branches
  157. test_trap_eret_branches
  158. any_call="CALL|IND_CALL|COND_CALL|SYSCALL|IRQ"
  159. if is_arm64; then
  160. any_call="$any_call|FAULT_DATA|FAULT_INST"
  161. fi
  162. test_filter "any_call" "$any_call"
  163. test_filter "call" "CALL|SYSCALL"
  164. test_filter "cond" "COND"
  165. test_filter "any_ret" "RET|COND_RET|SYSRET|ERET"
  166. test_filter "call,cond" "CALL|SYSCALL|COND"
  167. test_filter "any_call,cond" "$any_call|COND"
  168. test_filter "any_call,cond,any_ret" "$any_call|COND|RET|COND_RET"
  169. cleanup
  170. exit $err