-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathext4dist.bpf.c
129 lines (102 loc) · 2.28 KB
/
ext4dist.bpf.c
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
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "maps.bpf.h"
#define MAX_ENTRIES 10240
// 27 buckets for latency, max range is 33.6s .. 67.1s
#define MAX_LATENCY_SLOT 27
enum fs_file_op {
F_READ,
F_WRITE,
F_OPEN,
F_FSYNC,
F_GETATTR,
F_MAX
};
struct ext4_latency_key_t {
u8 op;
u8 bucket;
};
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, MAX_ENTRIES);
__type(key, u32);
__type(value, u64);
} start SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, (MAX_LATENCY_SLOT + 1) * F_MAX);
__type(key, struct ext4_latency_key_t);
__type(value, u64);
} ext4_latency_seconds SEC(".maps");
static int probe_entry()
{
u64 ts = bpf_ktime_get_ns();
u32 pid = bpf_get_current_pid_tgid();
bpf_map_update_elem(&start, &pid, &ts, BPF_ANY);
return 0;
}
static int probe_return(enum fs_file_op op)
{
u64 *tsp, delta_us, ts = bpf_ktime_get_ns();
u32 pid = bpf_get_current_pid_tgid();
struct ext4_latency_key_t key = { .op = (u8) op };
tsp = bpf_map_lookup_elem(&start, &pid);
if (!tsp) {
return 0;
}
delta_us = (ts - *tsp) / 1000;
increment_exp2_histogram(&ext4_latency_seconds, key, delta_us, MAX_LATENCY_SLOT);
bpf_map_delete_elem(&start, &pid);
return 0;
}
SEC("kprobe/ext4_file_read_iter")
int ext4_file_read_enter()
{
return probe_entry();
}
SEC("kretprobe/ext4_file_read_iter")
int ext4_file_read_exit()
{
return probe_return(F_READ);
}
SEC("kprobe/ext4_file_write_iter")
int ext4_file_write_enter()
{
return probe_entry();
}
SEC("kretprobe/ext4_file_write_iter")
int ext4_file_write_exit()
{
return probe_return(F_WRITE);
}
SEC("kprobe/ext4_file_open")
int ext4_file_open_enter()
{
return probe_entry();
}
SEC("kretprobe/ext4_file_open")
int ext4_file_open_exit()
{
return probe_return(F_OPEN);
}
SEC("kprobe/ext4_sync_file")
int ext4_file_sync_enter()
{
return probe_entry();
}
SEC("kretprobe/ext4_sync_file")
int ext4_file_sync_exit()
{
return probe_return(F_FSYNC);
}
SEC("kprobe/ext4_file_getattr")
int ext4_file_getattr_enter()
{
return probe_entry();
}
SEC("kretprobe/ext4_file_getattr")
int ext4_file_getattr_exit()
{
return probe_return(F_GETATTR);
}
char LICENSE[] SEC("license") = "GPL";