Debian Codegraph @

testsuite/systemtap.examples/process/syscalltimes

raw
1 #!/bin/bash
2
3 # Syscalltimes systemtap script
4 # Copyright (C) 2007 IBM Corp.
5 # Copyright (C) 2011,2018 Red Hat, Inc.
6 #
7 # This file is part of systemtap, and is free software. You can
8 # redistribute it and/or modify it under the terms of the GNU General
9 # Public License (GPL); either version 2, or (at your option) any
10 # later version.
11
12 ###
13 ### syscalltime - Combination shell/systemtap script to measure system call
14 ### counts and times. Can be filtered by process IDs, process
15 ### names and users.
16 ###
17
18 # Filter options
19 F_PIDSTR=""; F_PID=0 # Filter by process ID
20 F_EXECSTR=""; F_EXEC=0 # Filter by process name
21 F_UIDSTR=""; F_UID=0 # Filter by user
22 FILTER=0 # Any filters specified?
23
24 # Print options
25 P_TOTALS=0 # Print totals
26 P_VERBOSE_STR="" # Print verbose build output
27 P_PROCESS_STR="" # Run process during stap script invocation
28
29 DELIM=","
30
31 function usage {
32 echo "USAGE: syscalltimes [-n pname]... [-p pid]... [-u username]... [-c 'process']"
33 echo " [-v]... [-t] [-h]"
34 echo " -n pname # filter by this process name"
35 echo " -p pid # filter by this process ID"
36 echo " -u username # filter by this user"
37 echo " -c 'process' # run 'process' then exit to show totals"
38 echo " -t # print totals (default with filters: OFF"
39 echo " default without filters: ON)"
40 echo " -v # print verbose output during SystemTap module creation"
41 echo " -h # print this help text"
42 echo ""
43 echo "NOTE: This script can take long time to build. Use -v[vv] to monitor"
44 echo " the module creation and build process."
45 }
46
47 # Process options
48 while getopts n:p:u:c:vth option; do
49 case $option in
50 n) let "F_EXEC++"
51 F_EXECSTR=$OPTARG$DELIM$F_EXECSTR ;;
52
53 p) let "F_PID++"
54 F_PIDSTR=$OPTARG$DELIM$F_PIDSTR ;;
55
56 u) uid=`awk -F: '$1 == name {print $3}' name=$OPTARG /etc/passwd`
57 if [[ $uid != "" ]]; then
58 let "F_UID++"
59 F_UIDSTR=$uid$DELIM$F_UIDSTR
60 else
61 echo "ERROR: Unknown user:" $OPTARG
62 let "ERROR++"
63 fi ;;
64
65 v) P_VERBOSE_STR="-v "$P_VERBOSE_STR ;;
66
67 t) P_TOTALS=1 ;;
68
69 c) P_PROCESS_STR="-c $OPTARG" ;;
70
71 h|?|*) usage
72 exit 1 ;;
73 esac
74 done
75
76 if [[ $ERROR > 0 ]]; then
77 exit 1
78 fi
79
80 if [[ $F_EXEC > 0 || $F_PID > 0 || $F_UID > 0 ]]; then
81 FILTER=1
82 fi
83
84 echo "Creating and building SystemTap module..."
85
86 #
87 # SystemTap script
88 #
89 stap $P_VERBOSE_STR -w "$P_PROCESS_STR" -e '
90 global starttime, timebycall, timebypid, timebyuid, timebyexec
91 global f_exec_str, f_pid_str, f_uid_str
92 global f_exec, f_pid, f_uid
93 global prt_totals, filter_str
94
95 probe begin {
96 printf("Collecting data - type Ctrl-C to print output and exit...\n")
97
98 # If no filters specified, skip filter processing
99 if ('$FILTER' == 0) {
100 filter_str = " (unfiltered)"
101 prt_totals = 1 // On by default if no filtering
102 next
103 } else
104 filter_str = " (filtered)"
105
106 prt_totals = '$P_TOTALS'
107 if ('$F_EXEC') f_exec_str = "'$F_EXECSTR'"
108 if ('$F_PID') f_pid_str = "'$F_PIDSTR'"
109 if ('$F_UID') f_uid_str = "'$F_UIDSTR'"
110
111 delim = "'$DELIM'"
112
113 # Process names
114 if ('$F_EXEC') {
115 pname = tokenize(f_exec_str, delim)
116 while (pname != "") {
117 f_exec[pname] = 1
118 pname = tokenize("", delim)
119 }
120 }
121
122 # Process IDs
123 if ('$F_PID') {
124 pid = tokenize(f_pid_str, delim)
125 while (pid != "") {
126 f_pid[strtol(pid, 10)] = 1
127 pid = tokenize("", delim)
128 }
129 }
130
131 # User IDs
132 if ('$F_UID') {
133 uid = tokenize(f_uid_str, delim)
134 while (uid != "") {
135 f_uid[strtol(uid, 10)] = 1
136 uid = tokenize("", delim)
137 }
138 }
139 }
140
141 probe syscall_any {
142 starttime[name, tid()] = gettimeofday_ns()
143 }
144
145 probe syscall_any.return {
146 # Skip if we have not seen this before
147 if (!([name, tid()] in starttime)) next
148
149 delta = gettimeofday_ns() - starttime[name, tid()]
150
151 # Check filters
152 if ('$FILTER') {
153 target = 0
154 if (('$F_PID') && (pid() in f_pid)) {
155 timebypid[name, pid()] <<< delta
156 target = 1
157 }
158 if (('$F_UID') && (uid() in f_uid)) {
159 timebyuid[name, uid()] <<< delta
160 target = 1
161 }
162 if (('$F_EXEC') && (execname() in f_exec)) {
163 timebyexec[name, execname()] <<< delta
164 target = 1
165 }
166 } else
167 target = 1
168
169 # Totals
170 if (target && prt_totals)
171 timebycall[name] <<< delta
172
173 delete starttime[name, tid()]
174 }
175
176 function print_header() {
177 printf("%22s %10s %12s %12s %12s %12s\n",
178 "System Call", "Count", "Total ns",
179 "Avg ns", "Min ns", "Max ns")
180 }
181
182 probe end {
183 if (prt_totals) {
184 printf("\nTimes for all processes%s:\n\n", filter_str)
185 print_header()
186 foreach (call in timebycall)
187 printf("%22s %10d %12d %12d %12d %12d\n", call,
188 @count(timebycall[call]),
189 @sum(timebycall[call]),
190 @avg(timebycall[call]),
191 @min(timebycall[call]),
192 @max(timebycall[call]))
193 }
194
195 if ('$F_PID') {
196 curpid = -1
197 foreach ([call, pid-] in timebypid) {
198 if (curpid != pid) {
199 curpid = pid
200 printf("\nTimes for process ID %d:\n\n", curpid)
201 print_header()
202 }
203 printf("%22s %10d %12d %12d %12d %12d\n", call,
204 @count(timebypid[call, pid]),
205 @sum(timebypid[call, pid]),
206 @avg(timebypid[call, pid]),
207 @min(timebypid[call, pid]),
208 @max(timebypid[call, pid]))
209 }
210 printf("\n")
211 }
212
213 if ('$F_EXEC') {
214 curexec = ""
215 foreach ([call, exec-] in timebyexec) {
216 if (curexec != exec) {
217 curexec = exec
218 printf("\nTimes for process name %s:\n\n", curexec)
219 print_header()
220 }
221 printf("%22s %10d %12d %12d %12d %12d\n", call,
222 @count(timebyexec[call, exec]),
223 @sum(timebyexec[call, exec]),
224 @avg(timebyexec[call, exec]),
225 @min(timebyexec[call, exec]),
226 @max(timebyexec[call, exec]))
227 }
228 printf("\n")
229 }
230
231 if ('$F_UID') {
232 curuid = -1
233 foreach ([call, uid-] in timebyuid) {
234 if (curuid != uid) {
235 curuid = uid
236 printf("\nTimes for user ID %d:\n\n", curuid)
237 print_header()
238 }
239 printf("%22s %10d %12d %12d %12d %12d\n", call,
240 @count(timebyuid[call, uid]),
241 @sum(timebyuid[call, uid]),
242 @avg(timebyuid[call, uid]),
243 @min(timebyuid[call, uid]),
244 @max(timebyuid[call, uid]))
245 }
246 printf("\n")
247 }
248
249 delete timebycall
250 if ('$F_PID') delete timebypid
251 if ('$F_UID') delete timebyuid
252 if ('$F_EXEC') delete timebyexec
253 }'