Debian Codegraph @

testsuite/systemtap.examples/io/iotime.stp

raw
1 #! /usr/bin/env stap
2
3 /*
4 * Copyright (C) 2006-2018 Red Hat Inc.
5 *
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU General Public License v.2.
9 *
10 * You should have received a copy of the GNU General Public License
11 * along with this program. If not, see <http://www.gnu.org/licenses/>.
12 *
13 * Print out the amount of time spent in the read and write systemcall
14 * when each file opened by the process is closed. Note that the systemtap
15 * script needs to be running before the open operations occur for
16 * the script to record data.
17 *
18 * This script could be used to to find out which files are slow to load
19 * on a machine. e.g.
20 *
21 * stap iotime.stp -c 'firefox'
22 *
23 * Output format is:
24 * timestamp pid (executabable) info_type path ...
25 *
26 * 200283135 2573 (cupsd) access /etc/printcap read: 0 write: 7063
27 * 200283143 2573 (cupsd) iotime /etc/printcap time: 69
28 *
29 */
30
31 global start
32 global time_io
33
34 function timestamp:long() { return gettimeofday_us() - start }
35
36 function proc:string() { return sprintf("%d (%s)", pid(), execname()) }
37
38 probe begin { start = gettimeofday_us() }
39
40 global possible_filename, filehandles, fileread, filewrite
41
42 probe syscall.open, syscall.openat {
43 possible_filename[tid()] = filename
44 }
45
46 probe syscall.open.return, syscall.openat.return {
47 // Get filename even if non-dwarf syscall return probe are used.
48 filename = possible_filename[tid()]
49 delete possible_filename[tid()]
50 if (retval != -1) {
51 filehandles[pid(), retval] = filename
52 } else {
53 printf("%d %s access %s fail\n", timestamp(), proc(), filename)
54 }
55 }
56
57 global read_fds, write_fds
58
59 probe syscall.read { read_fds[tid()] = fd }
60
61 probe syscall.read.return {
62 p = pid()
63 // Get fd even if non-dwarf syscall return probe.
64 fd = read_fds[tid()]
65 delete read_fds[tid()]
66 bytes = retval
67 time = gettimeofday_us() - @entry(gettimeofday_us())
68 if (bytes > 0)
69 fileread[p, fd] <<< bytes
70 time_io[p, fd] <<< time
71 }
72
73 probe syscall.write { write_fds[tid()] = fd }
74
75 probe syscall.write.return {
76 p = pid()
77 // Get fd even if non-dwarf syscall return probe.
78 fd = write_fds[tid()]
79 delete write_fds[tid()]
80 bytes = retval
81 time = gettimeofday_us() - @entry(gettimeofday_us())
82 if (bytes > 0)
83 filewrite[p, fd] <<< bytes
84 time_io[p, fd] <<< time
85 }
86
87 probe syscall.close {
88 if ([pid(), fd] in filehandles) {
89 printf("%d %s access %s read: %d write: %d\n",
90 timestamp(), proc(), filehandles[pid(), fd],
91 @sum(fileread[pid(), fd]), @sum(filewrite[pid(), fd]))
92 if (@count(time_io[pid(), fd]))
93 printf("%d %s iotime %s time: %d\n", timestamp(), proc(),
94 filehandles[pid(), fd], @sum(time_io[pid(), fd]))
95 }
96 delete fileread[pid(), fd]
97 delete filewrite[pid(), fd]
98 delete filehandles[pid(), fd]
99 delete time_io[pid(),fd]
100 }