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)") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 41 | parser.add_argument("--ebpf", action="store_true", |
| 42 | help=argparse.SUPPRESS) |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 43 | args = parser.parse_args() |
| 44 | |
| 45 | # define BPF program |
| 46 | bpf_text = """ |
| 47 | #include <uapi/linux/ptrace.h> |
| 48 | #include <linux/fs.h> |
| 49 | #include <linux/sched.h> |
| 50 | |
| 51 | #define MAX_FILE_LEN 64 |
| 52 | |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 53 | enum lookup_type { |
| 54 | LOOKUP_MISS, |
| 55 | LOOKUP_REFERENCE, |
| 56 | }; |
| 57 | |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 58 | struct entry_t { |
| 59 | char name[MAX_FILE_LEN]; |
| 60 | }; |
| 61 | |
| 62 | BPF_HASH(entrybypid, u32, struct entry_t); |
| 63 | |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 64 | struct data_t { |
| 65 | u32 pid; |
| 66 | enum lookup_type type; |
| 67 | char comm[TASK_COMM_LEN]; |
| 68 | char filename[MAX_FILE_LEN]; |
| 69 | }; |
| 70 | |
| 71 | BPF_PERF_OUTPUT(events); |
| 72 | |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 73 | /* from fs/namei.c: */ |
| 74 | struct nameidata { |
| 75 | struct path path; |
| 76 | struct qstr last; |
| 77 | // [...] |
| 78 | }; |
| 79 | |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 80 | static inline |
| 81 | void submit_event(struct pt_regs *ctx, void *name, int type, u32 pid) |
| 82 | { |
| 83 | struct data_t data = { |
| 84 | .pid = pid, |
| 85 | .type = type, |
| 86 | }; |
| 87 | bpf_get_current_comm(&data.comm, sizeof(data.comm)); |
| 88 | bpf_probe_read(&data.filename, sizeof(data.filename), name); |
| 89 | events.perf_submit(ctx, &data, sizeof(data)); |
| 90 | } |
| 91 | |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 92 | int trace_fast(struct pt_regs *ctx, struct nameidata *nd, struct path *path) |
| 93 | { |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 94 | u32 pid = bpf_get_current_pid_tgid(); |
| 95 | submit_event(ctx, (void *)nd->last.name, LOOKUP_REFERENCE, pid); |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | int kprobe__d_lookup(struct pt_regs *ctx, const struct dentry *parent, |
| 100 | const struct qstr *name) |
| 101 | { |
| 102 | u32 pid = bpf_get_current_pid_tgid(); |
| 103 | struct entry_t entry = {}; |
Brendan Gregg | d18657e | 2016-02-10 16:38:18 -0800 | [diff] [blame] | 104 | const char *fname = name->name; |
| 105 | if (fname) { |
| 106 | bpf_probe_read(&entry.name, sizeof(entry.name), (void *)fname); |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 107 | } |
| 108 | entrybypid.update(&pid, &entry); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int kretprobe__d_lookup(struct pt_regs *ctx) |
| 113 | { |
| 114 | u32 pid = bpf_get_current_pid_tgid(); |
| 115 | struct entry_t *ep; |
| 116 | ep = entrybypid.lookup(&pid); |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 117 | if (ep == 0 || PT_REGS_RC(ctx) != 0) { |
| 118 | return 0; // missed entry or lookup didn't fail |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 119 | } |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 120 | submit_event(ctx, (void *)ep->name, LOOKUP_MISS, pid); |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 121 | entrybypid.delete(&pid); |
| 122 | return 0; |
| 123 | } |
| 124 | """ |
| 125 | |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 126 | TASK_COMM_LEN = 16 # linux/sched.h |
| 127 | MAX_FILE_LEN = 64 # see inline C |
| 128 | |
| 129 | class Data(ct.Structure): |
| 130 | _fields_ = [ |
| 131 | ("pid", ct.c_uint), |
| 132 | ("type", ct.c_int), |
| 133 | ("comm", ct.c_char * TASK_COMM_LEN), |
| 134 | ("filename", ct.c_char * MAX_FILE_LEN), |
| 135 | ] |
| 136 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 137 | if args.ebpf: |
| 138 | print(bpf_text) |
| 139 | exit() |
| 140 | |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 141 | # initialize BPF |
| 142 | b = BPF(text=bpf_text) |
| 143 | if args.all: |
| 144 | b.attach_kprobe(event="lookup_fast", fn_name="trace_fast") |
| 145 | |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 146 | mode_s = { |
| 147 | 0: 'M', |
| 148 | 1: 'R', |
| 149 | } |
| 150 | |
| 151 | start_ts = time.time() |
| 152 | |
| 153 | def print_event(cpu, data, size): |
| 154 | event = ct.cast(data, ct.POINTER(Data)).contents |
| 155 | print("%-11.6f %-6d %-16s %1s %s" % ( |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 156 | time.time() - start_ts, event.pid, event.comm.decode(), |
| 157 | mode_s[event.type], event.filename.decode())) |
Mark Drayton | 44b4b5f | 2016-07-13 18:15:05 +0100 | [diff] [blame] | 158 | |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 159 | # header |
| 160 | print("%-11s %-6s %-16s %1s %s" % ("TIME(s)", "PID", "COMM", "T", "FILE")) |
| 161 | |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 162 | b["events"].open_perf_buffer(print_event, page_cnt=64) |
Brendan Gregg | 2757f0e | 2016-02-10 01:38:32 -0800 | [diff] [blame] | 163 | while 1: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame^] | 164 | b.perf_buffer_poll() |