forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
biotop.c
452 lines (383 loc) · 9.5 KB
/
biotop.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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/*
* biotop Trace block I/O by process.
* Copyright (c) 2022 Francis Laniel <flaniel@linux.microsoft.com>
*
* Based on biotop(8) from BCC by Brendan Gregg.
* 03-Mar-2022 Francis Laniel Created this.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <argp.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include "biotop.h"
#include "biotop.skel.h"
#include "compat.h"
#include "trace_helpers.h"
#define warn(...) fprintf(stderr, __VA_ARGS__)
#define OUTPUT_ROWS_LIMIT 10240
enum SORT {
ALL,
IO,
BYTES,
TIME,
};
struct disk {
int major;
int minor;
char name[256];
};
struct vector {
size_t nr;
size_t capacity;
void **elems;
};
int grow_vector(struct vector *vector) {
if (vector->nr >= vector->capacity) {
void **reallocated;
if (!vector->capacity)
vector->capacity = 1;
else
vector->capacity *= 2;
reallocated = libbpf_reallocarray(vector->elems, vector->capacity, sizeof(*vector->elems));
if (!reallocated)
return -1;
vector->elems = reallocated;
}
return 0;
}
void free_vector(struct vector vector) {
for (size_t i = 0; i < vector.nr; i++)
if (vector.elems[i] != NULL)
free(vector.elems[i]);
free(vector.elems);
}
struct vector disks = {};
static volatile sig_atomic_t exiting = 0;
static bool clear_screen = true;
static int output_rows = 20;
static int sort_by = ALL;
static int interval = 1;
static int count = 99999999;
static bool verbose = false;
const char *argp_program_version = "biotop 0.1";
const char *argp_program_bug_address =
"https://github.com/iovisor/bcc/tree/master/libbpf-tools";
const char argp_program_doc[] =
"Trace file reads/writes by process.\n"
"\n"
"USAGE: biotop [-h] [interval] [count]\n"
"\n"
"EXAMPLES:\n"
" biotop # file I/O top, refresh every 1s\n"
" biotop 5 10 # 5s summaries, 10 times\n";
static const struct argp_option opts[] = {
{ "noclear", 'C', NULL, 0, "Don't clear the screen" },
{ "sort", 's', "SORT", 0, "Sort columns, default all [all, io, bytes, time]" },
{ "rows", 'r', "ROWS", 0, "Maximum rows to print, default 20" },
{ "verbose", 'v', NULL, 0, "Verbose debug output" },
{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
{},
};
static error_t parse_arg(int key, char *arg, struct argp_state *state)
{
long rows;
static int pos_args;
switch (key) {
case 'C':
clear_screen = false;
break;
case 's':
if (!strcmp(arg, "all")) {
sort_by = ALL;
} else if (!strcmp(arg, "io")) {
sort_by = IO;
} else if (!strcmp(arg, "bytes")) {
sort_by = BYTES;
} else if (!strcmp(arg, "time")) {
sort_by = TIME;
} else {
warn("invalid sort method: %s\n", arg);
argp_usage(state);
}
break;
case 'r':
errno = 0;
rows = strtol(arg, NULL, 10);
if (errno || rows <= 0) {
warn("invalid rows: %s\n", arg);
argp_usage(state);
}
output_rows = rows;
if (output_rows > OUTPUT_ROWS_LIMIT)
output_rows = OUTPUT_ROWS_LIMIT;
break;
case 'v':
verbose = true;
break;
case 'h':
argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
break;
case ARGP_KEY_ARG:
errno = 0;
if (pos_args == 0) {
interval = strtol(arg, NULL, 10);
if (errno || interval <= 0) {
warn("invalid interval\n");
argp_usage(state);
}
} else if (pos_args == 1) {
count = strtol(arg, NULL, 10);
if (errno || count <= 0) {
warn("invalid count\n");
argp_usage(state);
}
} else {
warn("unrecognized positional argument: %s\n", arg);
argp_usage(state);
}
pos_args++;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
{
if (level == LIBBPF_DEBUG && !verbose)
return 0;
return vfprintf(stderr, format, args);
}
static void sig_int(int signo)
{
exiting = 1;
}
struct data_t {
struct info_t key;
struct val_t value;
};
static int sort_column(const void *obj1, const void *obj2)
{
struct data_t *d1 = (struct data_t *) obj1;
struct data_t *d2 = (struct data_t *) obj2;
struct val_t *s1 = &d1->value;
struct val_t *s2 = &d2->value;
if (sort_by == IO)
return s2->io - s1->io;
else if (sort_by == BYTES)
return s2->bytes - s1->bytes;
else if (sort_by == TIME)
return s2->us - s1->us;
else
return (s2->io + s2->bytes + s2->us)
- (s1->io + s1->bytes + s1->us);
}
static void parse_disk_stat(void)
{
FILE *fp;
char *line;
size_t zero;
fp = fopen("/proc/diskstats", "r");
if (!fp)
return;
zero = 0;
while (getline(&line, &zero, fp) != -1) {
struct disk disk;
if (sscanf(line, "%d %d %s", &disk.major, &disk.minor, disk.name) != 3)
continue;
if (grow_vector(&disks) == -1)
goto err;
disks.elems[disks.nr] = malloc(sizeof(disk));
if (!disks.elems[disks.nr])
goto err;
memcpy(disks.elems[disks.nr], &disk, sizeof(disk));
disks.nr++;
}
free(line);
fclose(fp);
return;
err:
fprintf(stderr, "realloc or malloc failed\n");
free_vector(disks);
}
static char *search_disk_name(int major, int minor)
{
for (size_t i = 0; i < disks.nr; i++) {
struct disk *diskp;
if (!disks.elems[i])
continue;
diskp = (struct disk *) disks.elems[i];
if (diskp->major == major && diskp->minor == minor)
return diskp->name;
}
return "";
}
static int print_stat(struct biotop_bpf *obj)
{
FILE *f;
time_t t;
struct tm *tm;
char ts[16], buf[256];
struct info_t *prev_key = NULL;
static struct data_t datas[OUTPUT_ROWS_LIMIT];
int n, i, err = 0, rows = 0;
int fd = bpf_map__fd(obj->maps.counts);
f = fopen("/proc/loadavg", "r");
if (f) {
time(&t);
tm = localtime(&t);
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
memset(buf, 0, sizeof(buf));
n = fread(buf, 1, sizeof(buf), f);
if (n)
printf("%8s loadavg: %s\n", ts, buf);
fclose(f);
}
printf("%-7s %-16s %1s %-3s %-3s %-8s %5s %7s %6s\n",
"PID", "COMM", "D", "MAJ", "MIN", "DISK", "I/O", "Kbytes", "AVGms");
while (1) {
err = bpf_map_get_next_key(fd, prev_key, &datas[rows].key);
if (err) {
if (errno == ENOENT) {
err = 0;
break;
}
warn("bpf_map_get_next_key failed: %s\n", strerror(errno));
return err;
}
err = bpf_map_lookup_elem(fd, &datas[rows].key, &datas[rows].value);
if (err) {
warn("bpf_map_lookup_elem failed: %s\n", strerror(errno));
return err;
}
prev_key = &datas[rows].key;
rows++;
}
qsort(datas, rows, sizeof(struct data_t), sort_column);
rows = rows < output_rows ? rows : output_rows;
for (i = 0; i < rows; i++) {
int major;
int minor;
struct info_t *key = &datas[i].key;
struct val_t *value = &datas[i].value;
float avg_ms = 0;
/* To avoid floating point exception. */
if (value->io)
avg_ms = ((float) value->us) / 1000 / value->io;
major = key->major;
minor = key->minor;
printf("%-7d %-16s %1s %-3d %-3d %-8s %5d %7lld %6.2f\n",
key->pid, key->name, key->rwflag ? "W": "R",
major, minor, search_disk_name(major, minor),
value->io, value->bytes / 1024, avg_ms);
}
printf("\n");
prev_key = NULL;
while (1) {
struct info_t key;
err = bpf_map_get_next_key(fd, prev_key, &key);
if (err) {
if (errno == ENOENT) {
err = 0;
break;
}
warn("bpf_map_get_next_key failed: %s\n", strerror(errno));
return err;
}
err = bpf_map_delete_elem(fd, &key);
if (err) {
warn("bpf_map_delete_elem failed: %s\n", strerror(errno));
return err;
}
prev_key = &key;
}
return err;
}
int main(int argc, char **argv)
{
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
.doc = argp_program_doc,
};
struct biotop_bpf *obj;
struct ksyms *ksyms;
int err;
err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
if (err)
return err;
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn);
obj = biotop_bpf__open();
if (!obj) {
warn("failed to open BPF object\n");
return 1;
}
parse_disk_stat();
ksyms = ksyms__load();
if (!ksyms) {
err = -ENOMEM;
warn("failed to load kallsyms\n");
goto cleanup;
}
err = biotop_bpf__load(obj);
if (err) {
warn("failed to load BPF object: %d\n", err);
goto cleanup;
}
if (ksyms__get_symbol(ksyms, "__blk_account_io_start"))
obj->links.blk_account_io_start = bpf_program__attach_kprobe(obj->progs.blk_account_io_start, false, "__blk_account_io_start");
else
obj->links.blk_account_io_start = bpf_program__attach_kprobe(obj->progs.blk_account_io_start, false, "blk_account_io_start");
if (!obj->links.blk_account_io_start) {
warn("failed to load attach blk_account_io_start\n");
goto cleanup;
}
if (ksyms__get_symbol(ksyms, "__blk_account_io_done"))
obj->links.blk_account_io_done = bpf_program__attach_kprobe(obj->progs.blk_account_io_done, false, "__blk_account_io_done");
else
obj->links.blk_account_io_done = bpf_program__attach_kprobe(obj->progs.blk_account_io_done, false, "blk_account_io_done");
if (!obj->links.blk_account_io_done) {
warn("failed to load attach blk_account_io_done\n");
goto cleanup;
}
err = biotop_bpf__attach(obj);
if (err) {
warn("failed to attach BPF programs: %d\n", err);
goto cleanup;
}
if (signal(SIGINT, sig_int) == SIG_ERR) {
warn("can't set signal handler: %s\n", strerror(errno));
err = 1;
goto cleanup;
}
while (1) {
sleep(interval);
if (clear_screen) {
err = system("clear");
if (err)
goto cleanup;
}
err = print_stat(obj);
if (err)
goto cleanup;
count--;
if (exiting || !count)
goto cleanup;
}
cleanup:
ksyms__free(ksyms);
free_vector(disks);
biotop_bpf__destroy(obj);
return err != 0;
}