blob: 3fbc96d8d1ad1ece1585380faba043ce7e25a8d4 [file] [log] [blame]
Brendan Greggc937a9e2016-02-12 02:23:39 -08001#!/usr/bin/python
2# @lint-avoid-python-3-compatibility-imports
3#
4# xfsslower Trace slow XFS operations.
5# For Linux, uses BCC, eBPF.
6#
7# USAGE: xfsslower [-h] [-j] [-p PID] [min_ms]
8#
9# This script traces common XFS 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 XFS 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# By default, a minimum millisecond threshold of 10 is used.
20#
21# Copyright 2016 Netflix, Inc.
22# Licensed under the Apache License, Version 2.0 (the "License")
23#
24# 11-Feb-2016 Brendan Gregg Created this.
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +030025# 16-Oct-2016 Dina Goldshtein -p to filter by process ID.
Brendan Greggc937a9e2016-02-12 02:23:39 -080026
27from __future__ import print_function
28from bcc import BPF
29import argparse
30from time import strftime
31import ctypes as ct
32
33# arguments
34examples = """examples:
35 ./xfsslower # trace operations slower than 10 ms (default)
36 ./xfsslower 1 # trace operations slower than 1 ms
37 ./xfsslower -j 1 # ... 1 ms, parsable output (csv)
38 ./xfsslower 0 # trace all operations (warning: verbose)
39 ./xfsslower -p 185 # trace PID 185 only
40"""
41parser = argparse.ArgumentParser(
42 description="Trace common XFS file operations slower than a threshold",
43 formatter_class=argparse.RawDescriptionHelpFormatter,
44 epilog=examples)
45parser.add_argument("-j", "--csv", action="store_true",
46 help="just print fields: comma-separated values")
47parser.add_argument("-p", "--pid",
48 help="trace this PID only")
49parser.add_argument("min_ms", nargs="?", default='10',
50 help="minimum I/O duration to trace, in ms (default 10)")
51args = parser.parse_args()
52min_ms = int(args.min_ms)
53pid = args.pid
54csv = args.csv
55debug = 0
56
57# define BPF program
58bpf_text = """
59#include <uapi/linux/ptrace.h>
60#include <linux/fs.h>
61#include <linux/sched.h>
62#include <linux/dcache.h>
63
64// XXX: switch these to char's when supported
65#define TRACE_READ 0
66#define TRACE_WRITE 1
67#define TRACE_OPEN 2
68#define TRACE_FSYNC 3
69
70struct val_t {
71 u64 ts;
72 u64 offset;
73 struct file *fp;
74};
75
76struct data_t {
77 // XXX: switch some to u32's when supported
78 u64 ts_us;
79 u64 type;
80 u64 size;
81 u64 offset;
82 u64 delta_us;
83 u64 pid;
84 char task[TASK_COMM_LEN];
85 char file[DNAME_INLINE_LEN];
86};
87
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +030088BPF_HASH(entryinfo, u64, struct val_t);
Brendan Greggc937a9e2016-02-12 02:23:39 -080089BPF_PERF_OUTPUT(events);
90
91//
92// Store timestamp and size on entry
93//
94
95// xfs_file_read_iter(), xfs_file_write_iter():
96int trace_rw_entry(struct pt_regs *ctx, struct kiocb *iocb)
97{
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +030098 u64 id = bpf_get_current_pid_tgid();
99 u32 pid = id >> 32; // PID is higher part
100
Brendan Greggc937a9e2016-02-12 02:23:39 -0800101 if (FILTER_PID)
102 return 0;
103
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300104 // store filep and timestamp by id
Brendan Greggc937a9e2016-02-12 02:23:39 -0800105 struct val_t val = {};
106 val.ts = bpf_ktime_get_ns();
107 val.fp = iocb->ki_filp;
108 val.offset = iocb->ki_pos;
109 if (val.fp)
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300110 entryinfo.update(&id, &val);
Brendan Greggc937a9e2016-02-12 02:23:39 -0800111
112 return 0;
113}
114
115// xfs_file_open():
116int trace_open_entry(struct pt_regs *ctx, struct inode *inode,
117 struct file *file)
118{
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300119 u64 id = bpf_get_current_pid_tgid();
120 u32 pid = id >> 32; // PID is higher part
121
Brendan Greggc937a9e2016-02-12 02:23:39 -0800122 if (FILTER_PID)
123 return 0;
124
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300125 // store filep and timestamp by id
Brendan Greggc937a9e2016-02-12 02:23:39 -0800126 struct val_t val = {};
127 val.ts = bpf_ktime_get_ns();
128 val.fp = file;
129 val.offset = 0;
130 if (val.fp)
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300131 entryinfo.update(&id, &val);
Brendan Greggc937a9e2016-02-12 02:23:39 -0800132
133 return 0;
134}
135
136// xfs_file_fsync():
137int trace_fsync_entry(struct pt_regs *ctx, struct file *file)
138{
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300139 u64 id = bpf_get_current_pid_tgid();
140 u32 pid = id >> 32; // PID is higher part
141
Brendan Greggc937a9e2016-02-12 02:23:39 -0800142 if (FILTER_PID)
143 return 0;
144
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300145 // store filep and timestamp by id
Brendan Greggc937a9e2016-02-12 02:23:39 -0800146 struct val_t val = {};
147 val.ts = bpf_ktime_get_ns();
148 val.fp = file;
149 val.offset = 0;
150 if (val.fp)
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300151 entryinfo.update(&id, &val);
Brendan Greggc937a9e2016-02-12 02:23:39 -0800152
153 return 0;
154}
155
156//
157// Output
158//
159
160static int trace_return(struct pt_regs *ctx, int type)
161{
162 struct val_t *valp;
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300163 u64 id = bpf_get_current_pid_tgid();
164 u32 pid = id >> 32; // PID is higher part
Brendan Greggc937a9e2016-02-12 02:23:39 -0800165
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300166 valp = entryinfo.lookup(&id);
Brendan Greggc937a9e2016-02-12 02:23:39 -0800167 if (valp == 0) {
168 // missed tracing issue or filtered
169 return 0;
170 }
171
172 // calculate delta
173 u64 ts = bpf_ktime_get_ns();
174 u64 delta_us = (ts - valp->ts) / 1000;
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300175 entryinfo.delete(&id);
Brendan Greggc937a9e2016-02-12 02:23:39 -0800176 if (FILTER_US)
177 return 0;
178
Brendan Greggc937a9e2016-02-12 02:23:39 -0800179 // populate output struct
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530180 u32 size = PT_REGS_RC(ctx);
Brendan Greggc937a9e2016-02-12 02:23:39 -0800181 struct data_t data = {.type = type, .size = size, .delta_us = delta_us,
182 .pid = pid};
183 data.ts_us = ts / 1000;
184 data.offset = valp->offset;
Brendan Greggc937a9e2016-02-12 02:23:39 -0800185 bpf_get_current_comm(&data.task, sizeof(data.task));
186
Brendan Greggbe294db2016-10-20 21:46:09 -0700187 // workaround (rewriter should handle file to d_name in one step):
188 struct dentry *de = NULL;
189 struct qstr qs = {};
190 bpf_probe_read(&de, sizeof(de), &valp->fp->f_path.dentry);
191 bpf_probe_read(&qs, sizeof(qs), (void *)&de->d_name);
192 if (qs.len == 0)
193 return 0;
194 bpf_probe_read(&data.file, sizeof(data.file), (void *)qs.name);
195
196 // output
Brendan Greggc937a9e2016-02-12 02:23:39 -0800197 events.perf_submit(ctx, &data, sizeof(data));
198
199 return 0;
200}
201
202int trace_read_return(struct pt_regs *ctx)
203{
204 return trace_return(ctx, TRACE_READ);
205}
206
207int trace_write_return(struct pt_regs *ctx)
208{
209 return trace_return(ctx, TRACE_WRITE);
210}
211
212int trace_open_return(struct pt_regs *ctx)
213{
214 return trace_return(ctx, TRACE_OPEN);
215}
216
217int trace_fsync_return(struct pt_regs *ctx)
218{
219 return trace_return(ctx, TRACE_FSYNC);
220}
221
222"""
223if min_ms == 0:
224 bpf_text = bpf_text.replace('FILTER_US', '0')
225else:
226 bpf_text = bpf_text.replace('FILTER_US',
227 'delta_us <= %s' % str(min_ms * 1000))
228if args.pid:
229 bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid)
230else:
231 bpf_text = bpf_text.replace('FILTER_PID', '0')
232if debug:
233 print(bpf_text)
234
235# kernel->user event data: struct data_t
236DNAME_INLINE_LEN = 32 # linux/dcache.h
237TASK_COMM_LEN = 16 # linux/sched.h
238class Data(ct.Structure):
239 _fields_ = [
240 ("ts_us", ct.c_ulonglong),
241 ("type", ct.c_ulonglong),
242 ("size", ct.c_ulonglong),
243 ("offset", ct.c_ulonglong),
244 ("delta_us", ct.c_ulonglong),
245 ("pid", ct.c_ulonglong),
246 ("task", ct.c_char * TASK_COMM_LEN),
247 ("file", ct.c_char * DNAME_INLINE_LEN)
248 ]
249
250# process event
251def print_event(cpu, data, size):
252 event = ct.cast(data, ct.POINTER(Data)).contents
253
254 type = 'R'
255 if event.type == 1:
256 type = 'W'
257 elif event.type == 2:
258 type = 'O'
259 elif event.type == 3:
260 type = 'S'
261
262 if (csv):
263 print("%d,%s,%d,%s,%d,%d,%d,%s" % (
264 event.ts_us, event.task, event.pid, type, event.size,
265 event.offset, event.delta_us, event.file))
266 return
267 print("%-8s %-14.14s %-6s %1s %-7s %-8d %7.2f %s" % (strftime("%H:%M:%S"),
268 event.task, event.pid, type, event.size, event.offset / 1024,
269 float(event.delta_us) / 1000, event.file))
270
271# initialize BPF
272b = BPF(text=bpf_text)
273
274# common file functions
275b.attach_kprobe(event="xfs_file_read_iter", fn_name="trace_rw_entry")
276b.attach_kprobe(event="xfs_file_write_iter", fn_name="trace_rw_entry")
277b.attach_kprobe(event="xfs_file_open", fn_name="trace_open_entry")
278b.attach_kprobe(event="xfs_file_fsync", fn_name="trace_fsync_entry")
279b.attach_kretprobe(event="xfs_file_read_iter", fn_name="trace_read_return")
280b.attach_kretprobe(event="xfs_file_write_iter", fn_name="trace_write_return")
281b.attach_kretprobe(event="xfs_file_open", fn_name="trace_open_return")
282b.attach_kretprobe(event="xfs_file_fsync", fn_name="trace_fsync_return")
283
284# header
285if (csv):
286 print("ENDTIME_us,TASK,PID,TYPE,BYTES,OFFSET_b,LATENCY_us,FILE")
287else:
288 if min_ms == 0:
289 print("Tracing XFS operations")
290 else:
291 print("Tracing XFS operations slower than %d ms" % min_ms)
292 print("%-8s %-14s %-6s %1s %-7s %-8s %7s %s" % ("TIME", "COMM", "PID", "T",
293 "BYTES", "OFF_KB", "LAT(ms)", "FILENAME"))
294
295# read events
Mark Drayton5f5687e2017-02-20 18:13:03 +0000296b["events"].open_perf_buffer(print_event, page_cnt=64)
Brendan Greggc937a9e2016-02-12 02:23:39 -0800297while 1:
298 b.kprobe_poll()