Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "TAKANO Mitsuhiro" <takano32@gmail.com>,
	linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v3] perf stat: Count kernel events in a group led by a tool event
Date: Wed, 11 Nov 2026 11:33:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-208886@kernel.org> (raw)
In-Reply-To: <20261111110616.2249764-1-takano32@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf stat: broken tool event enable/disable for -D,
  --control, and exec in create_perf_stat_counter()
--

commit 0e981d5b2ece1f32ac47445309993697c6cdb096
Author: Hang Zhengyang <hang.zhengyang1010@gmail.com>

perf stat: Count kernel events in a group led by a tool event

This commit opens members of a group led by a tool event on their own
with group_fd = -1 and without PERF_FORMAT_GROUP, while opening kernel
event members initially disabled.

> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index 37752d6bfef45..57956f55ea09b 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
[ ... ]
> @@ -748,9 +749,16 @@ static int create_perf_stat_counter(struct evsel *evsel,
>  	/*
>  	 * Disabling all counters initially, they will be enabled
>  	 * either manually by us or by kernel via enable_on_exec
> -	 * set later.
> +	 * set later.  A kernel event that a tool event leader has as
> +	 * a member is not in the kernel's group either, so it has to
> +	 * be opened disabled to start together with the workload.
> +	 * A tool event member counts from open and is only stopped by
> +	 * an explicit disable, so leaving it enabled here keeps its
> +	 * count; disabling it would leave nobody to start it when
> +	 * perf relies on enable_on_exec.
>  	 */
> -	if (evsel__is_group_leader(evsel)) {
> +	if (evsel__is_group_leader(evsel) ||
> +	    (evsel__is_tool(leader) && !evsel__is_tool(evsel))) {
>  		attr->disabled = 1;
>  
>  		if (target__enable_on_exec(&target))

[Severity: Medium]
Does conditioning attr->disabled on leader vs. member rather than
target__enable_on_exec(&target) leave tool event members enabled during
-D / --control and leave tool event leaders disabled during exec
workloads?

In evsel__tool_pmu_open(), attr->disabled sets evsel->disabled = true and
start_time = INVALID_START_TIME; otherwise evsel->disabled is false and
start_time is sampled immediately at open.

When !target__enable_on_exec(&target) (such as with -D or --control),
enable_counters() calls evlist__enable(), and evsel__enable_cpu() enables
each member of a tool-led group via evsel__tool_pmu_enable_cpu():

tools/perf/util/tool_pmu.c:evsel__tool_pmu_enable_cpu() {
	if (!evsel->disabled)
		return 0;
    ...
}

Because a tool event member in a group like '{user_time,system_time}' is
skipped by !evsel__is_tool(evsel) and opened with attr->disabled == 0,
evsel__tool_pmu_enable_cpu() returns early without resetting start_time,
so the member counts continuously through the disabled delay window.

Conversely, when target__enable_on_exec(&target) is true (such as
'perf stat -e "{user_time,cycles}" -- sleep 0.2'), enable_counters() skips
evlist__enable() because kernel events are enabled on exec.  A tool event
leader still matches evsel__is_group_leader(evsel) and opens with
evsel->disabled = true; nothing enables it on exec, so
evsel__tool_pmu_read() returns 0.

Should tool events instead set attr->disabled = 1 only when
!target__enable_on_exec(&target), regardless of whether they are the
group leader or a member?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261111110616.2249764-1-takano32@gmail.com?part=1

  reply	other threads:[~2026-11-11 11:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11  6:04 [PATCH v2] perf stat: Count kernel events in a group led by a tool event Hang Zhengyang via B4 Relay
2026-11-11  6:18 ` sashiko-bot
2026-11-11 11:06 ` [PATCH v3] " TAKANO Mitsuhiro
2026-11-11 11:33   ` sashiko-bot [this message]
2026-11-12  3:49     ` Hang Zhengyang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-208886@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=takano32@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox