test_arm_spe.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash
  2. # Check Arm SPE trace data recording and synthesized samples (exclusive)
  3. # Uses the 'perf record' to record trace data of Arm SPE events;
  4. # then verify if any SPE event samples are generated by SPE with
  5. # 'perf script' and 'perf report' commands.
  6. # SPDX-License-Identifier: GPL-2.0
  7. # German Gomez <german.gomez@arm.com>, 2021
  8. skip_if_no_arm_spe_event() {
  9. perf list pmu | grep -E -q 'arm_spe_[0-9]+//' && return 0
  10. # arm_spe event doesn't exist
  11. return 2
  12. }
  13. skip_if_no_arm_spe_event || exit 2
  14. perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
  15. glb_err=0
  16. cleanup_files()
  17. {
  18. rm -f ${perfdata}
  19. rm -f ${perfdata}.old
  20. exit $glb_err
  21. }
  22. trap cleanup_files EXIT TERM INT
  23. arm_spe_report() {
  24. if [ $2 = 0 ]; then
  25. echo "$1: PASS"
  26. elif [ $2 = 2 ]; then
  27. echo "$1: SKIPPED"
  28. else
  29. echo "$1: FAIL"
  30. glb_err=$2
  31. fi
  32. }
  33. perf_script_samples() {
  34. echo "Looking at perf.data file for dumping samples:"
  35. # from arm-spe.c/arm_spe_synth_events()
  36. events="(ld1-miss|ld1-access|llc-miss|lld-access|tlb-miss|tlb-access|branch-miss|remote-access|memory)"
  37. # Below is an example of the samples dumping:
  38. # dd 3048 [002] 1 l1d-access: ffffaa64999c __GI___libc_write+0x3c (/lib/aarch64-linux-gnu/libc-2.27.so)
  39. # dd 3048 [002] 1 tlb-access: ffffaa64999c __GI___libc_write+0x3c (/lib/aarch64-linux-gnu/libc-2.27.so)
  40. # dd 3048 [002] 1 memory: ffffaa64999c __GI___libc_write+0x3c (/lib/aarch64-linux-gnu/libc-2.27.so)
  41. perf script -F,-time -i ${perfdata} 2>&1 | \
  42. grep -E " +$1 +[0-9]+ .* +${events}:(.*:)? +" > /dev/null 2>&1
  43. }
  44. perf_report_samples() {
  45. echo "Looking at perf.data file for reporting samples:"
  46. # Below is an example of the samples reporting:
  47. # 73.04% 73.04% dd libc-2.27.so [.] _dl_addr
  48. # 7.71% 7.71% dd libc-2.27.so [.] getenv
  49. # 2.59% 2.59% dd ld-2.27.so [.] strcmp
  50. perf report --stdio -i ${perfdata} 2>&1 | \
  51. grep -E " +[0-9]+\.[0-9]+% +[0-9]+\.[0-9]+% +$1 " > /dev/null 2>&1
  52. }
  53. arm_spe_snapshot_test() {
  54. echo "Recording trace with snapshot mode $perfdata"
  55. perf record -o ${perfdata} -e arm_spe// -S \
  56. -- dd if=/dev/zero of=/dev/null > /dev/null 2>&1 &
  57. PERFPID=$!
  58. # Wait for perf program
  59. sleep 1
  60. # Send signal to snapshot trace data
  61. kill -USR2 $PERFPID
  62. # Stop perf program
  63. kill $PERFPID
  64. wait $PERFPID
  65. perf_script_samples dd &&
  66. perf_report_samples dd
  67. err=$?
  68. arm_spe_report "SPE snapshot testing" $err
  69. }
  70. arm_spe_system_wide_test() {
  71. echo "Recording trace with system-wide mode $perfdata"
  72. perf record -o - -e dummy -a -B true > /dev/null 2>&1
  73. if [ $? != 0 ]; then
  74. arm_spe_report "SPE system-wide testing" 2
  75. return
  76. fi
  77. perf record -o ${perfdata} -e arm_spe// -a --no-bpf-event \
  78. -- dd if=/dev/zero of=/dev/null count=100000 > /dev/null 2>&1
  79. perf_script_samples dd &&
  80. perf_report_samples dd
  81. err=$?
  82. arm_spe_report "SPE system-wide testing" $err
  83. }
  84. arm_spe_discard_test() {
  85. echo "SPE discard mode"
  86. for f in /sys/bus/event_source/devices/arm_spe_*; do
  87. if [ -e "$f/format/discard" ]; then
  88. cpu=$(cut -c -1 "$f/cpumask")
  89. break
  90. fi
  91. done
  92. if [ -z $cpu ]; then
  93. arm_spe_report "SPE discard mode not present" 2
  94. return
  95. fi
  96. # Test can use wildcard SPE instance and Perf will only open the event
  97. # on instances that have that format flag. But make sure the target
  98. # runs on an instance with discard mode otherwise we're not testing
  99. # anything.
  100. perf record -o ${perfdata} -e arm_spe/discard/ -N -B --no-bpf-event \
  101. -- taskset --cpu-list $cpu true
  102. if perf report -i ${perfdata} --stats | grep 'AUX events\|AUXTRACE events'; then
  103. arm_spe_report "SPE discard mode found unexpected data" 1
  104. else
  105. arm_spe_report "SPE discard mode" 0
  106. fi
  107. }
  108. arm_spe_snapshot_test
  109. arm_spe_system_wide_test
  110. arm_spe_discard_test
  111. exit $glb_err