Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # filelife Trace the lifespan of short-lived files. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
| 6 | # |
| 7 | # This traces the creation and deletion of files, providing information |
| 8 | # on who deleted the file, the file age, and the file name. The intent is to |
| 9 | # provide information on short-lived files, for debugging or performance |
| 10 | # analysis. |
| 11 | # |
| 12 | # USAGE: filelife [-h] [-p PID] |
| 13 | # |
| 14 | # Copyright 2016 Netflix, Inc. |
| 15 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 16 | # |
| 17 | # 08-Feb-2015 Brendan Gregg Created this. |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 18 | # 17-Feb-2016 Allan McAleavy updated for BPF_PERF_OUTPUT |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 19 | |
| 20 | from __future__ import print_function |
| 21 | from bcc import BPF |
| 22 | import argparse |
| 23 | from time import strftime |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 24 | import ctypes as ct |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 25 | |
| 26 | # arguments |
| 27 | examples = """examples: |
| 28 | ./filelife # trace all stat() syscalls |
| 29 | ./filelife -p 181 # only trace PID 181 |
| 30 | """ |
| 31 | parser = argparse.ArgumentParser( |
| 32 | description="Trace stat() syscalls", |
| 33 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 34 | epilog=examples) |
| 35 | parser.add_argument("-p", "--pid", |
| 36 | help="trace this PID only") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 37 | parser.add_argument("--ebpf", action="store_true", |
| 38 | help=argparse.SUPPRESS) |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 39 | args = parser.parse_args() |
| 40 | debug = 0 |
| 41 | |
| 42 | # define BPF program |
| 43 | bpf_text = """ |
| 44 | #include <uapi/linux/ptrace.h> |
| 45 | #include <linux/fs.h> |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 46 | #include <linux/sched.h> |
| 47 | |
| 48 | struct data_t { |
| 49 | u32 pid; |
| 50 | u64 delta; |
| 51 | char comm[TASK_COMM_LEN]; |
| 52 | char fname[DNAME_INLINE_LEN]; |
| 53 | }; |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 54 | |
| 55 | BPF_HASH(birth, struct dentry *); |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 56 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 57 | |
| 58 | // trace file creation time |
| 59 | int trace_create(struct pt_regs *ctx, struct inode *dir, struct dentry *dentry) |
| 60 | { |
| 61 | u32 pid = bpf_get_current_pid_tgid(); |
| 62 | FILTER |
| 63 | |
| 64 | u64 ts = bpf_ktime_get_ns(); |
| 65 | birth.update(&dentry, &ts); |
| 66 | |
| 67 | return 0; |
| 68 | }; |
| 69 | |
| 70 | // trace file deletion and output details |
| 71 | int trace_unlink(struct pt_regs *ctx, struct inode *dir, struct dentry *dentry) |
| 72 | { |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 73 | struct data_t data = {}; |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 74 | u32 pid = bpf_get_current_pid_tgid(); |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 75 | |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 76 | FILTER |
| 77 | |
| 78 | u64 *tsp, delta; |
| 79 | tsp = birth.lookup(&dentry); |
| 80 | if (tsp == 0) { |
| 81 | return 0; // missed create |
| 82 | } |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 83 | |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 84 | delta = (bpf_ktime_get_ns() - *tsp) / 1000000; |
| 85 | birth.delete(&dentry); |
| 86 | |
Marco Leogrande | 6644186 | 2016-09-26 15:59:51 -0700 | [diff] [blame] | 87 | if (dentry->d_name.len == 0) |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 88 | return 0; |
| 89 | |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 90 | if (bpf_get_current_comm(&data.comm, sizeof(data.comm)) == 0) { |
| 91 | data.pid = pid; |
| 92 | data.delta = delta; |
Marco Leogrande | 6644186 | 2016-09-26 15:59:51 -0700 | [diff] [blame] | 93 | bpf_probe_read(&data.fname, sizeof(data.fname), |
| 94 | (void *)dentry->d_name.name); |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | events.perf_submit(ctx, &data, sizeof(data)); |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | """ |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 102 | |
| 103 | TASK_COMM_LEN = 16 # linux/sched.h |
| 104 | DNAME_INLINE_LEN = 255 # linux/dcache.h |
| 105 | |
| 106 | class Data(ct.Structure): |
| 107 | _fields_ = [ |
Nan Xiao | e12f55a | 2017-08-17 10:56:36 +0800 | [diff] [blame] | 108 | ("pid", ct.c_uint), |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 109 | ("delta", ct.c_ulonglong), |
| 110 | ("comm", ct.c_char * TASK_COMM_LEN), |
| 111 | ("fname", ct.c_char * DNAME_INLINE_LEN) |
| 112 | ] |
| 113 | |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 114 | if args.pid: |
| 115 | bpf_text = bpf_text.replace('FILTER', |
| 116 | 'if (pid != %s) { return 0; }' % args.pid) |
| 117 | else: |
| 118 | bpf_text = bpf_text.replace('FILTER', '') |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 119 | if debug or args.ebpf: |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 120 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 121 | if args.ebpf: |
| 122 | exit() |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 123 | |
| 124 | # initialize BPF |
| 125 | b = BPF(text=bpf_text) |
| 126 | b.attach_kprobe(event="vfs_create", fn_name="trace_create") |
Brendan Gregg | ba404cf | 2016-10-04 18:17:16 -0700 | [diff] [blame] | 127 | # newer kernels (say, 4.8) may don't fire vfs_create, so record (or overwrite) |
| 128 | # the timestamp in security_inode_create(): |
| 129 | b.attach_kprobe(event="security_inode_create", fn_name="trace_create") |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 130 | b.attach_kprobe(event="vfs_unlink", fn_name="trace_unlink") |
| 131 | |
| 132 | # header |
| 133 | print("%-8s %-6s %-16s %-7s %s" % ("TIME", "PID", "COMM", "AGE(s)", "FILE")) |
| 134 | |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 135 | # process event |
| 136 | def print_event(cpu, data, size): |
| 137 | event = ct.cast(data, ct.POINTER(Data)).contents |
| 138 | print("%-8s %-6d %-16s %-7.2f %s" % (strftime("%H:%M:%S"), event.pid, |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 139 | event.comm.decode(), float(event.delta) / 1000, event.fname.decode())) |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 140 | |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 141 | b["events"].open_perf_buffer(print_event) |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 142 | while 1: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 143 | b.perf_buffer_poll() |