Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH] perf: skip unnecessary userspace map synthesis
@ 2026-10-13 13:00 Max Kellermann
  2026-10-13 13:11 ` sashiko-bot
  2026-10-13 13:29 ` Ian Rogers
  0 siblings, 2 replies; 4+ messages in thread
From: Max Kellermann @ 2026-10-13 13:00 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, Max Kellermann, Dapeng Mi,
	linux-perf-users, linux-kernel

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf: skip unnecessary userspace map synthesis
  2026-10-13 13:00 [PATCH] perf: skip unnecessary userspace map synthesis Max Kellermann
@ 2026-10-13 13:11 ` sashiko-bot
  2026-10-13 13:29 ` Ian Rogers
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-10-13 13:11 UTC (permalink / raw)
  To: Max Kellermann, linux-perf-users

> 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>

Sashiko has reviewed this patch and found no issues. It looks great!

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261013130052.3012773-1-max.kellermann@ionos.com?part=1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf: skip unnecessary userspace map synthesis
  2026-10-13 13:00 [PATCH] perf: skip unnecessary userspace map synthesis Max Kellermann
  2026-10-13 13:11 ` sashiko-bot
@ 2026-10-13 13:29 ` Ian Rogers
  2026-10-13 14:22   ` Max Kellermann
  1 sibling, 1 reply; 4+ messages in thread
From: Ian Rogers @ 2026-10-13 13:29 UTC (permalink / raw)
  To: Max Kellermann
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Adrian Hunter, James Clark, Dapeng Mi, linux-perf-users,
	linux-kernel

On Tue, Oct 13, 2026 at 6:00 AM Max Kellermann <max.kellermann@ionos.com> wrote:
>
> 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;

Thanks for the contribution! I can see if there's a user event then
evlist__needs_user_mmaps will return true, otherwise we'll return
false if there's a kernel event but true if the list is empty or just
contains dummy events. Why do the empty list and dummy case need the
user mmap events?

Thanks,
Ian

> +}
> +
>  /*
>   * 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
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf: skip unnecessary userspace map synthesis
  2026-10-13 13:29 ` Ian Rogers
@ 2026-10-13 14:22   ` Max Kellermann
  0 siblings, 0 replies; 4+ messages in thread
From: Max Kellermann @ 2026-10-13 14:22 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Adrian Hunter, James Clark, Dapeng Mi, linux-perf-users,
	linux-kernel

On Tue, Oct 13, 2026 at 3:29 PM Ian Rogers <irogers@google.com> wrote:
> Why do the empty list and dummy case need the
> user mmap events?

I guess that's a corner case I missed indeed. I'll fix that for v2.

-- 
Max Kellermann
Principal Architect
Hosting Technology

cm4all | Im Mediapark 6a | 50670 Köln | Germany
General information about the company can be found here:
https://www.cm4all.com/impressum
A member of the IONOS Group

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-10-13 14:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-13 13:00 [PATCH] perf: skip unnecessary userspace map synthesis Max Kellermann
2026-10-13 13:11 ` sashiko-bot
2026-10-13 13:29 ` Ian Rogers
2026-10-13 14:22   ` Max Kellermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox