From: Tengda Wu <wutengda@huaweicloud.com>
To: Ian Rogers <irogers@google.com>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] perf bpf_counter: Fix opening of "any"(-1) CPU events
Date: Thu, 9 Oct 2025 15:52:22 +0800 [thread overview]
Message-ID: <02ac6916-7bfa-4b6b-8bae-64fe02580731@huaweicloud.com> (raw)
In-Reply-To: <20251008162347.4005288-1-irogers@google.com>
On 2025/10/9 0:23, Ian Rogers wrote:
> The bperf BPF counter code doesn't handle "any"(-1) CPU events, always
> wanting to aggregate a count against a CPU, which avoids the need for
> atomics so let's not change that. Force evsels used for BPF counters
> to require a CPU when not in system-wide mode so that the "any"(-1)
> value isn't used during map propagation and evsel's CPU map matches
> that of the PMU.
>
> Fixes: b91917c0c6fa ("perf bpf_counter: Fix handling of cpumap fixing hybrid")
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> tools/perf/builtin-stat.c | 13 +++++++++++++
> tools/perf/util/bpf_counter.c | 1 +
> 2 files changed, 14 insertions(+)
>
> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index 7006f848f87a..0fc6884c1bf1 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
> @@ -2540,6 +2540,7 @@ int cmd_stat(int argc, const char **argv)
> unsigned int interval, timeout;
> const char * const stat_subcommands[] = { "record", "report" };
> char errbuf[BUFSIZ];
> + struct evsel *counter;
>
> setlocale(LC_ALL, "");
>
> @@ -2797,6 +2798,18 @@ int cmd_stat(int argc, const char **argv)
>
> evlist__warn_user_requested_cpus(evsel_list, target.cpu_list);
>
> + evlist__for_each_entry(evsel_list, counter) {
> + /*
> + * Setup BPF counters to require CPUs as any(-1) isn't
> + * supported. evlist__create_maps below will propagate this
> + * information to the evsels. Note, evsel__is_bperf isn't yet
> + * set up, and this change must happen early, so directly use
> + * the bpf_counter variable.
> + */
> + if (counter->bpf_counter)
> + counter->core.requires_cpu = true;
> + }
> +
> if (evlist__create_maps(evsel_list, &target) < 0) {
> if (target__has_task(&target)) {
> pr_err("Problems finding threads of monitor\n");
> diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c
> index ca5d01b9017d..d3e5933b171b 100644
> --- a/tools/perf/util/bpf_counter.c
> +++ b/tools/perf/util/bpf_counter.c
> @@ -495,6 +495,7 @@ static int bperf_reload_leader_program(struct evsel *evsel, int attr_map_fd,
> * following evsel__open_per_cpu call
> */
> evsel->leader_skel = skel;
> + assert(!perf_cpu_map__has_any_cpu_or_is_empty(evsel->core.cpus));
> evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
>
> out:
I must point out that `requires_cpu + evsel__open(evsel, evsel->core.cpus, evsel->core.threads)`
is not equivalent to the original `evsel__open_per_cpu(evsel, all_cpu_map, -1)`. The former
specifies a pid, while the latter does not. This will lead to inaccurate final event counting.
For `evsel__open_per_cpu(evsel, all_cpu_map, -1)`:
$ ./perf stat -vv --bpf-counters -e task-clock ./perf test -w sqrtloop
sys_perf_event_open: pid -1 cpu 0 group_fd -1 flags 0x8 = 13
sys_perf_event_open: pid -1 cpu 1 group_fd -1 flags 0x8 = 14
sys_perf_event_open: pid -1 cpu 2 group_fd -1 flags 0x8 = 15
[...]
Performance counter stats for './perf test -w sqrtloop':
1,016,156,671 task-clock # 1.000 CPUs utilized
1.016294745 seconds time elapsed
1.005710000 seconds user
0.010637000 seconds sys
For `requires_cpu + evsel__open(evsel, evsel->core.cpus, evsel->core.threads)`:
$ ./perf stat -vv --bpf-counters -e task-clock ./perf test -w sqrtloop
sys_perf_event_open: pid 75099 cpu 0 group_fd -1 flags 0x8 = 13
sys_perf_event_open: pid 75099 cpu 1 group_fd -1 flags 0x8 = 14
sys_perf_event_open: pid 75099 cpu 2 group_fd -1 flags 0x8 = 15
[...]
Performance counter stats for './perf test -w sqrtloop':
16,184,507 task-clock # 0.016 CPUs utilized
1.018540734 seconds time elapsed
1.009143000 seconds user
0.009497000 seconds sys
As you can see, after specifying a pid, the task-clock count has significantly decreased.
So to correct the counting, we may also need to keep the pid as -1 without specifying it.
Thanks,
Tengda
next prev parent reply other threads:[~2025-10-09 7:52 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-08 16:23 [PATCH v2] perf bpf_counter: Fix opening of "any"(-1) CPU events Ian Rogers
2025-10-09 5:58 ` Namhyung Kim
2025-10-09 7:52 ` Tengda Wu [this message]
2025-10-09 13:03 ` Ian Rogers
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=02ac6916-7bfa-4b6b-8bae-64fe02580731@huaweicloud.com \
--to=wutengda@huaweicloud.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/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;
as well as URLs for NNTP newsgroup(s).