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