summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/benchs/bench_rename.c
blob: 3c203b6d6a6ecb6589c876ae26a936ff068f1eeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#include <fcntl.h>
#include "bench.h"
#include "test_overhead.skel.h"

/* BPF triggering benchmarks */
static struct ctx {
	struct test_overhead *skel;
	struct counter hits;
	int fd;
} ctx;

static void validate(void)
{
	if (env.producer_cnt != 1) {
		fprintf(stderr, "benchmark doesn't support multi-producer!\n");
		exit(1);
	}
	if (env.consumer_cnt != 1) {
		fprintf(stderr, "benchmark doesn't support multi-consumer!\n");
		exit(1);
	}
}

static void *producer(void *input)
{
	char buf[] = "test_overhead";
	int err;

	while (true) {
		err = write(ctx.fd, buf, sizeof(buf));
		if (err < 0) {
			fprintf(stderr, "write failed\n");
			exit(1);
		}
		atomic_inc(&ctx.hits.value);
	}
}

static void measure(struct bench_res *res)
{
	res->hits = atomic_swap(&ctx.hits.value, 0);
}

static void setup_ctx(void)
{
	setup_libbpf();

	ctx.skel = test_overhead__open_and_load();
	if (!ctx.skel) {
		fprintf(stderr, "failed to open skeleton\n");
		exit(1);
	}

	ctx.fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
	if (ctx.fd < 0) {
		fprintf(stderr, "failed to open /proc/self/comm: %d\n", -errno);
		exit(1);
	}
}

static void attach_bpf(struct bpf_program *prog)
{
	struct bpf_link *link;

	link = bpf_program__attach(prog);
	if (!link) {
		fprintf(stderr, "failed to attach program!\n");
		exit(1);
	}
}

static void setup_base(void)
{
	setup_ctx();
}

static void setup_kprobe(void)
{
	setup_ctx();
	attach_bpf(ctx.skel->progs.prog1);
}

static void setup_kretprobe(void)
{
	setup_ctx();
	attach_bpf(ctx.skel->progs.prog2);
}

static void setup_rawtp(void)
{
	setup_ctx();
	attach_bpf(ctx.skel->progs.prog3);
}

static void setup_fentry(void)
{
	setup_ctx();
	attach_bpf(ctx.skel->progs.prog4);
}

static void setup_fexit(void)
{
	setup_ctx();
	attach_bpf(ctx.skel->progs.prog5);
}

static void *consumer(void *input)
{
	return NULL;
}

const struct bench bench_rename_base = {
	.name = "rename-base",
	.validate = validate,
	.setup = setup_base,
	.producer_thread = producer,
	.consumer_thread = consumer,
	.measure = measure,
	.report_progress = hits_drops_report_progress,
	.report_final = hits_drops_report_final,
};

const struct bench bench_rename_kprobe = {
	.name = "rename-kprobe",
	.validate = validate,
	.setup = setup_kprobe,
	.producer_thread = producer,
	.consumer_thread = consumer,
	.measure = measure,
	.report_progress = hits_drops_report_progress,
	.report_final = hits_drops_report_final,
};

const struct bench bench_rename_kretprobe = {
	.name = "rename-kretprobe",
	.validate = validate,
	.setup = setup_kretprobe,
	.producer_thread = producer,
	.consumer_thread = consumer,
	.measure = measure,
	.report_progress = hits_drops_report_progress,
	.report_final = hits_drops_report_final,
};

const struct bench bench_rename_rawtp = {
	.name = "rename-rawtp",
	.validate = validate,
	.setup = setup_rawtp,
	.producer_thread = producer,
	.consumer_thread = consumer,
	.measure = measure,
	.report_progress = hits_drops_report_progress,
	.report_final = hits_drops_report_final,
};

const struct bench bench_rename_fentry = {
	.name = "rename-fentry",
	.validate = validate,
	.setup = setup_fentry,
	.producer_thread = producer,
	.consumer_thread = consumer,
	.measure = measure,
	.report_progress = hits_drops_report_progress,
	.report_final = hits_drops_report_final,
};

const struct bench bench_rename_fexit = {
	.name = "rename-fexit",
	.validate = validate,
	.setup = setup_fexit,
	.producer_thread = producer,
	.consumer_thread = consumer,
	.measure = measure,
	.report_progress = hits_drops_report_progress,
	.report_final = hits_drops_report_final,
};