Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 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") |
Joel Fernandes (Google) | c2b371d | 2019-03-08 11:39:42 -0500 | [diff] [blame] | 41 | parser.add_argument("-s", "--sort", default="all", |
| 42 | choices=["all", "reads", "writes", "rbytes", "wbytes"], |
Daniel Neiter | caa38c5 | 2017-03-01 17:21:25 -0800 | [diff] [blame] | 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") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 50 | parser.add_argument("--ebpf", action="store_true", |
| 51 | help=argparse.SUPPRESS) |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 52 | args = parser.parse_args() |
| 53 | interval = int(args.interval) |
| 54 | countdown = int(args.count) |
| 55 | maxrows = int(args.maxrows) |
| 56 | clear = not int(args.noclear) |
| 57 | debug = 0 |
| 58 | |
| 59 | # linux stats |
| 60 | loadavg = "/proc/loadavg" |
| 61 | |
| 62 | # signal handler |
Teng Qin | aaca976 | 2019-01-11 11:18:45 -0800 | [diff] [blame] | 63 | def signal_ignore(signal_value, frame): |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 64 | print() |
| 65 | |
| 66 | # define BPF program |
| 67 | bpf_text = """ |
| 68 | #include <uapi/linux/ptrace.h> |
| 69 | #include <linux/blkdev.h> |
| 70 | |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 71 | // the key for the output summary |
| 72 | struct info_t { |
| 73 | u32 pid; |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 74 | u32 name_len; |
| 75 | char comm[TASK_COMM_LEN]; |
| 76 | // de->d_name.name may point to de->d_iname so limit len accordingly |
| 77 | char name[DNAME_INLINE_LEN]; |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 78 | char type; |
| 79 | }; |
| 80 | |
| 81 | // the value of the output summary |
| 82 | struct val_t { |
| 83 | u64 reads; |
| 84 | u64 writes; |
| 85 | u64 rbytes; |
| 86 | u64 wbytes; |
| 87 | }; |
| 88 | |
| 89 | BPF_HASH(counts, struct info_t, struct val_t); |
| 90 | |
| 91 | static int do_entry(struct pt_regs *ctx, struct file *file, |
| 92 | char __user *buf, size_t count, int is_read) |
| 93 | { |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 94 | u32 tgid = bpf_get_current_pid_tgid() >> 32; |
| 95 | if (TGID_FILTER) |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 96 | return 0; |
| 97 | |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 98 | u32 pid = bpf_get_current_pid_tgid(); |
| 99 | |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 100 | // skip I/O lacking a filename |
| 101 | struct dentry *de = file->f_path.dentry; |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 102 | int mode = file->f_inode->i_mode; |
Paul Chaignon | f86f7e8 | 2018-06-14 02:20:03 +0200 | [diff] [blame] | 103 | struct qstr d_name = de->d_name; |
| 104 | if (d_name.len == 0 || TYPE_FILTER) |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 105 | return 0; |
| 106 | |
| 107 | // store counts and sizes by pid & file |
| 108 | struct info_t info = {.pid = pid}; |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 109 | bpf_get_current_comm(&info.comm, sizeof(info.comm)); |
Paul Chaignon | f86f7e8 | 2018-06-14 02:20:03 +0200 | [diff] [blame] | 110 | info.name_len = d_name.len; |
| 111 | bpf_probe_read(&info.name, sizeof(info.name), d_name.name); |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 112 | if (S_ISREG(mode)) { |
| 113 | info.type = 'R'; |
| 114 | } else if (S_ISSOCK(mode)) { |
| 115 | info.type = 'S'; |
| 116 | } else { |
| 117 | info.type = 'O'; |
| 118 | } |
| 119 | |
| 120 | struct val_t *valp, zero = {}; |
| 121 | valp = counts.lookup_or_init(&info, &zero); |
Philip Gladstone | ba64f03 | 2019-09-20 01:12:01 -0400 | [diff] [blame^] | 122 | if (valp) { |
| 123 | if (is_read) { |
| 124 | valp->reads++; |
| 125 | valp->rbytes += count; |
| 126 | } else { |
| 127 | valp->writes++; |
| 128 | valp->wbytes += count; |
| 129 | } |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | int trace_read_entry(struct pt_regs *ctx, struct file *file, |
| 136 | char __user *buf, size_t count) |
| 137 | { |
| 138 | return do_entry(ctx, file, buf, count, 1); |
| 139 | } |
| 140 | |
| 141 | int trace_write_entry(struct pt_regs *ctx, struct file *file, |
| 142 | char __user *buf, size_t count) |
| 143 | { |
| 144 | return do_entry(ctx, file, buf, count, 0); |
| 145 | } |
| 146 | |
| 147 | """ |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 148 | if args.tgid: |
| 149 | bpf_text = bpf_text.replace('TGID_FILTER', 'tgid != %d' % args.tgid) |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 150 | else: |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 151 | bpf_text = bpf_text.replace('TGID_FILTER', '0') |
| 152 | if args.all_files: |
| 153 | bpf_text = bpf_text.replace('TYPE_FILTER', '0') |
| 154 | else: |
| 155 | bpf_text = bpf_text.replace('TYPE_FILTER', '!S_ISREG(mode)') |
| 156 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 157 | if debug or args.ebpf: |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 158 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 159 | if args.ebpf: |
| 160 | exit() |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 161 | |
| 162 | # initialize BPF |
| 163 | b = BPF(text=bpf_text) |
Jazel Canseco | 6d818b6 | 2018-04-13 11:39:56 -0700 | [diff] [blame] | 164 | b.attach_kprobe(event="vfs_read", fn_name="trace_read_entry") |
| 165 | b.attach_kprobe(event="vfs_write", fn_name="trace_write_entry") |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 166 | |
| 167 | DNAME_INLINE_LEN = 32 # linux/dcache.h |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 168 | |
| 169 | print('Tracing... Output every %d secs. Hit Ctrl-C to end' % interval) |
| 170 | |
Joel Fernandes (Google) | c2b371d | 2019-03-08 11:39:42 -0500 | [diff] [blame] | 171 | def sort_fn(counts): |
| 172 | if args.sort == "all": |
| 173 | return (counts[1].rbytes + counts[1].wbytes + counts[1].reads + counts[1].writes) |
| 174 | else: |
| 175 | return getattr(counts[1], args.sort) |
| 176 | |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 177 | # output |
| 178 | exiting = 0 |
| 179 | while 1: |
| 180 | try: |
| 181 | sleep(interval) |
| 182 | except KeyboardInterrupt: |
| 183 | exiting = 1 |
| 184 | |
| 185 | # header |
| 186 | if clear: |
| 187 | call("clear") |
| 188 | else: |
| 189 | print() |
| 190 | with open(loadavg) as stats: |
| 191 | print("%-8s loadavg: %s" % (strftime("%H:%M:%S"), stats.read())) |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 192 | print("%-6s %-16s %-6s %-6s %-7s %-7s %1s %s" % ("TID", "COMM", |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 193 | "READS", "WRITES", "R_Kb", "W_Kb", "T", "FILE")) |
| 194 | |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 195 | # by-TID output |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 196 | counts = b.get_table("counts") |
| 197 | line = 0 |
| 198 | for k, v in reversed(sorted(counts.items(), |
Joel Fernandes (Google) | c2b371d | 2019-03-08 11:39:42 -0500 | [diff] [blame] | 199 | key=sort_fn)): |
jeromemarchand | b96ebcd | 2018-10-10 01:58:15 +0200 | [diff] [blame] | 200 | name = k.name.decode('utf-8', 'replace') |
Mark Drayton | 7434731 | 2016-08-25 20:46:35 +0100 | [diff] [blame] | 201 | if k.name_len > DNAME_INLINE_LEN: |
| 202 | name = name[:-3] + "..." |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 203 | |
| 204 | # print line |
Rafael Fonseca | 2939265 | 2017-02-06 17:07:28 +0100 | [diff] [blame] | 205 | print("%-6d %-16s %-6d %-6d %-7d %-7d %1s %s" % (k.pid, |
jeromemarchand | b96ebcd | 2018-10-10 01:58:15 +0200 | [diff] [blame] | 206 | k.comm.decode('utf-8', 'replace'), v.reads, v.writes, |
| 207 | v.rbytes / 1024, v.wbytes / 1024, |
| 208 | k.type.decode('utf-8', 'replace'), name)) |
Brendan Gregg | 08c2981 | 2016-02-09 00:36:43 -0800 | [diff] [blame] | 209 | |
| 210 | line += 1 |
| 211 | if line >= maxrows: |
| 212 | break |
| 213 | counts.clear() |
| 214 | |
| 215 | countdown -= 1 |
| 216 | if exiting or countdown == 0: |
| 217 | print("Detaching...") |
| 218 | exit() |