Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # filetop file reads and writes by process. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
| 7 | # USAGE: filetop.py [-h] [-C] [-r MAXROWS] [interval] [count] |
| 8 | # |
| 9 | # This uses in-kernel eBPF maps to store per process summaries for efficiency. |
| 10 | # |
| 11 | # Copyright 2016 Netflix, Inc. |
| 12 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 13 | # |
| 14 | # 06-Feb-2016 Brendan Gregg Created this. |
| 15 | |
| 16 | from __future__ import print_function |
| 17 | from bcc import BPF |
| 18 | from time import sleep, strftime |
| 19 | import argparse |
| 20 | import signal |
| 21 | from subprocess import call |
| 22 | |
| 23 | # arguments |
| 24 | examples = """examples: |
| 25 | ./filetop # file I/O top, 1 second refresh |
| 26 | ./filetop -C # don't clear the screen |
| 27 | ./filetop -p 181 # PID 181 only |
| 28 | ./filetop 5 # 5 second summaries |
| 29 | ./filetop 5 10 # 5 second summaries, 10 times only |
| 30 | """ |
| 31 | parser = argparse.ArgumentParser( |
| 32 | description="File reads and writes by process", |
| 33 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 34 | epilog=examples) |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 35 | parser.add_argument("-a", "--all-files", action="store_true", |
| 36 | help="include non-regular file types (sockets, FIFOs, etc)") |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 37 | parser.add_argument("-C", "--noclear", action="store_true", |
| 38 | help="don't clear the screen") |
| 39 | parser.add_argument("-r", "--maxrows", default=20, |
| 40 | help="maximum rows to print, default 20") |
Daniel Neiter | caa38c5 | 2017-03-01 17:21:25 -0800 | [diff] [blame] | 41 | parser.add_argument("-s", "--sort", default="rbytes", |
| 42 | choices=["reads", "writes", "rbytes", "wbytes"], |
| 43 | help="sort column, default rbytes") |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 44 | parser.add_argument("-p", "--pid", type=int, metavar="PID", dest="tgid", |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 45 | help="trace this PID only") |
| 46 | parser.add_argument("interval", nargs="?", default=1, |
| 47 | help="output interval, in seconds") |
| 48 | parser.add_argument("count", nargs="?", default=99999999, |
| 49 | help="number of outputs") |
| 50 | args = parser.parse_args() |
| 51 | interval = int(args.interval) |
| 52 | countdown = int(args.count) |
| 53 | maxrows = int(args.maxrows) |
| 54 | clear = not int(args.noclear) |
| 55 | debug = 0 |
| 56 | |
| 57 | # linux stats |
| 58 | loadavg = "/proc/loadavg" |
| 59 | |
| 60 | # signal handler |
| 61 | def signal_ignore(signal, frame): |
| 62 | print() |
| 63 | |
| 64 | # define BPF program |
| 65 | bpf_text = """ |
| 66 | #include <uapi/linux/ptrace.h> |
| 67 | #include <linux/blkdev.h> |
| 68 | |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 69 | // the key for the output summary |
| 70 | struct info_t { |
| 71 | u32 pid; |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 72 | u32 name_len; |
| 73 | char comm[TASK_COMM_LEN]; |
| 74 | // de->d_name.name may point to de->d_iname so limit len accordingly |
| 75 | char name[DNAME_INLINE_LEN]; |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 76 | char type; |
| 77 | }; |
| 78 | |
| 79 | // the value of the output summary |
| 80 | struct val_t { |
| 81 | u64 reads; |
| 82 | u64 writes; |
| 83 | u64 rbytes; |
| 84 | u64 wbytes; |
| 85 | }; |
| 86 | |
| 87 | BPF_HASH(counts, struct info_t, struct val_t); |
| 88 | |
| 89 | static int do_entry(struct pt_regs *ctx, struct file *file, |
| 90 | char __user *buf, size_t count, int is_read) |
| 91 | { |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 92 | u32 tgid = bpf_get_current_pid_tgid() >> 32; |
| 93 | if (TGID_FILTER) |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 94 | return 0; |
| 95 | |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 96 | u32 pid = bpf_get_current_pid_tgid(); |
| 97 | |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 98 | // skip I/O lacking a filename |
| 99 | struct dentry *de = file->f_path.dentry; |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 100 | int mode = file->f_inode->i_mode; |
| 101 | if (de->d_name.len == 0 || TYPE_FILTER) |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 102 | return 0; |
| 103 | |
| 104 | // store counts and sizes by pid & file |
| 105 | struct info_t info = {.pid = pid}; |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 106 | bpf_get_current_comm(&info.comm, sizeof(info.comm)); |
| 107 | info.name_len = de->d_name.len; |
| 108 | bpf_probe_read(&info.name, sizeof(info.name), (void *)de->d_name.name); |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 109 | if (S_ISREG(mode)) { |
| 110 | info.type = 'R'; |
| 111 | } else if (S_ISSOCK(mode)) { |
| 112 | info.type = 'S'; |
| 113 | } else { |
| 114 | info.type = 'O'; |
| 115 | } |
| 116 | |
| 117 | struct val_t *valp, zero = {}; |
| 118 | valp = counts.lookup_or_init(&info, &zero); |
| 119 | if (is_read) { |
| 120 | valp->reads++; |
| 121 | valp->rbytes += count; |
| 122 | } else { |
| 123 | valp->writes++; |
| 124 | valp->wbytes += count; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int trace_read_entry(struct pt_regs *ctx, struct file *file, |
| 131 | char __user *buf, size_t count) |
| 132 | { |
| 133 | return do_entry(ctx, file, buf, count, 1); |
| 134 | } |
| 135 | |
| 136 | int trace_write_entry(struct pt_regs *ctx, struct file *file, |
| 137 | char __user *buf, size_t count) |
| 138 | { |
| 139 | return do_entry(ctx, file, buf, count, 0); |
| 140 | } |
| 141 | |
| 142 | """ |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 143 | if args.tgid: |
| 144 | bpf_text = bpf_text.replace('TGID_FILTER', 'tgid != %d' % args.tgid) |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 145 | else: |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 146 | bpf_text = bpf_text.replace('TGID_FILTER', '0') |
| 147 | if args.all_files: |
| 148 | bpf_text = bpf_text.replace('TYPE_FILTER', '0') |
| 149 | else: |
| 150 | bpf_text = bpf_text.replace('TYPE_FILTER', '!S_ISREG(mode)') |
| 151 | |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 152 | if debug: |
| 153 | print(bpf_text) |
| 154 | |
| 155 | # initialize BPF |
| 156 | b = BPF(text=bpf_text) |
| 157 | b.attach_kprobe(event="__vfs_read", fn_name="trace_read_entry") |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 158 | try: |
| 159 | b.attach_kprobe(event="__vfs_write", fn_name="trace_write_entry") |
| 160 | except: |
| 161 | # older kernels don't have __vfs_write so try vfs_write instead |
| 162 | b.attach_kprobe(event="vfs_write", fn_name="trace_write_entry") |
| 163 | |
| 164 | DNAME_INLINE_LEN = 32 # linux/dcache.h |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 165 | |
| 166 | print('Tracing... Output every %d secs. Hit Ctrl-C to end' % interval) |
| 167 | |
| 168 | # output |
| 169 | exiting = 0 |
| 170 | while 1: |
| 171 | try: |
| 172 | sleep(interval) |
| 173 | except KeyboardInterrupt: |
| 174 | exiting = 1 |
| 175 | |
| 176 | # header |
| 177 | if clear: |
| 178 | call("clear") |
| 179 | else: |
| 180 | print() |
| 181 | with open(loadavg) as stats: |
| 182 | print("%-8s loadavg: %s" % (strftime("%H:%M:%S"), stats.read())) |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 183 | print("%-6s %-16s %-6s %-6s %-7s %-7s %1s %s" % ("TID", "COMM", |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 184 | "READS", "WRITES", "R_Kb", "W_Kb", "T", "FILE")) |
| 185 | |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 186 | # by-TID output |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 187 | counts = b.get_table("counts") |
| 188 | line = 0 |
| 189 | for k, v in reversed(sorted(counts.items(), |
Daniel Neiter | caa38c5 | 2017-03-01 17:21:25 -0800 | [diff] [blame] | 190 | key=lambda counts: |
| 191 | getattr(counts[1], args.sort))): |
Rafael Fonseca | 2939265 | 2017-02-06 17:07:28 +0100 | [diff] [blame] | 192 | name = k.name.decode() |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 193 | if k.name_len > DNAME_INLINE_LEN: |
| 194 | name = name[:-3] + "..." |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 195 | |
| 196 | # print line |
Rafael Fonseca | 2939265 | 2017-02-06 17:07:28 +0100 | [diff] [blame] | 197 | print("%-6d %-16s %-6d %-6d %-7d %-7d %1s %s" % (k.pid, |
| 198 | k.comm.decode(), v.reads, v.writes, v.rbytes / 1024, |
| 199 | v.wbytes / 1024, k.type.decode(), name)) |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 200 | |
| 201 | line += 1 |
| 202 | if line >= maxrows: |
| 203 | break |
| 204 | counts.clear() |
| 205 | |
| 206 | countdown -= 1 |
| 207 | if exiting or countdown == 0: |
| 208 | print("Detaching...") |
| 209 | exit() |