Skip to contentSkip to conversation

perf trace: Filter enum arguments with enum names

PATCH In review Howard Chu 2 participants 3 messages Jun 15, 2024 – Jun 17, 2024 perf events

lore
Share & tools
lore

Howard Chu

author
#0 lore raw

PATCH 1/1 perf trace: Filter enum arguments with enum names

Before:

perf $ ./perf trace -e timer:hrtimer_start --filter='mode!=HRTIMER_MODE_ABS_PINNED_HARD' --max-events=1
No resolver (strtoul) for "mode" in "timer:hrtimer_start", can't set filter "(mode!=HRTIMER_MODE_ABS_PINNED_HARD) && (common_pid != 281988)"

After:

perf $ ./perf trace -e timer:hrtimer_start --filter='mode!=HRTIMER_MODE_ABS_PINNED_HARD' --max-events=1

     0.000 :0/0 timer:hrtimer_start(hrtimer: 0xffff9498a6ca5f18, function: 0xffffffffa77a5be0, expires: 12351248764875, softexpires: 12351248764875, mode: HRTIMER_MODE_ABS)

&& and ||:

perf $ ./perf trace -e timer:hrtimer_start --filter='mode != HRTIMER_MODE_ABS_PINNED_HARD && mode != HRTIMER_MODE_ABS' --max-events=1

     0.000 Hyprland/534 timer:hrtimer_start(hrtimer: 0xffff9497801a84d0, function: 0xffffffffc04cdbe0, expires: 12639434638458, softexpires: 12639433638458, mode: HRTIMER_MODE_REL)

perf $ ./perf trace -e timer:hrtimer_start --filter='mode == HRTIMER_MODE_REL || mode == HRTIMER_MODE_PINNED' --max-events=1

     0.000 ldlck-test/60639 timer:hrtimer_start(hrtimer: 0xffffb16404ee7bf8, function: 0xffffffffa7790420, expires: 12772614418016, softexpires: 12772614368016, mode: HRTIMER_MODE_REL)

Switching it up, using both enum name and integer value(--filter='mode == HRTIMER_MODE_ABS_PINNED_HARD || mode == 0'):

perf $ ./perf trace -e timer:hrtimer_start --filter='mode == HRTIMER_MODE_ABS_PINNED_HARD || mode == 0' --max-events=3

     0.000 :0/0 timer:hrtimer_start(hrtimer: 0xffff9498a6ca5f18, function: 0xffffffffa77a5be0, expires: 12601748739825, softexpires: 12601748739825, mode: HRTIMER_MODE_ABS_PINNED_HARD)
     0.036 :0/0 timer:hrtimer_start(hrtimer: 0xffff9498a6ca5f18, function: 0xffffffffa77a5be0, expires: 12518758748124, softexpires: 12518758748124, mode: HRTIMER_MODE_ABS_PINNED_HARD)
     0.172 tmux: server/41881 timer:hrtimer_start(hrtimer: 0xffffb164081e7838, function: 0xffffffffa7790420, expires: 12518768255836, softexpires: 12518768205836, mode: HRTIMER_MODE_ABS)

P.S.
perf $ pahole hrtimer_mode
enum hrtimer_mode {

        HRTIMER_MODE_ABS             = 0,
        HRTIMER_MODE_REL             = 1,
        HRTIMER_MODE_PINNED          = 2,
        HRTIMER_MODE_SOFT            = 4,
        HRTIMER_MODE_HARD            = 8,
        HRTIMER_MODE_ABS_PINNED      = 2,
        HRTIMER_MODE_REL_PINNED      = 3,
        HRTIMER_MODE_ABS_SOFT        = 4,
        HRTIMER_MODE_REL_SOFT        = 5,

HRTIMER_MODE_ABS_PINNED_SOFT = 6,
HRTIMER_MODE_REL_PINNED_SOFT = 7,
HRTIMER_MODE_ABS_HARD = 8,
HRTIMER_MODE_REL_HARD = 9,
HRTIMER_MODE_ABS_PINNED_HARD = 10,
HRTIMER_MODE_REL_PINNED_HARD = 11,
};

---
 tools/perf/builtin-trace.c | 89 ++++++++++++++++++++++++++++++++------
 1 file changed, 76 insertions(+), 13 deletions(-)
tools/perf/builtin-trace.c +76 -13
index bd16679fb4c0..1148c3edee97 100644
@@ -904,11 +904,36 @@ static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
 	    .strtoul	= STUL_STRARRAY_FLAGS, \
 	    .parm	= &strarray__##array, }
 
-static int btf_enum_find_entry(struct btf *btf, char *type, struct syscall_arg_fmt *arg_fmt)
+#define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
+
+static const struct btf_type *btf_find_type(struct btf *btf, char *type)
 {
 	const struct btf_type *bt;
+	int id = btf__find_by_name(btf, type);
+
+	if (id < 0)
+		return NULL;
+
+	bt = btf__type_by_id(btf, id);
+	if (bt == NULL)
+		return NULL;
+
+	return bt;
+}
+
+struct btf_parm {
+	struct btf *btf;
+	char *type;
+};
+
+static bool syscall_arg__strtoul_btf_enum(char *bf, size_t size, struct syscall_arg *arg, u64 *val)
+{
+	struct btf_parm *bparm = arg->parm;
+	struct btf *btf = bparm->btf;
+	char *type      = bparm->type;
 	char enum_prefix[][16] = { "enum", "const enum" }, *ep;
-	int id;
+	struct btf_enum *be;
+	const struct btf_type *bt;
 	size_t i;
 
 	for (i = 0; i < ARRAY_SIZE(enum_prefix); i++) {
@@ -917,11 +942,38 @@ static int btf_enum_find_entry(struct btf *btf, char *type, struct syscall_arg_f
 			type += strlen(ep) + 1;
 	}
 
-	id = btf__find_by_name(btf, type);
-	if (id < 0)
-		return -1;
+	bt = btf_find_type(btf, type);
+	if (bt == NULL)
+		return false;
 
-	bt = btf__type_by_id(btf, id);
+	for (be = btf_enum(bt), i = 0; i < btf_vlen(bt); ++i, ++be) {
+		const char *name = btf__name_by_offset(btf, be->name_off);
+		int max_len = max(size, strlen(name));
+
+		if (strncmp(name, bf, max_len) == 0) {
+			*val = be->val;
+			return true;
+		}
+	}
+
+	return false;
+}
+
+#define STUL_BTF_ENUM syscall_arg__strtoul_btf_enum
+
+static int btf_enum_find_entry(struct btf *btf, char *type, struct syscall_arg_fmt *arg_fmt)
+{
+	char enum_prefix[][16] = { "enum", "const enum" }, *ep;
+	const struct btf_type *bt;
+	size_t i;
+
+	for (i = 0; i < ARRAY_SIZE(enum_prefix); i++) {
+		ep = enum_prefix[i];
+		if (strlen(type) > strlen(ep) + 1 && strstarts(type, ep))
+			type += strlen(ep) + 1;
+	}
+
+	bt = btf_find_type(btf, type);
 	if (bt == NULL)
 		return -1;
 
@@ -1850,6 +1902,7 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_field
 			arg->scnprintf = SCA_FD;
 		} else if (strstr(field->type, "enum") && use_btf != NULL) {
 			*use_btf = arg->is_enum = true;
+			arg->strtoul = STUL_BTF_ENUM;
 		} else {
 			const struct syscall_arg_fmt *fmt =
 				syscall_arg_fmt__find_by_name(field->name);
@@ -3776,7 +3829,8 @@ static int ordered_events__deliver_event(struct ordered_events *oe,
 	return __trace__deliver_event(trace, event->event);
 }
 
-static struct syscall_arg_fmt *evsel__find_syscall_arg_fmt_by_name(struct evsel *evsel, char *arg)
+static struct syscall_arg_fmt *evsel__find_syscall_arg_fmt_by_name(struct evsel *evsel, char *arg,
+								   char **type)
 {
 	struct tep_format_field *field;
 	struct syscall_arg_fmt *fmt = __evsel__syscall_arg_fmt(evsel);
@@ -3785,8 +3839,10 @@ static struct syscall_arg_fmt *evsel__find_syscall_arg_fmt_by_name(struct evsel
 		return NULL;
 
 	for (field = evsel->tp_format->format.fields; field; field = field->next, ++fmt)
-		if (strcmp(field->name, arg) == 0)
+		if (strcmp(field->name, arg) == 0) {
+			*type = field->type;
 			return fmt;
+		}
 
 	return NULL;
 }
@@ -3824,14 +3880,14 @@ static int trace__expand_filter(struct trace *trace __maybe_unused, struct evsel
 			struct syscall_arg_fmt *fmt;
 			int left_size = tok - left,
 			    right_size = right_end - right;
-			char arg[128];
+			char arg[128], *type;
 
 			while (isspace(left[left_size - 1]))
 				--left_size;
 
 			scnprintf(arg, sizeof(arg), "%.*s", left_size, left);
 
-			fmt = evsel__find_syscall_arg_fmt_by_name(evsel, arg);
+			fmt = evsel__find_syscall_arg_fmt_by_name(evsel, arg, &type);
 			if (fmt == NULL) {
 				pr_err("\"%s\" not found in \"%s\", can't set filter \"%s\"\n",
 				       arg, evsel->name, evsel->filter);
@@ -3843,9 +3899,16 @@ static int trace__expand_filter(struct trace *trace __maybe_unused, struct evsel
 
 			if (fmt->strtoul) {
 				u64 val;
-				struct syscall_arg syscall_arg = {
-					.parm = fmt->parm,
-				};
+				struct syscall_arg syscall_arg;
+				struct btf_parm bparm;
+
+				if (fmt->is_enum) {
+					bparm.btf  = trace->btf;
+					bparm.type = type;
+					syscall_arg.parm = &bparm;
+				} else {
+					syscall_arg.parm = fmt->parm;
+				}
 
 				if (fmt->strtoul(right, right_size, &syscall_arg, &val)) {
 					char *n, expansion[19];
Signature · 1 line
2.45.2
#1 lore raw
Quoted 9 lines · Howard Chu

Before:

perf $ ./perf trace -e timer:hrtimer_start --filter='mode!=HRTIMER_MODE_ABS_PINNED_HARD' --max-events=1
No resolver (strtoul) for "mode" in "timer:hrtimer_start", can't set filter "(mode!=HRTIMER_MODE_ABS_PINNED_HARD) && (common_pid != 281988)"

After:

perf $ ./perf trace -e timer:hrtimer_start --filter='mode!=HRTIMER_MODE_ABS_PINNED_HARD' --max-events=1

     0.000 :0/0 timer:hrtimer_start(hrtimer: 0xffff9498a6ca5f18, function: 0xffffffffa77a5be0, expires: 12351248764875, softexpires: 12351248764875, mode: HRTIMER_MODE_ABS)

This one I had to apply manually after applying the other two patches:

⬢[acme@toolbox perf-tools-next]$ git am ./20240615_howardchu95_perf_trace_filter_enum_arguments_with_enum_names.mbx
Applying: perf trace: Filter enum arguments with enum names
error: patch failed: tools/perf/builtin-trace.c:904
error: tools/perf/builtin-trace.c: patch does not apply
Patch failed at 0001 perf trace: Filter enum arguments with enum names
hint: Use 'git am --show-current-patch=diff' to see the failed patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
⬢[acme@toolbox perf-tools-next]$ git am --abort
⬢[acme@toolbox perf-tools-next]$ patch -p1 < ./20240615_howardchu95_perf_trace_filter_enum_arguments_with_enum_names.mbx patching file tools/perf/builtin-trace.c
Hunk #1 succeeded at 894 with fuzz 2 (offset -10 lines).
Hunk #2 succeeded at 932 (offset -10 lines).
Hunk #3 succeeded at 1905 (offset 3 lines).
Hunk #4 succeeded at 3832 (offset 3 lines).
Hunk #5 succeeded at 3842 (offset 3 lines).
Hunk #6 succeeded at 3883 (offset 3 lines).
Hunk #7 succeeded at 3902 (offset 3 lines).
⬢[acme@toolbox perf-tools-next]$

I'll push what I have to that tmp.perf-tools-next on my git repo at:

https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git

- Arnaldo

#2 lore raw
Quoted 35 lines · Arnaldo Carvalho de Melo
Quoted 9 lines · Howard Chu

Before:

perf $ ./perf trace -e timer:hrtimer_start --filter='mode!=HRTIMER_MODE_ABS_PINNED_HARD' --max-events=1
No resolver (strtoul) for "mode" in "timer:hrtimer_start", can't set filter "(mode!=HRTIMER_MODE_ABS_PINNED_HARD) && (common_pid != 281988)"

After:

perf $ ./perf trace -e timer:hrtimer_start --filter='mode!=HRTIMER_MODE_ABS_PINNED_HARD' --max-events=1

     0.000 :0/0 timer:hrtimer_start(hrtimer: 0xffff9498a6ca5f18, function: 0xffffffffa77a5be0, expires: 12351248764875, softexpires: 12351248764875, mode: HRTIMER_MODE_ABS)

This one I had to apply manually after applying the other two patches:

⬢[acme@toolbox perf-tools-next]$ git am ./20240615_howardchu95_perf_trace_filter_enum_arguments_with_enum_names.mbx
Applying: perf trace: Filter enum arguments with enum names
error: patch failed: tools/perf/builtin-trace.c:904
error: tools/perf/builtin-trace.c: patch does not apply
Patch failed at 0001 perf trace: Filter enum arguments with enum names
hint: Use 'git am --show-current-patch=diff' to see the failed patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
⬢[acme@toolbox perf-tools-next]$ git am --abort
⬢[acme@toolbox perf-tools-next]$ patch -p1 < ./20240615_howardchu95_perf_trace_filter_enum_arguments_with_enum_names.mbx patching file tools/perf/builtin-trace.c
Hunk #1 succeeded at 894 with fuzz 2 (offset -10 lines).
Hunk #2 succeeded at 932 (offset -10 lines).
Hunk #3 succeeded at 1905 (offset 3 lines).
Hunk #4 succeeded at 3832 (offset 3 lines).
Hunk #5 succeeded at 3842 (offset 3 lines).
Hunk #6 succeeded at 3883 (offset 3 lines).
Hunk #7 succeeded at 3902 (offset 3 lines).
⬢[acme@toolbox perf-tools-next]$

Quoted 3 lines

I'll push what I have to that tmp.perf-tools-next on my git repo at:

https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git

I've put it also in the perf_trace-btf_enum branch:

https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/log/?h=perf_trace-btf_enum

Maybe we can then ask Namhyung to merge from that branch into perf-tools-next/perf-tools-next.

Namhyung, wdyt? I'll do some more tests and review the code one more time early tomorrow, but all seems to be work as expected.

The way it was submitted was a bit convoluted, I was expecting for Howard to have the patches the way I put it in the perf_trace-btf_enum branch, then use:

⬢[acme@toolbox perf-tools-next]$ rm -f 0*.patch ⬢[acme@toolbox perf-tools-next]$ git format-patch -n HEAD~4 0000-cover-letter.patch 0001-perf-trace-Fix-iteration-of-syscall-ids-in-syscalltb.patch 0002-perf-trace-BTF-based-enum-pretty-printing-for-syscal.patch 0003-perf-trace-Augment-non-syscall-tracepoints-with-enum.patch 0004-perf-trace-Filter-enum-arguments-with-enum-names.patch ⬢[acme@toolbox perf-tools-next]$

And then use:

git send-email --from "Howard Chu <[email protected]>" \
                --to "Arnaldo Carvalho de Melo <[email protected]>"
                --cc "Jiri Olsa <[email protected]>" \
                --cc "Namhyung Kim <[email protected]>" \
                --cc "Ian Rogers <[email protected]>" \
                --cc "Adrian Hunter <[email protected]>" \
                --cc "Kan Liang <[email protected]>" \
                --cc [email protected] \
                --cc [email protected] \
                --no-validate \
                --smtp-debug=1 \
                --no-chain-reply-to 0*.patch

After editing 0000-cover-letter.patch to add an explanation about the series. The version of the series would be in the Subject line for the cover letter, etc.

Lets try to use this in the next series, concentrating now in reviewing if what I have at:

https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/log/?h=perf_trace-btf_enum

Can already be merged by Namhyung on perf-tools-next/perf-tools-next.

Thanks a lot!

- Arnaldo