Skip to content

Commit 7901086

Browse files
namhyungacmel
authored andcommitted
perf test: Add a new test for perf stat cgroup BPF counter
$ sudo ./perf test -v each-cgroup 96: perf stat --bpf-counters --for-each-cgroup test : --- start --- test child forked, pid 79600 test child finished with 0 ---- end ---- perf stat --bpf-counters --for-each-cgroup test: Ok Signed-off-by: Namhyung Kim <namhyung@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Ian Rogers <irogers@google.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Song Liu <songliubraving@fb.com> Cc: bpf@vger.kernel.org Link: https://lore.kernel.org/r/20220916184132.1161506-5-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 8a92605 commit 7901086

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/sh
2+
# perf stat --bpf-counters --for-each-cgroup test
3+
# SPDX-License-Identifier: GPL-2.0
4+
5+
set -e
6+
7+
test_cgroups=
8+
if [ "$1" = "-v" ]; then
9+
verbose="1"
10+
fi
11+
12+
# skip if --bpf-counters --for-each-cgroup is not supported
13+
check_bpf_counter()
14+
{
15+
if ! perf stat -a --bpf-counters --for-each-cgroup / true > /dev/null 2>&1; then
16+
if [ "${verbose}" = "1" ]; then
17+
echo "Skipping: --bpf-counters --for-each-cgroup not supported"
18+
perf --no-pager stat -a --bpf-counters --for-each-cgroup / true || true
19+
fi
20+
exit 2
21+
fi
22+
}
23+
24+
# find two cgroups to measure
25+
find_cgroups()
26+
{
27+
# try usual systemd slices first
28+
if [ -d /sys/fs/cgroup/system.slice -a -d /sys/fs/cgroup/user.slice ]; then
29+
test_cgroups="system.slice,user.slice"
30+
return
31+
fi
32+
33+
# try root and self cgroups
34+
local self_cgrp=$(grep perf_event /proc/self/cgroup | cut -d: -f3)
35+
if [ -z ${self_cgrp} ]; then
36+
# cgroup v2 doesn't specify perf_event
37+
self_cgrp=$(grep ^0: /proc/self/cgroup | cut -d: -f3)
38+
fi
39+
40+
if [ -z ${self_cgrp} ]; then
41+
test_cgroups="/"
42+
else
43+
test_cgroups="/,${self_cgrp}"
44+
fi
45+
}
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.
49+
check_system_wide_counted()
50+
{
51+
local output
52+
53+
output=$(perf stat -a --bpf-counters --for-each-cgroup ${test_cgroups} -e cpu-clock -x, sleep 1 2>&1)
54+
if echo ${output} | grep -q -F "<not "; then
55+
echo "Some system-wide events are not counted"
56+
if [ "${verbose}" = "1" ]; then
57+
echo ${output}
58+
fi
59+
exit 1
60+
fi
61+
}
62+
63+
check_cpu_list_counted()
64+
{
65+
local output
66+
67+
output=$(perf stat -C 1 --bpf-counters --for-each-cgroup ${test_cgroups} -e cpu-clock -x, taskset -c 1 sleep 1 2>&1)
68+
if echo ${output} | grep -q -F "<not "; then
69+
echo "Some CPU events are not counted"
70+
if [ "${verbose}" = "1" ]; then
71+
echo ${output}
72+
fi
73+
exit 1
74+
fi
75+
}
76+
77+
check_bpf_counter
78+
find_cgroups
79+
80+
check_system_wide_counted
81+
check_cpu_list_counted
82+
83+
exit 0

0 commit comments

Comments
 (0)