stat_bpf_counters.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. # perf stat --bpf-counters test (exclusive)
  3. # SPDX-License-Identifier: GPL-2.0
  4. set -e
  5. workload="perf test -w sqrtloop"
  6. # check whether $2 is within +/- 20% of $1
  7. compare_number()
  8. {
  9. first_num=$1
  10. second_num=$2
  11. # upper bound is first_num * 120%
  12. upper=$(expr $first_num + $first_num / 5 )
  13. # lower bound is first_num * 80%
  14. lower=$(expr $first_num - $first_num / 5 )
  15. if [ $second_num -gt $upper ] || [ $second_num -lt $lower ]; then
  16. echo "The difference between $first_num and $second_num are greater than 20%."
  17. exit 1
  18. fi
  19. }
  20. check_counts()
  21. {
  22. base_instructions=$1
  23. bpf_instructions=$2
  24. if [ "$base_instructions" = "<not" ]; then
  25. echo "Skipping: instructions event not counted"
  26. exit 2
  27. fi
  28. if [ "$bpf_instructions" = "<not" ]; then
  29. echo "Failed: instructions not counted with --bpf-counters"
  30. exit 1
  31. fi
  32. }
  33. test_bpf_counters()
  34. {
  35. printf "Testing --bpf-counters "
  36. base_instructions=$(perf stat --no-big-num -e instructions -- $workload 2>&1 | awk '/instructions/ {print $1}')
  37. bpf_instructions=$(perf stat --no-big-num --bpf-counters -e instructions -- $workload 2>&1 | awk '/instructions/ {print $1}')
  38. check_counts $base_instructions $bpf_instructions
  39. compare_number $base_instructions $bpf_instructions
  40. echo "[Success]"
  41. }
  42. test_bpf_modifier()
  43. {
  44. printf "Testing bpf event modifier "
  45. stat_output=$(perf stat --no-big-num -e instructions/name=base_instructions/,instructions/name=bpf_instructions/b -- $workload 2>&1)
  46. base_instructions=$(echo "$stat_output"| awk '/base_instructions/ {print $1}')
  47. bpf_instructions=$(echo "$stat_output"| awk '/bpf_instructions/ {print $1}')
  48. check_counts $base_instructions $bpf_instructions
  49. compare_number $base_instructions $bpf_instructions
  50. echo "[Success]"
  51. }
  52. # skip if --bpf-counters is not supported
  53. if ! perf stat -e instructions --bpf-counters true > /dev/null 2>&1; then
  54. if [ "$1" = "-v" ]; then
  55. echo "Skipping: --bpf-counters not supported"
  56. perf --no-pager stat -e instructions --bpf-counters true || true
  57. fi
  58. exit 2
  59. fi
  60. test_bpf_counters
  61. test_bpf_modifier
  62. exit 0