Brendan Gregg | ebdb242 | 2015-08-18 16:53:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # vfsstat Count some VFS calls. |
| 5 | # For Linux, uses BCC, eBPF. See .c file. |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 6 | # |
| 7 | # Written as a basic example of counting multiple events as a stat tool. |
| 8 | # |
| 9 | # USAGE: vfsstat [interval [count]] |
| 10 | # |
| 11 | # Copyright (c) 2015 Brendan Gregg. |
| 12 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 13 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 14 | # 14-Aug-2015 Brendan Gregg Created this. |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 15 | |
| 16 | from __future__ import print_function |
Brenden Blanco | c35989d | 2015-09-02 18:04:07 -0700 | [diff] [blame] | 17 | from bcc import BPF |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 18 | from ctypes import c_int |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 19 | from time import sleep, strftime |
| 20 | from sys import argv |
| 21 | |
| 22 | def usage(): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 23 | print("USAGE: %s [interval [count]]" % argv[0]) |
| 24 | exit() |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 25 | |
| 26 | # arguments |
| 27 | interval = 1 |
| 28 | count = -1 |
| 29 | if len(argv) > 1: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 30 | try: |
| 31 | interval = int(argv[1]) |
| 32 | if interval == 0: |
| 33 | raise |
| 34 | if len(argv) > 2: |
| 35 | count = int(argv[2]) |
| 36 | except: # also catches -h, --help |
| 37 | usage() |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 38 | |
| 39 | # load BPF program |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 40 | b = BPF(text=""" |
| 41 | #include <uapi/linux/ptrace.h> |
| 42 | |
| 43 | enum stat_types { |
| 44 | S_READ = 1, |
| 45 | S_WRITE, |
| 46 | S_FSYNC, |
| 47 | S_OPEN, |
| 48 | S_CREATE, |
| 49 | S_MAXSTAT |
| 50 | }; |
| 51 | |
| 52 | BPF_TABLE("array", int, u64, stats, S_MAXSTAT + 1); |
| 53 | |
| 54 | void stats_increment(int key) { |
| 55 | u64 *leaf = stats.lookup(&key); |
| 56 | if (leaf) (*leaf)++; |
| 57 | } |
| 58 | |
| 59 | void do_read(struct pt_regs *ctx) { stats_increment(S_READ); } |
| 60 | void do_write(struct pt_regs *ctx) { stats_increment(S_WRITE); } |
| 61 | void do_fsync(struct pt_regs *ctx) { stats_increment(S_FSYNC); } |
| 62 | void do_open(struct pt_regs *ctx) { stats_increment(S_OPEN); } |
| 63 | void do_create(struct pt_regs *ctx) { stats_increment(S_CREATE); } |
| 64 | """) |
Brenden Blanco | 5eef65e | 2015-08-19 15:39:19 -0700 | [diff] [blame] | 65 | b.attach_kprobe(event="vfs_read", fn_name="do_read") |
| 66 | b.attach_kprobe(event="vfs_write", fn_name="do_write") |
| 67 | b.attach_kprobe(event="vfs_fsync", fn_name="do_fsync") |
| 68 | b.attach_kprobe(event="vfs_open", fn_name="do_open") |
| 69 | b.attach_kprobe(event="vfs_create", fn_name="do_create") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 70 | |
| 71 | # stat column labels and indexes |
| 72 | stat_types = { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 73 | "READ": 1, |
| 74 | "WRITE": 2, |
| 75 | "FSYNC": 3, |
| 76 | "OPEN": 4, |
| 77 | "CREATE": 5 |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | # header |
| 81 | print("%-8s " % "TIME", end="") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 82 | for stype in stat_types.keys(): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 83 | print(" %8s" % (stype + "/s"), end="") |
| 84 | idx = stat_types[stype] |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 85 | print("") |
| 86 | |
| 87 | # output |
| 88 | i = 0 |
| 89 | while (1): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 90 | if count > 0: |
| 91 | i += 1 |
| 92 | if i > count: |
| 93 | exit() |
| 94 | try: |
| 95 | sleep(interval) |
| 96 | except KeyboardInterrupt: |
| 97 | pass |
| 98 | exit() |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 99 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 100 | print("%-8s: " % strftime("%H:%M:%S"), end="") |
| 101 | # print each statistic as a column |
| 102 | for stype in stat_types.keys(): |
| 103 | idx = stat_types[stype] |
| 104 | try: |
| 105 | val = b["stats"][c_int(idx)].value / interval |
| 106 | print(" %8d" % val, end="") |
| 107 | except: |
| 108 | print(" %8d" % 0, end="") |
| 109 | b["stats"].clear() |
| 110 | print("") |