Skip to content

Commit

Permalink
{libbpf-tools,tools}/vfsstat: Add unlink,mkdir,rmdir stat (#4995)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Rtoax committed May 10, 2024
1 parent aa5ae41 commit 01bd3e4
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 17 deletions.
36 changes: 36 additions & 0 deletions libbpf-tools/vfsstat.bpf.c
Expand Up @@ -45,6 +45,24 @@ int BPF_KPROBE(kprobe_vfs_create)
return inc_stats(S_CREATE);
}

SEC("kprobe/vfs_unlink")
int BPF_KPROBE(kprobe_vfs_unlink)
{
return inc_stats(S_UNLINK);
}

SEC("kprobe/vfs_mkdir")
int BPF_KPROBE(kprobe_vfs_mkdir)
{
return inc_stats(S_MKDIR);
}

SEC("kprobe/vfs_rmdir")
int BPF_KPROBE(kprobe_vfs_rmdir)
{
return inc_stats(S_RMDIR);
}

SEC("fentry/vfs_read")
int BPF_PROG(fentry_vfs_read)
{
Expand Down Expand Up @@ -75,4 +93,22 @@ int BPF_PROG(fentry_vfs_create)
return inc_stats(S_CREATE);
}

SEC("fentry/vfs_unlink")
int BPF_PROG(fentry_vfs_unlink)
{
return inc_stats(S_UNLINK);
}

SEC("fentry/vfs_mkdir")
int BPF_PROG(fentry_vfs_mkdir)
{
return inc_stats(S_MKDIR);
}

SEC("fentry/vfs_rmdir")
int BPF_PROG(fentry_vfs_rmdir)
{
return inc_stats(S_RMDIR);
}

char LICENSE[] SEC("license") = "GPL";
9 changes: 9 additions & 0 deletions libbpf-tools/vfsstat.c
Expand Up @@ -110,6 +110,9 @@ static const char *stat_types_names[] = {
[S_FSYNC] = "FSYNC",
[S_OPEN] = "OPEN",
[S_CREATE] = "CREATE",
[S_UNLINK] = "UNLINK",
[S_MKDIR] = "MKDIR",
[S_RMDIR] = "RMDIR",
};

static void print_header(void)
Expand Down Expand Up @@ -174,12 +177,18 @@ int main(int argc, char **argv)
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);
bpf_program__set_autoload(skel->progs.kprobe_vfs_unlink, false);
bpf_program__set_autoload(skel->progs.kprobe_vfs_mkdir, false);
bpf_program__set_autoload(skel->progs.kprobe_vfs_rmdir, 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);
bpf_program__set_autoload(skel->progs.fentry_vfs_unlink, false);
bpf_program__set_autoload(skel->progs.fentry_vfs_mkdir, false);
bpf_program__set_autoload(skel->progs.fentry_vfs_rmdir, false);
}

err = vfsstat_bpf__load(skel);
Expand Down
3 changes: 3 additions & 0 deletions libbpf-tools/vfsstat.h
Expand Up @@ -9,6 +9,9 @@ enum stat_types {
S_FSYNC,
S_OPEN,
S_CREATE,
S_UNLINK,
S_MKDIR,
S_RMDIR,
S_MAXSTAT,
};

Expand Down
18 changes: 17 additions & 1 deletion tools/vfsstat.py
Expand Up @@ -13,6 +13,7 @@
#
# 14-Aug-2015 Brendan Gregg Created this.
# 12-Oct-2022 Rocky Xing Added PID filter support.
# 09-May-2024 Rong Tao Add unlink,mkdir,rmdir stat.

from __future__ import print_function
from bcc import BPF
Expand Down Expand Up @@ -54,6 +55,9 @@
S_FSYNC,
S_OPEN,
S_CREATE,
S_UNLINK,
S_MKDIR,
S_RMDIR,
S_MAXSTAT
};
Expand All @@ -71,6 +75,9 @@
void do_fsync(struct pt_regs *ctx) { stats_try_increment(S_FSYNC); }
void do_open(struct pt_regs *ctx) { stats_try_increment(S_OPEN); }
void do_create(struct pt_regs *ctx) { stats_try_increment(S_CREATE); }
void do_unlink(struct pt_regs *ctx) { stats_try_increment(S_UNLINK); }
void do_mkdir(struct pt_regs *ctx) { stats_try_increment(S_MKDIR); }
void do_rmdir(struct pt_regs *ctx) { stats_try_increment(S_RMDIR); }
"""

bpf_text_kfunc = """
Expand All @@ -79,6 +86,9 @@
KFUNC_PROBE(vfs_fsync_range) { stats_try_increment(S_FSYNC); return 0; }
KFUNC_PROBE(vfs_open) { stats_try_increment(S_OPEN); return 0; }
KFUNC_PROBE(vfs_create) { stats_try_increment(S_CREATE); return 0; }
KFUNC_PROBE(vfs_unlink) { stats_try_increment(S_UNLINK); return 0; }
KFUNC_PROBE(vfs_mkdir) { stats_try_increment(S_MKDIR); return 0; }
KFUNC_PROBE(vfs_rmdir) { stats_try_increment(S_RMDIR); return 0; }
"""

is_support_kfunc = BPF.support_kfunc()
Expand Down Expand Up @@ -109,14 +119,20 @@
b.attach_kprobe(event="vfs_fsync_range", fn_name="do_fsync")
b.attach_kprobe(event="vfs_open", fn_name="do_open")
b.attach_kprobe(event="vfs_create", fn_name="do_create")
b.attach_kprobe(event="vfs_unlink", fn_name="do_unlink")
b.attach_kprobe(event="vfs_mkdir", fn_name="do_mkdir")
b.attach_kprobe(event="vfs_rmdir", fn_name="do_rmdir")

# stat column labels and indexes
stat_types = {
"READ": 1,
"WRITE": 2,
"FSYNC": 3,
"OPEN": 4,
"CREATE": 5
"CREATE": 5,
"UNLINK": 6,
"MKDIR": 7,
"RMDIR": 8,
}

# header
Expand Down
39 changes: 23 additions & 16 deletions tools/vfsstat_example.txt
Expand Up @@ -5,29 +5,36 @@ This traces some common VFS calls and prints per-second summaries. By default,
the output interval is one second:

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


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

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


Full usage:
Expand Down

0 comments on commit 01bd3e4

Please sign in to comment.