xref: /linux/tools/testing/selftests/bpf/benchs/bench_rename.c (revision 4eaf0b5c5e04c21a866431bd763ab4b1f24c4d16)
1*4eaf0b5cSAndrii Nakryiko // SPDX-License-Identifier: GPL-2.0
2*4eaf0b5cSAndrii Nakryiko /* Copyright (c) 2020 Facebook */
3*4eaf0b5cSAndrii Nakryiko #include <fcntl.h>
4*4eaf0b5cSAndrii Nakryiko #include "bench.h"
5*4eaf0b5cSAndrii Nakryiko #include "test_overhead.skel.h"
6*4eaf0b5cSAndrii Nakryiko 
7*4eaf0b5cSAndrii Nakryiko /* BPF triggering benchmarks */
8*4eaf0b5cSAndrii Nakryiko static struct ctx {
9*4eaf0b5cSAndrii Nakryiko 	struct test_overhead *skel;
10*4eaf0b5cSAndrii Nakryiko 	struct counter hits;
11*4eaf0b5cSAndrii Nakryiko 	int fd;
12*4eaf0b5cSAndrii Nakryiko } ctx;
13*4eaf0b5cSAndrii Nakryiko 
14*4eaf0b5cSAndrii Nakryiko static void validate()
15*4eaf0b5cSAndrii Nakryiko {
16*4eaf0b5cSAndrii Nakryiko 	if (env.producer_cnt != 1) {
17*4eaf0b5cSAndrii Nakryiko 		fprintf(stderr, "benchmark doesn't support multi-producer!\n");
18*4eaf0b5cSAndrii Nakryiko 		exit(1);
19*4eaf0b5cSAndrii Nakryiko 	}
20*4eaf0b5cSAndrii Nakryiko 	if (env.consumer_cnt != 1) {
21*4eaf0b5cSAndrii Nakryiko 		fprintf(stderr, "benchmark doesn't support multi-consumer!\n");
22*4eaf0b5cSAndrii Nakryiko 		exit(1);
23*4eaf0b5cSAndrii Nakryiko 	}
24*4eaf0b5cSAndrii Nakryiko }
25*4eaf0b5cSAndrii Nakryiko 
26*4eaf0b5cSAndrii Nakryiko static void *producer(void *input)
27*4eaf0b5cSAndrii Nakryiko {
28*4eaf0b5cSAndrii Nakryiko 	char buf[] = "test_overhead";
29*4eaf0b5cSAndrii Nakryiko 	int err;
30*4eaf0b5cSAndrii Nakryiko 
31*4eaf0b5cSAndrii Nakryiko 	while (true) {
32*4eaf0b5cSAndrii Nakryiko 		err = write(ctx.fd, buf, sizeof(buf));
33*4eaf0b5cSAndrii Nakryiko 		if (err < 0) {
34*4eaf0b5cSAndrii Nakryiko 			fprintf(stderr, "write failed\n");
35*4eaf0b5cSAndrii Nakryiko 			exit(1);
36*4eaf0b5cSAndrii Nakryiko 		}
37*4eaf0b5cSAndrii Nakryiko 		atomic_inc(&ctx.hits.value);
38*4eaf0b5cSAndrii Nakryiko 	}
39*4eaf0b5cSAndrii Nakryiko }
40*4eaf0b5cSAndrii Nakryiko 
41*4eaf0b5cSAndrii Nakryiko static void measure(struct bench_res *res)
42*4eaf0b5cSAndrii Nakryiko {
43*4eaf0b5cSAndrii Nakryiko 	res->hits = atomic_swap(&ctx.hits.value, 0);
44*4eaf0b5cSAndrii Nakryiko }
45*4eaf0b5cSAndrii Nakryiko 
46*4eaf0b5cSAndrii Nakryiko static void setup_ctx()
47*4eaf0b5cSAndrii Nakryiko {
48*4eaf0b5cSAndrii Nakryiko 	setup_libbpf();
49*4eaf0b5cSAndrii Nakryiko 
50*4eaf0b5cSAndrii Nakryiko 	ctx.skel = test_overhead__open_and_load();
51*4eaf0b5cSAndrii Nakryiko 	if (!ctx.skel) {
52*4eaf0b5cSAndrii Nakryiko 		fprintf(stderr, "failed to open skeleton\n");
53*4eaf0b5cSAndrii Nakryiko 		exit(1);
54*4eaf0b5cSAndrii Nakryiko 	}
55*4eaf0b5cSAndrii Nakryiko 
56*4eaf0b5cSAndrii Nakryiko 	ctx.fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
57*4eaf0b5cSAndrii Nakryiko 	if (ctx.fd < 0) {
58*4eaf0b5cSAndrii Nakryiko 		fprintf(stderr, "failed to open /proc/self/comm: %d\n", -errno);
59*4eaf0b5cSAndrii Nakryiko 		exit(1);
60*4eaf0b5cSAndrii Nakryiko 	}
61*4eaf0b5cSAndrii Nakryiko }
62*4eaf0b5cSAndrii Nakryiko 
63*4eaf0b5cSAndrii Nakryiko static void attach_bpf(struct bpf_program *prog)
64*4eaf0b5cSAndrii Nakryiko {
65*4eaf0b5cSAndrii Nakryiko 	struct bpf_link *link;
66*4eaf0b5cSAndrii Nakryiko 
67*4eaf0b5cSAndrii Nakryiko 	link = bpf_program__attach(prog);
68*4eaf0b5cSAndrii Nakryiko 	if (IS_ERR(link)) {
69*4eaf0b5cSAndrii Nakryiko 		fprintf(stderr, "failed to attach program!\n");
70*4eaf0b5cSAndrii Nakryiko 		exit(1);
71*4eaf0b5cSAndrii Nakryiko 	}
72*4eaf0b5cSAndrii Nakryiko }
73*4eaf0b5cSAndrii Nakryiko 
74*4eaf0b5cSAndrii Nakryiko static void setup_base()
75*4eaf0b5cSAndrii Nakryiko {
76*4eaf0b5cSAndrii Nakryiko 	setup_ctx();
77*4eaf0b5cSAndrii Nakryiko }
78*4eaf0b5cSAndrii Nakryiko 
79*4eaf0b5cSAndrii Nakryiko static void setup_kprobe()
80*4eaf0b5cSAndrii Nakryiko {
81*4eaf0b5cSAndrii Nakryiko 	setup_ctx();
82*4eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog1);
83*4eaf0b5cSAndrii Nakryiko }
84*4eaf0b5cSAndrii Nakryiko 
85*4eaf0b5cSAndrii Nakryiko static void setup_kretprobe()
86*4eaf0b5cSAndrii Nakryiko {
87*4eaf0b5cSAndrii Nakryiko 	setup_ctx();
88*4eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog2);
89*4eaf0b5cSAndrii Nakryiko }
90*4eaf0b5cSAndrii Nakryiko 
91*4eaf0b5cSAndrii Nakryiko static void setup_rawtp()
92*4eaf0b5cSAndrii Nakryiko {
93*4eaf0b5cSAndrii Nakryiko 	setup_ctx();
94*4eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog3);
95*4eaf0b5cSAndrii Nakryiko }
96*4eaf0b5cSAndrii Nakryiko 
97*4eaf0b5cSAndrii Nakryiko static void setup_fentry()
98*4eaf0b5cSAndrii Nakryiko {
99*4eaf0b5cSAndrii Nakryiko 	setup_ctx();
100*4eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog4);
101*4eaf0b5cSAndrii Nakryiko }
102*4eaf0b5cSAndrii Nakryiko 
103*4eaf0b5cSAndrii Nakryiko static void setup_fexit()
104*4eaf0b5cSAndrii Nakryiko {
105*4eaf0b5cSAndrii Nakryiko 	setup_ctx();
106*4eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog5);
107*4eaf0b5cSAndrii Nakryiko }
108*4eaf0b5cSAndrii Nakryiko 
109*4eaf0b5cSAndrii Nakryiko static void setup_fmodret()
110*4eaf0b5cSAndrii Nakryiko {
111*4eaf0b5cSAndrii Nakryiko 	setup_ctx();
112*4eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog6);
113*4eaf0b5cSAndrii Nakryiko }
114*4eaf0b5cSAndrii Nakryiko 
115*4eaf0b5cSAndrii Nakryiko static void *consumer(void *input)
116*4eaf0b5cSAndrii Nakryiko {
117*4eaf0b5cSAndrii Nakryiko 	return NULL;
118*4eaf0b5cSAndrii Nakryiko }
119*4eaf0b5cSAndrii Nakryiko 
120*4eaf0b5cSAndrii Nakryiko const struct bench bench_rename_base = {
121*4eaf0b5cSAndrii Nakryiko 	.name = "rename-base",
122*4eaf0b5cSAndrii Nakryiko 	.validate = validate,
123*4eaf0b5cSAndrii Nakryiko 	.setup = setup_base,
124*4eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
125*4eaf0b5cSAndrii Nakryiko 	.consumer_thread = consumer,
126*4eaf0b5cSAndrii Nakryiko 	.measure = measure,
127*4eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
128*4eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
129*4eaf0b5cSAndrii Nakryiko };
130*4eaf0b5cSAndrii Nakryiko 
131*4eaf0b5cSAndrii Nakryiko const struct bench bench_rename_kprobe = {
132*4eaf0b5cSAndrii Nakryiko 	.name = "rename-kprobe",
133*4eaf0b5cSAndrii Nakryiko 	.validate = validate,
134*4eaf0b5cSAndrii Nakryiko 	.setup = setup_kprobe,
135*4eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
136*4eaf0b5cSAndrii Nakryiko 	.consumer_thread = consumer,
137*4eaf0b5cSAndrii Nakryiko 	.measure = measure,
138*4eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
139*4eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
140*4eaf0b5cSAndrii Nakryiko };
141*4eaf0b5cSAndrii Nakryiko 
142*4eaf0b5cSAndrii Nakryiko const struct bench bench_rename_kretprobe = {
143*4eaf0b5cSAndrii Nakryiko 	.name = "rename-kretprobe",
144*4eaf0b5cSAndrii Nakryiko 	.validate = validate,
145*4eaf0b5cSAndrii Nakryiko 	.setup = setup_kretprobe,
146*4eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
147*4eaf0b5cSAndrii Nakryiko 	.consumer_thread = consumer,
148*4eaf0b5cSAndrii Nakryiko 	.measure = measure,
149*4eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
150*4eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
151*4eaf0b5cSAndrii Nakryiko };
152*4eaf0b5cSAndrii Nakryiko 
153*4eaf0b5cSAndrii Nakryiko const struct bench bench_rename_rawtp = {
154*4eaf0b5cSAndrii Nakryiko 	.name = "rename-rawtp",
155*4eaf0b5cSAndrii Nakryiko 	.validate = validate,
156*4eaf0b5cSAndrii Nakryiko 	.setup = setup_rawtp,
157*4eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
158*4eaf0b5cSAndrii Nakryiko 	.consumer_thread = consumer,
159*4eaf0b5cSAndrii Nakryiko 	.measure = measure,
160*4eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
161*4eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
162*4eaf0b5cSAndrii Nakryiko };
163*4eaf0b5cSAndrii Nakryiko 
164*4eaf0b5cSAndrii Nakryiko const struct bench bench_rename_fentry = {
165*4eaf0b5cSAndrii Nakryiko 	.name = "rename-fentry",
166*4eaf0b5cSAndrii Nakryiko 	.validate = validate,
167*4eaf0b5cSAndrii Nakryiko 	.setup = setup_fentry,
168*4eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
169*4eaf0b5cSAndrii Nakryiko 	.consumer_thread = consumer,
170*4eaf0b5cSAndrii Nakryiko 	.measure = measure,
171*4eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
172*4eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
173*4eaf0b5cSAndrii Nakryiko };
174*4eaf0b5cSAndrii Nakryiko 
175*4eaf0b5cSAndrii Nakryiko const struct bench bench_rename_fexit = {
176*4eaf0b5cSAndrii Nakryiko 	.name = "rename-fexit",
177*4eaf0b5cSAndrii Nakryiko 	.validate = validate,
178*4eaf0b5cSAndrii Nakryiko 	.setup = setup_fexit,
179*4eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
180*4eaf0b5cSAndrii Nakryiko 	.consumer_thread = consumer,
181*4eaf0b5cSAndrii Nakryiko 	.measure = measure,
182*4eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
183*4eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
184*4eaf0b5cSAndrii Nakryiko };
185*4eaf0b5cSAndrii Nakryiko 
186*4eaf0b5cSAndrii Nakryiko const struct bench bench_rename_fmodret = {
187*4eaf0b5cSAndrii Nakryiko 	.name = "rename-fmodret",
188*4eaf0b5cSAndrii Nakryiko 	.validate = validate,
189*4eaf0b5cSAndrii Nakryiko 	.setup = setup_fmodret,
190*4eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
191*4eaf0b5cSAndrii Nakryiko 	.consumer_thread = consumer,
192*4eaf0b5cSAndrii Nakryiko 	.measure = measure,
193*4eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
194*4eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
195*4eaf0b5cSAndrii Nakryiko };
196