Debian Codegraph @

testsuite/systemtap.examples/lwtools/fsslower-nd.stp

raw
1 #!/usr/bin/stap
2 /*
3 * fslower-nd.stp Trace slow file system sync reads and writes.
4 * For Linux, uses SystemTap (non-debuginfo).
5 *
6 * USAGE: ./fsslower-nd.stp [min_ms]
7 *
8 * This script uses kernel dynamic tracing of two common file system functions:
9 * do_sync_read() and do_sync_write(). This provides a view of just two file
10 * system request types. There are typically many others: asynchronous I/O,
11 * directory operations, file handle operations, etc, that this script does
12 * not instrument.
13 *
14 * By default, a minimum millisecond threshold of 10 is used.
15 *
16 * From systemtap-lwtools: https://github.com/brendangregg/systemtap-lwtools
17 *
18 * See the corresponding man page (in systemtap-lwtools) for more info.
19 *
20 * Copyright (C) 2015 Brendan Gregg.
21 * Copyright (C) 2015 Red Hat, Inc.
22 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * 06-Feb-2015 Brendan Gregg Created this.
34 */
35
36 global ts[65536]; # 65536 is max concurrent I/O
37 global sz[65536]; # "
38 global min_ms = 10;
39
40 probe begin
41 {
42 if (argv_1 != "") {
43 min_ms = strtol(argv_1, 10);
44 }
45 printf("Tracing FS sync reads and writes slower than %d ms...", min_ms);
46 printf(" Hit Ctrl-C to end.\n");
47 printf("%-8s %-6s %-16s %-14s %-7s %8s\n", "TIME", "PID", "COMM",
48 "FUNC", "SIZE", "LAT(ms)");
49 }
50
51 function hhmmss() {
52 return substr(ctime(gettimeofday_s()), 11, 8);
53 }
54
55 probe read_funcs, write_funcs
56 {
57 sz[tid()] = int_arg(3);
58 ts[tid()] = gettimeofday_ns();
59 }
60
61 probe read_funcs = kprobe.function("do_sync_read") !, __vfs_read
62 {
63 }
64 probe __vfs_read = kprobe.function("__vfs_read")
65 {
66 # Skip the call if new_sync_read() wouldn't be called.
67 file = pointer_arg(1)
68 if (!file || @cast(file, "file", "kernel")->f_op->read
69 || !@cast(file, "file", "kernel")->f_op->read_iter)
70 next
71 }
72
73 probe write_funcs = kprobe.function("do_sync_write") !, __vfs_write
74 {
75 }
76 probe __vfs_write = kprobe.function("__vfs_write")
77 {
78 # Skip the call if new_sync_write() wouldn't be called.
79 file = pointer_arg(1)
80 if (!file || @cast(file, "file", "kernel")->f_op->write
81 || !@cast(file, "file", "kernel")->f_op->write_iter)
82 next
83 }
84
85 probe kprobe.function("sock_write_iter") !, kprobe.function("sock_aio_write")
86 {
87 /* not a file system write */
88 sz[tid()] = 0;
89 ts[tid()] = 0;
90 }
91
92 probe read_funcs.return, write_funcs.return
93 {
94 if (ts[tid()]) {
95 lat = (gettimeofday_ns() - ts[tid()]) / 1000000;
96 if (lat >= min_ms) {
97 printf("%-8s %-6d %-16s %-14s %-7d %8d\n", hhmmss(),
98 pid(), execname(), ppfunc(), sz[tid()], lat);
99 }
100 delete sz[tid()];
101 delete ts[tid()];
102 }
103 }
104
105 probe read_funcs.return = kprobe.function("do_sync_read").return !,
106 __vfs_read.return
107 {
108 }
109 probe __vfs_read.return = kprobe.function("__vfs_read").return
110 {
111 # Skip the call if new_sync_read() wouldn't be called.
112 file = @entry(pointer_arg(1))
113 if (!file || @cast(file, "file", "kernel")->f_op->read
114 || !@cast(file, "file", "kernel")->f_op->read_iter)
115 next
116 }
117
118 probe write_funcs.return = kprobe.function("do_sync_write").return !,
119 __vfs_write.return
120 {
121 }
122 probe __vfs_write.return = kprobe.function("__vfs_write").return
123 {
124 # Skip the call if new_sync_write() wouldn't be called.
125 file = @entry(pointer_arg(1))
126 if (!file || @cast(file, "file", "kernel")->f_op->write
127 || !@cast(file, "file", "kernel")->f_op->write_iter)
128 next
129 }