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") |
| 37 | args = parser.parse_args() |
| 38 | debug = 0 |
| 39 | |
| 40 | # define BPF program |
| 41 | bpf_text = """ |
| 42 | #include <uapi/linux/ptrace.h> |
| 43 | #include <linux/fs.h> |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 44 | #include <linux/sched.h> |
| 45 | |
| 46 | struct data_t { |
| 47 | u32 pid; |
| 48 | u64 delta; |
| 49 | char comm[TASK_COMM_LEN]; |
| 50 | char fname[DNAME_INLINE_LEN]; |
| 51 | }; |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 52 | |
| 53 | BPF_HASH(birth, struct dentry *); |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 54 | BPF_PERF_OUTPUT(events); |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 55 | |
| 56 | // trace file creation time |
| 57 | int trace_create(struct pt_regs *ctx, struct inode *dir, struct dentry *dentry) |
| 58 | { |
| 59 | u32 pid = bpf_get_current_pid_tgid(); |
| 60 | FILTER |
| 61 | |
| 62 | u64 ts = bpf_ktime_get_ns(); |
| 63 | birth.update(&dentry, &ts); |
| 64 | |
| 65 | return 0; |
| 66 | }; |
| 67 | |
| 68 | // trace file deletion and output details |
| 69 | int trace_unlink(struct pt_regs *ctx, struct inode *dir, struct dentry *dentry) |
| 70 | { |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 71 | struct data_t data = {}; |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 72 | u32 pid = bpf_get_current_pid_tgid(); |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 73 | |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 74 | FILTER |
| 75 | |
| 76 | u64 *tsp, delta; |
| 77 | tsp = birth.lookup(&dentry); |
| 78 | if (tsp == 0) { |
| 79 | return 0; // missed create |
| 80 | } |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 81 | |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 82 | delta = (bpf_ktime_get_ns() - *tsp) / 1000000; |
| 83 | birth.delete(&dentry); |
| 84 | |
Marco Leogrande | 6644186 | 2016-09-26 15:59:51 -0700 | [diff] [blame] | 85 | if (dentry->d_name.len == 0) |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 86 | return 0; |
| 87 | |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 88 | if (bpf_get_current_comm(&data.comm, sizeof(data.comm)) == 0) { |
| 89 | data.pid = pid; |
| 90 | data.delta = delta; |
Marco Leogrande | 6644186 | 2016-09-26 15:59:51 -0700 | [diff] [blame] | 91 | bpf_probe_read(&data.fname, sizeof(data.fname), |
| 92 | (void *)dentry->d_name.name); |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | events.perf_submit(ctx, &data, sizeof(data)); |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | """ |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 100 | |
| 101 | TASK_COMM_LEN = 16 # linux/sched.h |
| 102 | DNAME_INLINE_LEN = 255 # linux/dcache.h |
| 103 | |
| 104 | class Data(ct.Structure): |
| 105 | _fields_ = [ |
Nan Xiao | e12f55a | 2017-08-17 10:56:36 +0800 | [diff] [blame] | 106 | ("pid", ct.c_uint), |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 107 | ("delta", ct.c_ulonglong), |
| 108 | ("comm", ct.c_char * TASK_COMM_LEN), |
| 109 | ("fname", ct.c_char * DNAME_INLINE_LEN) |
| 110 | ] |
| 111 | |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 112 | if args.pid: |
| 113 | bpf_text = bpf_text.replace('FILTER', |
| 114 | 'if (pid != %s) { return 0; }' % args.pid) |
| 115 | else: |
| 116 | bpf_text = bpf_text.replace('FILTER', '') |
| 117 | if debug: |
| 118 | print(bpf_text) |
| 119 | |
| 120 | # initialize BPF |
| 121 | b = BPF(text=bpf_text) |
| 122 | b.attach_kprobe(event="vfs_create", fn_name="trace_create") |
Brendan Gregg | ba404cf | 2016-10-04 18:17:16 -0700 | [diff] [blame] | 123 | # newer kernels (say, 4.8) may don't fire vfs_create, so record (or overwrite) |
| 124 | # the timestamp in security_inode_create(): |
| 125 | b.attach_kprobe(event="security_inode_create", fn_name="trace_create") |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 126 | b.attach_kprobe(event="vfs_unlink", fn_name="trace_unlink") |
| 127 | |
| 128 | # header |
| 129 | print("%-8s %-6s %-16s %-7s %s" % ("TIME", "PID", "COMM", "AGE(s)", "FILE")) |
| 130 | |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 131 | # process event |
| 132 | def print_event(cpu, data, size): |
| 133 | event = ct.cast(data, ct.POINTER(Data)).contents |
| 134 | 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] | 135 | event.comm.decode(), float(event.delta) / 1000, event.fname.decode())) |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 136 | |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 137 | b["events"].open_perf_buffer(print_event) |
Brendan Gregg | dc642c5 | 2016-02-09 00:32:51 -0800 | [diff] [blame] | 138 | while 1: |
mcaleavya | cfc3150 | 2016-02-19 17:59:23 +0000 | [diff] [blame] | 139 | b.kprobe_poll() |