[PATCH v1 22/41] perf script: Don't drop metric samples for CPUs missing from the evsel

From: Ian Rogers

Date: Tue Nov 03 2026 - 18:52:33 EST


perf script -F metric looks up the index of the sample's CPU in the
evsel's CPU map and, when it isn't present, prints "Missing CPU map
entry for CPU <n>" and ignores the sample. When the perf.data file
doesn't record the CPUs of an event, as is the case for perf record,
__perf_evlist__propagate_maps() sets the evsel's CPUs to the host's
online CPUs. If a CPU is offline when perf script is run, or the
perf.data file was recorded on a machine with more CPUs, then every
sample on such a CPU is dropped from the metrics with a message, for
example thousands of:

Missing CPU map entry for CPU 1

perf script currently only supports global aggregation of the counts
used for metrics, the counts of all CPUs are summed. So place the counts
of a sample whose CPU isn't in the evsel's CPU map in the first CPU of
the map, as was already done for samples without a CPU. This doesn't
change the metric values computed for samples of the CPUs in the map,
the summed counts just include the previously dropped samples, and so
isn't a regression. Aggregating per CPU, and using the CPUs of the
perf.data file's environment rather than the host's, would be future
improvements.

Fixes: 3622990efaab ("perf script: Change metric format to use json metrics")
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-script.c | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 199a5af81f37..afed2353ef80 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -2409,15 +2409,14 @@ static void perf_sample__fprint_metric(struct thread *thread,
/* Update the evsel's count using the sample's data. */
cpu_map_idx = perf_cpu_map__idx(evsel->core.cpus, (struct perf_cpu){sample->cpu});
if (cpu_map_idx < 0) {
- /* Missing CPU, check for any CPU. */
- if (perf_cpu_map__cpu(evsel->core.cpus, /*idx=*/0).cpu == -1 ||
- sample->cpu == (u32)-1) {
- /* Place the counts in the which ever CPU is first in the map. */
- cpu_map_idx = 0;
- } else {
- pr_info("Missing CPU map entry for CPU %d\n", sample->cpu);
- return;
- }
+ /*
+ * The evsel's CPUs may not contain the sample's CPU, for
+ * example the sample may have no CPU or, when the perf.data
+ * file doesn't record the evsel's CPUs, the evsel's CPUs are
+ * the host's online CPUs. As only global aggregation is
+ * supported, place the counts in the first CPU of the map.
+ */
+ cpu_map_idx = 0;
}
thread_map_idx = perf_thread_map__idx(evsel->core.threads, sample->tid);
if (thread_map_idx < 0) {
--
2.56.0.385.gac671c819a-goog