1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2
3 /*
4 * fsdist Summarize file system operations latency.
5 *
6 * Copyright (c) 2021 Wenbo Zhang
7 * Copyright (c) 2021 Hengqi Chen
8 *
9 * Based on ext4dist(8) from BCC by Brendan Gregg.
10 * 9-Feb-2021 Wenbo Zhang Created this.
11 * 20-May-2021 Hengqi Chen Migrated to fsdist.
12 */
13 #include <argp.h>
14 #include <libgen.h>
15 #include <signal.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <time.h>
20 #include <unistd.h>
21
22 #include <bpf/libbpf.h>
23 #include <bpf/bpf.h>
24
25 #include "fsdist.h"
26 #include "fsdist.skel.h"
27 #include "trace_helpers.h"
28
29 #define warn(...) fprintf(stderr, __VA_ARGS__)
30
31 enum fs_type {
32 NONE,
33 BTRFS,
34 EXT4,
35 NFS,
36 XFS,
37 };
38
39 static struct fs_config {
40 const char *fs;
41 const char *op_funcs[MAX_OP];
42 } fs_configs[] = {
43 [BTRFS] = { "btrfs", {
44 [READ] = "btrfs_file_read_iter",
45 [WRITE] = "btrfs_file_write_iter",
46 [OPEN] = "btrfs_file_open",
47 [FSYNC] = "btrfs_sync_file",
48 [GETATTR] = NULL, /* not supported */
49 }},
50 [EXT4] = { "ext4", {
51 [READ] = "ext4_file_read_iter",
52 [WRITE] = "ext4_file_write_iter",
53 [OPEN] = "ext4_file_open",
54 [FSYNC] = "ext4_sync_file",
55 [GETATTR] = "ext4_file_getattr",
56 }},
57 [NFS] = { "nfs", {
58 [READ] = "nfs_file_read",
59 [WRITE] = "nfs_file_write",
60 [OPEN] = "nfs_file_open",
61 [FSYNC] = "nfs_file_fsync",
62 [GETATTR] = "nfs_getattr",
63 }},
64 [XFS] = { "xfs", {
65 [READ] = "xfs_file_read_iter",
66 [WRITE] = "xfs_file_write_iter",
67 [OPEN] = "xfs_file_open",
68 [FSYNC] = "xfs_file_fsync",
69 [GETATTR] = NULL, /* not supported */
70 }},
71 };
72
73 static char *file_op_names[] = {
74 [READ] = "read",
75 [WRITE] = "write",
76 [OPEN] = "open",
77 [FSYNC] = "fsync",
78 [GETATTR] = "getattr",
79 };
80
81 static struct hist zero;
82 static volatile sig_atomic_t exiting;
83
84 /* options */
85 static enum fs_type fs_type = NONE;
86 static bool emit_timestamp = false;
87 static bool timestamp_in_ms = false;
88 static pid_t target_pid = 0;
89 static int interval = 99999999;
90 static int count = 99999999;
91 static bool verbose = false;
92
93 const char *argp_program_version = "fsdist 0.1";
94 const char *argp_program_bug_address =
95 "https://github.com/iovisor/bcc/tree/master/libbpf-tools";
96 const char argp_program_doc[] =
97 "Summarize file system operations latency.\n"
98 "\n"
99 "Usage: fsdist [-h] [-t] [-T] [-m] [-p PID] [interval] [count]\n"
100 "\n"
101 "EXAMPLES:\n"
102 " fsdist -t ext4 # show ext4 operations latency as a histogram\n"
103 " fsdist -t nfs -p 1216 # trace nfs operations with PID 1216 only\n"
104 " fsdist -t xfs 1 10 # trace xfs operations, 1s summaries, 10 times\n"
105 " fsdist -t btrfs -m 5 # trace btrfs operation, 5s summaries, in ms\n";
106
107 static const struct argp_option opts[] = {
108 { "timestamp", 'T', NULL, 0, "Print timestamp" },
109 { "milliseconds", 'm', NULL, 0, "Millisecond histogram" },
110 { "pid", 'p', "PID", 0, "Process ID to trace" },
111 { "type", 't', "Filesystem", 0, "Which filesystem to trace, [btrfs/ext4/nfs/xfs]" },
112 { "verbose", 'v', NULL, 0, "Verbose debug output" },
113 { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
114 {},
115 };
116
parse_arg(int key,char * arg,struct argp_state * state)117 static error_t parse_arg(int key, char *arg, struct argp_state *state)
118 {
119 static int pos_args;
120
121 switch (key) {
122 case 'v':
123 verbose = true;
124 break;
125 case 'T':
126 emit_timestamp = true;
127 break;
128 case 'm':
129 timestamp_in_ms = true;
130 break;
131 case 't':
132 if (!strcmp(arg, "btrfs")) {
133 fs_type = BTRFS;
134 } else if (!strcmp(arg, "ext4")) {
135 fs_type = EXT4;
136 } else if (!strcmp(arg, "nfs")) {
137 fs_type = NFS;
138 } else if (!strcmp(arg, "xfs")) {
139 fs_type = XFS;
140 } else {
141 warn("invalid filesystem\n");
142 argp_usage(state);
143 }
144 break;
145 case 'p':
146 errno = 0;
147 target_pid = strtol(arg, NULL, 10);
148 if (errno || target_pid <= 0) {
149 warn("invalid PID: %s\n", arg);
150 argp_usage(state);
151 }
152 break;
153 case 'h':
154 argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
155 break;
156 case ARGP_KEY_ARG:
157 errno = 0;
158 if (pos_args == 0) {
159 interval = strtol(arg, NULL, 10);
160 if (errno) {
161 warn("invalid internal\n");
162 argp_usage(state);
163 }
164 } else if (pos_args == 1) {
165 count = strtol(arg, NULL, 10);
166 if (errno) {
167 warn("invalid count\n");
168 argp_usage(state);
169 }
170 } else {
171 warn("unrecognized positional argument: %s\n", arg);
172 argp_usage(state);
173 }
174 pos_args++;
175 break;
176 default:
177 return ARGP_ERR_UNKNOWN;
178 }
179 return 0;
180 }
181
alias_parse(char * prog)182 static void alias_parse(char *prog)
183 {
184 char *name = basename(prog);
185
186 if (!strcmp(name, "btrfsdist")) {
187 fs_type = BTRFS;
188 } else if (!strcmp(name, "ext4dist")) {
189 fs_type = EXT4;
190 } else if (!strcmp(name, "nfsdist")) {
191 fs_type = NFS;
192 } else if (!strcmp(name, "xfsdist")) {
193 fs_type = XFS;
194 }
195 }
196
libbpf_print_fn(enum libbpf_print_level level,const char * format,va_list args)197 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
198 {
199 if (level == LIBBPF_DEBUG && !verbose)
200 return 0;
201 return vfprintf(stderr, format, args);
202 }
203
sig_handler(int sig)204 static void sig_handler(int sig)
205 {
206 exiting = 1;
207 }
208
print_hists(struct fsdist_bpf__bss * bss)209 static int print_hists(struct fsdist_bpf__bss *bss)
210 {
211 const char *units = timestamp_in_ms ? "msecs" : "usecs";
212 enum fs_file_op op;
213
214 for (op = READ; op < MAX_OP; op++) {
215 struct hist hist = bss->hists[op];
216
217 bss->hists[op] = zero;
218 if (!memcmp(&zero, &hist, sizeof(hist)))
219 continue;
220 printf("operation = '%s'\n", file_op_names[op]);
221 print_log2_hist(hist.slots, MAX_SLOTS, units);
222 printf("\n");
223 }
224 return 0;
225 }
226
check_fentry()227 static bool check_fentry()
228 {
229 int i;
230 const char *fn_name, *module;
231 bool support_fentry = true;
232
233 for (i = 0; i < MAX_OP; i++) {
234 fn_name = fs_configs[fs_type].op_funcs[i];
235 module = fs_configs[fs_type].fs;
236 if (fn_name && !fentry_can_attach(fn_name, module)) {
237 support_fentry = false;
238 break;
239 }
240 }
241 return support_fentry;
242 }
243
fentry_set_attach_target(struct fsdist_bpf * obj)244 static int fentry_set_attach_target(struct fsdist_bpf *obj)
245 {
246 struct fs_config *cfg = &fs_configs[fs_type];
247 int err = 0;
248
249 err = err ?: bpf_program__set_attach_target(obj->progs.file_read_fentry, 0, cfg->op_funcs[READ]);
250 err = err ?: bpf_program__set_attach_target(obj->progs.file_read_fexit, 0, cfg->op_funcs[READ]);
251 err = err ?: bpf_program__set_attach_target(obj->progs.file_write_fentry, 0, cfg->op_funcs[WRITE]);
252 err = err ?: bpf_program__set_attach_target(obj->progs.file_write_fexit, 0, cfg->op_funcs[WRITE]);
253 err = err ?: bpf_program__set_attach_target(obj->progs.file_open_fentry, 0, cfg->op_funcs[OPEN]);
254 err = err ?: bpf_program__set_attach_target(obj->progs.file_open_fexit, 0, cfg->op_funcs[OPEN]);
255 err = err ?: bpf_program__set_attach_target(obj->progs.file_sync_fentry, 0, cfg->op_funcs[FSYNC]);
256 err = err ?: bpf_program__set_attach_target(obj->progs.file_sync_fexit, 0, cfg->op_funcs[FSYNC]);
257 if (cfg->op_funcs[GETATTR]) {
258 err = err ?: bpf_program__set_attach_target(obj->progs.getattr_fentry, 0, cfg->op_funcs[GETATTR]);
259 err = err ?: bpf_program__set_attach_target(obj->progs.getattr_fexit, 0, cfg->op_funcs[GETATTR]);
260 } else {
261 bpf_program__set_autoload(obj->progs.getattr_fentry, false);
262 bpf_program__set_autoload(obj->progs.getattr_fexit, false);
263 }
264 return err;
265 }
266
disable_fentry(struct fsdist_bpf * obj)267 static void disable_fentry(struct fsdist_bpf *obj)
268 {
269 bpf_program__set_autoload(obj->progs.file_read_fentry, false);
270 bpf_program__set_autoload(obj->progs.file_read_fexit, false);
271 bpf_program__set_autoload(obj->progs.file_write_fentry, false);
272 bpf_program__set_autoload(obj->progs.file_write_fexit, false);
273 bpf_program__set_autoload(obj->progs.file_open_fentry, false);
274 bpf_program__set_autoload(obj->progs.file_open_fexit, false);
275 bpf_program__set_autoload(obj->progs.file_sync_fentry, false);
276 bpf_program__set_autoload(obj->progs.file_sync_fexit, false);
277 bpf_program__set_autoload(obj->progs.getattr_fentry, false);
278 bpf_program__set_autoload(obj->progs.getattr_fexit, false);
279 }
280
disable_kprobes(struct fsdist_bpf * obj)281 static void disable_kprobes(struct fsdist_bpf *obj)
282 {
283 bpf_program__set_autoload(obj->progs.file_read_entry, false);
284 bpf_program__set_autoload(obj->progs.file_read_exit, false);
285 bpf_program__set_autoload(obj->progs.file_write_entry, false);
286 bpf_program__set_autoload(obj->progs.file_write_exit, false);
287 bpf_program__set_autoload(obj->progs.file_open_entry, false);
288 bpf_program__set_autoload(obj->progs.file_open_exit, false);
289 bpf_program__set_autoload(obj->progs.file_sync_entry, false);
290 bpf_program__set_autoload(obj->progs.file_sync_exit, false);
291 bpf_program__set_autoload(obj->progs.getattr_entry, false);
292 bpf_program__set_autoload(obj->progs.getattr_exit, false);
293 }
294
attach_kprobes(struct fsdist_bpf * obj)295 static int attach_kprobes(struct fsdist_bpf *obj)
296 {
297 long err = 0;
298 struct fs_config *cfg = &fs_configs[fs_type];
299
300 /* READ */
301 obj->links.file_read_entry = bpf_program__attach_kprobe(obj->progs.file_read_entry, false, cfg->op_funcs[READ]);
302 if (!obj->links.file_read_entry)
303 goto errout;
304 obj->links.file_read_exit = bpf_program__attach_kprobe(obj->progs.file_read_exit, true, cfg->op_funcs[READ]);
305 if (!obj->links.file_read_exit)
306 goto errout;
307 /* WRITE */
308 obj->links.file_write_entry = bpf_program__attach_kprobe(obj->progs.file_write_entry, false, cfg->op_funcs[WRITE]);
309 if (!obj->links.file_write_entry)
310 goto errout;
311 obj->links.file_write_exit = bpf_program__attach_kprobe(obj->progs.file_write_exit, true, cfg->op_funcs[WRITE]);
312 if (!obj->links.file_write_exit)
313 goto errout;
314 /* OPEN */
315 obj->links.file_open_entry = bpf_program__attach_kprobe(obj->progs.file_open_entry, false, cfg->op_funcs[OPEN]);
316 if (!obj->links.file_open_entry)
317 goto errout;
318 obj->links.file_open_exit = bpf_program__attach_kprobe(obj->progs.file_open_exit, true, cfg->op_funcs[OPEN]);
319 if (!obj->links.file_open_exit)
320 goto errout;
321 /* FSYNC */
322 obj->links.file_sync_entry = bpf_program__attach_kprobe(obj->progs.file_sync_entry, false, cfg->op_funcs[FSYNC]);
323 if (!obj->links.file_sync_entry)
324 goto errout;
325 obj->links.file_sync_exit = bpf_program__attach_kprobe(obj->progs.file_sync_exit, true, cfg->op_funcs[FSYNC]);
326 if (!obj->links.file_sync_exit)
327 goto errout;
328 /* GETATTR */
329 if (!cfg->op_funcs[GETATTR])
330 return 0;
331 obj->links.getattr_entry = bpf_program__attach_kprobe(obj->progs.getattr_entry, false, cfg->op_funcs[GETATTR]);
332 if (!obj->links.getattr_entry)
333 goto errout;
334 obj->links.getattr_exit = bpf_program__attach_kprobe(obj->progs.getattr_exit, true, cfg->op_funcs[GETATTR]);
335 if (!obj->links.getattr_exit)
336 goto errout;
337 return 0;
338 errout:
339 err = -errno;
340 warn("failed to attach kprobe: %ld\n", err);
341 return err;
342 }
343
main(int argc,char ** argv)344 int main(int argc, char **argv)
345 {
346 static const struct argp argp = {
347 .options = opts,
348 .parser = parse_arg,
349 .doc = argp_program_doc,
350 };
351 struct fsdist_bpf *skel;
352 struct tm *tm;
353 char ts[32];
354 time_t t;
355 int err;
356 bool support_fentry;
357
358 alias_parse(argv[0]);
359 err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
360 if (err)
361 return err;
362 if (fs_type == NONE) {
363 warn("filesystem must be specified using -t option.\n");
364 return 1;
365 }
366
367 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
368 libbpf_set_print(libbpf_print_fn);
369
370 skel = fsdist_bpf__open();
371 if (!skel) {
372 warn("failed to open BPF object\n");
373 return 1;
374 }
375
376 skel->rodata->target_pid = target_pid;
377 skel->rodata->in_ms = timestamp_in_ms;
378
379 /*
380 * before load
381 * if fentry is supported, we set attach target and disable kprobes
382 * otherwise, we disable fentry and attach kprobes after loading
383 */
384 support_fentry = check_fentry();
385 if (support_fentry) {
386 err = fentry_set_attach_target(skel);
387 if (err) {
388 warn("failed to set attach target: %d\n", err);
389 goto cleanup;
390 }
391 disable_kprobes(skel);
392 } else {
393 disable_fentry(skel);
394 }
395
396 err = fsdist_bpf__load(skel);
397 if (err) {
398 warn("failed to load BPF object: %d\n", err);
399 goto cleanup;
400 }
401
402 /*
403 * after load
404 * if fentry is supported, let libbpf do auto load
405 * otherwise, we attach to kprobes manually
406 */
407 err = support_fentry ? fsdist_bpf__attach(skel) : attach_kprobes(skel);
408 if (err) {
409 warn("failed to attach BPF programs: %d\n", err);
410 goto cleanup;
411 }
412
413 signal(SIGINT, sig_handler);
414
415 printf("Tracing %s operation latency... Hit Ctrl-C to end.\n",
416 fs_configs[fs_type].fs);
417
418 while (1) {
419 sleep(interval);
420 printf("\n");
421
422 if (emit_timestamp) {
423 time(&t);
424 tm = localtime(&t);
425 strftime(ts, sizeof(ts), "%H:%M:%S", tm);
426 printf("%-8s\n", ts);
427 }
428
429 err = print_hists(skel->bss);
430 if (err)
431 break;
432
433 if (exiting || --count == 0)
434 break;
435 }
436
437 cleanup:
438 fsdist_bpf__destroy(skel);
439
440 return err != 0;
441 }
442