Skip to content

Commit 01bd3e4

Browse files
authored
{libbpf-tools,tools}/vfsstat: Add unlink,mkdir,rmdir stat (#4995)
The program counts OPEN and CREATE, so UNLINK statistics are very valuable. At the same time, statistics on folder creation and deletion are added. Test MKDIR/RMDIR: $ while :; do mkdir a.dir && rmdir a.dir; done Test UNLINK: $ while :; do touch a.out && rm a.out; done Signed-off-by: Rong Tao <rongtao@cestc.cn>
1 parent aa5ae41 commit 01bd3e4

File tree

5 files changed

+88
-17
lines changed

5 files changed

+88
-17
lines changed

libbpf-tools/vfsstat.bpf.c

+36
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ int BPF_KPROBE(kprobe_vfs_create)
4545
return inc_stats(S_CREATE);
4646
}
4747

48+
SEC("kprobe/vfs_unlink")
49+
int BPF_KPROBE(kprobe_vfs_unlink)
50+
{
51+
return inc_stats(S_UNLINK);
52+
}
53+
54+
SEC("kprobe/vfs_mkdir")
55+
int BPF_KPROBE(kprobe_vfs_mkdir)
56+
{
57+
return inc_stats(S_MKDIR);
58+
}
59+
60+
SEC("kprobe/vfs_rmdir")
61+
int BPF_KPROBE(kprobe_vfs_rmdir)
62+
{
63+
return inc_stats(S_RMDIR);
64+
}
65+
4866
SEC("fentry/vfs_read")
4967
int BPF_PROG(fentry_vfs_read)
5068
{
@@ -75,4 +93,22 @@ int BPF_PROG(fentry_vfs_create)
7593
return inc_stats(S_CREATE);
7694
}
7795

96+
SEC("fentry/vfs_unlink")
97+
int BPF_PROG(fentry_vfs_unlink)
98+
{
99+
return inc_stats(S_UNLINK);
100+
}
101+
102+
SEC("fentry/vfs_mkdir")
103+
int BPF_PROG(fentry_vfs_mkdir)
104+
{
105+
return inc_stats(S_MKDIR);
106+
}
107+
108+
SEC("fentry/vfs_rmdir")
109+
int BPF_PROG(fentry_vfs_rmdir)
110+
{
111+
return inc_stats(S_RMDIR);
112+
}
113+
78114
char LICENSE[] SEC("license") = "GPL";

libbpf-tools/vfsstat.c

+9
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ static const char *stat_types_names[] = {
110110
[S_FSYNC] = "FSYNC",
111111
[S_OPEN] = "OPEN",
112112
[S_CREATE] = "CREATE",
113+
[S_UNLINK] = "UNLINK",
114+
[S_MKDIR] = "MKDIR",
115+
[S_RMDIR] = "RMDIR",
113116
};
114117

115118
static void print_header(void)
@@ -174,12 +177,18 @@ int main(int argc, char **argv)
174177
bpf_program__set_autoload(skel->progs.kprobe_vfs_fsync, false);
175178
bpf_program__set_autoload(skel->progs.kprobe_vfs_open, false);
176179
bpf_program__set_autoload(skel->progs.kprobe_vfs_create, false);
180+
bpf_program__set_autoload(skel->progs.kprobe_vfs_unlink, false);
181+
bpf_program__set_autoload(skel->progs.kprobe_vfs_mkdir, false);
182+
bpf_program__set_autoload(skel->progs.kprobe_vfs_rmdir, false);
177183
} else {
178184
bpf_program__set_autoload(skel->progs.fentry_vfs_read, false);
179185
bpf_program__set_autoload(skel->progs.fentry_vfs_write, false);
180186
bpf_program__set_autoload(skel->progs.fentry_vfs_fsync, false);
181187
bpf_program__set_autoload(skel->progs.fentry_vfs_open, false);
182188
bpf_program__set_autoload(skel->progs.fentry_vfs_create, false);
189+
bpf_program__set_autoload(skel->progs.fentry_vfs_unlink, false);
190+
bpf_program__set_autoload(skel->progs.fentry_vfs_mkdir, false);
191+
bpf_program__set_autoload(skel->progs.fentry_vfs_rmdir, false);
183192
}
184193

185194
err = vfsstat_bpf__load(skel);

libbpf-tools/vfsstat.h

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ enum stat_types {
99
S_FSYNC,
1010
S_OPEN,
1111
S_CREATE,
12+
S_UNLINK,
13+
S_MKDIR,
14+
S_RMDIR,
1215
S_MAXSTAT,
1316
};
1417

tools/vfsstat.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#
1414
# 14-Aug-2015 Brendan Gregg Created this.
1515
# 12-Oct-2022 Rocky Xing Added PID filter support.
16+
# 09-May-2024 Rong Tao Add unlink,mkdir,rmdir stat.
1617

1718
from __future__ import print_function
1819
from bcc import BPF
@@ -54,6 +55,9 @@
5455
S_FSYNC,
5556
S_OPEN,
5657
S_CREATE,
58+
S_UNLINK,
59+
S_MKDIR,
60+
S_RMDIR,
5761
S_MAXSTAT
5862
};
5963
@@ -71,6 +75,9 @@
7175
void do_fsync(struct pt_regs *ctx) { stats_try_increment(S_FSYNC); }
7276
void do_open(struct pt_regs *ctx) { stats_try_increment(S_OPEN); }
7377
void do_create(struct pt_regs *ctx) { stats_try_increment(S_CREATE); }
78+
void do_unlink(struct pt_regs *ctx) { stats_try_increment(S_UNLINK); }
79+
void do_mkdir(struct pt_regs *ctx) { stats_try_increment(S_MKDIR); }
80+
void do_rmdir(struct pt_regs *ctx) { stats_try_increment(S_RMDIR); }
7481
"""
7582

7683
bpf_text_kfunc = """
@@ -79,6 +86,9 @@
7986
KFUNC_PROBE(vfs_fsync_range) { stats_try_increment(S_FSYNC); return 0; }
8087
KFUNC_PROBE(vfs_open) { stats_try_increment(S_OPEN); return 0; }
8188
KFUNC_PROBE(vfs_create) { stats_try_increment(S_CREATE); return 0; }
89+
KFUNC_PROBE(vfs_unlink) { stats_try_increment(S_UNLINK); return 0; }
90+
KFUNC_PROBE(vfs_mkdir) { stats_try_increment(S_MKDIR); return 0; }
91+
KFUNC_PROBE(vfs_rmdir) { stats_try_increment(S_RMDIR); return 0; }
8292
"""
8393

8494
is_support_kfunc = BPF.support_kfunc()
@@ -109,14 +119,20 @@
109119
b.attach_kprobe(event="vfs_fsync_range", fn_name="do_fsync")
110120
b.attach_kprobe(event="vfs_open", fn_name="do_open")
111121
b.attach_kprobe(event="vfs_create", fn_name="do_create")
122+
b.attach_kprobe(event="vfs_unlink", fn_name="do_unlink")
123+
b.attach_kprobe(event="vfs_mkdir", fn_name="do_mkdir")
124+
b.attach_kprobe(event="vfs_rmdir", fn_name="do_rmdir")
112125

113126
# stat column labels and indexes
114127
stat_types = {
115128
"READ": 1,
116129
"WRITE": 2,
117130
"FSYNC": 3,
118131
"OPEN": 4,
119-
"CREATE": 5
132+
"CREATE": 5,
133+
"UNLINK": 6,
134+
"MKDIR": 7,
135+
"RMDIR": 8,
120136
}
121137

122138
# header

tools/vfsstat_example.txt

+23-16
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,36 @@ This traces some common VFS calls and prints per-second summaries. By default,
55
the output interval is one second:
66

77
# ./vfsstat
8-
TIME READ/s WRITE/s CREATE/s OPEN/s FSYNC/s
9-
18:35:32: 231 12 4 98 0
10-
18:35:33: 274 13 4 106 0
11-
18:35:34: 586 86 4 251 0
12-
18:35:35: 241 15 4 99 0
13-
18:35:36: 232 10 4 98 0
14-
18:35:37: 244 10 4 107 0
15-
18:35:38: 235 13 4 97 0
16-
18:35:39: 6749 2633 4 1446 0
17-
18:35:40: 277 31 4 115 0
18-
18:35:41: 238 16 6 102 0
19-
18:35:42: 284 50 8 114 0
8+
TIME READ/s WRITE/s FSYNC/s OPEN/s CREATE/s UNLINK/s MKDIR/s RMDIR/s
9+
10:42:35: 5172 454 0 111 0 0 0 0
10+
10:42:36: 478 701 0 1 0 0 0 0
11+
10:42:37: 873 267 0 861 0 72 0 0
12+
10:42:38: 1599 146 0 1989 0 177 0 0
13+
10:42:39: 1876 135 0 2379 0 212 0 0
14+
10:42:40: 2566 201 0 3207 0 287 0 0
15+
10:42:41: 772 508 0 563 0 49 0 0
16+
10:42:42: 189 141 0 48 0 0 0 0
17+
10:42:43: 468 558 0 48 0 0 0 0
18+
10:42:44: 1144 841 0 624 0 0 0 0
19+
10:42:45: 4094 2554 0 2715 0 0 0 0
20+
10:42:46: 397 585 0 12 0 0 0 0
21+
10:42:47: 684 859 0 56 0 0 0 0
22+
10:42:48: 432 471 0 63 0 1 0 0
23+
10:42:49: 1890 259 0 1997 0 0 162 162
24+
10:42:50: 1990 143 0 2213 0 0 181 180
25+
10:42:51: 2256 197 0 2472 0 0 205 206
26+
10:42:52: 1674 351 0 1609 0 0 129 129
2027
^C
2128

2229

2330
Here we are using an output interval of five seconds, and printing three output
2431
lines:
2532

2633
# ./vfsstat 5 3
27-
TIME READ/s WRITE/s CREATE/s OPEN/s FSYNC/s
28-
18:35:55: 238 8 3 101 0
29-
18:36:00: 962 233 4 247 0
30-
18:36:05: 241 8 3 100 0
34+
TIME READ/s WRITE/s FSYNC/s OPEN/s CREATE/s UNLINK/s MKDIR/s RMDIR/s
35+
10:43:46: 2141 273 0 1240 0 0 99 99
36+
10:43:51: 2673 141 0 3039 0 0 250 249
37+
10:43:56: 1939 433 0 1895 0 0 154 154
3138

3239

3340
Full usage:

0 commit comments

Comments
 (0)