forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsslower.bpf.c
208 lines (172 loc) · 4.55 KB
/
fsslower.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2020 Wenbo Zhang */
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>
#include "bits.bpf.h"
#include "fsslower.h"
#define MAX_ENTRIES 8192
const volatile pid_t target_pid = 0;
const volatile __u64 min_lat_ns = 0;
struct data {
__u64 ts;
loff_t start;
loff_t end;
struct file *fp;
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, MAX_ENTRIES);
__type(key, __u32);
__type(value, struct data);
} starts SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(__u32));
} events SEC(".maps");
static int probe_entry(struct file *fp, loff_t start, loff_t end)
{
__u64 pid_tgid = bpf_get_current_pid_tgid();
__u32 pid = pid_tgid >> 32;
__u32 tid = (__u32)pid_tgid;
struct data data;
if (!fp)
return 0;
if (target_pid && target_pid != pid)
return 0;
data.ts = bpf_ktime_get_ns();
data.start = start;
data.end = end;
data.fp = fp;
bpf_map_update_elem(&starts, &tid, &data, BPF_ANY);
return 0;
}
static int probe_exit(void *ctx, enum fs_file_op op, ssize_t size)
{
__u64 pid_tgid = bpf_get_current_pid_tgid();
__u32 pid = pid_tgid >> 32;
__u32 tid = (__u32)pid_tgid;
__u64 end_ns, delta_ns;
const __u8 *file_name;
struct data *datap;
struct event event = {};
struct dentry *dentry;
struct file *fp;
if (target_pid && target_pid != pid)
return 0;
datap = bpf_map_lookup_elem(&starts, &tid);
if (!datap)
return 0;
bpf_map_delete_elem(&starts, &tid);
end_ns = bpf_ktime_get_ns();
delta_ns = end_ns - datap->ts;
if (delta_ns <= min_lat_ns)
return 0;
event.delta_us = delta_ns / 1000;
event.end_ns = end_ns;
event.offset = datap->start;
if (op != F_FSYNC)
event.size = size;
else
event.size = datap->end - datap->start;
event.pid = pid;
event.op = op;
fp = datap->fp;
dentry = BPF_CORE_READ(fp, f_path.dentry);
file_name = BPF_CORE_READ(dentry, d_name.name);
bpf_probe_read_kernel_str(&event.file, sizeof(event.file), file_name);
bpf_get_current_comm(&event.task, sizeof(event.task));
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
return 0;
}
SEC("kprobe/dummy_file_read")
int BPF_KPROBE(file_read_entry, struct kiocb *iocb)
{
struct file *fp = BPF_CORE_READ(iocb, ki_filp);
loff_t start = BPF_CORE_READ(iocb, ki_pos);
return probe_entry(fp, start, 0);
}
SEC("kretprobe/dummy_file_read")
int BPF_KRETPROBE(file_read_exit, ssize_t ret)
{
return probe_exit(ctx, F_READ, ret);
}
SEC("kprobe/dummy_file_write")
int BPF_KPROBE(file_write_entry, struct kiocb *iocb)
{
struct file *fp = BPF_CORE_READ(iocb, ki_filp);
loff_t start = BPF_CORE_READ(iocb, ki_pos);
return probe_entry(fp, start, 0);
}
SEC("kretprobe/dummy_file_write")
int BPF_KRETPROBE(file_write_exit, ssize_t ret)
{
return probe_exit(ctx, F_WRITE, ret);
}
SEC("kprobe/dummy_file_open")
int BPF_KPROBE(file_open_entry, struct inode *inode, struct file *file)
{
return probe_entry(file, 0, 0);
}
SEC("kretprobe/dummy_file_open")
int BPF_KRETPROBE(file_open_exit)
{
return probe_exit(ctx, F_OPEN, 0);
}
SEC("kprobe/dummy_file_sync")
int BPF_KPROBE(file_sync_entry, struct file *file, loff_t start, loff_t end)
{
return probe_entry(file, start, end);
}
SEC("kretprobe/dummy_file_sync")
int BPF_KRETPROBE(file_sync_exit)
{
return probe_exit(ctx, F_FSYNC, 0);
}
SEC("fentry/dummy_file_read")
int BPF_PROG(file_read_fentry, struct kiocb *iocb)
{
struct file *fp = iocb->ki_filp;
loff_t start = iocb->ki_pos;
return probe_entry(fp, start, 0);
}
SEC("fexit/dummy_file_read")
int BPF_PROG(file_read_fexit, struct kiocb *iocb, struct iov_iter *to, ssize_t ret)
{
return probe_exit(ctx, F_READ, ret);
}
SEC("fentry/dummy_file_write")
int BPF_PROG(file_write_fentry, struct kiocb *iocb)
{
struct file *fp = iocb->ki_filp;
loff_t start = iocb->ki_pos;
return probe_entry(fp, start, 0);
}
SEC("fexit/dummy_file_write")
int BPF_PROG(file_write_fexit, struct kiocb *iocb, struct iov_iter *from, ssize_t ret)
{
return probe_exit(ctx, F_WRITE, ret);
}
SEC("fentry/dummy_file_open")
int BPF_PROG(file_open_fentry, struct inode *inode, struct file *file)
{
return probe_entry(file, 0, 0);
}
SEC("fexit/dummy_file_open")
int BPF_PROG(file_open_fexit)
{
return probe_exit(ctx, F_OPEN, 0);
}
SEC("fentry/dummy_file_sync")
int BPF_PROG(file_sync_fentry, struct file *file, loff_t start, loff_t end)
{
return probe_entry(file, start, end);
}
SEC("fexit/dummy_file_sync")
int BPF_PROG(file_sync_fexit)
{
return probe_exit(ctx, F_FSYNC, 0);
}
char LICENSE[] SEC("license") = "GPL";