blob: 45fc6be1c6f418ba6d7360cded901aca964fdf19 [file] [log] [blame]
Brendan Gregg2757f0e2016-02-10 01:38:32 -08001#!/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
23from __future__ import print_function
24from bcc import BPF
25import argparse
Mark Drayton44b4b5f2016-07-13 18:15:05 +010026import ctypes as ct
Brendan Gregg2757f0e2016-02-10 01:38:32 -080027import re
Mark Drayton44b4b5f2016-07-13 18:15:05 +010028import time
Brendan Gregg2757f0e2016-02-10 01:38:32 -080029
30# arguments
31examples = """examples:
32 ./dcsnoop # trace failed dcache lookups
33 ./dcsnoop -a # trace all dcache lookups
34"""
35parser = argparse.ArgumentParser(
36 description="Trace directory entry cache (dcache) lookups",
37 formatter_class=argparse.RawDescriptionHelpFormatter,
38 epilog=examples)
39parser.add_argument("-a", "--all", action="store_true",
40 help="trace all lookups (default is fails only)")
41args = parser.parse_args()
42
43# define BPF program
44bpf_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 Drayton44b4b5f2016-07-13 18:15:05 +010051enum lookup_type {
52 LOOKUP_MISS,
53 LOOKUP_REFERENCE,
54};
55
Brendan Gregg2757f0e2016-02-10 01:38:32 -080056struct entry_t {
57 char name[MAX_FILE_LEN];
58};
59
60BPF_HASH(entrybypid, u32, struct entry_t);
61
Mark Drayton44b4b5f2016-07-13 18:15:05 +010062struct data_t {
63 u32 pid;
64 enum lookup_type type;
65 char comm[TASK_COMM_LEN];
66 char filename[MAX_FILE_LEN];
67};
68
69BPF_PERF_OUTPUT(events);
70
Brendan Gregg2757f0e2016-02-10 01:38:32 -080071/* from fs/namei.c: */
72struct nameidata {
73 struct path path;
74 struct qstr last;
75 // [...]
76};
77
Mark Drayton44b4b5f2016-07-13 18:15:05 +010078static inline
79void 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 Gregg2757f0e2016-02-10 01:38:32 -080090int trace_fast(struct pt_regs *ctx, struct nameidata *nd, struct path *path)
91{
Mark Drayton44b4b5f2016-07-13 18:15:05 +010092 u32 pid = bpf_get_current_pid_tgid();
93 submit_event(ctx, (void *)nd->last.name, LOOKUP_REFERENCE, pid);
Brendan Gregg2757f0e2016-02-10 01:38:32 -080094 return 1;
95}
96
97int 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 Greggd18657e2016-02-10 16:38:18 -0800102 const char *fname = name->name;
103 if (fname) {
104 bpf_probe_read(&entry.name, sizeof(entry.name), (void *)fname);
Brendan Gregg2757f0e2016-02-10 01:38:32 -0800105 }
106 entrybypid.update(&pid, &entry);
107 return 0;
108}
109
110int 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 Drayton44b4b5f2016-07-13 18:15:05 +0100115 if (ep == 0 || PT_REGS_RC(ctx) != 0) {
116 return 0; // missed entry or lookup didn't fail
Brendan Gregg2757f0e2016-02-10 01:38:32 -0800117 }
Mark Drayton44b4b5f2016-07-13 18:15:05 +0100118 submit_event(ctx, (void *)ep->name, LOOKUP_MISS, pid);
Brendan Gregg2757f0e2016-02-10 01:38:32 -0800119 entrybypid.delete(&pid);
120 return 0;
121}
122"""
123
Mark Drayton44b4b5f2016-07-13 18:15:05 +0100124TASK_COMM_LEN = 16 # linux/sched.h
125MAX_FILE_LEN = 64 # see inline C
126
127class 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 Gregg2757f0e2016-02-10 01:38:32 -0800135# initialize BPF
136b = BPF(text=bpf_text)
137if args.all:
138 b.attach_kprobe(event="lookup_fast", fn_name="trace_fast")
139
Mark Drayton44b4b5f2016-07-13 18:15:05 +0100140mode_s = {
141 0: 'M',
142 1: 'R',
143}
144
145start_ts = time.time()
146
147def print_event(cpu, data, size):
148 event = ct.cast(data, ct.POINTER(Data)).contents
149 print("%-11.6f %-6d %-16s %1s %s" % (
Rafael F78948e42017-03-26 14:54:25 +0200150 time.time() - start_ts, event.pid, event.comm.decode(),
151 mode_s[event.type], event.filename.decode()))
Mark Drayton44b4b5f2016-07-13 18:15:05 +0100152
Brendan Gregg2757f0e2016-02-10 01:38:32 -0800153# header
154print("%-11s %-6s %-16s %1s %s" % ("TIME(s)", "PID", "COMM", "T", "FILE"))
155
Mark Drayton5f5687e2017-02-20 18:13:03 +0000156b["events"].open_perf_buffer(print_event, page_cnt=64)
Brendan Gregg2757f0e2016-02-10 01:38:32 -0800157while 1:
Mark Drayton44b4b5f2016-07-13 18:15:05 +0100158 b.kprobe_poll()