Skip to contentSkip to conversation

perf: skip unnecessary userspace map synthesis

PATCH In review Max Kellermann 3 participants 5 messages Oct 13 – Oct 30 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: skip unnecessary userspace map synthesis

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.

---
 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(-)
tools/perf/Documentation/perf-record.txt +4 -0
index 178f483140ed..cdef14f22df3 100644
@@ -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.
tools/perf/builtin-record.c +8 -0
index a57987851cf0..f8b4e5279fca 100644
@@ -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;
 }
 
tools/perf/builtin-top.c +10 -9
index 570410599f1b..d85ecc140eda 100644
@@ -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();
tools/perf/util/evlist.c +40 -0
index c3d784727810..6636edee0d2b 100644
@@ -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
tools/perf/util/evlist.h +1 -0
index 838e263b76f3..b208bf7211e8 100644
@@ -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);
 
tools/perf/util/record.h +1 -0
index 93627c9a7338..1317ddce3cf0 100644
@@ -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;
Signature · 1 line
2.47.3
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

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.

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

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer
#2 lore raw
In reply to Max Kellermann
Quoted 140 lines · Max Kellermann

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.

---
 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(-)
tools/perf/Documentation/perf-record.txt +4 -0
index 178f483140ed..cdef14f22df3 100644
@@ -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.
tools/perf/builtin-record.c +8 -0
index a57987851cf0..f8b4e5279fca 100644
@@ -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;
 }
 
tools/perf/builtin-top.c +10 -9
index 570410599f1b..d85ecc140eda 100644
@@ -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();
tools/perf/util/evlist.c +28 -0
index c3d784727810..6636edee0d2b 100644
@@ -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 |

tools/perf/util/evlist.c bool evlist__exclude_kernel(struct evlist *evlist) · line 2030

Quoted 10 lines
tools/perf/util/evlist.c +10 -0
+                                        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

Quoted 32 lines
tools/perf/util/evlist.c +2 -0
+}
+
 /*
  * 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
tools/perf/util/evlist.h +1 -0
index 838e263b76f3..b208bf7211e8 100644
@@ -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);
 
tools/perf/util/record.h +1 -1
index 93627c9a7338..1317ddce3cf0 100644
@@ -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

#3 lore raw
Quoted 2 lines · Ian Rogers

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.

Signature · 8 lines
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
#4 lore raw
In reply to Ian Rogers
Quoted 5 lines · Ian Rogers

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?

I checked it again (and got confused a bit more than I should). Is the empty list really possible? If no event is specified, "cycles" is the implicit default in both tools.

With "perf -e dummy", the kernel emits user mmap records even with "exclude_user", so this would be a possibly user-visible change. Do you want me to remove the "dummy" check, or do you want me to add a comment explaining this? Or is it possible to check whether a dummy event is guaranteed to never require user mmaps?

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