stat_bpf_counters_cgrp.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/sh
  2. # perf stat --bpf-counters --for-each-cgroup test
  3. # SPDX-License-Identifier: GPL-2.0
  4. set -e
  5. test_cgroups=
  6. if [ "$1" = "-v" ]; then
  7. verbose="1"
  8. fi
  9. # skip if --bpf-counters --for-each-cgroup is not supported
  10. check_bpf_counter()
  11. {
  12. if ! perf stat -a --bpf-counters --for-each-cgroup / true > /dev/null 2>&1; then
  13. if [ "${verbose}" = "1" ]; then
  14. echo "Skipping: --bpf-counters --for-each-cgroup not supported"
  15. perf --no-pager stat -a --bpf-counters --for-each-cgroup / true || true
  16. fi
  17. exit 2
  18. fi
  19. }
  20. # find two cgroups to measure
  21. find_cgroups()
  22. {
  23. # try usual systemd slices first
  24. if [ -d /sys/fs/cgroup/system.slice ] && [ -d /sys/fs/cgroup/user.slice ]; then
  25. test_cgroups="system.slice,user.slice"
  26. return
  27. fi
  28. # try root and self cgroups
  29. find_cgroups_self_cgrp=$(grep perf_event /proc/self/cgroup | cut -d: -f3)
  30. if [ -z ${find_cgroups_self_cgrp} ]; then
  31. # cgroup v2 doesn't specify perf_event
  32. find_cgroups_self_cgrp=$(grep ^0: /proc/self/cgroup | cut -d: -f3)
  33. fi
  34. if [ -z ${find_cgroups_self_cgrp} ]; then
  35. test_cgroups="/"
  36. else
  37. test_cgroups="/,${find_cgroups_self_cgrp}"
  38. fi
  39. }
  40. # As cgroup events are cpu-wide, we cannot simply compare the result.
  41. # Just check if it runs without failure and has non-zero results.
  42. check_system_wide_counted()
  43. {
  44. check_system_wide_counted_output=$(perf stat -a --bpf-counters --for-each-cgroup ${test_cgroups} -e cpu-clock -x, sleep 1 2>&1)
  45. if echo ${check_system_wide_counted_output} | grep -q -F "<not "; then
  46. echo "Some system-wide events are not counted"
  47. if [ "${verbose}" = "1" ]; then
  48. echo ${check_system_wide_counted_output}
  49. fi
  50. exit 1
  51. fi
  52. }
  53. check_cpu_list_counted()
  54. {
  55. check_cpu_list_counted_output=$(perf stat -C 0,1 --bpf-counters --for-each-cgroup ${test_cgroups} -e cpu-clock -x, taskset -c 1 sleep 1 2>&1)
  56. if echo ${check_cpu_list_counted_output} | grep -q -F "<not "; then
  57. echo "Some CPU events are not counted"
  58. if [ "${verbose}" = "1" ]; then
  59. echo ${check_cpu_list_counted_output}
  60. fi
  61. exit 1
  62. fi
  63. }
  64. check_bpf_counter
  65. find_cgroups
  66. check_system_wide_counted
  67. check_cpu_list_counted
  68. exit 0