blob: e2be68410583178a080ed32246511ae57f5726e5 [file] [log] [blame]
Brendan Greggbc54bb62016-02-14 23:13:13 -08001#!/usr/bin/python
2# @lint-avoid-python-3-compatibility-imports
3#
4# zfsslower Trace slow ZFS operations.
5# For Linux, uses BCC, eBPF.
6#
7# USAGE: zfsslower [-h] [-j] [-p PID] [min_ms]
8#
9# This script traces common ZFS file operations: reads, writes, opens, and
10# syncs. It measures the time spent in these operations, and prints details
11# for each that exceeded a threshold.
12#
13# WARNING: This adds low-overhead instrumentation to these ZFS operations,
14# including reads and writes from the file system cache. Such reads and writes
15# can be very frequent (depending on the workload; eg, 1M/sec), at which
16# point the overhead of this tool (even if it prints no "slower" events) can
17# begin to become significant.
18#
19# This works by using kernel dynamic tracing of the ZPL interface, and will
20# need updates to match any changes to this interface.
21#
22# By default, a minimum millisecond threshold of 10 is used.
23#
24# Copyright 2016 Netflix, Inc.
25# Licensed under the Apache License, Version 2.0 (the "License")
26#
27# 14-Feb-2016 Brendan Gregg Created this.
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +030028# 16-Oct-2016 Dina Goldshtein -p to filter by process ID.
Brendan Greggbc54bb62016-02-14 23:13:13 -080029
30from __future__ import print_function
31from bcc import BPF
32import argparse
33from time import strftime
34import ctypes as ct
35
36# arguments
37examples = """examples:
38 ./zfsslower # trace operations slower than 10 ms (default)
39 ./zfsslower 1 # trace operations slower than 1 ms
40 ./zfsslower -j 1 # ... 1 ms, parsable output (csv)
41 ./zfsslower 0 # trace all operations (warning: verbose)
42 ./zfsslower -p 185 # trace PID 185 only
43"""
44parser = argparse.ArgumentParser(
45 description="Trace common ZFS file operations slower than a threshold",
46 formatter_class=argparse.RawDescriptionHelpFormatter,
47 epilog=examples)
48parser.add_argument("-j", "--csv", action="store_true",
49 help="just print fields: comma-separated values")
50parser.add_argument("-p", "--pid",
51 help="trace this PID only")
52parser.add_argument("min_ms", nargs="?", default='10',
53 help="minimum I/O duration to trace, in ms (default 10)")
54args = parser.parse_args()
55min_ms = int(args.min_ms)
56pid = args.pid
57csv = args.csv
58debug = 0
59
60# define BPF program
61bpf_text = """
62#include <uapi/linux/ptrace.h>
63#include <linux/fs.h>
64#include <linux/sched.h>
65#include <linux/dcache.h>
66
67// XXX: switch these to char's when supported
68#define TRACE_READ 0
69#define TRACE_WRITE 1
70#define TRACE_OPEN 2
71#define TRACE_FSYNC 3
72
73struct val_t {
74 u64 ts;
75 u64 offset;
76 struct file *fp;
77};
78
79struct data_t {
80 // XXX: switch some to u32's when supported
81 u64 ts_us;
82 u64 type;
83 u64 size;
84 u64 offset;
85 u64 delta_us;
86 u64 pid;
87 char task[TASK_COMM_LEN];
88 char file[DNAME_INLINE_LEN];
89};
90
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +030091BPF_HASH(entryinfo, u64, struct val_t);
Brendan Greggbc54bb62016-02-14 23:13:13 -080092BPF_PERF_OUTPUT(events);
93
94//
95// Store timestamp and size on entry
96//
97
98// zpl_read(), zpl_write():
99int trace_rw_entry(struct pt_regs *ctx, struct file *filp, char __user *buf,
100 size_t len, loff_t *ppos)
101{
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300102 u64 id = bpf_get_current_pid_tgid();
103 u32 pid = id >> 32; // PID is higher part
104
Brendan Greggbc54bb62016-02-14 23:13:13 -0800105 if (FILTER_PID)
106 return 0;
107
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300108 // store filep and timestamp by id
Brendan Greggbc54bb62016-02-14 23:13:13 -0800109 struct val_t val = {};
110 val.ts = bpf_ktime_get_ns();
111 val.fp = filp;
112 val.offset = *ppos;
113 if (val.fp)
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300114 entryinfo.update(&id, &val);
Brendan Greggbc54bb62016-02-14 23:13:13 -0800115
116 return 0;
117}
118
119// zpl_open():
120int trace_open_entry(struct pt_regs *ctx, struct inode *inode,
121 struct file *filp)
122{
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300123 u64 id = bpf_get_current_pid_tgid();
124 u32 pid = id >> 32; // PID is higher part
125
Brendan Greggbc54bb62016-02-14 23:13:13 -0800126 if (FILTER_PID)
127 return 0;
128
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300129 // store filep and timestamp by id
Brendan Greggbc54bb62016-02-14 23:13:13 -0800130 struct val_t val = {};
131 val.ts = bpf_ktime_get_ns();
132 val.fp = filp;
133 val.offset = 0;
134 if (val.fp)
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300135 entryinfo.update(&id, &val);
Brendan Greggbc54bb62016-02-14 23:13:13 -0800136
137 return 0;
138}
139
140// zpl_fsync():
141int trace_fsync_entry(struct pt_regs *ctx, struct file *filp)
142{
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300143 u64 id = bpf_get_current_pid_tgid();
144 u32 pid = id >> 32; // PID is higher part
145
Brendan Greggbc54bb62016-02-14 23:13:13 -0800146 if (FILTER_PID)
147 return 0;
148
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300149 // store filp and timestamp by id
Brendan Greggbc54bb62016-02-14 23:13:13 -0800150 struct val_t val = {};
151 val.ts = bpf_ktime_get_ns();
152 val.fp = filp;
153 val.offset = 0;
154 if (val.fp)
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300155 entryinfo.update(&id, &val);
Brendan Greggbc54bb62016-02-14 23:13:13 -0800156
157 return 0;
158}
159
160//
161// Output
162//
163
164static int trace_return(struct pt_regs *ctx, int type)
165{
166 struct val_t *valp;
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300167 u64 id = bpf_get_current_pid_tgid();
168 u32 pid = id >> 32; // PID is higher part
Brendan Greggbc54bb62016-02-14 23:13:13 -0800169
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300170 valp = entryinfo.lookup(&id);
Brendan Greggbc54bb62016-02-14 23:13:13 -0800171 if (valp == 0) {
172 // missed tracing issue or filtered
173 return 0;
174 }
175
176 // calculate delta
177 u64 ts = bpf_ktime_get_ns();
178 u64 delta_us = (ts - valp->ts) / 1000;
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300179 entryinfo.delete(&id);
Brendan Greggbc54bb62016-02-14 23:13:13 -0800180 if (FILTER_US)
181 return 0;
182
Brendan Greggbc54bb62016-02-14 23:13:13 -0800183 // populate output struct
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530184 u32 size = PT_REGS_RC(ctx);
Brendan Greggbc54bb62016-02-14 23:13:13 -0800185 struct data_t data = {.type = type, .size = size, .delta_us = delta_us,
186 .pid = pid};
187 data.ts_us = ts / 1000;
188 data.offset = valp->offset;
Brendan Greggbc54bb62016-02-14 23:13:13 -0800189 bpf_get_current_comm(&data.task, sizeof(data.task));
190
Brendan Greggbe294db2016-10-20 21:46:09 -0700191 // workaround (rewriter should handle file to d_name in one step):
192 struct dentry *de = NULL;
193 struct qstr qs = {};
194 bpf_probe_read(&de, sizeof(de), &valp->fp->f_path.dentry);
195 bpf_probe_read(&qs, sizeof(qs), (void *)&de->d_name);
196 if (qs.len == 0)
197 return 0;
198 bpf_probe_read(&data.file, sizeof(data.file), (void *)qs.name);
199
200 // output
Brendan Greggbc54bb62016-02-14 23:13:13 -0800201 events.perf_submit(ctx, &data, sizeof(data));
202
203 return 0;
204}
205
206int trace_read_return(struct pt_regs *ctx)
207{
208 return trace_return(ctx, TRACE_READ);
209}
210
211int trace_write_return(struct pt_regs *ctx)
212{
213 return trace_return(ctx, TRACE_WRITE);
214}
215
216int trace_open_return(struct pt_regs *ctx)
217{
218 return trace_return(ctx, TRACE_OPEN);
219}
220
221int trace_fsync_return(struct pt_regs *ctx)
222{
223 return trace_return(ctx, TRACE_FSYNC);
224}
225
226"""
227if min_ms == 0:
228 bpf_text = bpf_text.replace('FILTER_US', '0')
229else:
230 bpf_text = bpf_text.replace('FILTER_US',
231 'delta_us <= %s' % str(min_ms * 1000))
232if args.pid:
233 bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid)
234else:
235 bpf_text = bpf_text.replace('FILTER_PID', '0')
236if debug:
237 print(bpf_text)
238
239# kernel->user event data: struct data_t
240DNAME_INLINE_LEN = 32 # linux/dcache.h
241TASK_COMM_LEN = 16 # linux/sched.h
242class Data(ct.Structure):
243 _fields_ = [
244 ("ts_us", ct.c_ulonglong),
245 ("type", ct.c_ulonglong),
246 ("size", ct.c_ulonglong),
247 ("offset", ct.c_ulonglong),
248 ("delta_us", ct.c_ulonglong),
249 ("pid", ct.c_ulonglong),
250 ("task", ct.c_char * TASK_COMM_LEN),
251 ("file", ct.c_char * DNAME_INLINE_LEN)
252 ]
253
254# process event
255def print_event(cpu, data, size):
256 event = ct.cast(data, ct.POINTER(Data)).contents
257
258 type = 'R'
259 if event.type == 1:
260 type = 'W'
261 elif event.type == 2:
262 type = 'O'
263 elif event.type == 3:
264 type = 'S'
265
266 if (csv):
267 print("%d,%s,%d,%s,%d,%d,%d,%s" % (
268 event.ts_us, event.task, event.pid, type, event.size,
269 event.offset, event.delta_us, event.file))
270 return
271 print("%-8s %-14.14s %-6s %1s %-7s %-8d %7.2f %s" % (strftime("%H:%M:%S"),
272 event.task, event.pid, type, event.size, event.offset / 1024,
273 float(event.delta_us) / 1000, event.file))
274
275# initialize BPF
276b = BPF(text=bpf_text)
277
278# common file functions
279b.attach_kprobe(event="zpl_read", fn_name="trace_rw_entry")
280b.attach_kprobe(event="zpl_write", fn_name="trace_rw_entry")
281b.attach_kprobe(event="zpl_open", fn_name="trace_open_entry")
282b.attach_kprobe(event="zpl_fsync", fn_name="trace_fsync_entry")
283b.attach_kretprobe(event="zpl_read", fn_name="trace_read_return")
284b.attach_kretprobe(event="zpl_write", fn_name="trace_write_return")
285b.attach_kretprobe(event="zpl_open", fn_name="trace_open_return")
286b.attach_kretprobe(event="zpl_fsync", fn_name="trace_fsync_return")
287
288# header
289if (csv):
290 print("ENDTIME_us,TASK,PID,TYPE,BYTES,OFFSET_b,LATENCY_us,FILE")
291else:
292 if min_ms == 0:
293 print("Tracing ZFS operations")
294 else:
295 print("Tracing ZFS operations slower than %d ms" % min_ms)
296 print("%-8s %-14s %-6s %1s %-7s %-8s %7s %s" % ("TIME", "COMM", "PID", "T",
297 "BYTES", "OFF_KB", "LAT(ms)", "FILENAME"))
298
299# read events
300b["events"].open_perf_buffer(print_event)
301while 1:
302 b.kprobe_poll()