blob: 5cd75abc3f52de28cedd66a89c84b2b0a8073769 [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Brendan Greggcd1cad12016-02-12 02:27:19 -08002# @lint-avoid-python-3-compatibility-imports
3#
4# ext4slower Trace slow ext4 operations.
5# For Linux, uses BCC, eBPF.
6#
7# USAGE: ext4slower [-h] [-j] [-p PID] [min_ms]
8#
9# This script traces common ext4 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 ext4 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# 15-Oct-2016 Dina Goldshtein -p to filter by process ID.
Joe Yine07f2ed2018-06-14 12:21:46 +080026# 13-Jun-2018 Joe Yin modify generic_file_read_iter to ext4_file_read_iter.
Brendan Greggcd1cad12016-02-12 02:27:19 -080027
28from __future__ import print_function
29from bcc import BPF
30import argparse
31from time import strftime
Brendan Greggcd1cad12016-02-12 02:27:19 -080032
33# symbols
34kallsyms = "/proc/kallsyms"
35
36# arguments
37examples = """examples:
38 ./ext4slower # trace operations slower than 10 ms (default)
39 ./ext4slower 1 # trace operations slower than 1 ms
40 ./ext4slower -j 1 # ... 1 ms, parsable output (csv)
41 ./ext4slower 0 # trace all operations (warning: verbose)
42 ./ext4slower -p 185 # trace PID 185 only
43"""
44parser = argparse.ArgumentParser(
45 description="Trace common ext4 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)")
Nathan Scottcf0792f2018-02-02 16:56:50 +110054parser.add_argument("--ebpf", action="store_true",
55 help=argparse.SUPPRESS)
Brendan Greggcd1cad12016-02-12 02:27:19 -080056args = parser.parse_args()
57min_ms = int(args.min_ms)
58pid = args.pid
59csv = args.csv
60debug = 0
61
62# define BPF program
63bpf_text = """
64#include <uapi/linux/ptrace.h>
65#include <linux/fs.h>
66#include <linux/sched.h>
67#include <linux/dcache.h>
68
69// XXX: switch these to char's when supported
70#define TRACE_READ 0
71#define TRACE_WRITE 1
72#define TRACE_OPEN 2
73#define TRACE_FSYNC 3
74
75struct val_t {
76 u64 ts;
77 u64 offset;
78 struct file *fp;
79};
80
81struct data_t {
82 // XXX: switch some to u32's when supported
83 u64 ts_us;
84 u64 type;
85 u64 size;
86 u64 offset;
87 u64 delta_us;
88 u64 pid;
89 char task[TASK_COMM_LEN];
90 char file[DNAME_INLINE_LEN];
91};
92
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +030093BPF_HASH(entryinfo, u64, struct val_t);
Brendan Greggcd1cad12016-02-12 02:27:19 -080094BPF_PERF_OUTPUT(events);
95
96//
97// Store timestamp and size on entry
98//
99
100// The current ext4 (Linux 4.5) uses generic_file_read_iter(), instead of it's
101// own function, for reads. So we need to trace that and then filter on ext4,
102// which I do by checking file->f_op.
Joe Yine07f2ed2018-06-14 12:21:46 +0800103// The new Linux version (since form 4.10) uses ext4_file_read_iter(), And if the 'CONFIG_FS_DAX'
Yuntao Wang6ae20772022-02-18 11:23:55 +0800104// is not set, then ext4_file_read_iter() will call generic_file_read_iter(), else it will call
Joe Yine07f2ed2018-06-14 12:21:46 +0800105// ext4_dax_read_iter(), and trace generic_file_read_iter() will fail.
Brendan Greggcd1cad12016-02-12 02:27:19 -0800106int trace_read_entry(struct pt_regs *ctx, struct kiocb *iocb)
107{
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300108 u64 id = bpf_get_current_pid_tgid();
109 u32 pid = id >> 32; // PID is higher part
110
Brendan Greggcd1cad12016-02-12 02:27:19 -0800111 if (FILTER_PID)
112 return 0;
113
114 // ext4 filter on file->f_op == ext4_file_operations
115 struct file *fp = iocb->ki_filp;
116 if ((u64)fp->f_op != EXT4_FILE_OPERATIONS)
117 return 0;
118
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300119 // store filep and timestamp by id
Brendan Greggcd1cad12016-02-12 02:27:19 -0800120 struct val_t val = {};
121 val.ts = bpf_ktime_get_ns();
122 val.fp = fp;
123 val.offset = iocb->ki_pos;
124 if (val.fp)
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300125 entryinfo.update(&id, &val);
Brendan Greggcd1cad12016-02-12 02:27:19 -0800126
127 return 0;
128}
129
130// ext4_file_write_iter():
131int trace_write_entry(struct pt_regs *ctx, struct kiocb *iocb)
132{
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300133 u64 id = bpf_get_current_pid_tgid();
134 u32 pid = id >> 32; // PID is higher part
135
Brendan Greggcd1cad12016-02-12 02:27:19 -0800136 if (FILTER_PID)
137 return 0;
138
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300139 // store filep and timestamp by id
Brendan Greggcd1cad12016-02-12 02:27:19 -0800140 struct val_t val = {};
141 val.ts = bpf_ktime_get_ns();
142 val.fp = iocb->ki_filp;
143 val.offset = iocb->ki_pos;
144 if (val.fp)
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300145 entryinfo.update(&id, &val);
Brendan Greggcd1cad12016-02-12 02:27:19 -0800146
147 return 0;
148}
149
150// ext4_file_open():
151int trace_open_entry(struct pt_regs *ctx, struct inode *inode,
152 struct file *file)
153{
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300154 u64 id = bpf_get_current_pid_tgid();
155 u32 pid = id >> 32; // PID is higher part
156
Brendan Greggcd1cad12016-02-12 02:27:19 -0800157 if (FILTER_PID)
158 return 0;
159
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300160 // store filep and timestamp by id
Brendan Greggcd1cad12016-02-12 02:27:19 -0800161 struct val_t val = {};
162 val.ts = bpf_ktime_get_ns();
163 val.fp = file;
164 val.offset = 0;
165 if (val.fp)
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300166 entryinfo.update(&id, &val);
Brendan Greggcd1cad12016-02-12 02:27:19 -0800167
168 return 0;
169}
170
171// ext4_sync_file():
172int trace_fsync_entry(struct pt_regs *ctx, struct file *file)
173{
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300174 u64 id = bpf_get_current_pid_tgid();
175 u32 pid = id >> 32; // PID is higher part
176
Brendan Greggcd1cad12016-02-12 02:27:19 -0800177 if (FILTER_PID)
178 return 0;
179
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300180 // store filep and timestamp by id
Brendan Greggcd1cad12016-02-12 02:27:19 -0800181 struct val_t val = {};
182 val.ts = bpf_ktime_get_ns();
183 val.fp = file;
184 val.offset = 0;
185 if (val.fp)
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300186 entryinfo.update(&id, &val);
Brendan Greggcd1cad12016-02-12 02:27:19 -0800187
188 return 0;
189}
190
191//
192// Output
193//
194
195static int trace_return(struct pt_regs *ctx, int type)
196{
197 struct val_t *valp;
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300198 u64 id = bpf_get_current_pid_tgid();
199 u32 pid = id >> 32; // PID is higher part
Brendan Greggcd1cad12016-02-12 02:27:19 -0800200
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300201 valp = entryinfo.lookup(&id);
Brendan Greggcd1cad12016-02-12 02:27:19 -0800202 if (valp == 0) {
203 // missed tracing issue or filtered
204 return 0;
205 }
206
207 // calculate delta
208 u64 ts = bpf_ktime_get_ns();
209 u64 delta_us = (ts - valp->ts) / 1000;
Dina Goldshteinc8b9ae32016-10-18 03:01:05 +0300210 entryinfo.delete(&id);
Brendan Greggcd1cad12016-02-12 02:27:19 -0800211 if (FILTER_US)
212 return 0;
213
Brendan Greggcd1cad12016-02-12 02:27:19 -0800214 // populate output struct
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530215 u32 size = PT_REGS_RC(ctx);
Brendan Greggcd1cad12016-02-12 02:27:19 -0800216 struct data_t data = {.type = type, .size = size, .delta_us = delta_us,
217 .pid = pid};
218 data.ts_us = ts / 1000;
219 data.offset = valp->offset;
Brendan Greggcd1cad12016-02-12 02:27:19 -0800220 bpf_get_current_comm(&data.task, sizeof(data.task));
221
Brendan Greggbe294db2016-10-20 21:46:09 -0700222 // workaround (rewriter should handle file to d_name in one step):
223 struct dentry *de = NULL;
224 struct qstr qs = {};
Paul Chaignon0cdf2962018-05-05 10:47:44 +0200225 de = valp->fp->f_path.dentry;
226 qs = de->d_name;
Brendan Greggbe294db2016-10-20 21:46:09 -0700227 if (qs.len == 0)
228 return 0;
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500229 bpf_probe_read_kernel(&data.file, sizeof(data.file), (void *)qs.name);
Brendan Greggbe294db2016-10-20 21:46:09 -0700230
231 // output
Brendan Greggcd1cad12016-02-12 02:27:19 -0800232 events.perf_submit(ctx, &data, sizeof(data));
233
234 return 0;
235}
236
237int trace_read_return(struct pt_regs *ctx)
238{
239 return trace_return(ctx, TRACE_READ);
240}
241
242int trace_write_return(struct pt_regs *ctx)
243{
244 return trace_return(ctx, TRACE_WRITE);
245}
246
247int trace_open_return(struct pt_regs *ctx)
248{
249 return trace_return(ctx, TRACE_OPEN);
250}
251
252int trace_fsync_return(struct pt_regs *ctx)
253{
254 return trace_return(ctx, TRACE_FSYNC);
255}
256
257"""
258
259# code replacements
260with open(kallsyms) as syms:
261 ops = ''
262 for line in syms:
263 (addr, size, name) = line.rstrip().split(" ", 2)
ygrek189f87f2016-06-27 11:07:47 -0700264 name = name.split("\t")[0]
Brendan Greggcd1cad12016-02-12 02:27:19 -0800265 if name == "ext4_file_operations":
266 ops = "0x" + addr
267 break
268 if ops == '':
269 print("ERROR: no ext4_file_operations in /proc/kallsyms. Exiting.")
Aleksander Alekseev4a950bf2017-10-23 21:22:36 +0300270 print("HINT: the kernel should be built with CONFIG_KALLSYMS_ALL.")
Brendan Greggcd1cad12016-02-12 02:27:19 -0800271 exit()
272 bpf_text = bpf_text.replace('EXT4_FILE_OPERATIONS', ops)
273if min_ms == 0:
274 bpf_text = bpf_text.replace('FILTER_US', '0')
275else:
276 bpf_text = bpf_text.replace('FILTER_US',
277 'delta_us <= %s' % str(min_ms * 1000))
278if args.pid:
279 bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid)
280else:
281 bpf_text = bpf_text.replace('FILTER_PID', '0')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100282if debug or args.ebpf:
Brendan Greggcd1cad12016-02-12 02:27:19 -0800283 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100284 if args.ebpf:
285 exit()
Brendan Greggcd1cad12016-02-12 02:27:19 -0800286
Brendan Greggcd1cad12016-02-12 02:27:19 -0800287# process event
288def print_event(cpu, data, size):
Xiaozhou Liu51d62d32019-02-15 13:03:05 +0800289 event = b["events"].event(data)
Brendan Greggcd1cad12016-02-12 02:27:19 -0800290
291 type = 'R'
292 if event.type == 1:
293 type = 'W'
294 elif event.type == 2:
295 type = 'O'
296 elif event.type == 3:
297 type = 'S'
298
299 if (csv):
300 print("%d,%s,%d,%s,%d,%d,%d,%s" % (
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200301 event.ts_us, event.task.decode('utf-8', 'replace'), event.pid,
302 type, event.size, event.offset, event.delta_us,
303 event.file.decode('utf-8', 'replace')))
Brendan Greggcd1cad12016-02-12 02:27:19 -0800304 return
305 print("%-8s %-14.14s %-6s %1s %-7s %-8d %7.2f %s" % (strftime("%H:%M:%S"),
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200306 event.task.decode('utf-8', 'replace'), event.pid, type, event.size,
307 event.offset / 1024, float(event.delta_us) / 1000,
308 event.file.decode('utf-8', 'replace')))
Brendan Greggcd1cad12016-02-12 02:27:19 -0800309
310# initialize BPF
311b = BPF(text=bpf_text)
312
313# Common file functions. See earlier comment about generic_file_read_iter().
Joe Yine07f2ed2018-06-14 12:21:46 +0800314if BPF.get_kprobe_functions(b'ext4_file_read_iter'):
315 b.attach_kprobe(event="ext4_file_read_iter", fn_name="trace_read_entry")
316else:
317 b.attach_kprobe(event="generic_file_read_iter", fn_name="trace_read_entry")
Brendan Greggcd1cad12016-02-12 02:27:19 -0800318b.attach_kprobe(event="ext4_file_write_iter", fn_name="trace_write_entry")
319b.attach_kprobe(event="ext4_file_open", fn_name="trace_open_entry")
320b.attach_kprobe(event="ext4_sync_file", fn_name="trace_fsync_entry")
Joe Yine07f2ed2018-06-14 12:21:46 +0800321if BPF.get_kprobe_functions(b'ext4_file_read_iter'):
322 b.attach_kretprobe(event="ext4_file_read_iter", fn_name="trace_read_return")
323else:
324 b.attach_kretprobe(event="generic_file_read_iter", fn_name="trace_read_return")
Brendan Greggcd1cad12016-02-12 02:27:19 -0800325b.attach_kretprobe(event="ext4_file_write_iter", fn_name="trace_write_return")
326b.attach_kretprobe(event="ext4_file_open", fn_name="trace_open_return")
327b.attach_kretprobe(event="ext4_sync_file", fn_name="trace_fsync_return")
328
329# header
330if (csv):
331 print("ENDTIME_us,TASK,PID,TYPE,BYTES,OFFSET_b,LATENCY_us,FILE")
332else:
333 if min_ms == 0:
334 print("Tracing ext4 operations")
335 else:
336 print("Tracing ext4 operations slower than %d ms" % min_ms)
337 print("%-8s %-14s %-6s %1s %-7s %-8s %7s %s" % ("TIME", "COMM", "PID", "T",
338 "BYTES", "OFF_KB", "LAT(ms)", "FILENAME"))
339
340# read events
Mark Drayton5f5687e2017-02-20 18:13:03 +0000341b["events"].open_perf_buffer(print_event, page_cnt=64)
Brendan Greggcd1cad12016-02-12 02:27:19 -0800342while 1:
Jerome Marchand51671272018-12-19 01:57:24 +0100343 try:
344 b.perf_buffer_poll()
345 except KeyboardInterrupt:
346 exit()