-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
biosnoop.py
executable file
·438 lines (370 loc) · 10.8 KB
/
biosnoop.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/usr/bin/env python
# @lint-avoid-python-3-compatibility-imports
#
# biosnoop Trace block device I/O and print details including issuing PID.
# For Linux, uses BCC, eBPF.
#
# This uses in-kernel eBPF maps to cache process details (PID and comm) by I/O
# request, as well as a starting timestamp for calculating I/O latency.
#
# Copyright (c) 2015 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 16-Sep-2015 Brendan Gregg Created this.
# 11-Feb-2016 Allan McAleavy updated for BPF_PERF_OUTPUT
# 21-Jun-2022 Rocky Xing Added disk filter support.
# 13-Oct-2022 Rocky Xing Added support for displaying block I/O pattern.
# 01-Aug-2023 Jerome Marchand Added support for block tracepoints
from __future__ import print_function
from bcc import BPF
import argparse
import os
# arguments
examples = """examples:
./biosnoop # trace all block I/O
./biosnoop -Q # include OS queued time
./biosnoop -d sdc # trace sdc only
./biosnoop -P # display block I/O pattern
"""
parser = argparse.ArgumentParser(
description="Trace block I/O",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=examples)
parser.add_argument("-Q", "--queue", action="store_true",
help="include OS queued time")
parser.add_argument("-d", "--disk", type=str,
help="trace this disk only")
parser.add_argument("-P", "--pattern", action="store_true",
help="display block I/O pattern (sequential or random)")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
debug = 0
# define BPF program
bpf_text = """
#include <uapi/linux/ptrace.h>
#include <linux/blk-mq.h>
"""
if args.pattern:
bpf_text += "#define INCLUDE_PATTERN\n"
bpf_text += """
// for saving the timestamp and __data_len of each request
struct start_req_t {
u64 ts;
u64 data_len;
};
struct val_t {
u64 ts;
u32 pid;
char name[TASK_COMM_LEN];
};
struct tp_args {
u64 __unused__;
dev_t dev;
sector_t sector;
unsigned int nr_sector;
unsigned int bytes;
char rwbs[8];
char comm[16];
char cmd[];
};
struct hash_key {
dev_t dev;
u32 rwflag;
sector_t sector;
};
#ifdef INCLUDE_PATTERN
struct sector_key_t {
u32 dev_major;
u32 dev_minor;
};
enum bio_pattern {
UNKNOWN,
SEQUENTIAL,
RANDOM,
};
#endif
struct data_t {
u32 pid;
u32 dev;
u64 rwflag;
u64 delta;
u64 qdelta;
u64 sector;
u64 len;
#ifdef INCLUDE_PATTERN
enum bio_pattern pattern;
#endif
u64 ts;
char name[TASK_COMM_LEN];
};
#ifdef INCLUDE_PATTERN
BPF_HASH(last_sectors, struct sector_key_t, u64);
#endif
BPF_HASH(start, struct hash_key, struct start_req_t);
BPF_HASH(infobyreq, struct hash_key, struct val_t);
BPF_PERF_OUTPUT(events);
static dev_t ddevt(struct gendisk *disk) {
return (disk->major << 20) | disk->first_minor;
}
/*
* The following deals with a kernel version change (in mainline 4.7, although
* it may be backported to earlier kernels) with how block request write flags
* are tested. We handle both pre- and post-change versions here. Please avoid
* kernel version tests like this as much as possible: they inflate the code,
* test, and maintenance burden.
*/
static int get_rwflag(u32 cmd_flags) {
#ifdef REQ_WRITE
return !!(cmd_flags & REQ_WRITE);
#elif defined(REQ_OP_SHIFT)
return !!((cmd_flags >> REQ_OP_SHIFT) == REQ_OP_WRITE);
#else
return !!((cmd_flags & REQ_OP_MASK) == REQ_OP_WRITE);
#endif
}
#define RWBS_LEN 8
static int get_rwflag_tp(char *rwbs) {
for (int i = 0; i < RWBS_LEN; i++) {
if (rwbs[i] == 'W')
return 1;
if (rwbs[i] == '\\0')
return 0;
}
return 0;
}
// cache PID and comm by-req
static int __trace_pid_start(struct hash_key key)
{
DISK_FILTER
struct val_t val = {};
u64 ts;
if (bpf_get_current_comm(&val.name, sizeof(val.name)) == 0) {
val.pid = bpf_get_current_pid_tgid() >> 32;
if (##QUEUE##) {
val.ts = bpf_ktime_get_ns();
}
infobyreq.update(&key, &val);
}
return 0;
}
int trace_pid_start(struct pt_regs *ctx, struct request *req)
{
struct hash_key key = {
.dev = ddevt(req->__RQ_DISK__),
.rwflag = get_rwflag(req->cmd_flags),
.sector = req->__sector
};
return __trace_pid_start(key);
}
int trace_pid_start_tp(struct tp_args *args)
{
struct hash_key key = {
.dev = args->dev,
.rwflag = get_rwflag_tp(args->rwbs),
.sector = args->sector
};
return __trace_pid_start(key);
}
// time block I/O
int trace_req_start(struct pt_regs *ctx, struct request *req)
{
struct hash_key key = {
.dev = ddevt(req->__RQ_DISK__),
.rwflag = get_rwflag(req->cmd_flags),
.sector = req->__sector
};
DISK_FILTER
struct start_req_t start_req = {
.ts = bpf_ktime_get_ns(),
.data_len = req->__data_len
};
start.update(&key, &start_req);
return 0;
}
// output
static int __trace_req_completion(void *ctx, struct hash_key key)
{
struct start_req_t *startp;
struct val_t *valp;
struct data_t data = {};
//struct gendisk *rq_disk;
u64 ts;
// fetch timestamp and calculate delta
startp = start.lookup(&key);
if (startp == 0) {
// missed tracing issue
return 0;
}
ts = bpf_ktime_get_ns();
//rq_disk = req->__RQ_DISK__;
data.delta = ts - startp->ts;
data.ts = ts / 1000;
data.qdelta = 0;
data.len = startp->data_len;
valp = infobyreq.lookup(&key);
if (valp == 0) {
data.name[0] = '?';
data.name[1] = 0;
} else {
if (##QUEUE##) {
data.qdelta = startp->ts - valp->ts;
}
data.pid = valp->pid;
data.sector = key.sector;
data.dev = key.dev;
bpf_probe_read_kernel(&data.name, sizeof(data.name), valp->name);
}
#ifdef INCLUDE_PATTERN
data.pattern = UNKNOWN;
u64 *sector, last_sector;
struct sector_key_t sector_key = {
.dev_major = key.dev >> 20,
.dev_minor = key.dev & ((1 << 20) - 1)
};
sector = last_sectors.lookup(§or_key);
if (sector != 0) {
data.pattern = req->__sector == *sector ? SEQUENTIAL : RANDOM;
}
last_sector = req->__sector + data.len / 512;
last_sectors.update(§or_key, &last_sector);
#endif
data.rwflag = key.rwflag;
events.perf_submit(ctx, &data, sizeof(data));
start.delete(&key);
infobyreq.delete(&key);
return 0;
}
int trace_req_completion(struct pt_regs *ctx, struct request *req)
{
struct hash_key key = {
.dev = ddevt(req->__RQ_DISK__),
.rwflag = get_rwflag(req->cmd_flags),
.sector = req->__sector
};
return __trace_req_completion(ctx, key);
}
int trace_req_completion_tp(struct tp_args *args)
{
struct hash_key key = {
.dev = args->dev,
.rwflag = get_rwflag_tp(args->rwbs),
.sector = args->sector
};
return __trace_req_completion(args, key);
}
"""
if args.queue:
bpf_text = bpf_text.replace('##QUEUE##', '1')
else:
bpf_text = bpf_text.replace('##QUEUE##', '0')
if BPF.kernel_struct_has_field(b'request', b'rq_disk') == 1:
bpf_text = bpf_text.replace('__RQ_DISK__', 'rq_disk')
else:
bpf_text = bpf_text.replace('__RQ_DISK__', 'q->disk')
if args.disk is not None:
disk_path = os.path.join('/dev', args.disk)
if not os.path.exists(disk_path):
print("no such disk '%s'" % args.disk)
exit(1)
stat_info = os.stat(disk_path)
dev = os.major(stat_info.st_rdev) << 20 | os.minor(stat_info.st_rdev)
disk_filter_str = """
if(key.dev != %s) {
return 0;
}
""" % (dev)
bpf_text = bpf_text.replace('DISK_FILTER', disk_filter_str)
else:
bpf_text = bpf_text.replace('DISK_FILTER', '')
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()
# initialize BPF
b = BPF(text=bpf_text)
if BPF.tracepoint_exists("block", "block_io_start"):
b.attach_tracepoint(tp="block:block_io_start", fn_name="trace_pid_start_tp")
elif BPF.get_kprobe_functions(b'__blk_account_io_start'):
b.attach_kprobe(event="__blk_account_io_start", fn_name="trace_pid_start")
elif BPF.get_kprobe_functions(b'blk_account_io_start'):
b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start")
else:
print("ERROR: No found any block io start probe/tp.")
exit()
if BPF.get_kprobe_functions(b'blk_start_request'):
b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start")
b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start")
if BPF.tracepoint_exists("block", "block_io_done"):
b.attach_tracepoint(tp="block:block_io_done", fn_name="trace_req_completion_tp")
elif BPF.get_kprobe_functions(b'__blk_account_io_done'):
b.attach_kprobe(event="__blk_account_io_done", fn_name="trace_req_completion")
elif BPF.get_kprobe_functions(b'blk_account_io_done'):
b.attach_kprobe(event="blk_account_io_done", fn_name="trace_req_completion")
else:
print("ERROR: No found any block io done probe/tp.")
exit()
# header
print("%-11s %-14s %-7s %-9s %-1s %-10s %-7s" % ("TIME(s)", "COMM", "PID",
"DISK", "T", "SECTOR", "BYTES"), end="")
if args.pattern:
print("%-1s " % ("P"), end="")
if args.queue:
print("%7s " % ("QUE(ms)"), end="")
print("%7s" % "LAT(ms)")
# cache disk major,minor -> diskname
diskstats = "/proc/diskstats"
disklookup = {}
with open(diskstats) as stats:
for line in stats:
a = line.split()
disklookup[a[0] + "," + a[1]] = a[2]
def disk_print(d):
major = d >> 20
minor = d & ((1 << 20) - 1)
disk = str(major) + "," + str(minor)
if disk in disklookup:
diskname = disklookup[disk]
else:
diskname = "<unknown>"
return diskname
rwflg = ""
pattern = ""
start_ts = 0
prev_ts = 0
delta = 0
P_SEQUENTIAL = 1
P_RANDOM = 2
# process event
def print_event(cpu, data, size):
event = b["events"].event(data)
global start_ts
if start_ts == 0:
start_ts = event.ts
if event.rwflag == 1:
rwflg = "W"
else:
rwflg = "R"
delta = float(event.ts) - start_ts
disk_name = disk_print(event.dev)
print("%-11.6f %-14.14s %-7s %-9s %-1s %-10s %-7s" % (
delta / 1000000, event.name.decode('utf-8', 'replace'), event.pid,
disk_name, rwflg, event.sector, event.len), end="")
if args.pattern:
if event.pattern == P_SEQUENTIAL:
pattern = "S"
elif event.pattern == P_RANDOM:
pattern = "R"
else:
pattern = "?"
print("%-1s " % pattern, end="")
if args.queue:
print("%7.2f " % (float(event.qdelta) / 1000000), end="")
print("%7.2f" % (float(event.delta) / 1000000))
# loop with callback to print_event
b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1:
try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()