[PATCH v2 0/2] Fix cgroup metric association with BPF counters

From: Ian Rogers

Date: Mon May 18 2026 - 21:51:24 EST


This series fixes an issue where cgroup metrics were not being correctly
associated and reported (showing `nan %`) when using BPF counters
(i.e., with `--bpf-counters`).

The root cause is that cgroup BPF counters only open the "leader" events
(for the first cgroup) and leave "follower" events unopened. Unopened
events have their `supported` flag set to `false` by default. During
metric calculation, `prepare_metric` checks `evsel->supported` and
discards the value (setting it to `NAN`) if it is `false`, leading to
`nan %` in the output.

The first patch fixes this by propagating the `supported` flag from the
leader events to the follower events in `bperf_load_program`.

The second patch adds a new shell test (`stat_metrics_cgrp.sh`) to
permanently cover this scenario (testing both with and without BPF
counters) and prevent regressions.

Reported-by: Svilen Kanev <skanev@xxxxxxxxxx>

Changes since v1:
- Fix commit message for patch 2 to remove the mention of
insn_per_cycle check (which was simplified out).
- Quote variables in shell script to prevent word splitting.
- Use grep -F in shell script for literal matching of cgroup
names containing dots.

Ian Rogers (2):
perf stat: Propagate supported flag to follower cgroup BPF events
perf test: Add stat metrics --for-each-cgroup test

tools/perf/tests/shell/stat_metrics_cgrp.sh | 123 ++++++++++++++++++++
tools/perf/util/bpf_counter_cgroup.c | 15 +++
2 files changed, 138 insertions(+)
create mode 100755 tools/perf/tests/shell/stat_metrics_cgrp.sh

--
2.54.0.631.ge1b05301d1-goog