Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # dcsnoop Trace directory entry cache (dcache) lookups. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
| 6 | # |
| 7 | # USAGE: dcsnoop [-h] [-a] |
| 8 | # |
| 9 | # By default, this traces every failed dcache lookup, and shows the process |
| 10 | # performing the lookup and the filename requested. A -a option can be used |
| 11 | # to show all lookups, not just failed ones. |
| 12 | # |
| 13 | # This uses kernel dynamic tracing of the d_lookup() function, and will need |
| 14 | # to be modified to match kernel changes. |
| 15 | # |
| 16 | # Also see dcstat(8), for per-second summaries. |
| 17 | # |
| 18 | # Copyright 2016 Netflix, Inc. |
| 19 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 20 | # |
| 21 | # 09-Feb-2016 Brendan Gregg Created this. |
| 22 | |
| 23 | from __future__ import print_function |
| 24 | from bcc import BPF |
| 25 | import argparse |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 26 | import ctypes as ct |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 27 | import re |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 28 | import time |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 29 | |
| 30 | # arguments |
| 31 | examples = """examples: |
| 32 | ./dcsnoop # trace failed dcache lookups |
| 33 | ./dcsnoop -a # trace all dcache lookups |
| 34 | """ |
| 35 | parser = argparse.ArgumentParser( |
| 36 | description="Trace directory entry cache (dcache) lookups", |
| 37 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 38 | epilog=examples) |
| 39 | parser.add_argument("-a", "--all", action="store_true", |
| 40 | help="trace all lookups (default is fails only)") |
| 41 | args = parser.parse_args() |
| 42 | |
| 43 | # define BPF program |
| 44 | bpf_text = """ |
| 45 | #include <uapi/linux/ptrace.h> |
| 46 | #include <linux/fs.h> |
| 47 | #include <linux/sched.h> |
| 48 | |
| 49 | #define MAX_FILE_LEN 64 |
| 50 | |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 51 | enum lookup_type { |
| 52 | LOOKUP_MISS, |
| 53 | LOOKUP_REFERENCE, |
| 54 | }; |
| 55 | |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 56 | struct entry_t { |
| 57 | char name[MAX_FILE_LEN]; |
| 58 | }; |
| 59 | |
| 60 | BPF_HASH(entrybypid, u32, struct entry_t); |
| 61 | |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 62 | struct data_t { |
| 63 | u32 pid; |
| 64 | enum lookup_type type; |
| 65 | char comm[TASK_COMM_LEN]; |
| 66 | char filename[MAX_FILE_LEN]; |
| 67 | }; |
| 68 | |
| 69 | BPF_PERF_OUTPUT(events); |
| 70 | |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 71 | /* from fs/namei.c: */ |
| 72 | struct nameidata { |
| 73 | struct path path; |
| 74 | struct qstr last; |
| 75 | // [...] |
| 76 | }; |
| 77 | |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 78 | static inline |
| 79 | void submit_event(struct pt_regs *ctx, void *name, int type, u32 pid) |
| 80 | { |
| 81 | struct data_t data = { |
| 82 | .pid = pid, |
| 83 | .type = type, |
| 84 | }; |
| 85 | bpf_get_current_comm(&data.comm, sizeof(data.comm)); |
| 86 | bpf_probe_read(&data.filename, sizeof(data.filename), name); |
| 87 | events.perf_submit(ctx, &data, sizeof(data)); |
| 88 | } |
| 89 | |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 90 | int trace_fast(struct pt_regs *ctx, struct nameidata *nd, struct path *path) |
| 91 | { |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 92 | u32 pid = bpf_get_current_pid_tgid(); |
| 93 | submit_event(ctx, (void *)nd->last.name, LOOKUP_REFERENCE, pid); |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | int kprobe__d_lookup(struct pt_regs *ctx, const struct dentry *parent, |
| 98 | const struct qstr *name) |
| 99 | { |
| 100 | u32 pid = bpf_get_current_pid_tgid(); |
| 101 | struct entry_t entry = {}; |
Brendan Gregg | d18657e | 2016-02-10 16:38:18 -0800 | [diff] [blame] | 102 | const char *fname = name->name; |
| 103 | if (fname) { |
| 104 | bpf_probe_read(&entry.name, sizeof(entry.name), (void *)fname); |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 105 | } |
| 106 | entrybypid.update(&pid, &entry); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int kretprobe__d_lookup(struct pt_regs *ctx) |
| 111 | { |
| 112 | u32 pid = bpf_get_current_pid_tgid(); |
| 113 | struct entry_t *ep; |
| 114 | ep = entrybypid.lookup(&pid); |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 115 | if (ep == 0 || PT_REGS_RC(ctx) != 0) { |
| 116 | return 0; // missed entry or lookup didn't fail |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 117 | } |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 118 | submit_event(ctx, (void *)ep->name, LOOKUP_MISS, pid); |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 119 | entrybypid.delete(&pid); |
| 120 | return 0; |
| 121 | } |
| 122 | """ |
| 123 | |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 124 | TASK_COMM_LEN = 16 # linux/sched.h |
| 125 | MAX_FILE_LEN = 64 # see inline C |
| 126 | |
| 127 | class Data(ct.Structure): |
| 128 | _fields_ = [ |
| 129 | ("pid", ct.c_uint), |
| 130 | ("type", ct.c_int), |
| 131 | ("comm", ct.c_char * TASK_COMM_LEN), |
| 132 | ("filename", ct.c_char * MAX_FILE_LEN), |
| 133 | ] |
| 134 | |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 135 | # initialize BPF |
| 136 | b = BPF(text=bpf_text) |
| 137 | if args.all: |
| 138 | b.attach_kprobe(event="lookup_fast", fn_name="trace_fast") |
| 139 | |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 140 | mode_s = { |
| 141 | 0: 'M', |
| 142 | 1: 'R', |
| 143 | } |
| 144 | |
| 145 | start_ts = time.time() |
| 146 | |
| 147 | def print_event(cpu, data, size): |
| 148 | event = ct.cast(data, ct.POINTER(Data)).contents |
| 149 | print("%-11.6f %-6d %-16s %1s %s" % ( |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 150 | time.time() - start_ts, event.pid, event.comm.decode(), |
| 151 | mode_s[event.type], event.filename.decode())) |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 152 | |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 153 | # header |
| 154 | print("%-11s %-6s %-16s %1s %s" % ("TIME(s)", "PID", "COMM", "T", "FILE")) |
| 155 | |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 156 | b["events"].open_perf_buffer(print_event, page_cnt=64) |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 157 | while 1: |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 158 | b.kprobe_poll() |