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