Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame^] | 1 | #!/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 | |
| 15 | from __future__ import print_function |
| 16 | from bpf import BPF |
| 17 | from ctypes import c_ushort, c_int, c_ulonglong |
| 18 | from time import sleep, strftime |
| 19 | from sys import argv |
| 20 | |
| 21 | def usage(): |
| 22 | print("USAGE: %s [interval [count]]" % argv[0]) |
| 23 | exit() |
| 24 | |
| 25 | # arguments |
| 26 | interval = 1 |
| 27 | count = -1 |
| 28 | if 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 |
| 39 | b = BPF(src_file = "vfsstat.c") |
| 40 | BPF.attach_kprobe(b.load_func("do_read", BPF.KPROBE), "vfs_read") |
| 41 | BPF.attach_kprobe(b.load_func("do_write", BPF.KPROBE), "vfs_write") |
| 42 | BPF.attach_kprobe(b.load_func("do_fsync", BPF.KPROBE), "vfs_fsync") |
| 43 | BPF.attach_kprobe(b.load_func("do_open", BPF.KPROBE), "vfs_open") |
| 44 | BPF.attach_kprobe(b.load_func("do_create", BPF.KPROBE), "vfs_create") |
| 45 | stats = b.get_table("stats", c_int, c_ulonglong) |
| 46 | |
| 47 | # stat column labels and indexes |
| 48 | stat_types = { |
| 49 | "READ" : 1, |
| 50 | "WRITE" : 2, |
| 51 | "FSYNC" : 3, |
| 52 | "OPEN" : 4, |
| 53 | "CREATE" : 5 |
| 54 | } |
| 55 | |
| 56 | # header |
| 57 | print("%-8s " % "TIME", end="") |
| 58 | last = {} |
| 59 | for stype in stat_types.keys(): |
| 60 | print(" %8s" % (stype + "/s"), end="") |
| 61 | idx = stat_types[stype] |
| 62 | last[idx] = 0 |
| 63 | print("") |
| 64 | |
| 65 | # output |
| 66 | i = 0 |
| 67 | while (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("") |