stat_bpf_counters_cgrp.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  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_bpf_counter
  54. find_cgroups
  55. check_system_wide_counted
  56. exit 0