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>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
"Dr. David Alan Gilbert" <linux@treblig.org>,
Yang Li <yang.lee@linux.alibaba.com>,
James Clark <james.clark@linaro.org>,
Thomas Falcon <thomas.falcon@intel.com>,
Thomas Richter <tmricht@linux.ibm.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
Andi Kleen <ak@linux.intel.com>,
Dapeng Mi <dapeng1.mi@linux.intel.com>,
Andres Freund <andres@anarazel.de>
Subject: [PATCH v7 3/6] perf evlist: Special map propagation for tool events that read on 1 CPU
Date: Tue, 3 Feb 2026 14:51:26 -0800 [thread overview]
Message-ID: <20260203225129.4077140-4-irogers@google.com> (raw)
In-Reply-To: <20260203225129.4077140-1-irogers@google.com>
Tool events like duration_time don't need a perf_cpu_map that contains
all online CPUs. Having such a perf_cpu_map causes overheads when
iterating between events for CPU affinity. During parsing mark events
that just read on a single CPU map index as such, then during map
propagation set up the evsel's CPUs and thereby the evlists
accordingly. The setting cannot be done early in parsing as user CPUs
are only fully known when evlist__create_maps is called.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/lib/perf/evlist.c | 36 ++++++++++++++++++++++---
tools/lib/perf/include/internal/evsel.h | 2 ++
tools/perf/util/parse-events.c | 1 +
tools/perf/util/pmu.c | 11 ++++++++
tools/perf/util/pmu.h | 2 ++
5 files changed, 48 insertions(+), 4 deletions(-)
diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c
index 3ed023f4b190..1f210dadd666 100644
--- a/tools/lib/perf/evlist.c
+++ b/tools/lib/perf/evlist.c
@@ -101,6 +101,28 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
evsel->cpus = perf_cpu_map__get(evlist->user_requested_cpus);
}
+ /*
+ * Tool events may only read on the first CPU index to avoid double
+ * counting things like duration_time. Make the evsel->cpus contain just
+ * that single entry otherwise we may spend time changing affinity to
+ * CPUs that just have tool events, etc.
+ */
+ if (evsel->reads_only_on_cpu_idx0 && perf_cpu_map__nr(evsel->cpus) > 0) {
+ struct perf_cpu_map *srcs[3] = {
+ evlist->all_cpus,
+ evlist->user_requested_cpus,
+ evsel->pmu_cpus,
+ };
+ for (size_t i = 0; i < ARRAY_SIZE(srcs); i++) {
+ if (!srcs[i])
+ continue;
+
+ perf_cpu_map__put(evsel->cpus);
+ evsel->cpus = perf_cpu_map__new_int(perf_cpu_map__cpu(srcs[i], 0).cpu);
+ break;
+ }
+ }
+
/* Sanity check assert before the evsel is potentially removed. */
assert(!evsel->requires_cpu || !perf_cpu_map__has_any_cpu(evsel->cpus));
@@ -133,16 +155,22 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
{
- struct perf_evsel *evsel, *n;
-
evlist->needs_map_propagation = true;
/* Clear the all_cpus set which will be merged into during propagation. */
perf_cpu_map__put(evlist->all_cpus);
evlist->all_cpus = NULL;
- list_for_each_entry_safe(evsel, n, &evlist->entries, node)
- __perf_evlist__propagate_maps(evlist, evsel);
+ /* 2 rounds so that reads_only_on_cpu_idx0 benefit from knowing the other CPU maps. */
+ for (int round = 0; round < 2; round++) {
+ struct perf_evsel *evsel, *n;
+
+ list_for_each_entry_safe(evsel, n, &evlist->entries, node) {
+ if ((!evsel->reads_only_on_cpu_idx0 && round == 0) ||
+ (evsel->reads_only_on_cpu_idx0 && round == 1))
+ __perf_evlist__propagate_maps(evlist, evsel);
+ }
+ }
}
void perf_evlist__add(struct perf_evlist *evlist,
diff --git a/tools/lib/perf/include/internal/evsel.h b/tools/lib/perf/include/internal/evsel.h
index fefe64ba5e26..b988034f1371 100644
--- a/tools/lib/perf/include/internal/evsel.h
+++ b/tools/lib/perf/include/internal/evsel.h
@@ -128,6 +128,8 @@ struct perf_evsel {
bool requires_cpu;
/** Is the PMU for the event a core one? Effects the handling of own_cpus. */
bool is_pmu_core;
+ /** Does the evsel on read on the first CPU index such as tool time events? */
+ bool reads_only_on_cpu_idx0;
int idx;
};
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index f631bf7a919f..b9efb296bba5 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -269,6 +269,7 @@ __add_event(struct list_head *list, int *idx,
evsel->core.pmu_cpus = pmu_cpus;
evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
evsel->core.is_pmu_core = is_pmu_core;
+ evsel->core.reads_only_on_cpu_idx0 = perf_pmu__reads_only_on_cpu_idx0(attr);
evsel->pmu = pmu;
evsel->alternate_hw_config = alternate_hw_config;
evsel->first_wildcard_match = first_wildcard_match;
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index bb399a47d2b4..81ab74681c9b 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -2718,3 +2718,14 @@ const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config)
}
return NULL;
}
+
+bool perf_pmu__reads_only_on_cpu_idx0(const struct perf_event_attr *attr)
+{
+ enum tool_pmu_event event;
+
+ if (attr->type != PERF_PMU_TYPE_TOOL)
+ return false;
+
+ event = (enum tool_pmu_event)attr->config;
+ return event != TOOL_PMU__EVENT_USER_TIME && event != TOOL_PMU__EVENT_SYSTEM_TIME;
+}
diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
index 7ef90b54a149..41c21389f393 100644
--- a/tools/perf/util/pmu.h
+++ b/tools/perf/util/pmu.h
@@ -350,6 +350,8 @@ void perf_pmu__delete(struct perf_pmu *pmu);
const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config);
bool perf_pmu__is_fake(const struct perf_pmu *pmu);
+bool perf_pmu__reads_only_on_cpu_idx0(const struct perf_event_attr *attr);
+
static inline enum pmu_kind perf_pmu__kind(const struct perf_pmu *pmu)
{
__u32 type;
--
2.53.0.rc2.204.g2597b5adb4-goog
next prev parent reply other threads:[~2026-02-03 22:51 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-03 22:51 [PATCH v7 0/6] perf stat affinity changes Ian Rogers
2026-02-03 22:51 ` [PATCH v7 1/6] Revert "perf tool_pmu: More accurately set the cpus for tool events" Ian Rogers
2026-02-03 22:51 ` [PATCH v7 2/6] perf stat-shadow: In prepare_metric fix guard on reading NULL perf_stat_evsel Ian Rogers
2026-02-03 22:51 ` Ian Rogers [this message]
2026-02-03 22:51 ` [PATCH v7 4/6] perf evlist: Missing TPEBS close in evlist__close Ian Rogers
2026-02-03 22:51 ` [PATCH v7 5/6] perf evlist: Reduce affinity use and move into iterator, fix no affinity Ian Rogers
2026-02-03 22:51 ` [PATCH v7 6/6] perf stat: Add no-affinity flag Ian Rogers
2026-02-06 21:35 ` [PATCH v7 0/6] perf stat affinity changes Arnaldo Carvalho de Melo
2026-02-06 22:01 ` Ian Rogers
2026-02-06 22:13 ` Arnaldo Carvalho de Melo
2026-02-06 22:25 ` [PATCH v8 " Ian Rogers
2026-02-06 22:25 ` [PATCH v8 1/6] Revert "perf tool_pmu: More accurately set the cpus for tool events" Ian Rogers
2026-02-09 23:10 ` Ian Rogers
2026-02-06 22:25 ` [PATCH v8 2/6] perf stat-shadow: In prepare_metric fix guard on reading NULL perf_stat_evsel Ian Rogers
2026-02-06 22:25 ` [PATCH v8 3/6] perf evlist: Special map propagation for tool events that read on 1 CPU Ian Rogers
2026-02-06 22:25 ` [PATCH v8 4/6] perf evlist: Missing TPEBS close in evlist__close Ian Rogers
2026-02-06 22:25 ` [PATCH v8 5/6] perf evlist: Reduce affinity use and move into iterator, fix no affinity Ian Rogers
2026-02-06 22:25 ` [PATCH v8 6/6] perf stat: Add no-affinity flag Ian Rogers
2026-02-07 12:51 ` Arnaldo Carvalho de Melo
2026-02-07 15:54 ` Ian Rogers
2026-02-10 1:18 ` Namhyung Kim
2026-02-10 6:03 ` [PATCH v9 0/6] perf stat affinity changes Ian Rogers
2026-02-10 6:03 ` Ian Rogers
2026-02-10 6:03 ` [PATCH v9 1/6] Revert "perf tool_pmu: More accurately set the cpus for tool events" Ian Rogers
2026-02-10 6:03 ` [PATCH v9 2/6] perf stat-shadow: In prepare_metric fix guard on reading NULL perf_stat_evsel Ian Rogers
2026-02-10 6:03 ` [PATCH v9 3/6] perf evlist: Special map propagation for tool events that read on 1 CPU Ian Rogers
2026-02-10 6:03 ` [PATCH v9 4/6] perf evlist: Missing TPEBS close in evlist__close Ian Rogers
2026-02-10 6:03 ` [PATCH v9 5/6] perf evlist: Reduce affinity use and move into iterator, fix no affinity Ian Rogers
2026-02-10 6:03 ` [PATCH v9 6/6] perf stat: Add no-affinity flag 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=20260203225129.4077140-4-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=andres@anarazel.de \
--cc=dapeng1.mi@linux.intel.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux@treblig.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=thomas.falcon@intel.com \
--cc=tmricht@linux.ibm.com \
--cc=yang.lee@linux.alibaba.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