Linux Perf Users
 help / color / mirror / Atom feed
From: Max Kellermann <max.kellermann@ionos.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	Max Kellermann <max.kellermann@ionos.com>,
	Dapeng Mi <dapeng1.mi@linux.intel.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] perf: skip unnecessary userspace map synthesis
Date: Tue, 13 Oct 2026 15:00:43 +0200	[thread overview]
Message-ID: <20261013130052.3012773-1-max.kellermann@ionos.com> (raw)

Reading `/proc/PID/task/PID/maps` for existing processes is expensive
on systems with many processes.  `perf record` already allows skipping
these reads with the `--synth=` option, but `perf top` has no
equivalent option.

Skip userspace map synthesis by default in `record` and `top` when all
events exclude userspace and no sample feature needs user mappings.

Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
---
 tools/perf/Documentation/perf-record.txt |  4 +++
 tools/perf/builtin-record.c              |  8 +++++
 tools/perf/builtin-top.c                 | 19 +++++------
 tools/perf/util/evlist.c                 | 40 ++++++++++++++++++++++++
 tools/perf/util/evlist.h                 |  1 +
 tools/perf/util/record.h                 |  1 +
 6 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 178f483140ed..cdef14f22df3 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -696,6 +696,10 @@ Available types are:
   - 'all'     - synthesize all events (default)
   - 'no'      - do not synthesize any of the above events
 
+By default, userspace MMAP synthesis is skipped if all events exclude
+userspace (for example, with --all-kernel or -e cycles:k) and their
+samples do not need userspace maps.
+
 --tail-synthesize::
 Instead of collecting non-sample events (for example, fork, comm, mmap) at
 the beginning of record, collect them during finalizing an output file.
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index a57987851cf0..f8b4e5279fca 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -2574,6 +2574,13 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 
 	evlist__config(rec->evlist, opts, &callchain_param);
 
+	if (!opts->synth_set && !opts->full_auxtrace &&
+	    !opts->auxtrace_sample_mode && !rec->off_cpu && !rec->buildid_all &&
+	    !evlist__needs_user_mmaps(rec->evlist)) {
+		opts->synth &= ~PERF_SYNTH_MMAP;
+		pr_debug("Skipping synthesis of userspace maps for kernel-only recording\n");
+	}
+
 	/* Debug message used by test scripts */
 	pr_debug3("perf record opening and mmapping events\n");
 	if (record__open(rec) != 0) {
@@ -3412,6 +3419,7 @@ static int parse_record_synth_option(const struct option *opt,
 		pr_err("Invalid synth option: %s\n", str);
 		return -1;
 	}
+	opts->synth_set = true;
 	return 0;
 }
 
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 570410599f1b..d85ecc140eda 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1017,14 +1017,6 @@ static int perf_top__start_counters(struct perf_top *top)
 	struct evlist *evlist = top->evlist;
 	struct record_opts *opts = &top->record_opts;
 
-	if (perf_top__overwrite_check(top)) {
-		ui__error("perf top only support consistent per-event "
-			  "overwrite setting for all events\n");
-		goto out_err;
-	}
-
-	evlist__config(evlist, opts, &callchain_param);
-
 	evlist__for_each_entry(evlist, counter) {
 try_again:
 		if (evsel__open(counter, counter->core.cpus,
@@ -1265,6 +1257,14 @@ static int __cmd_top(struct perf_top *top)
 	if (ret)
 		return ret;
 
+	if (perf_top__overwrite_check(top)) {
+		ui__error("perf top only support consistent per-event "
+			  "overwrite setting for all events\n");
+		return -1;
+	}
+
+	evlist__config(top->evlist, opts, &callchain_param);
+
 	if (perf_session__register_idle_thread(top->session) < 0)
 		return ret;
 
@@ -1296,7 +1296,8 @@ static int __cmd_top(struct perf_top *top)
 		pr_debug("Couldn't synthesize cgroup events.\n");
 
 	machine__synthesize_threads(&top->session->machines.host, &opts->target,
-				    evlist__core(top->evlist)->threads, true, false,
+				    evlist__core(top->evlist)->threads,
+				    evlist__needs_user_mmaps(top->evlist), false,
 				    top->nr_threads_synthesize);
 
 	perf_set_multithreaded();
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index c3d784727810..6636edee0d2b 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1990,6 +1990,46 @@ bool evlist__exclude_kernel(struct evlist *evlist)
 	return true;
 }
 
+bool evlist__needs_user_mmaps(struct evlist *evlist)
+{
+	struct evsel *evsel;
+	bool has_kernel_event = false;
+
+	evlist__for_each_entry(evlist, evsel) {
+		struct perf_event_attr *attr = &evsel->core.attr;
+
+		/* Tracking events do not produce samples, regardless of modifiers. */
+		if (evsel__is_dummy_event(evsel))
+			continue;
+
+		if (!attr->exclude_user)
+			return true;
+
+		/* Kernel samples can still contain a userspace callchain. */
+		if ((attr->sample_type & PERF_SAMPLE_CALLCHAIN) &&
+		    !attr->exclude_callchain_user)
+			return true;
+
+		/*
+		 * These may reference userspace even when the sampled
+		 * IP is in the kernel.  Keep maps for opaque trace
+		 * payloads and AUX trace decoding too, whose address
+		 * requirements are PMU specific.
+		 */
+		if (attr->sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_RAW |
+					 PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER |
+					 PERF_SAMPLE_BRANCH_STACK | PERF_SAMPLE_AUX))
+			return true;
+
+		if (evsel__is_aux_event(evsel))
+			return true;
+
+		has_kernel_event = true;
+	}
+
+	return !has_kernel_event;
+}
+
 /*
  * Events in data file are not collect in groups, but we still want
  * the group display. Set the artificial group and set the leader's
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 838e263b76f3..b208bf7211e8 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -631,6 +631,7 @@ struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str);
 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event);
 
 bool evlist__exclude_kernel(struct evlist *evlist);
+bool evlist__needs_user_mmaps(struct evlist *evlist);
 
 void evlist__force_leader(struct evlist *evlist);
 
diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h
index 93627c9a7338..1317ddce3cf0 100644
--- a/tools/perf/util/record.h
+++ b/tools/perf/util/record.h
@@ -80,6 +80,7 @@ struct record_opts {
 	int	      ctl_fd_ack;
 	bool	      ctl_fd_close;
 	int	      synth;
+	bool	      synth_set;
 	int	      threads_spec;
 	const char    *threads_user_spec;
 	u64	      off_cpu_thresh_ns;
-- 
2.47.3


             reply	other threads:[~2026-10-13 13:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-13 13:00 Max Kellermann [this message]
2026-10-13 13:11 ` [PATCH] perf: skip unnecessary userspace map synthesis 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=20261013130052.3012773-1-max.kellermann@ionos.com \
    --to=max.kellermann@ionos.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --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