Skip to contentSkip to conversation

perf tools: Add cached probe type detection for evsel

PATCH Superseded Arnaldo Carvalho de Melo 2 participants 4 messages Jun 16 perf events

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Arnaldo Carvalho de Melo

author maintainer
#0 lore raw

v2 cover perf tools: Add cached probe type detection for evsel

Hi,

Checking whether an evsel is a kprobe or uprobe currently requires walking the PMU list via evsel__find_pmu() on every call. This is wasteful when the same evsel is checked repeatedly in hot paths like trace__fprintf_tp_fields().

Patch 1 adds evsel__is_kprobe(), evsel__is_uprobe(), and evsel__is_probe() helpers that resolve the probe type on first call and cache the result in a 3-bit field that fits in existing struct padding.

Detection covers both PMU-based probes (kprobe/uprobe PMU, detected by PMU name) and ftrace-based dynamic probes (kprobes/uprobes/fprobes created via tracefs, which report as PMU "tracepoint"). Ftrace probes are detected by the __probe_ip field that the kernel adds to all dynamic probe formats — this is authoritative regardless of the group/system name the user chose.

Patch 2 is the first user: it guards the __probe_ip field name comparison in perf trace with evsel__is_probe(), so the strcmp is skipped entirely for the common case of non-probe tracepoint events.

Build-tested with gcc and clang.

Changes in v2 (patch 1 only):
- Detect ftrace-based dynamic probes (kprobes/uprobes/fprobes created via tracefs) that report PMU "tracepoint" instead of "kprobe"/ "uprobe". Check for the __probe_ip field presence in the tracepoint format. Without this, evsel__is_probe() returned false for ftrace probes, breaking __probe_ip suppression in perf trace for probes created via 'perf probe' (sashiko-bot).
- Widen probe_type bitfield from 2 to 3 bits for PROBE__FTRACE.

Arnaldo Carvalho de Melo (2):
perf evsel: Add lazy-initialized probe type detection helpers perf trace: Guard __probe_ip suppression with evsel__is_probe()

 tools/perf/builtin-trace.c |  2 +-
 tools/perf/util/evsel.c    | 53 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/evsel.h    |  5 +++++
 3 files changed, 59 insertions(+), 1 deletion(-)
Cc Aaron Tomlin <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Namhyung Kim <[email protected]>

Developed with AI assistance (Claude/sashiko), tagged in commits.

Thanks,

- Arnaldo

author maintainer
#1 lore raw

PATCH v2 1/2 perf evsel: Add lazy-initialized probe type detection helpers

From: Arnaldo Carvalho de Melo <[email protected]>

Several places in perf need to check whether an evsel is a kprobe or uprobe, which requires looking up the PMU by name via evsel__find_pmu(). This lookup walks the PMU list each time, which is wasteful when the same evsel is checked repeatedly.

Add evsel__is_kprobe(), evsel__is_uprobe(), and evsel__is_probe() that resolve the probe type on first call via evsel__pmu_name() and cache the result in a 3-bit field (probe_type) in struct evsel. The field fits in existing padding after the bool fields, so struct size does not grow.

The enum uses PROBE__UNKNOWN (0) as the uninitialized sentinel — explicitly set in evsel__init() — so the lookup happens on first use. PROBE__NOPE (1) caches "not a probe" to avoid repeated negative lookups.

PMU-based probes (kprobe/uprobe PMU) are detected by PMU name. Ftrace-based dynamic probes (created via tracefs, reported as PMU "tracepoint") are detected by the __probe_ip field that the kernel adds to all dynamic probe formats. This covers kprobes, uprobes, and fprobes regardless of their group/system name.

Assisted-by Claude Opus 4.6 <[email protected]>
---
 tools/perf/util/evsel.c | 53 +++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/evsel.h |  5 ++++
 2 files changed, 58 insertions(+)
tools/perf/util/evsel.c +53 -0
index 05fa0010c858a51e..ea9fa04429f08a9e 100644
@@ -254,6 +254,58 @@ const char *evsel__pmu_name(const struct evsel *evsel)
 	return event_type(evsel->core.attr.type);
 }
 
+enum evsel_probe_type {
+	PROBE__UNKNOWN	= 0,
+	PROBE__NOPE	= 1,
+	PROBE__KPROBE	= 2,
+	PROBE__UPROBE	= 3,
+	/*
+	 * Ftrace-based dynamic probes (kprobes/uprobes/fprobes created via
+	 * tracefs) report PMU "tracepoint", not "kprobe"/"uprobe".  Detect
+	 * them by the __probe_ip field that the kernel adds to all dynamic
+	 * probe formats.
+	 */
+	PROBE__FTRACE	= 4,
+};
+
+static void evsel__resolve_probe_type(struct evsel *evsel)
+{
+	const char *name = evsel__pmu_name(evsel);
+
+	if (!strcmp(name, "kprobe"))
+		evsel->probe_type = PROBE__KPROBE;
+	else if (!strcmp(name, "uprobe"))
+		evsel->probe_type = PROBE__UPROBE;
+	else if (!strcmp(name, "tracepoint") && evsel__field(evsel, "__probe_ip"))
+		evsel->probe_type = PROBE__FTRACE;
+	else
+		evsel->probe_type = PROBE__NOPE;
+}
+
+bool evsel__is_probe(struct evsel *evsel)
+{
+	if (evsel->probe_type == PROBE__UNKNOWN)
+		evsel__resolve_probe_type(evsel);
+
+	return evsel->probe_type > PROBE__NOPE;
+}
+
+bool evsel__is_kprobe(struct evsel *evsel)
+{
+	if (evsel->probe_type == PROBE__UNKNOWN)
+		evsel__resolve_probe_type(evsel);
+
+	return evsel->probe_type == PROBE__KPROBE;
+}
+
+bool evsel__is_uprobe(struct evsel *evsel)
+{
+	if (evsel->probe_type == PROBE__UNKNOWN)
+		evsel__resolve_probe_type(evsel);
+
+	return evsel->probe_type == PROBE__UPROBE;
+}
+
 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
 
 int __evsel__sample_size(u64 sample_type)
@@ -413,6 +465,7 @@ void evsel__init(struct evsel *evsel,
 	evsel->supported     = true;
 	evsel->alternate_hw_config = PERF_COUNT_HW_MAX;
 	evsel->script_output_type = -1; // FIXME: OUTPUT_TYPE_UNSET, see builtin-script.c
+	evsel->probe_type = PROBE__UNKNOWN;
 }
 
 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx)
tools/perf/util/evsel.h +5 -0
index 8009be22cc3f1055..300c64d1e0c15e7d 100644
@@ -126,6 +126,7 @@ struct evsel {
 	bool			needs_uniquify;
 	bool			fallenback_eacces;
 	bool			fallenback_eopnotsupp;
+	u8			probe_type:3;
 	struct hashmap		*per_pkg_mask;
 	int			err;
 	int			script_output_type;
@@ -259,6 +260,10 @@ struct perf_pmu *evsel__find_pmu(const struct evsel *evsel);
 const char *evsel__pmu_name(const struct evsel *evsel);
 bool evsel__is_aux_event(const struct evsel *evsel);
 
+bool evsel__is_probe(struct evsel *evsel);
+bool evsel__is_kprobe(struct evsel *evsel);
+bool evsel__is_uprobe(struct evsel *evsel);
+
 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx);
 
 static inline struct evsel *evsel__new(struct perf_event_attr *attr)
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH 1/2] perf evsel: Add lazy-initialized probe type detection helpers

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Calling `evsel__field()` unconditionally without a `HAVE_LIBTRACEEVENT` feature guard or fallback…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Calling evsel__field() unconditionally without a HAVE_LIBTRACEEVENT feature guard or fallback stub causes linker errors when libtraceevent is omitted. --

commit 1f0d622399b5f71c138cdc45ef059e320a38fea7
Author: Arnaldo Carvalho de Melo <[email protected]>

perf evsel: Add lazy-initialized probe type detection helpers

This commit adds helpers to lazily detect and cache whether an evsel is a kprobe, uprobe, or ftrace-based dynamic probe. It introduces a 3-bit probe_type field in struct evsel to avoid repeated PMU lookups.

Quoted 3 lines
tools/perf/util/evsel.c +0 -0

[ ... ]

tools/perf/util/evsel.c const char *evsel__pmu_name(const struct evsel *evsel) · line 279

Quoted 9 lines
tools/perf/util/evsel.c +9 -0
+static void evsel__resolve_probe_type(struct evsel *evsel)
+{
+	const char *name = evsel__pmu_name(evsel);
+
+	if (!strcmp(name, "kprobe"))
+		evsel->probe_type = PROBE__KPROBE;
+	else if (!strcmp(name, "uprobe"))
+		evsel->probe_type = PROBE__UPROBE;
+	else if (!strcmp(name, "tracepoint") && evsel__field(evsel, "__probe_ip"))

[Severity: High]
Will this cause a linker error when compiling without libtraceevent?

This unconditionally calls evsel__field. The actual implementation of evsel__field is strictly wrapped in HAVE_LIBTRACEEVENT guards. Without a fallback stub in tools/perf/util/evsel.h that returns NULL for the !HAVE_LIBTRACEEVENT case, omitting libtraceevent could cause an undefined reference error during linking since evsel__is_probe prevents dead-code elimination of this function.

Quoted 4 lines
tools/perf/util/evsel.c +4 -0
+		evsel->probe_type = PROBE__FTRACE;
+	else
+		evsel->probe_type = PROBE__NOPE;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH v2 2/2 perf trace: Guard __probe_ip suppression with evsel__is_probe()

In reply to Arnaldo Carvalho de Melo

From: Arnaldo Carvalho de Melo <[email protected]>

trace__fprintf_tp_fields() compares every field name against "__probe_ip" for all tracepoint events, but this field is only implicitly added by the Ftrace subsystem to bare dynamic probes.

Add an evsel__is_probe() check before the strcmp so the string comparison is skipped entirely for non-probe events.

---
 tools/perf/builtin-trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-trace.c +1 -1
index a8492da23a9cc178..57f3f14c5d435805 100644
@@ -3267,7 +3267,7 @@ static size_t trace__fprintf_tp_fields(struct trace *trace, struct perf_sample *
 		 * If verbose mode is enabled, ensure it is formatted as a
 		 * hexadecimal memory address rather than a signed integer.
 		 */
-		if (!strcmp(field->name, "__probe_ip")) {
+		if (evsel__is_probe(evsel) && !strcmp(field->name, "__probe_ip")) {
 			if (!verbose)
 				continue;
 
Signature · 1 line
2.54.0