linux/tools/perf/tests/shell/stat_bpf_counters_cgrp.sh
James Clark 2f5d370dec perf test: Change all remaining #!/bin/sh to #!/bin/bash
There are 43 instances of posix shell tests and 35 instances of bash. To
give us a single consistent language for testing in, replace
all #!/bin/sh to #!/bin/bash. Common sources that are included in both
different shells will now work as expected. And we no longer have to fix
up bashisms that appear to work when someone's system has sh symlinked
to bash, but don't work on other systems that have both shells
installed.

Although we could have chosen sh, it's not backwards compatible so it
wouldn't be possible to bulk convert without re-writing the existing
bash tests.

Choosing bash also gives us some nicer features including 'local'
variable definitions and regexes in if statements that are already
widely used in the tests.

It's not expected that there are any users with only sh available due to
the large number of bash tests that exist.

Discussed in relation to running shellcheck here:
https://lore.kernel.org/linux-perf-users/e3751a74be34bbf3781c4644f518702a7270220b.1749785642.git.collin.funk1@gmail.com/

Signed-off-by: James Clark <james.clark@linaro.org>
Reviewed-by: Collin Funk <collin.funk1@gmail.com>
Acked-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Link: https://lore.kernel.org/r/20250623-james-perf-bash-tests-v1-1-f572f54d4559@linaro.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
2025-06-26 10:31:05 -07:00

66 lines
1.7 KiB
Bash
Executable file

#!/bin/bash
# perf stat --bpf-counters --for-each-cgroup test
# SPDX-License-Identifier: GPL-2.0
set -e
test_cgroups=
if [ "$1" = "-v" ]; then
verbose="1"
fi
# skip if --bpf-counters --for-each-cgroup is not supported
check_bpf_counter()
{
if ! perf stat -a --bpf-counters --for-each-cgroup / true > /dev/null 2>&1; then
if [ "${verbose}" = "1" ]; then
echo "Skipping: --bpf-counters --for-each-cgroup not supported"
perf --no-pager stat -a --bpf-counters --for-each-cgroup / true || true
fi
exit 2
fi
}
# find two cgroups to measure
find_cgroups()
{
# try usual systemd slices first
if [ -d /sys/fs/cgroup/system.slice ] && [ -d /sys/fs/cgroup/user.slice ]; then
test_cgroups="system.slice,user.slice"
return
fi
# try root and self cgroups
find_cgroups_self_cgrp=$(grep perf_event /proc/self/cgroup | cut -d: -f3)
if [ -z ${find_cgroups_self_cgrp} ]; then
# cgroup v2 doesn't specify perf_event
find_cgroups_self_cgrp=$(grep ^0: /proc/self/cgroup | cut -d: -f3)
fi
if [ -z ${find_cgroups_self_cgrp} ]; then
test_cgroups="/"
else
test_cgroups="/,${find_cgroups_self_cgrp}"
fi
}
# As cgroup events are cpu-wide, we cannot simply compare the result.
# Just check if it runs without failure and has non-zero results.
check_system_wide_counted()
{
check_system_wide_counted_output=$(perf stat -a --bpf-counters --for-each-cgroup ${test_cgroups} -e cpu-clock -x, sleep 1 2>&1)
if echo ${check_system_wide_counted_output} | grep -q -F "<not "; then
echo "Some system-wide events are not counted"
if [ "${verbose}" = "1" ]; then
echo ${check_system_wide_counted_output}
fi
exit 1
fi
}
check_bpf_counter
find_cgroups
check_system_wide_counted
exit 0