Skip to content

Commit 5cbcfa7

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
selftests/bpf: Move tracing_multi_bench_attach to bench
In CI, the tracing_multi_bench_attach test could cost >10s, which should be avoided in daily CI running. ./test_progs -t tracing_multi_bench_attach -v WATCHDOG: test case tracing_multi_bench_attach executes for 10 seconds... serial_test_tracing_multi_bench_attach: found 40163 functions serial_test_tracing_multi_bench_attach: attached in 9.367s serial_test_tracing_multi_bench_attach: detached in 1.798s #546 tracing_multi_bench_attach:OK Since it is for benchmark attachment of tracing_multi link, move it to the generic bench framework. ./bench tracing-multi-attach Setting up benchmark 'tracing-multi-attach'... tracing-multi-attach: found 40166 functions tracing-multi-attach: attached in 9.278s tracing-multi-attach: detached in 1.836s Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
1 parent dd2acb0 commit 5cbcfa7

4 files changed

Lines changed: 181 additions & 125 deletions

File tree

tools/testing/selftests/bpf/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ $(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h
976976
$(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h
977977
$(OUTPUT)/bench_bpf_for.o: $(OUTPUT)/bpf_for_bench.skel.h
978978
$(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h
979+
$(OUTPUT)/bench_tracing_multi_attach.o: $(OUTPUT)/tracing_multi_bench.skel.h
979980
$(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h
980981
$(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h
981982
$(OUTPUT)/bench_local_storage_rcu_tasks_trace.o: $(OUTPUT)/local_storage_rcu_tasks_trace_bench.skel.h
@@ -1002,6 +1003,7 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \
10021003
$(OUTPUT)/bench_bpf_loop.o \
10031004
$(OUTPUT)/bench_bpf_for.o \
10041005
$(OUTPUT)/bench_strncmp.o \
1006+
$(OUTPUT)/bench_tracing_multi_attach.o \
10051007
$(OUTPUT)/bench_bpf_hashmap_full_update.o \
10061008
$(OUTPUT)/bench_local_storage.o \
10071009
$(OUTPUT)/bench_local_storage_rcu_tasks_trace.o \

tools/testing/selftests/bpf/bench.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ extern const struct bench bench_trig_uprobe_multi_nop5;
547547
extern const struct bench bench_trig_uretprobe_multi_nop5;
548548
extern const struct bench bench_trig_usdt_nop;
549549
extern const struct bench bench_trig_usdt_nop5;
550+
extern const struct bench bench_tracing_multi_attach;
550551
#endif
551552

552553
extern const struct bench bench_rb_libbpf;
@@ -631,6 +632,7 @@ static const struct bench *benchs[] = {
631632
&bench_trig_uretprobe_multi_nop5,
632633
&bench_trig_usdt_nop,
633634
&bench_trig_usdt_nop5,
635+
&bench_tracing_multi_attach,
634636
#endif
635637
/* ringbuf/perfbuf benchmarks */
636638
&bench_rb_libbpf,
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#define _GNU_SOURCE
3+
#include <search.h>
4+
#include <stdio.h>
5+
#include <string.h>
6+
#include <bpf/btf.h>
7+
#include "bench.h"
8+
#include "testing_helpers.h"
9+
#include "trace_helpers.h"
10+
#include "tracing_multi_bench.skel.h"
11+
#include "bpf/libbpf_internal.h"
12+
13+
static int compare(const void *ppa, const void *ppb)
14+
{
15+
const char *pa = *(const char **)ppa;
16+
const char *pb = *(const char **)ppb;
17+
18+
return strcmp(pa, pb);
19+
}
20+
21+
static void tdestroy_free_nop(void *ptr)
22+
{
23+
}
24+
25+
static void tracing_multi_attach_setup(void)
26+
{
27+
LIBBPF_OPTS(bpf_tracing_multi_opts, opts);
28+
struct tracing_multi_bench *skel = NULL;
29+
long attach_start_ns, attach_end_ns;
30+
long detach_start_ns, detach_end_ns;
31+
double attach_delta, detach_delta;
32+
struct bpf_link *link = NULL;
33+
size_t i, cap = 0, cnt = 0;
34+
struct ksyms *ksyms = NULL;
35+
void *root = NULL;
36+
void *dups = NULL;
37+
__u32 *ids = NULL;
38+
__u32 nr, type_id;
39+
struct btf *btf;
40+
int err;
41+
42+
setup_libbpf();
43+
44+
btf = btf__load_vmlinux_btf();
45+
err = libbpf_get_error(btf);
46+
if (err) {
47+
btf = NULL;
48+
fprintf(stderr, "failed to load vmlinux BTF: %s\n", strerror(-err));
49+
goto cleanup;
50+
}
51+
52+
skel = tracing_multi_bench__open_and_load();
53+
err = libbpf_get_error(skel);
54+
if (!skel) {
55+
fprintf(stderr, "failed to open and load skeleton: %s\n", strerror(-err));
56+
goto cleanup;
57+
}
58+
59+
err = bpf_get_ksyms(&ksyms, true);
60+
if (err) {
61+
fprintf(stderr, "failed to get kernel symbols: %s\n", strerror(-err));
62+
goto cleanup;
63+
}
64+
65+
/* Get all ftrace 'safe' symbols.. */
66+
for (i = 0; i < ksyms->filtered_cnt; i++) {
67+
if (!tsearch(&ksyms->filtered_syms[i], &root, compare)) {
68+
err = -ENOMEM;
69+
fprintf(stderr, "failed to index ftrace symbols\n");
70+
goto cleanup;
71+
}
72+
}
73+
74+
/*
75+
* Collect names that are not unique in kallsyms. The kernel resolves a
76+
* tracing-multi BTF id to an address with kallsyms_lookup_name(), which
77+
* returns the first symbol of that name. For a duplicate name that may
78+
* be a different (non-ftrace-able) instance than the ftrace-able one in
79+
* available_filter_functions, so attaching to it by BTF id fails with
80+
* -ENOENT (e.g. t_start/t_next/t_stop). ksyms->syms is sorted by name,
81+
* so equal names are adjacent.
82+
*/
83+
for (i = 1; i < ksyms->sym_cnt; i++) {
84+
if (strcmp(ksyms->syms[i].name, ksyms->syms[i - 1].name))
85+
continue;
86+
if (!tsearch(&ksyms->syms[i].name, &dups, compare)) {
87+
err = -ENOMEM;
88+
fprintf(stderr, "failed to index duplicate kernel symbols\n");
89+
goto cleanup;
90+
}
91+
}
92+
93+
/* ..and filter them through BTF and btf_type_is_traceable_func. */
94+
nr = btf__type_cnt(btf);
95+
for (type_id = 1; type_id < nr; type_id++) {
96+
const struct btf_type *type;
97+
const char *str;
98+
99+
type = btf__type_by_id(btf, type_id);
100+
if (!type)
101+
break;
102+
103+
if (BTF_INFO_KIND(type->info) != BTF_KIND_FUNC)
104+
continue;
105+
106+
str = btf__name_by_offset(btf, type->name_off);
107+
if (!str)
108+
break;
109+
110+
if (!tfind(&str, &root, compare))
111+
continue;
112+
113+
/* Skip names that are not unique in kallsyms, see above. */
114+
if (tfind(&str, &dups, compare))
115+
continue;
116+
117+
if (!btf_type_is_traceable_func(btf, type))
118+
continue;
119+
120+
err = libbpf_ensure_mem((void **)&ids, &cap, sizeof(*ids), cnt + 1);
121+
if (err) {
122+
fprintf(stderr, "failed to allocate BTF ID array: %s\n",
123+
strerror(-err));
124+
goto cleanup;
125+
}
126+
127+
ids[cnt++] = type_id;
128+
}
129+
130+
opts.ids = ids;
131+
opts.cnt = cnt;
132+
133+
attach_start_ns = get_time_ns();
134+
link = bpf_program__attach_tracing_multi(skel->progs.bench, NULL, &opts);
135+
attach_end_ns = get_time_ns();
136+
137+
err = libbpf_get_error(link);
138+
if (err) {
139+
link = NULL;
140+
fprintf(stderr, "failed to attach tracing multi link: %s\n",
141+
strerror(-err));
142+
goto cleanup;
143+
}
144+
145+
detach_start_ns = get_time_ns();
146+
err = bpf_link__destroy(link);
147+
detach_end_ns = get_time_ns();
148+
link = NULL;
149+
if (err) {
150+
fprintf(stderr, "failed to detach tracing multi link: %s\n",
151+
strerror(-err));
152+
goto cleanup;
153+
}
154+
155+
attach_delta = (attach_end_ns - attach_start_ns) / 1000000000.0;
156+
detach_delta = (detach_end_ns - detach_start_ns) / 1000000000.0;
157+
158+
printf("%s: found %zu functions\n", bench->name, cnt);
159+
printf("%s: attached in %7.3lfs\n", bench->name, attach_delta);
160+
printf("%s: detached in %7.3lfs\n", bench->name, detach_delta);
161+
162+
cleanup:
163+
bpf_link__destroy(link);
164+
tracing_multi_bench__destroy(skel);
165+
tdestroy(root, tdestroy_free_nop);
166+
tdestroy(dups, tdestroy_free_nop);
167+
free_kallsyms_local(ksyms);
168+
free(ids);
169+
btf__free(btf);
170+
171+
exit(err ? 1 : 0);
172+
}
173+
174+
const struct bench bench_tracing_multi_attach = {
175+
.name = "tracing-multi-attach",
176+
.setup = tracing_multi_attach_setup,
177+
};

tools/testing/selftests/bpf/prog_tests/tracing_multi.c

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
#include <test_progs.h>
44
#include <bpf/btf.h>
55
#include <search.h>
6-
#include "bpf/libbpf_internal.h"
76
#include "tracing_multi.skel.h"
87
#include "tracing_multi_module.skel.h"
98
#include "tracing_multi_intersect.skel.h"
109
#include "tracing_multi_session.skel.h"
1110
#include "tracing_multi_fail.skel.h"
1211
#include "tracing_multi_verifier.skel.h"
13-
#include "tracing_multi_bench.skel.h"
1412
#include "tracing_multi_rollback.skel.h"
1513
#include "trace_helpers.h"
1614

@@ -662,129 +660,6 @@ static void test_attach_api_fails(void)
662660
free(ids2);
663661
}
664662

665-
void serial_test_tracing_multi_bench_attach(void)
666-
{
667-
LIBBPF_OPTS(bpf_tracing_multi_opts, opts);
668-
struct tracing_multi_bench *skel = NULL;
669-
long attach_start_ns, attach_end_ns;
670-
long detach_start_ns, detach_end_ns;
671-
double attach_delta, detach_delta;
672-
struct bpf_link *link = NULL;
673-
size_t i, cap = 0, cnt = 0;
674-
struct ksyms *ksyms = NULL;
675-
void *root = NULL;
676-
void *dups = NULL;
677-
__u32 *ids = NULL;
678-
__u32 nr, type_id;
679-
struct btf *btf;
680-
int err;
681-
682-
#ifndef __x86_64__
683-
test__skip();
684-
return;
685-
#endif
686-
687-
btf = btf__load_vmlinux_btf();
688-
if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf"))
689-
return;
690-
691-
skel = tracing_multi_bench__open_and_load();
692-
if (!ASSERT_OK_PTR(skel, "tracing_multi_bench__open_and_load"))
693-
goto cleanup;
694-
695-
if (!ASSERT_OK(bpf_get_ksyms(&ksyms, true), "get_syms"))
696-
goto cleanup;
697-
698-
/* Get all ftrace 'safe' symbols.. */
699-
for (i = 0; i < ksyms->filtered_cnt; i++) {
700-
if (!tsearch(&ksyms->filtered_syms[i], &root, compare)) {
701-
ASSERT_FAIL("tsearch failed");
702-
goto cleanup;
703-
}
704-
}
705-
706-
/*
707-
* Collect names that are not unique in kallsyms. The kernel resolves a
708-
* tracing-multi BTF id to an address with kallsyms_lookup_name(), which
709-
* returns the first symbol of that name. For a duplicate name that may
710-
* be a different (non-ftrace-able) instance than the ftrace-able one in
711-
* available_filter_functions, so attaching to it by BTF id fails with
712-
* -ENOENT (e.g. t_start/t_next/t_stop). ksyms->syms is sorted by name,
713-
* so equal names are adjacent.
714-
*/
715-
for (i = 1; i < ksyms->sym_cnt; i++) {
716-
if (strcmp(ksyms->syms[i].name, ksyms->syms[i - 1].name))
717-
continue;
718-
if (!tsearch(&ksyms->syms[i].name, &dups, compare)) {
719-
ASSERT_FAIL("tsearch failed");
720-
goto cleanup;
721-
}
722-
}
723-
724-
/* ..and filter them through BTF and btf_type_is_traceable_func. */
725-
nr = btf__type_cnt(btf);
726-
for (type_id = 1; type_id < nr; type_id++) {
727-
const struct btf_type *type;
728-
const char *str;
729-
730-
type = btf__type_by_id(btf, type_id);
731-
if (!type)
732-
break;
733-
734-
if (BTF_INFO_KIND(type->info) != BTF_KIND_FUNC)
735-
continue;
736-
737-
str = btf__name_by_offset(btf, type->name_off);
738-
if (!str)
739-
break;
740-
741-
if (!tfind(&str, &root, compare))
742-
continue;
743-
744-
/* Skip names that are not unique in kallsyms, see above. */
745-
if (tfind(&str, &dups, compare))
746-
continue;
747-
748-
if (!btf_type_is_traceable_func(btf, type))
749-
continue;
750-
751-
err = libbpf_ensure_mem((void **) &ids, &cap, sizeof(*ids), cnt + 1);
752-
if (err)
753-
goto cleanup;
754-
755-
ids[cnt++] = type_id;
756-
}
757-
758-
opts.ids = ids;
759-
opts.cnt = cnt;
760-
761-
attach_start_ns = get_time_ns();
762-
link = bpf_program__attach_tracing_multi(skel->progs.bench, NULL, &opts);
763-
attach_end_ns = get_time_ns();
764-
765-
if (!ASSERT_OK_PTR(link, "bpf_program__attach_tracing_multi"))
766-
goto cleanup;
767-
768-
detach_start_ns = get_time_ns();
769-
bpf_link__destroy(link);
770-
detach_end_ns = get_time_ns();
771-
772-
attach_delta = (attach_end_ns - attach_start_ns) / 1000000000.0;
773-
detach_delta = (detach_end_ns - detach_start_ns) / 1000000000.0;
774-
775-
printf("%s: found %lu functions\n", __func__, cnt);
776-
printf("%s: attached in %7.3lfs\n", __func__, attach_delta);
777-
printf("%s: detached in %7.3lfs\n", __func__, detach_delta);
778-
779-
cleanup:
780-
tracing_multi_bench__destroy(skel);
781-
tdestroy(root, tdestroy_free_nop);
782-
tdestroy(dups, tdestroy_free_nop);
783-
free_kallsyms_local(ksyms);
784-
free(ids);
785-
btf__free(btf);
786-
}
787-
788663
static void tracing_multi_rollback_run(struct tracing_multi_rollback *skel)
789664
{
790665
LIBBPF_OPTS(bpf_test_run_opts, topts);

0 commit comments

Comments
 (0)