blob: 3c5ee997661c91c29d43418f869337107c69e3be [file] [log] [blame]
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07001#!/usr/bin/python
2#
3# vfsstat Count some VFS calls.
4# For Linux, uses BCC, eBPF. See .c file.
5#
6# Written as a basic example of counting multiple events as a stat tool.
7#
8# USAGE: vfsstat [interval [count]]
9#
10# Copyright (c) 2015 Brendan Gregg.
11# Licensed under the Apache License, Version 2.0 (the "License")
12#
13# 14-Aug-2015 Brendan Gregg Created this.
14
15from __future__ import print_function
16from bpf import BPF
17from ctypes import c_ushort, c_int, c_ulonglong
18from time import sleep, strftime
19from sys import argv
20
21def usage():
22 print("USAGE: %s [interval [count]]" % argv[0])
23 exit()
24
25# arguments
26interval = 1
27count = -1
28if len(argv) > 1:
29 try:
30 interval = int(argv[1])
31 if interval == 0:
32 raise
33 if len(argv) > 2:
34 count = int(argv[2])
35 except: # also catches -h, --help
36 usage()
37
38# load BPF program
39b = BPF(src_file = "vfsstat.c")
40BPF.attach_kprobe(b.load_func("do_read", BPF.KPROBE), "vfs_read")
41BPF.attach_kprobe(b.load_func("do_write", BPF.KPROBE), "vfs_write")
42BPF.attach_kprobe(b.load_func("do_fsync", BPF.KPROBE), "vfs_fsync")
43BPF.attach_kprobe(b.load_func("do_open", BPF.KPROBE), "vfs_open")
44BPF.attach_kprobe(b.load_func("do_create", BPF.KPROBE), "vfs_create")
45stats = b.get_table("stats", c_int, c_ulonglong)
46
47# stat column labels and indexes
48stat_types = {
49 "READ" : 1,
50 "WRITE" : 2,
51 "FSYNC" : 3,
52 "OPEN" : 4,
53 "CREATE" : 5
54}
55
56# header
57print("%-8s " % "TIME", end="")
58last = {}
59for stype in stat_types.keys():
60 print(" %8s" % (stype + "/s"), end="")
61 idx = stat_types[stype]
62 last[idx] = 0
63print("")
64
65# output
66i = 0
67while (1):
68 if count > 0:
69 i += 1
70 if i > count:
71 exit()
72 try:
73 sleep(interval)
74 except KeyboardInterrupt:
75 pass; exit()
76
77 print("%-8s: " % strftime("%H:%M:%S"), end="")
78 # print each statistic as a column
79 for stype in stat_types.keys():
80 idx = stat_types[stype]
81 try:
82 delta = stats[c_int(idx)].value - last[idx]
83 print(" %8d" % (delta / interval), end="")
84 last[idx] = stats[c_int(idx)].value
85 except:
86 print(" %8d" % 0, end="")
87 print("")