forked from cloudflare/ebpf_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.bpf.h
103 lines (85 loc) · 2.55 KB
/
benchmark.bpf.h
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
#include "vmlinux.h"
#include <bpf/bpf_tracing.h>
#if defined(__TARGET_ARCH_x86)
#define FENTRY_SEC() SEC("fentry/__do_sys_getpid")
#elif defined(__TARGET_ARCH_arm64)
#define FENTRY_SEC() SEC("fentry/__arm64_sys_getpid")
#else
#error Unknown target for this architecture
#endif
#if defined(__TARGET_ARCH_x86)
#define KPROBE_SEC() SEC("kprobe/__do_sys_getpid")
#elif defined(__TARGET_ARCH_arm64)
#define KPROBE_SEC() SEC("kprobe/__arm64_sys_getpid")
#else
#error Unknown target for this architecture
#endif
#define TRACEPOINT_SEC() SEC("tp_btf/sys_enter")
#define UPROBE_SEC() SEC("uprobe//proc/self/exe:uprobe_target")
#define BENCHMARK_PROBE(sec, impl) \
sec() int probe() \
{ \
return impl(); \
}
static u64 zero = 0;
#ifdef BENCHMARK_NO_MAP
static inline int empty_probe()
{
return 0;
}
#endif
#ifdef BENCHMARK_SIMPLE_MAP
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, u32);
__type(value, u64);
} counts SEC(".maps");
static inline int simple_probe()
{
u32 key = bpf_get_current_pid_tgid();
u64 *count;
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
return 0;
}
}
__sync_fetch_and_add(count, 1);
return 0;
}
#endif
#ifdef BENCHMARK_COMPLEX_MAP
struct key_t {
u64 pid;
u64 random;
char command[32];
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, struct key_t);
__type(value, u64);
} counts SEC(".maps");
static inline int complex_probe()
{
u64 *count;
struct key_t key = {};
key.pid = bpf_get_current_pid_tgid();
key.random = bpf_ktime_get_ns() % 1024;
bpf_get_current_comm(&key.command, sizeof(key.command));
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
return 0;
}
}
__sync_fetch_and_add(count, 1);
return 0;
}
#endif
char LICENSE[] SEC("license") = "GPL";