/
vfsstat.c
214 lines (188 loc) · 4.93 KB
/
vfsstat.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
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2020 Anton Protopopov
//
// Based on vfsstat(8) from BCC by Brendan Gregg
#include <argp.h>
#include <unistd.h>
#include <time.h>
#include <bpf/bpf.h>
#include "vfsstat.h"
#include "vfsstat.skel.h"
#include "btf_helpers.h"
#include "trace_helpers.h"
const char *argp_program_version = "vfsstat 0.1";
const char *argp_program_bug_address =
"https://github.com/iovisor/bcc/tree/master/libbpf-tools";
static const char argp_program_doc[] =
"\nvfsstat: Count some VFS calls\n"
"\n"
"EXAMPLES:\n"
" vfsstat # interval one second\n"
" vfsstat 5 3 # interval five seconds, three output lines\n";
static char args_doc[] = "[interval [count]]";
static const struct argp_option opts[] = {
{ "verbose", 'v', NULL, 0, "Verbose debug output", 0 },
{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help", 0 },
{},
};
static struct env {
bool verbose;
int count;
int interval;
} env = {
.interval = 1, /* once a second */
};
static error_t parse_arg(int key, char *arg, struct argp_state *state)
{
long interval;
long count;
switch (key) {
case 'h':
argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
break;
case 'v':
env.verbose = true;
break;
case ARGP_KEY_ARG:
switch (state->arg_num) {
case 0:
errno = 0;
interval = strtol(arg, NULL, 10);
if (errno || interval <= 0 || interval > INT_MAX) {
fprintf(stderr, "invalid interval: %s\n", arg);
argp_usage(state);
}
env.interval = interval;
break;
case 1:
errno = 0;
count = strtol(arg, NULL, 10);
if (errno || count < 0 || count > INT_MAX) {
fprintf(stderr, "invalid count: %s\n", arg);
argp_usage(state);
}
env.count = count;
break;
default:
argp_usage(state);
break;
}
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 && !env.verbose)
return 0;
return vfprintf(stderr, format, args);
}
static const char *strftime_now(char *s, size_t max, const char *format)
{
struct tm *tm;
time_t t;
t = time(NULL);
tm = localtime(&t);
if (tm == NULL) {
fprintf(stderr, "localtime: %s\n", strerror(errno));
return "<failed>";
}
if (strftime(s, max, format, tm) == 0) {
fprintf(stderr, "strftime error\n");
return "<failed>";
}
return s;
}
static const char *stat_types_names[] = {
[S_READ] = "READ",
[S_WRITE] = "WRITE",
[S_FSYNC] = "FSYNC",
[S_OPEN] = "OPEN",
[S_CREATE] = "CREATE",
};
static void print_header(void)
{
int i;
printf("%-8s ", "TIME");
for (i = 0; i < S_MAXSTAT; i++)
printf(" %6s/s", stat_types_names[i]);
printf("\n");
}
static void print_and_reset_stats(__u64 stats[S_MAXSTAT])
{
char s[16];
__u64 val;
int i;
printf("%-8s: ", strftime_now(s, sizeof(s), "%H:%M:%S"));
for (i = 0; i < S_MAXSTAT; i++) {
val = __atomic_exchange_n(&stats[i], 0, __ATOMIC_RELAXED);
printf(" %8llu", val / env.interval);
}
printf("\n");
}
int main(int argc, char **argv)
{
LIBBPF_OPTS(bpf_object_open_opts, open_opts);
static const struct argp argp = {
.options = opts,
.parser = parse_arg,
.doc = argp_program_doc,
.args_doc = args_doc,
};
struct vfsstat_bpf *skel;
int err;
err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
if (err)
return err;
libbpf_set_print(libbpf_print_fn);
err = ensure_core_btf(&open_opts);
if (err) {
fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
return 1;
}
skel = vfsstat_bpf__open();
if (!skel) {
fprintf(stderr, "failed to open BPF skelect\n");
return 1;
}
/* It fallbacks to kprobes when kernel does not support fentry. */
if (fentry_can_attach("vfs_read", NULL)) {
bpf_program__set_autoload(skel->progs.kprobe_vfs_read, false);
bpf_program__set_autoload(skel->progs.kprobe_vfs_write, false);
bpf_program__set_autoload(skel->progs.kprobe_vfs_fsync, false);
bpf_program__set_autoload(skel->progs.kprobe_vfs_open, false);
bpf_program__set_autoload(skel->progs.kprobe_vfs_create, false);
} else {
bpf_program__set_autoload(skel->progs.fentry_vfs_read, false);
bpf_program__set_autoload(skel->progs.fentry_vfs_write, false);
bpf_program__set_autoload(skel->progs.fentry_vfs_fsync, false);
bpf_program__set_autoload(skel->progs.fentry_vfs_open, false);
bpf_program__set_autoload(skel->progs.fentry_vfs_create, false);
}
err = vfsstat_bpf__load(skel);
if (err) {
fprintf(stderr, "failed to load BPF skelect: %d\n", err);
goto cleanup;
}
if (!skel->bss) {
fprintf(stderr, "Memory-mapping BPF maps is supported starting from Linux 5.7, please upgrade.\n");
goto cleanup;
}
err = vfsstat_bpf__attach(skel);
if (err) {
fprintf(stderr, "failed to attach BPF programs: %s\n",
strerror(-err));
goto cleanup;
}
print_header();
do {
sleep(env.interval);
print_and_reset_stats(skel->bss->stats);
} while (!env.count || --env.count);
cleanup:
vfsstat_bpf__destroy(skel);
cleanup_core_btf(&open_opts);
return err != 0;
}