80 lines · bash
1#!/bin/sh2# perf stat --bpf-counters --for-each-cgroup test3# SPDX-License-Identifier: GPL-2.04 5set -e6 7test_cgroups=8if [ "$1" = "-v" ]; then9 verbose="1"10fi11 12# skip if --bpf-counters --for-each-cgroup is not supported13check_bpf_counter()14{15 if ! perf stat -a --bpf-counters --for-each-cgroup / true > /dev/null 2>&1; then16 if [ "${verbose}" = "1" ]; then17 echo "Skipping: --bpf-counters --for-each-cgroup not supported"18 perf --no-pager stat -a --bpf-counters --for-each-cgroup / true || true19 fi20 exit 221 fi22}23 24# find two cgroups to measure25find_cgroups()26{27 # try usual systemd slices first28 if [ -d /sys/fs/cgroup/system.slice ] && [ -d /sys/fs/cgroup/user.slice ]; then29 test_cgroups="system.slice,user.slice"30 return31 fi32 33 # try root and self cgroups34 find_cgroups_self_cgrp=$(grep perf_event /proc/self/cgroup | cut -d: -f3)35 if [ -z ${find_cgroups_self_cgrp} ]; then36 # cgroup v2 doesn't specify perf_event37 find_cgroups_self_cgrp=$(grep ^0: /proc/self/cgroup | cut -d: -f3)38 fi39 40 if [ -z ${find_cgroups_self_cgrp} ]; then41 test_cgroups="/"42 else43 test_cgroups="/,${find_cgroups_self_cgrp}"44 fi45}46 47# As cgroup events are cpu-wide, we cannot simply compare the result.48# Just check if it runs without failure and has non-zero results.49check_system_wide_counted()50{51 check_system_wide_counted_output=$(perf stat -a --bpf-counters --for-each-cgroup ${test_cgroups} -e cpu-clock -x, sleep 1 2>&1)52 if echo ${check_system_wide_counted_output} | grep -q -F "<not "; then53 echo "Some system-wide events are not counted"54 if [ "${verbose}" = "1" ]; then55 echo ${check_system_wide_counted_output}56 fi57 exit 158 fi59}60 61check_cpu_list_counted()62{63 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)64 if echo ${check_cpu_list_counted_output} | grep -q -F "<not "; then65 echo "Some CPU events are not counted"66 if [ "${verbose}" = "1" ]; then67 echo ${check_cpu_list_counted_output}68 fi69 exit 170 fi71}72 73check_bpf_counter74find_cgroups75 76check_system_wide_counted77check_cpu_list_counted78 79exit 080