From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>, Jiri Olsa <jolsa@kernel.org>,
Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>
Cc: Howard Chu <howardchu95@gmail.com>,
Thomas Falcon <thomas.falcon@intel.com>,
Song Liu <songliubraving@fb.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v1 05/13] perf stat: Preserve per-evsel CPU maps across stat record and report
Date: Sun, 22 Nov 2026 23:19:05 -0800 [thread overview]
Message-ID: <20261123071913.653315-6-irogers@google.com> (raw)
In-Reply-To: <20261123071913.653315-1-irogers@google.com>
Commit 6d765f5f7ec6 ("libperf evsel: Rename own_cpus to pmu_cpus") and
commit 811082e4b668 ("perf parse-events: Support user CPUs mixed with
threads/processes") separated an evsel's PMU cpumask (pmu_cpus) from the
actual CPUs on which the event is opened (cpus) and taught
__perf_evlist__propagate_maps() to collapse evsel->cpus to the "any CPU"
(-1) map when user_requested_cpus is -1 and !evsel->requires_cpu.
However, perf_event__synthesize_event_update_cpus() continued to
serialize evsel->core.pmu_cpus instead of evsel->core.cpus, and
perf_event__process_event_update() only populated evsel->core.pmu_cpus
without setting evsel->core.cpus or evsel->core.requires_cpu. This broke
perf stat record/report (and perf script) in two ways:
1. When recording an event opened per-CPU alongside a workload target
(where user_requested_cpus is -1), such as an event with a cpu= term,
an uncore counter, --bpf-counters, or a BPF-filtered counter,
PERF_RECORD_STAT events are written with concrete CPU numbers, but
perf stat report's perf_evlist__set_maps() collapses evsel->cpus to
-1 and fails with:
$ perf stat record -e 'task-clock/cpu=0/,task-clock' -- true
$ perf stat report
Invalid CPU 0 for event task-clock/cpu=0/.
2. In system-wide mode (-a), per-evsel CPU restrictions such as cpu=0
were overwritten by the full PMU cpumask during report, causing
perf stat report -A to report bogus <not counted> entries on every
other CPU.
Fix perf_event__synthesize_event_update_cpus() to serialize
evsel->core.cpus, and update perf_event__process_event_update() to
populate evsel->core.cpus and set requires_cpu when the recorded CPU map
does not use the "any CPU" (-1) placeholder. For backward compatibility
with data files recorded by older perf binaries that wrote pmu_cpus for
per-task (-1) counters, fall back to the "any CPU" map in
perf_event__process_stat_event() if a -1 CPU stat event arrives for an
evsel without -1 in its CPU map.
Update the event_update unit test and stat.sh record/report/script tests
to cover per-evsel CPU maps on workload targets.
Fixes: 6d765f5f7ec6 ("libperf evsel: Rename own_cpus to pmu_cpus")
Fixes: 811082e4b668 ("perf parse-events: Support user CPUs mixed with threads/processes")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/event_update.c | 43 ++++++++++++++++++++++++++----
tools/perf/tests/shell/stat.sh | 16 +++++++++++
tools/perf/util/evsel.c | 2 +-
| 5 ++++
tools/perf/util/stat.c | 18 +++++++++++++
tools/perf/util/synthetic-events.c | 7 +++--
6 files changed, 83 insertions(+), 8 deletions(-)
diff --git a/tools/perf/tests/event_update.c b/tools/perf/tests/event_update.c
index 220cc0347747..bacf3c44c8e2 100644
--- a/tools/perf/tests/event_update.c
+++ b/tools/perf/tests/event_update.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/compiler.h>
#include <perf/cpumap.h>
+#include <perf/evlist.h>
#include <string.h>
#include "cpumap.h"
#include "evlist.h"
@@ -58,13 +59,20 @@ static int process_event_name(const struct perf_tool *tool,
return 0;
}
-static int process_event_cpus(const struct perf_tool *tool __maybe_unused,
+struct event_cpus {
+ struct perf_tool tool;
+ struct evlist *evlist;
+};
+
+static int process_event_cpus(const struct perf_tool *tool,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
struct machine *machine __maybe_unused)
{
+ struct event_cpus *tmp = container_of(tool, struct event_cpus, tool);
struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
- struct perf_cpu_map *map;
+ struct perf_cpu_map *map, *any_cpu;
+ struct evsel *evsel;
map = cpu_map__new_data(&ev->cpus.cpus);
@@ -75,6 +83,26 @@ static int process_event_cpus(const struct perf_tool *tool __maybe_unused,
TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 1).cpu == 2);
TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 2).cpu == 3);
perf_cpu_map__put(map);
+
+ evsel = evlist__first(tmp->evlist);
+ perf_cpu_map__put(evsel->core.cpus);
+ evsel->core.cpus = NULL;
+ perf_cpu_map__put(evsel->core.pmu_cpus);
+ evsel->core.pmu_cpus = NULL;
+ evsel->core.requires_cpu = false;
+
+ TEST_ASSERT_VAL("failed to process attr update cpus",
+ !perf_event__process_event_update(tool, event, &tmp->evlist));
+
+ any_cpu = perf_cpu_map__new_any_cpu();
+ TEST_ASSERT_VAL("failed to allocate any_cpu map", any_cpu);
+ perf_evlist__set_maps(evlist__core(tmp->evlist), any_cpu, NULL);
+ perf_cpu_map__put(any_cpu);
+
+ TEST_ASSERT_VAL("wrong processed cpus", perf_cpu_map__nr(evsel->core.cpus) == 3);
+ TEST_ASSERT_VAL("wrong processed cpus", perf_cpu_map__cpu(evsel->core.cpus, 0).cpu == 1);
+ TEST_ASSERT_VAL("wrong processed cpus", perf_cpu_map__cpu(evsel->core.cpus, 1).cpu == 2);
+ TEST_ASSERT_VAL("wrong processed cpus", perf_cpu_map__cpu(evsel->core.cpus, 2).cpu == 3);
return 0;
}
@@ -82,6 +110,7 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
{
struct evsel *evsel;
struct event_name tmp;
+ struct event_cpus tmp_cpus;
struct target target = {};
struct evlist *evlist = evlist__new_default(&target, /*sample_callchains=*/false);
@@ -111,11 +140,15 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
TEST_ASSERT_VAL("failed to synthesize attr update name",
!perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
- perf_cpu_map__put(evsel->core.pmu_cpus);
- evsel->core.pmu_cpus = perf_cpu_map__new("1,2,3");
+ perf_cpu_map__put(evsel->core.cpus);
+ evsel->core.cpus = perf_cpu_map__new("1,2,3");
+
+ perf_tool__init(&tmp_cpus.tool, /*ordered_events=*/false);
+ tmp_cpus.evlist = evlist;
TEST_ASSERT_VAL("failed to synthesize attr update cpus",
- !perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus));
+ !perf_event__synthesize_event_update_cpus(&tmp_cpus.tool, evsel,
+ process_event_cpus));
evlist__put(evlist);
return 0;
diff --git a/tools/perf/tests/shell/stat.sh b/tools/perf/tests/shell/stat.sh
index ac9e031831da..43a158ee3609 100755
--- a/tools/perf/tests/shell/stat.sh
+++ b/tools/perf/tests/shell/stat.sh
@@ -77,6 +77,14 @@ test_stat_record_report() {
err=1
return
fi
+ cpu=$(sed 's/[-,].*//' /sys/devices/system/cpu/online)
+ if ! perf stat record -e "task-clock/cpu=${cpu}/,task-clock" -o - true | \
+ perf stat report -i - 2>&1 | grep -E -q "task-clock/cpu=${cpu}/"
+ then
+ echo "stat record and report test [Failed per-evsel CPU]"
+ err=1
+ return
+ fi
echo "stat record and report test [Success]"
}
@@ -89,6 +97,14 @@ test_stat_record_script() {
err=1
return
fi
+ cpu=$(sed 's/[-,].*//' /sys/devices/system/cpu/online)
+ if ! perf stat record -e "task-clock/cpu=${cpu}/,task-clock" -o - true | \
+ perf script -i - 2>&1 | grep -E -q "task-clock/cpu=${cpu}/"
+ then
+ echo "stat record and script test [Failed per-evsel CPU]"
+ err=1
+ return
+ fi
echo "stat record and script test [Success]"
}
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 1f96c170d277..64daef4cc345 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1822,7 +1822,7 @@ void evsel__config(struct evsel *evsel, const struct record_opts *opts,
attr->exclude_user = 1;
}
- if (evsel->core.pmu_cpus || evsel->unit)
+ if (evsel->core.cpus || evsel->core.pmu_cpus || evsel->unit)
evsel->core.attr.read_format |= PERF_FORMAT_ID;
/*
--git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 9eec6deee190..e9af99b8c0f8 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -5962,6 +5962,11 @@ int perf_event__process_event_update(const struct perf_tool *tool __maybe_unused
if (map) {
perf_cpu_map__put(evsel->core.pmu_cpus);
evsel->core.pmu_cpus = map;
+ if (!evsel->counts) {
+ perf_cpu_map__put(evsel->core.cpus);
+ evsel->core.cpus = perf_cpu_map__get(map);
+ evsel->core.requires_cpu = !perf_cpu_map__has_any_cpu(map);
+ }
} else
pr_err("failed to get event_update cpus\n");
break;
diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c
index 6da3dbbde0e2..429bfc339308 100644
--- a/tools/perf/util/stat.c
+++ b/tools/perf/util/stat.c
@@ -8,6 +8,7 @@
#include <linux/err.h>
#include <linux/zalloc.h>
+#include <perf/cpumap.h>
#include "counts.h"
#include "cpumap.h"
@@ -674,6 +675,23 @@ int perf_event__process_stat_event(const struct perf_tool *tool __maybe_unused,
return -EINVAL;
}
cpu_map_idx = perf_cpu_map__idx(evsel__cpus(counter), (struct perf_cpu){.cpu = st->cpu});
+ if (cpu_map_idx == -1 && (s32)st->cpu == -1 && counter->counts &&
+ evsel__nr_cpus(counter) > 0) {
+ /*
+ * Older perf tools wrote pmu_cpus instead of cpus in
+ * PERF_EVENT_UPDATE__CPUS, leaving per-task counters with a
+ * CPU list instead of the "any CPU" (-1) map used by their
+ * PERF_RECORD_STAT events.
+ */
+ struct perf_cpu_map *any_cpu = perf_cpu_map__new_any_cpu();
+
+ if (any_cpu) {
+ perf_cpu_map__put(counter->core.cpus);
+ counter->core.cpus = any_cpu;
+ counter->core.requires_cpu = false;
+ cpu_map_idx = 0;
+ }
+ }
if (cpu_map_idx == -1) {
pr_err("Invalid CPU %d for event %s.\n", st->cpu, evsel__name(counter));
return -EINVAL;
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index 3c8d29f27fd9..b87d15a5be6a 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -2234,10 +2234,13 @@ int perf_event__synthesize_event_update_name(const struct perf_tool *tool, struc
int perf_event__synthesize_event_update_cpus(const struct perf_tool *tool, struct evsel *evsel,
perf_event__handler_t process)
{
- struct synthesize_cpu_map_data syn_data = { .map = evsel->core.pmu_cpus };
+ struct synthesize_cpu_map_data syn_data = { .map = evsel->core.cpus };
struct perf_record_event_update *ev;
int err;
+ if (!evsel->core.cpus)
+ return 0;
+
ev = cpu_map_data__alloc(&syn_data, sizeof(struct perf_event_header) + 2 * sizeof(u64));
if (!ev)
return -ENOMEM;
@@ -2315,7 +2318,7 @@ int perf_event__synthesize_extra_attr(const struct perf_tool *tool, struct evlis
}
}
- if (evsel->core.pmu_cpus) {
+ if (evsel->core.cpus) {
err = perf_event__synthesize_event_update_cpus(tool, evsel, process);
if (err < 0) {
pr_err("Couldn't synthesize evsel cpus.\n");
--
2.56.1.750.gf70dc682cf-goog
next prev parent reply other threads:[~2026-11-23 7:19 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 7:19 [PATCH v1 00/13] perf fixes: off-cpu, stat, tool PMU, bperf, and bpf-filter fixes Ian Rogers
2026-11-23 7:19 ` [PATCH v1 01/13] perf off-cpu: Bound callchain length in off_cpu_write() Ian Rogers
2026-11-23 7:19 ` [PATCH v1 02/13] perf off-cpu: Fix swapped PID and TID in direct off_cpu_dump() samples Ian Rogers
2026-11-23 7:19 ` [PATCH v1 03/13] libperf evlist: Recompute CPU map when an evsel goes system wide Ian Rogers
2026-11-23 7:19 ` [PATCH v1 04/13] perf stat: Give non-perf_event_open events unique IDs for stat record Ian Rogers
2026-11-23 7:37 ` sashiko-bot
2026-11-23 7:19 ` Ian Rogers [this message]
2026-11-23 7:35 ` [PATCH v1 05/13] perf stat: Preserve per-evsel CPU maps across stat record and report sashiko-bot
2026-11-23 17:12 ` Athira Rajeev
2026-11-24 4:23 ` Ian Rogers
2026-11-24 7:24 ` Athira Rajeev
2026-11-23 7:19 ` [PATCH v1 06/13] perf pmu: Don't treat tool PMU as a kernel software PMU for event grouping Ian Rogers
2026-11-23 23:12 ` TAKANO Mitsuhiro
2026-11-24 0:16 ` Namhyung Kim
2026-11-23 7:19 ` [PATCH v1 07/13] perf evlist: Enable tool PMU events when starting a workload Ian Rogers
2026-11-23 7:19 ` [PATCH v1 08/13] perf stat: Respect the enabled time of exited user_time/system_time events Ian Rogers
2026-11-23 7:19 ` [PATCH v1 09/13] perf stat: Fix --bpf-counters with non-bperf, hybrid, and mixed events Ian Rogers
2026-11-23 7:19 ` [PATCH v1 10/13] perf bpf-filter: Fix bpf_link leak by using PERF_EVENT_IOC_SET_BPF Ian Rogers
2026-11-23 7:19 ` [PATCH v1 11/13] perf bpf-filter: Copy bpf_filters in evsel__clone() and close on evsel__close() Ian Rogers
2026-11-23 7:36 ` sashiko-bot
2026-11-23 7:19 ` [PATCH v1 12/13] perf bpf-filter: Support BPF sample filters on tracepoint events Ian Rogers
2026-11-23 7:19 ` [PATCH v1 13/13] perf parse-events: Route non-field tracepoint filter terms to BPF filter Ian Rogers
2026-11-23 7:44 ` [PATCH v1 00/13] perf fixes: off-cpu, stat, tool PMU, bperf, and bpf-filter fixes sashiko-bot
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=20261123071913.653315-6-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=howardchu95@gmail.com \
--cc=james.clark@linaro.org \
--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 \
--cc=songliubraving@fb.com \
--cc=thomas.falcon@intel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.