Debian Codegraph @

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

raw
1 #!/usr/bin/stap
2 /*
3 * fslatency-nd.stp Measure file system I/O latency distribution.
4 * For Linux, uses SystemTap (non-debuginfo).
5 *
6 * USAGE: ./fslatency-nd.stp [interval [count]]
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 * This current implementation also traces sock_aio_write(), to exclude that
15 * write time from the report.
16 *
17 * From systemtap-lwtools: https://github.com/brendangregg/systemtap-lwtools
18 *
19 * See the corresponding man page (in systemtap-lwtools) for more info.
20 *
21 * Copyright (C) 2015 Brendan Gregg.
22 * Copyright (C) 2015 Red Hat, Inc.
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * 06-Feb-2015 Brendan Gregg Created this.
35 */
36
37 global ts[65536]; # 65536 is max concurrent I/O
38 global lat, interval = 0, secs = 0, count = 0, output = 0;
39
40 probe begin
41 {
42 printf("Tracing FS sync reads and writes... ");
43 if (argv_1 != "") {
44 interval = strtol(argv_1, 10);
45 if (argv_2 != "") {
46 count = strtol(argv_2, 10);
47 }
48 printf("Output every %d secs.\n", interval);
49 } else {
50 printf("Hit Ctrl-C to end.\n");
51 }
52 }
53
54 probe read_funcs, write_funcs
55 {
56 ts[tid()] = gettimeofday_ns();
57 }
58
59 probe read_funcs = kprobe.function("do_sync_read") !, __vfs_read
60 {
61 }
62 probe __vfs_read = kprobe.function("__vfs_read")
63 {
64 # Skip the call if new_sync_read() wouldn't be called.
65 file = pointer_arg(1)
66 if (!file || @cast(file, "file", "kernel")->f_op->read
67 || !@cast(file, "file", "kernel")->f_op->read_iter)
68 next
69 }
70
71 probe write_funcs = kprobe.function("do_sync_write") !, __vfs_write
72 {
73 }
74 probe __vfs_write = kprobe.function("__vfs_write")
75 {
76 # Skip the call if new_sync_write() wouldn't be called.
77 file = pointer_arg(1)
78 if (!file || @cast(file, "file", "kernel")->f_op->write
79 || !@cast(file, "file", "kernel")->f_op->write_iter)
80 next
81 }
82
83 probe kprobe.function("sock_write_iter") !, kprobe.function("sock_aio_write")
84 {
85 /* not a file system write */
86 ts[tid()] = 0;
87 }
88
89 probe read_funcs.return, write_funcs.return
90 {
91 if (ts[tid()]) {
92 lat[ppfunc()] <<< gettimeofday_ns() - ts[tid()];
93 delete ts[tid()];
94 }
95 }
96
97 probe read_funcs.return = kprobe.function("do_sync_read").return !,
98 __vfs_read.return
99 {
100 }
101 probe __vfs_read.return = kprobe.function("__vfs_read").return
102 {
103 # Skip the call if new_sync_read() wouldn't be called.
104 file = @entry(pointer_arg(1))
105 if (!file || @cast(file, "file", "kernel")->f_op->read
106 || !@cast(file, "file", "kernel")->f_op->read_iter)
107 next
108 }
109
110 probe write_funcs.return = kprobe.function("do_sync_write").return !,
111 __vfs_write.return
112 {
113 }
114 probe __vfs_write.return = kprobe.function("__vfs_write").return
115 {
116 # Skip the call if new_sync_write() wouldn't be called.
117 file = @entry(pointer_arg(1))
118 if (!file || @cast(file, "file", "kernel")->f_op->write
119 || !@cast(file, "file", "kernel")->f_op->write_iter)
120 next
121 }
122
123 function print_report()
124 {
125 printf("\n");
126 if (interval) { printf("%s ", ctime(gettimeofday_s())); }
127 printf("FS latency (ns):\n\n");
128 foreach (name in lat+) {
129 printf("FS call: %s()\n", name);
130 print(@hist_log(lat[name]));
131 }
132 delete lat;
133 }
134
135 probe timer.s(1)
136 {
137 if (interval) {
138 if (++secs == interval) {
139 print_report();
140 secs = 0;
141 count && (++output == count) && exit();
142 }
143 }
144 }
145
146 probe end
147 {
148 !interval && print_report();
149 delete secs;
150 delete output;
151 }