179010860SNamhyung Kim#!/bin/sh 279010860SNamhyung Kim# perf stat --bpf-counters --for-each-cgroup test 379010860SNamhyung Kim# SPDX-License-Identifier: GPL-2.0 479010860SNamhyung Kim 579010860SNamhyung Kimset -e 679010860SNamhyung Kim 779010860SNamhyung Kimtest_cgroups= 879010860SNamhyung Kimif [ "$1" = "-v" ]; then 979010860SNamhyung Kim verbose="1" 1079010860SNamhyung Kimfi 1179010860SNamhyung Kim 1279010860SNamhyung Kim# skip if --bpf-counters --for-each-cgroup is not supported 1379010860SNamhyung Kimcheck_bpf_counter() 1479010860SNamhyung Kim{ 1579010860SNamhyung Kim if ! perf stat -a --bpf-counters --for-each-cgroup / true > /dev/null 2>&1; then 1679010860SNamhyung Kim if [ "${verbose}" = "1" ]; then 1779010860SNamhyung Kim echo "Skipping: --bpf-counters --for-each-cgroup not supported" 1879010860SNamhyung Kim perf --no-pager stat -a --bpf-counters --for-each-cgroup / true || true 1979010860SNamhyung Kim fi 2079010860SNamhyung Kim exit 2 2179010860SNamhyung Kim fi 2279010860SNamhyung Kim} 2379010860SNamhyung Kim 2479010860SNamhyung Kim# find two cgroups to measure 2579010860SNamhyung Kimfind_cgroups() 2679010860SNamhyung Kim{ 2779010860SNamhyung Kim # try usual systemd slices first 280dd1f815SKajol Jain if [ -d /sys/fs/cgroup/system.slice ] && [ -d /sys/fs/cgroup/user.slice ]; then 2979010860SNamhyung Kim test_cgroups="system.slice,user.slice" 3079010860SNamhyung Kim return 3179010860SNamhyung Kim fi 3279010860SNamhyung Kim 3379010860SNamhyung Kim # try root and self cgroups 340dd1f815SKajol Jain find_cgroups_self_cgrp=$(grep perf_event /proc/self/cgroup | cut -d: -f3) 350dd1f815SKajol Jain if [ -z ${find_cgroups_self_cgrp} ]; then 3679010860SNamhyung Kim # cgroup v2 doesn't specify perf_event 370dd1f815SKajol Jain find_cgroups_self_cgrp=$(grep ^0: /proc/self/cgroup | cut -d: -f3) 3879010860SNamhyung Kim fi 3979010860SNamhyung Kim 400dd1f815SKajol Jain if [ -z ${find_cgroups_self_cgrp} ]; then 4179010860SNamhyung Kim test_cgroups="/" 4279010860SNamhyung Kim else 430dd1f815SKajol Jain test_cgroups="/,${find_cgroups_self_cgrp}" 4479010860SNamhyung Kim fi 4579010860SNamhyung Kim} 4679010860SNamhyung Kim 4779010860SNamhyung Kim# As cgroup events are cpu-wide, we cannot simply compare the result. 4879010860SNamhyung Kim# Just check if it runs without failure and has non-zero results. 4979010860SNamhyung Kimcheck_system_wide_counted() 5079010860SNamhyung Kim{ 510dd1f815SKajol Jain check_system_wide_counted_output=$(perf stat -a --bpf-counters --for-each-cgroup ${test_cgroups} -e cpu-clock -x, sleep 1 2>&1) 520dd1f815SKajol Jain if echo ${check_system_wide_counted_output} | grep -q -F "<not "; then 5379010860SNamhyung Kim echo "Some system-wide events are not counted" 5479010860SNamhyung Kim if [ "${verbose}" = "1" ]; then 550dd1f815SKajol Jain echo ${check_system_wide_counted_output} 5679010860SNamhyung Kim fi 5779010860SNamhyung Kim exit 1 5879010860SNamhyung Kim fi 5979010860SNamhyung Kim} 6079010860SNamhyung Kim 6179010860SNamhyung Kimcheck_cpu_list_counted() 6279010860SNamhyung Kim{ 63*a84260e3SNamhyung Kim 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) 640dd1f815SKajol Jain if echo ${check_cpu_list_counted_output} | grep -q -F "<not "; then 6579010860SNamhyung Kim echo "Some CPU events are not counted" 6679010860SNamhyung Kim if [ "${verbose}" = "1" ]; then 670dd1f815SKajol Jain echo ${check_cpu_list_counted_output} 6879010860SNamhyung Kim fi 6979010860SNamhyung Kim exit 1 7079010860SNamhyung Kim fi 7179010860SNamhyung Kim} 7279010860SNamhyung Kim 7379010860SNamhyung Kimcheck_bpf_counter 7479010860SNamhyung Kimfind_cgroups 7579010860SNamhyung Kim 7679010860SNamhyung Kimcheck_system_wide_counted 7779010860SNamhyung Kimcheck_cpu_list_counted 7879010860SNamhyung Kim 7979010860SNamhyung Kimexit 0 80