Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [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 |
Jiri Olsa | 2fa54c0 | 2020-02-19 18:48:44 +0100 | [diff] [blame] | 40 | bpf_text = """ |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 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 | |
Brenden Blanco | d51870b | 2017-08-16 11:29:23 -0700 | [diff] [blame] | 52 | BPF_ARRAY(stats, u64, S_MAXSTAT); |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 53 | |
Brenden Blanco | 3f28e7b | 2017-04-20 09:33:44 -0700 | [diff] [blame] | 54 | static void stats_increment(int key) { |
zcy | 80242fb | 2021-07-02 00:12:32 +0800 | [diff] [blame] | 55 | stats.atomic_increment(key); |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 56 | } |
Jiri Olsa | 2fa54c0 | 2020-02-19 18:48:44 +0100 | [diff] [blame] | 57 | """ |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 58 | |
Jiri Olsa | 2fa54c0 | 2020-02-19 18:48:44 +0100 | [diff] [blame] | 59 | bpf_text_kprobe = """ |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 60 | void do_read(struct pt_regs *ctx) { stats_increment(S_READ); } |
| 61 | void do_write(struct pt_regs *ctx) { stats_increment(S_WRITE); } |
| 62 | void do_fsync(struct pt_regs *ctx) { stats_increment(S_FSYNC); } |
| 63 | void do_open(struct pt_regs *ctx) { stats_increment(S_OPEN); } |
| 64 | void do_create(struct pt_regs *ctx) { stats_increment(S_CREATE); } |
Jiri Olsa | 2fa54c0 | 2020-02-19 18:48:44 +0100 | [diff] [blame] | 65 | """ |
| 66 | |
| 67 | bpf_text_kfunc = """ |
Hengqi Chen | 065ec13 | 2022-03-20 17:03:56 +0800 | [diff] [blame] | 68 | KFUNC_PROBE(vfs_read) { stats_increment(S_READ); return 0; } |
| 69 | KFUNC_PROBE(vfs_write) { stats_increment(S_WRITE); return 0; } |
| 70 | KFUNC_PROBE(vfs_fsync_range) { stats_increment(S_FSYNC); return 0; } |
| 71 | KFUNC_PROBE(vfs_open) { stats_increment(S_OPEN); return 0; } |
| 72 | KFUNC_PROBE(vfs_create) { stats_increment(S_CREATE); return 0; } |
Jiri Olsa | 2fa54c0 | 2020-02-19 18:48:44 +0100 | [diff] [blame] | 73 | """ |
| 74 | |
| 75 | is_support_kfunc = BPF.support_kfunc() |
| 76 | #is_support_kfunc = False #BPF.support_kfunc() |
| 77 | if is_support_kfunc: |
| 78 | bpf_text += bpf_text_kfunc |
| 79 | else: |
| 80 | bpf_text += bpf_text_kprobe |
| 81 | |
| 82 | b = BPF(text=bpf_text) |
| 83 | if not is_support_kfunc: |
Hengqi Chen | 065ec13 | 2022-03-20 17:03:56 +0800 | [diff] [blame] | 84 | b.attach_kprobe(event="vfs_read", fn_name="do_read") |
| 85 | b.attach_kprobe(event="vfs_write", fn_name="do_write") |
| 86 | b.attach_kprobe(event="vfs_fsync_range", fn_name="do_fsync") |
| 87 | b.attach_kprobe(event="vfs_open", fn_name="do_open") |
| 88 | b.attach_kprobe(event="vfs_create", fn_name="do_create") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 89 | |
| 90 | # stat column labels and indexes |
| 91 | stat_types = { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 92 | "READ": 1, |
| 93 | "WRITE": 2, |
| 94 | "FSYNC": 3, |
| 95 | "OPEN": 4, |
| 96 | "CREATE": 5 |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | # header |
| 100 | print("%-8s " % "TIME", end="") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 101 | for stype in stat_types.keys(): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 102 | print(" %8s" % (stype + "/s"), end="") |
| 103 | idx = stat_types[stype] |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 104 | print("") |
| 105 | |
| 106 | # output |
| 107 | i = 0 |
| 108 | while (1): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 109 | if count > 0: |
| 110 | i += 1 |
| 111 | if i > count: |
| 112 | exit() |
| 113 | try: |
| 114 | sleep(interval) |
| 115 | except KeyboardInterrupt: |
| 116 | pass |
| 117 | exit() |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 118 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 119 | print("%-8s: " % strftime("%H:%M:%S"), end="") |
| 120 | # print each statistic as a column |
| 121 | for stype in stat_types.keys(): |
| 122 | idx = stat_types[stype] |
| 123 | try: |
| 124 | val = b["stats"][c_int(idx)].value / interval |
| 125 | print(" %8d" % val, end="") |
| 126 | except: |
| 127 | print(" %8d" % 0, end="") |
| 128 | b["stats"].clear() |
| 129 | print("") |