vfsstat 2 KB
Newer Older
Brendan Gregg's avatar
Brendan Gregg committed
1
#!/usr/bin/python
2
# @lint-avoid-python-3-compatibility-imports
Brendan Gregg's avatar
Brendan Gregg committed
3
#
4 5
# vfsstat   Count some VFS calls.
#           For Linux, uses BCC, eBPF. See .c file.
Brendan Gregg's avatar
Brendan Gregg committed
6 7 8 9 10 11 12 13
#
# Written as a basic example of counting multiple events as a stat tool.
#
# USAGE: vfsstat [interval [count]]
#
# Copyright (c) 2015 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
#
14
# 14-Aug-2015   Brendan Gregg   Created this.
Brendan Gregg's avatar
Brendan Gregg committed
15 16

from __future__ import print_function
17
from bcc import BPF
18
from ctypes import c_int
Brendan Gregg's avatar
Brendan Gregg committed
19 20 21 22
from time import sleep, strftime
from sys import argv

def usage():
23 24
    print("USAGE: %s [interval [count]]" % argv[0])
    exit()
Brendan Gregg's avatar
Brendan Gregg committed
25 26 27 28 29

# arguments
interval = 1
count = -1
if len(argv) > 1:
30 31 32 33 34 35 36 37
    try:
        interval = int(argv[1])
        if interval == 0:
            raise
        if len(argv) > 2:
            count = int(argv[2])
    except:  # also catches -h, --help
        usage()
Brendan Gregg's avatar
Brendan Gregg committed
38 39

# load BPF program
40
b = BPF(src_file="vfsstat.c")
41 42 43 44 45
b.attach_kprobe(event="vfs_read", fn_name="do_read")
b.attach_kprobe(event="vfs_write", fn_name="do_write")
b.attach_kprobe(event="vfs_fsync", fn_name="do_fsync")
b.attach_kprobe(event="vfs_open", fn_name="do_open")
b.attach_kprobe(event="vfs_create", fn_name="do_create")
Brendan Gregg's avatar
Brendan Gregg committed
46 47 48

# stat column labels and indexes
stat_types = {
49 50 51 52 53
    "READ": 1,
    "WRITE": 2,
    "FSYNC": 3,
    "OPEN": 4,
    "CREATE": 5
Brendan Gregg's avatar
Brendan Gregg committed
54 55 56 57 58
}

# header
print("%-8s  " % "TIME", end="")
for stype in stat_types.keys():
59 60
    print(" %8s" % (stype + "/s"), end="")
    idx = stat_types[stype]
Brendan Gregg's avatar
Brendan Gregg committed
61 62 63 64 65
print("")

# output
i = 0
while (1):
66 67 68 69 70 71 72 73 74
    if count > 0:
        i += 1
        if i > count:
            exit()
    try:
        sleep(interval)
    except KeyboardInterrupt:
        pass
        exit()
Brendan Gregg's avatar
Brendan Gregg committed
75

76 77 78 79 80 81 82 83 84 85 86
    print("%-8s: " % strftime("%H:%M:%S"), end="")
    # print each statistic as a column
    for stype in stat_types.keys():
        idx = stat_types[stype]
        try:
            val = b["stats"][c_int(idx)].value / interval
            print(" %8d" % val, end="")
        except:
            print(" %8d" % 0, end="")
    b["stats"].clear()
    print("")