blob: 9405af3aae27f4a62d82a24a15cfda358350e4bd [file] [log] [blame]
Brendan Greggdc642c52016-02-09 00:32:51 -08001#!/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.
mcaleavyacfc31502016-02-19 17:59:23 +000018# 17-Feb-2016 Allan McAleavy updated for BPF_PERF_OUTPUT
Brendan Greggdc642c52016-02-09 00:32:51 -080019
20from __future__ import print_function
21from bcc import BPF
22import argparse
23from time import strftime
mcaleavyacfc31502016-02-19 17:59:23 +000024import ctypes as ct
Brendan Greggdc642c52016-02-09 00:32:51 -080025
26# arguments
27examples = """examples:
28 ./filelife # trace all stat() syscalls
29 ./filelife -p 181 # only trace PID 181
30"""
31parser = argparse.ArgumentParser(
32 description="Trace stat() syscalls",
33 formatter_class=argparse.RawDescriptionHelpFormatter,
34 epilog=examples)
35parser.add_argument("-p", "--pid",
36 help="trace this PID only")
37args = parser.parse_args()
38debug = 0
39
40# define BPF program
41bpf_text = """
42#include <uapi/linux/ptrace.h>
43#include <linux/fs.h>
mcaleavyacfc31502016-02-19 17:59:23 +000044#include <linux/sched.h>
45
46struct data_t {
47 u32 pid;
48 u64 delta;
49 char comm[TASK_COMM_LEN];
50 char fname[DNAME_INLINE_LEN];
51};
Brendan Greggdc642c52016-02-09 00:32:51 -080052
53BPF_HASH(birth, struct dentry *);
mcaleavyacfc31502016-02-19 17:59:23 +000054BPF_PERF_OUTPUT(events);
Brendan Greggdc642c52016-02-09 00:32:51 -080055
56// trace file creation time
57int 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
69int trace_unlink(struct pt_regs *ctx, struct inode *dir, struct dentry *dentry)
70{
mcaleavyacfc31502016-02-19 17:59:23 +000071 struct data_t data = {};
Brendan Greggdc642c52016-02-09 00:32:51 -080072 u32 pid = bpf_get_current_pid_tgid();
mcaleavyacfc31502016-02-19 17:59:23 +000073
Brendan Greggdc642c52016-02-09 00:32:51 -080074 FILTER
75
76 u64 *tsp, delta;
77 tsp = birth.lookup(&dentry);
78 if (tsp == 0) {
79 return 0; // missed create
80 }
mcaleavyacfc31502016-02-19 17:59:23 +000081
Brendan Greggdc642c52016-02-09 00:32:51 -080082 delta = (bpf_ktime_get_ns() - *tsp) / 1000000;
83 birth.delete(&dentry);
84
Marco Leogrande66441862016-09-26 15:59:51 -070085 if (dentry->d_name.len == 0)
Brendan Greggdc642c52016-02-09 00:32:51 -080086 return 0;
87
mcaleavyacfc31502016-02-19 17:59:23 +000088 if (bpf_get_current_comm(&data.comm, sizeof(data.comm)) == 0) {
89 data.pid = pid;
90 data.delta = delta;
Marco Leogrande66441862016-09-26 15:59:51 -070091 bpf_probe_read(&data.fname, sizeof(data.fname),
92 (void *)dentry->d_name.name);
mcaleavyacfc31502016-02-19 17:59:23 +000093 }
94
95 events.perf_submit(ctx, &data, sizeof(data));
Brendan Greggdc642c52016-02-09 00:32:51 -080096
97 return 0;
98}
99"""
mcaleavyacfc31502016-02-19 17:59:23 +0000100
101TASK_COMM_LEN = 16 # linux/sched.h
102DNAME_INLINE_LEN = 255 # linux/dcache.h
103
104class Data(ct.Structure):
105 _fields_ = [
Nan Xiaoe12f55a2017-08-17 10:56:36 +0800106 ("pid", ct.c_uint),
mcaleavyacfc31502016-02-19 17:59:23 +0000107 ("delta", ct.c_ulonglong),
108 ("comm", ct.c_char * TASK_COMM_LEN),
109 ("fname", ct.c_char * DNAME_INLINE_LEN)
110 ]
111
Brendan Greggdc642c52016-02-09 00:32:51 -0800112if args.pid:
113 bpf_text = bpf_text.replace('FILTER',
114 'if (pid != %s) { return 0; }' % args.pid)
115else:
116 bpf_text = bpf_text.replace('FILTER', '')
117if debug:
118 print(bpf_text)
119
120# initialize BPF
121b = BPF(text=bpf_text)
122b.attach_kprobe(event="vfs_create", fn_name="trace_create")
Brendan Greggba404cf2016-10-04 18:17:16 -0700123# newer kernels (say, 4.8) may don't fire vfs_create, so record (or overwrite)
124# the timestamp in security_inode_create():
125b.attach_kprobe(event="security_inode_create", fn_name="trace_create")
Brendan Greggdc642c52016-02-09 00:32:51 -0800126b.attach_kprobe(event="vfs_unlink", fn_name="trace_unlink")
127
128# header
129print("%-8s %-6s %-16s %-7s %s" % ("TIME", "PID", "COMM", "AGE(s)", "FILE"))
130
mcaleavyacfc31502016-02-19 17:59:23 +0000131# process event
132def 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 F78948e42017-03-26 14:54:25 +0200135 event.comm.decode(), float(event.delta) / 1000, event.fname.decode()))
Brendan Greggdc642c52016-02-09 00:32:51 -0800136
mcaleavyacfc31502016-02-19 17:59:23 +0000137b["events"].open_perf_buffer(print_event)
Brendan Greggdc642c52016-02-09 00:32:51 -0800138while 1:
mcaleavyacfc31502016-02-19 17:59:23 +0000139 b.kprobe_poll()