xref: /linux/tools/testing/selftests/bpf/progs/ringbuf_bench.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
3 
4 #include <linux/bpf.h>
5 #include <stdint.h>
6 #include <bpf/bpf_helpers.h>
7 #include "bpf_misc.h"
8 
9 char _license[] SEC("license") = "GPL";
10 
11 struct {
12 	__uint(type, BPF_MAP_TYPE_RINGBUF);
13 } ringbuf SEC(".maps");
14 
15 const volatile int batch_cnt = 0;
16 const volatile long use_output = 0;
17 
18 long sample_val = 42;
19 long dropped __attribute__((aligned(128))) = 0;
20 
21 const volatile long wakeup_data_size = 0;
22 
23 static __always_inline long get_flags()
24 {
25 	long sz;
26 
27 	if (!wakeup_data_size)
28 		return 0;
29 
30 	sz = bpf_ringbuf_query(&ringbuf, BPF_RB_AVAIL_DATA);
31 	return sz >= wakeup_data_size ? BPF_RB_FORCE_WAKEUP : BPF_RB_NO_WAKEUP;
32 }
33 
34 SEC("fentry/" SYS_PREFIX "sys_getpgid")
35 int bench_ringbuf(void *ctx)
36 {
37 	long *sample, flags;
38 	int i;
39 
40 	if (!use_output) {
41 		for (i = 0; i < batch_cnt; i++) {
42 			sample = bpf_ringbuf_reserve(&ringbuf,
43 					             sizeof(sample_val), 0);
44 			if (!sample) {
45 				__sync_add_and_fetch(&dropped, 1);
46 			} else {
47 				*sample = sample_val;
48 				flags = get_flags();
49 				bpf_ringbuf_submit(sample, flags);
50 			}
51 		}
52 	} else {
53 		for (i = 0; i < batch_cnt; i++) {
54 			flags = get_flags();
55 			if (bpf_ringbuf_output(&ringbuf, &sample_val,
56 					       sizeof(sample_val), flags))
57 				__sync_add_and_fetch(&dropped, 1);
58 		}
59 	}
60 	return 0;
61 }
62