Skip to contentSkip to conversation

perf tools: Skip BPF sideband event for userspace profiling

PATCH In review Namhyung Kim 2 participants 4 messages Feb 27, 2025 – Mar 1, 2025 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH v2 1/1 perf tools: Skip BPF sideband event for userspace profiling

The BPF sideband information is tracked using a separate thread and evlist. But it's only useful for profiling kernel and we can skip it when users profile their application only in userspace.

It seems it already fails to open the sideband event in that case. Let's remove the noise in the verbose output anyway.

---
v2) add evlist__setup_sb_evlist()

 tools/perf/builtin-record.c       | 24 +++++++++---------------
 tools/perf/builtin-top.c          | 20 ++++----------------
 tools/perf/util/evlist.c          | 14 ++++++++++++++
 tools/perf/util/evlist.h          |  4 ++++
 tools/perf/util/sideband_evlist.c | 31 +++++++++++++++++++++++++++++++
 5 files changed, 62 insertions(+), 31 deletions(-)
tools/perf/builtin-record.c +9 -15
index 0e45bd64185403ae..ab6bef710a689f7f 100644
@@ -2166,23 +2166,14 @@ static int record__setup_sb_evlist(struct record *rec)
 		evlist__set_cb(rec->sb_evlist, record__process_signal_event, rec);
 		rec->thread_id = pthread_self();
 	}
-#ifdef HAVE_LIBBPF_SUPPORT
-	if (!opts->no_bpf_event) {
-		if (rec->sb_evlist == NULL) {
-			rec->sb_evlist = evlist__new();
-
-			if (rec->sb_evlist == NULL) {
-				pr_err("Couldn't create side band evlist.\n.");
-				return -1;
-			}
-		}
 
-		if (evlist__add_bpf_sb_event(rec->sb_evlist, &rec->session->header.env)) {
-			pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
-			return -1;
-		}
+	if (rec->sb_evlist == NULL) {
+		rec->sb_evlist = evlist__setup_sb_evlist(rec->evlist, opts,
+							 &rec->session->header.env);
+		if (IS_ERR(rec->sb_evlist))
+			return PTR_ERR(rec->sb_evlist);
 	}
-#endif
+
 	if (evlist__start_sb_thread(rec->sb_evlist, &rec->opts.target)) {
 		pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
 		opts->no_bpf_event = true;
@@ -2535,6 +2526,9 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 		goto out_free_threads;
 	}
 
+	if (!evlist__needs_bpf_sb_event(rec->evlist))
+		opts->no_bpf_event = true;
+
 	err = record__setup_sb_evlist(rec);
 	if (err)
 		goto out_free_threads;
tools/perf/builtin-top.c +4 -16
index 6440b5c1757d92ce..f727738faa154564 100644
@@ -1833,23 +1833,11 @@ int cmd_top(int argc, const char **argv)
 		goto out_delete_evlist;
 	}
 
-#ifdef HAVE_LIBBPF_SUPPORT
-	if (!top.record_opts.no_bpf_event) {
-		top.sb_evlist = evlist__new();
-
-		if (top.sb_evlist == NULL) {
-			pr_err("Couldn't create side band evlist.\n.");
-			status = -EINVAL;
-			goto out_delete_evlist;
-		}
-
-		if (evlist__add_bpf_sb_event(top.sb_evlist, &perf_env)) {
-			pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
-			status = -EINVAL;
-			goto out_delete_evlist;
-		}
+	top.sb_evlist = evlist__setup_sb_evlist(top.evlist, &top.record_opts, &perf_env);
+	if (IS_ERR(top.sb_evlist)) {
+		status = PTR_ERR(top.sb_evlist);
+		goto out_delete_evlist;
 	}
-#endif
 
 	if (evlist__start_sb_thread(top.sb_evlist, target)) {
 		pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
tools/perf/util/evlist.c +14 -0
index f0dd174e2debdbe8..43adf6b3d855771a 100644
@@ -2594,3 +2594,17 @@ bool evlist__has_bpf_output(struct evlist *evlist)
 
 	return false;
 }
+
+bool evlist__needs_bpf_sb_event(struct evlist *evlist)
+{
+	struct evsel *evsel;
+
+	evlist__for_each_entry(evlist, evsel) {
+		if (evsel__is_dummy_event(evsel))
+			continue;
+		if (!evsel->core.attr.exclude_kernel)
+			return true;
+	}
+
+	return false;
+}
tools/perf/util/evlist.h +4 -0
index adddb1db1ad2b25d..1bba5c73b1425834 100644
@@ -19,6 +19,7 @@
 struct pollfd;
 struct thread_map;
 struct perf_cpu_map;
+struct perf_env;
 struct record_opts;
 struct target;
 
@@ -117,6 +118,8 @@ struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide);
 int evlist__add_sb_event(struct evlist *evlist, struct perf_event_attr *attr,
 			 evsel__sb_cb_t cb, void *data);
 void evlist__set_cb(struct evlist *evlist, evsel__sb_cb_t cb, void *data);
+struct evlist *evlist__setup_sb_evlist(struct evlist *evlist, struct record_opts *opts,
+				       struct perf_env *env);
 int evlist__start_sb_thread(struct evlist *evlist, struct target *target);
 void evlist__stop_sb_thread(struct evlist *evlist);
 
@@ -435,5 +438,6 @@ void evlist__check_mem_load_aux(struct evlist *evlist);
 void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list);
 void evlist__uniquify_name(struct evlist *evlist);
 bool evlist__has_bpf_output(struct evlist *evlist);
+bool evlist__needs_bpf_sb_event(struct evlist *evlist);
 
 #endif /* __PERF_EVLIST_H */
tools/perf/util/sideband_evlist.c +31 -0
index 388846f17bc13fb9..e75a335e11676cdc 100644
@@ -1,11 +1,14 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
+#include "util/bpf-event.h"
 #include "util/debug.h"
 #include "util/evlist.h"
 #include "util/evsel.h"
 #include "util/mmap.h"
 #include "util/perf_api_probe.h"
+#include "util/record.h"
 #include <perf/mmap.h>
+#include <linux/err.h>
 #include <linux/perf_event.h>
 #include <limits.h>
 #include <pthread.h>
@@ -94,6 +97,34 @@ void evlist__set_cb(struct evlist *evlist, evsel__sb_cb_t cb, void *data)
       }
 }
 
+struct evlist *evlist__setup_sb_evlist(struct evlist *evlist, struct record_opts *opts,
+				       struct perf_env *env)
+{
+	struct evlist *sb_evlist = NULL;
+
+	if (!evlist__needs_bpf_sb_event(evlist))
+		opts->no_bpf_event = true;
+
+	if (opts->no_bpf_event)
+		return NULL;
+
+#ifdef HAVE_LIBBPF_SUPPORT
+	sb_evlist = evlist__new();
+	if (sb_evlist == NULL) {
+		pr_err("Couldn't create side band evlist.\n.");
+		return ERR_PTR(-ENOMEM);
+	}
+
+	if (evlist__add_bpf_sb_event(sb_evlist, env)) {
+		pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
+		evlist__delete(sb_evlist);
+		return ERR_PTR(-EINVAL);
+	}
+#endif
+
+	return sb_evlist;
+}
+
 int evlist__start_sb_thread(struct evlist *evlist, struct target *target)
 {
 	struct evsel *counter;
Signature · 1 line
2.48.1.658.g4767266eb4-goog
#1 lore raw
Quoted 10 lines · Namhyung Kim

The BPF sideband information is tracked using a separate thread and evlist. But it's only useful for profiling kernel and we can skip it when users profile their application only in userspace.

It seems it already fails to open the sideband event in that case. Let's remove the noise in the verbose output anyway.

This turns out to be more complicated than I thought. I actually like v1 better.

Thanks,
Song

author maintainer
#2 lore raw
Quoted 6 lines · Song Liu
Quoted 5 lines · Namhyung Kim

The BPF sideband information is tracked using a separate thread and evlist. But it's only useful for profiling kernel and we can skip it when users profile their application only in userspace.

Quoted 8 lines
Quoted 5 lines

It seems it already fails to open the sideband event in that case. Let's remove the noise in the verbose output anyway.

This turns out to be more complicated than I thought. I actually like v1 better.

Sure, the complexity comes from moving the code to reduce the duplication. But either is fine to me.

Can I add your Acked-by to v1, then?

Thanks,
Namhyung

Acked-by
#3 lore raw
Quoted 14 lines · Namhyung Kim
Quoted 12 lines · Song Liu
Quoted 10 lines · Namhyung Kim

The BPF sideband information is tracked using a separate thread and evlist. But it's only useful for profiling kernel and we can skip it when users profile their application only in userspace.

It seems it already fails to open the sideband event in that case. Let's remove the noise in the verbose output anyway.

Quoted 7 lines
Quoted 2 lines

This turns out to be more complicated than I thought. I actually like v1 better.

Sure, the complexity comes from moving the code to reduce the duplication. But either is fine to me.

Can I add your Acked-by to v1, then?

Yes, you can add to v1:

Thanks,
Song