summaryrefslogtreecommitdiffstats
path: root/syscalltimes
blob: 84ca77a9f45bc8c2c062f74dc407c3d201291840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash

# Syscalltimes systemtap script
# Copyright (C) 2007 IBM Corp.
#
# This file is part of systemtap, and is free software.  You can
# redistribute it and/or modify it under the terms of the GNU General
# Public License (GPL); either version 2, or (at your option) any
# later version.

###
### syscalltime - Combination shell/systemtap script to measure system call
###               counts and times.  Can be filtered by process IDs, process
###               names and users.
###

# Filter options
F_PIDSTR="";  F_PID=0   # Filter by process ID
F_EXECSTR=""; F_EXEC=0  # Filter by process name
F_UIDSTR="";  F_UID=0   # Filter by user
FILTER=0                # Any filters specified?

# Print options
P_TOTALS=0              # Print totals
P_VERBOSE_STR=""        # Print verbose build output

DELIM=","

function usage {
	echo "USAGE: syscalltimes [-n pname]... [-p pid]... [-u username]..."
	echo "                    [-v]... [-t] [-h]"
	echo "    -n pname     # filter by this process name"
	echo "    -p pid       # filter by this process ID"
	echo "    -u username  # filter by this user"
	echo "    -t           # print totals (default with filters: OFF"
	echo "                                 default without filters: ON)"
	echo "    -v           # print verbose output during SystemTap module creation"
	echo "    -h           # print this help text"
	echo ""
	echo "NOTE: This script can take long time to build. Use -v[vv] to monitor"
	echo "      the module creation and build process."
}

# Process options
while getopts n:p:u:vth option; do
	case $option in
	n)	let "F_EXEC++"
		F_EXECSTR=$OPTARG$DELIM$F_EXECSTR ;;

	p)	let "F_PID++"
		F_PIDSTR=$OPTARG$DELIM$F_PIDSTR ;;

	u)	uid=`awk -F: '$1 == name {print $3}' name=$OPTARG /etc/passwd`
		if [[ $uid != "" ]]; then
			let "F_UID++"
			F_UIDSTR=$uid$DELIM$F_UIDSTR
		else
			echo "ERROR: Unknown user:" $OPTARG
			let "ERROR++"
		fi ;;

	v)	P_VERBOSE_STR="-v "$P_VERBOSE_STR ;;

	t)	P_TOTALS=1 ;;

	h|?|*)  usage
		exit 1 ;;
	esac
done

if [[ $ERROR > 0 ]]; then
	exit 1
fi

if [[ $F_EXEC > 0 || $F_PID > 0 || $F_UID > 0 ]]; then
	FILTER=1
fi

echo "Creating and building SystemTap module..."

#
# SystemTap script
#
stap $P_VERBOSE_STR -e '
global start, timebycall, timebypid, timebyuid, timebyexec
global f_exec_str, f_pid_str, f_uid_str
global f_exec, f_pid, f_uid
global prt_totals, filter_str

probe begin {
	printf("Collecting data - type Ctrl-C to print output and exit...\n")
	
	# If no filters specified, skip filter processing
	if ('$FILTER' == 0) {
		filter_str = " (unfiltered)"
		prt_totals = 1  // On by default if no filtering
		next
	} else
		filter_str = " (filtered)"

	prt_totals   = '$P_TOTALS'
	f_exec_str   = "'$F_EXECSTR'"
	f_pid_str    = "'$F_PIDSTR'"
	f_uid_str    = "'$F_UIDSTR'"

	delim = "'$DELIM'"

	# Process names
	if ('$F_EXEC') {
		pname = tokenize(f_exec_str, delim)
		while (pname != "") {
			f_exec[pname] = 1
			pname = tokenize("", delim)
		}
	}

	# Process IDs
	if ('$F_PID') {
		pid = tokenize(f_pid_str, delim)
		while (pid != "") {
			f_pid[strtol(pid, 10)] = 1
			pid = tokenize("", delim)
		}
	}

	# User IDs
	if ('$F_UID') {
		uid = tokenize(f_uid_str, delim)
		while (uid != "") {
			f_uid[strtol(uid, 10)] = 1
			uid = tokenize("", delim)
		}
	}
}

probe syscall.* {
	start[name, tid()] = gettimeofday_ns()
}

probe syscall.*.return {
	# Skip if we have not seen this before
	if (!([name, tid()] in start)) next

	delta = gettimeofday_ns() - start[name, tid()]

	# Check filters
	if ('$FILTER') {
		target = 0
		if (pid() in f_pid) {
			timebypid[name, pid()] <<< delta
			target = 1
		}
		if (uid() in f_uid) {
			timebyuid[name, uid()] <<< delta
			target = 1
		}
		if (execname() in f_exec) {
			timebyexec[name, execname()] <<< delta
			target = 1
		}
	} else
		target = 1

	# Totals
	if (target && prt_totals)
		timebycall[name] <<< delta

	delete start[name, tid()]
}

function print_header() {
	printf("%22s %10s %12s %12s %12s %12s\n",
	       "System Call", "Count", "Total ns",
	       "Avg ns", "Min ns", "Max ns")
}

probe end {
	if (prt_totals) {
		printf("\nTimes for all processes%s:\n\n", filter_str)
		print_header()
		foreach (call in timebycall)
			printf("%22s %10d %12d %12d %12d %12d\n", call,
			       @count(timebycall[call]),
			       @sum(timebycall[call]),
			       @avg(timebycall[call]),
		               @min(timebycall[call]),
			       @max(timebycall[call]))
	}

	if ('$F_PID') {
		curpid = -1
		foreach ([call, pid-] in timebypid) {
			if (curpid != pid) {
				curpid = pid
				printf("\nTimes for process ID %d:\n\n", curpid)
				print_header()
			}
			printf("%22s %10d %12d %12d %12d %12d\n", call,
			       @count(timebypid[call, pid]),
			       @sum(timebypid[call, pid]),
			       @avg(timebypid[call, pid]),
			       @min(timebypid[call, pid]),
			       @max(timebypid[call, pid]))
		}
		printf("\n")
	}

	if ('$F_EXEC') {
		curexec = ""
		foreach ([call, exec-] in timebyexec) {
			if (curexec != exec) {
				curexec = exec
				printf("\nTimes for process name %s:\n\n", curexec)
				print_header()
			}
			printf("%22s %10d %12d %12d %12d %12d\n", call,
			       @count(timebyexec[call, exec]),
			       @sum(timebyexec[call, exec]),
			       @avg(timebyexec[call, exec]),
			       @min(timebyexec[call, exec]),
			       @max(timebyexec[call, exec]))
		}
		printf("\n")
	}

	if ('$F_UID') {
		curuid = -1
		foreach ([call, uid-] in timebyuid) {
			if (curuid != uid) {
				curuid = uid
				printf("\nTimes for user ID %d:\n\n", curuid)
				print_header()
			}
			printf("%22s %10d %12d %12d %12d %12d\n", call,
			       @count(timebyuid[call, uid]),
			       @sum(timebyuid[call, uid]),
			       @avg(timebyuid[call, uid]),
			       @min(timebyuid[call, uid]),
			       @max(timebyuid[call, uid]))
		}
		printf("\n")
	}

	delete timebycall
	delete timebypid
	delete timebyuid
	delete timebyexec
}'