Re: [PATCH v3] perf stat: Count kernel events in a group led by a tool event

From: Namhyung Kim

Date: Fri Nov 20 2026 - 16:45:28 EST


On Thu, Nov 19, 2026 at 10:08:38AM +0000, TAKANO Mitsuhiro wrote:
> Hi Namhyung,
>
> > I think it's better to make the first non-tool event as a leader
>
> I agree, and I think that is where this ends up. What stops me from
> doing it in this patch is the group read: with a kernel event as the
> leader, evsel__group_read_nr_members() still counts a tool event member,
> while the kernel knows nothing about it (a tool event is not opened
> through perf_event_open, it reads /proc/stat) and returns one entry
> fewer. evsel__process_group_data() then rejects the read with -EINVAL
> and the kernel events of that group come out as <not counted> as well.
> The smallest version of that is a few lines, but it changes what every
> group with a tool event member prints, so it wants its own test and its
> own commit rather than riding along with the EBADF fix.
>
> > but I'm afraid it'd make evlist management complex
>
> v3 leaves the group structure alone: no new evlist entry, nr_members
> unchanged, and evsel->leader stays whoever it already was. What changes
> is how the members are opened:
>
> - get_group_fd(): with group_fd = -1 instead of the tool event's
> /proc/stat fd, or -2 when the leader failed to open, the same -2 the
> skipped-leader case already returns for a broken group;
> - create_perf_stat_counter(): a kernel event member is opened disabled,
> as a leader is, so that it starts with the workload, and
> PERF_FORMAT_GROUP is not requested for the members - read_format is
> per fd, and GROUP is what would send each member's read down the
> group path that only a leader can take. PERF_FORMAT_ID goes with it:
> without a group read nothing demuxes ids.
>
> The cost is real and the changelog states it: in -e '{user_time,a,b}',
> a and b no longer share an on/off window.
>
> Two things this does not change, both of which happen without the patch:
> a tool event member still counts through a -D / --control window, and a
> tool event leader under enable_on_exec is still not started by anything -
> that is what "perf stat: Start tool events when enable_on_exec is used"
> is for.
>
> Either way the leader move is a separate series; I can post it once the
> direction is settled.

I think grouping with tool events would cause more headaches. Probably
the simplest fix is to prohibit grouping.

Thanks,
Namhyung