Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/sh
0002 # perf stat --bpf-counters --for-each-cgroup test
0003 # SPDX-License-Identifier: GPL-2.0
0004 
0005 set -e
0006 
0007 test_cgroups=
0008 if [ "$1" = "-v" ]; then
0009         verbose="1"
0010 fi
0011 
0012 # skip if --bpf-counters --for-each-cgroup is not supported
0013 check_bpf_counter()
0014 {
0015         if ! perf stat -a --bpf-counters --for-each-cgroup / true > /dev/null 2>&1; then
0016                 if [ "${verbose}" = "1" ]; then
0017                         echo "Skipping: --bpf-counters --for-each-cgroup not supported"
0018                         perf --no-pager stat -a --bpf-counters --for-each-cgroup / true || true
0019                 fi
0020                 exit 2
0021         fi
0022 }
0023 
0024 # find two cgroups to measure
0025 find_cgroups()
0026 {
0027         # try usual systemd slices first
0028         if [ -d /sys/fs/cgroup/system.slice -a -d /sys/fs/cgroup/user.slice ]; then
0029                 test_cgroups="system.slice,user.slice"
0030                 return
0031         fi
0032 
0033         # try root and self cgroups
0034         local self_cgrp=$(grep perf_event /proc/self/cgroup | cut -d: -f3)
0035         if [ -z ${self_cgrp} ]; then
0036                 # cgroup v2 doesn't specify perf_event
0037                 self_cgrp=$(grep ^0: /proc/self/cgroup | cut -d: -f3)
0038         fi
0039 
0040         if [ -z ${self_cgrp} ]; then
0041                 test_cgroups="/"
0042         else
0043                 test_cgroups="/,${self_cgrp}"
0044         fi
0045 }
0046 
0047 # As cgroup events are cpu-wide, we cannot simply compare the result.
0048 # Just check if it runs without failure and has non-zero results.
0049 check_system_wide_counted()
0050 {
0051         local output
0052 
0053         output=$(perf stat -a --bpf-counters --for-each-cgroup ${test_cgroups} -e cpu-clock -x, sleep 1  2>&1)
0054         if echo ${output} | grep -q -F "<not "; then
0055                 echo "Some system-wide events are not counted"
0056                 if [ "${verbose}" = "1" ]; then
0057                         echo ${output}
0058                 fi
0059                 exit 1
0060         fi
0061 }
0062 
0063 check_cpu_list_counted()
0064 {
0065         local output
0066 
0067         output=$(perf stat -C 1 --bpf-counters --for-each-cgroup ${test_cgroups} -e cpu-clock -x, taskset -c 1 sleep 1  2>&1)
0068         if echo ${output} | grep -q -F "<not "; then
0069                 echo "Some CPU events are not counted"
0070                 if [ "${verbose}" = "1" ]; then
0071                         echo ${output}
0072                 fi
0073                 exit 1
0074         fi
0075 }
0076 
0077 check_bpf_counter
0078 find_cgroups
0079 
0080 check_system_wide_counted
0081 check_cpu_list_counted
0082 
0083 exit 0