aboutsummaryrefslogtreecommitdiff
path: root/cddl/contrib/dtracetoolkit/dvmstat
blob: 41f40e39e785718260cee2184ffb1e033a46a1d4 (plain) (blame)
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
249
250
#!/usr/bin/sh
#
# dvmstat - vmstat by PID/name/command.
#           Written using DTrace (Solaris 10 3/05).
#
# This program provides vmstat like data for one particular PID, a 
# process name, or when running a command. It prints statistics
# every second.
#
# $Id: dvmstat 3 2007-08-01 10:50:08Z brendan $
#
# USAGE:	dvmstat { -p PID | -n name | command }
#  eg,
#		dvmstat -p 1871       # examine PID 1871
#		dvmstat -n tar        # examine processes called "tar"
#		dvmstat df -h         # run and examine "df -h"
#
# FIELDS: 
#		re	page reclaims		Kbytes
#		maj	major faults		Kbytes
#		mf	minor faults		Kbytes
#		fr	page frees		Kbytes
#		epi	executable page ins	Kbytes
#		epo	executable page out	Kbytes
#		api	anonymous page ins	Kbytes
#		apo	anonymous page outs	Kbytes
#		fpi	filesystem page ins	Kbytes
#		fpo	filesystem page outs	Kbytes
#		sy	system calls		number
#
# SEE ALSO:	vmstat(1M)
#
# NOTES:
#
# When using dvmstat to run a command - if the command takes some time
# to execute, dvmstat will print output every second. If the command runs
# in less than a second, then the only one line of output will be printed.
#
# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
#
# CDDL HEADER START
#
#  The contents of this file are subject to the terms of the
#  Common Development and Distribution License, Version 1.0 only
#  (the "License").  You may not use this file except in compliance
#  with the License.
#
#  You can obtain a copy of the license at Docs/cddl1.txt
#  or http://www.opensolaris.org/os/licensing.
#  See the License for the specific language governing permissions
#  and limitations under the License.
#
# CDDL HEADER END
#
# Author: Brendan Gregg  [Sydney, Australia]
#
# 12-Jun-2005	Brendan Gregg	Created this.
# 08-Jan-2006	   "      "	Last update.
# 

##############################
# --- Process Arguments ---
#

### Default variables
opt_pid=0; opt_name=0; pid=0; pname="."; opt_command=0; command=""

### Process options
while getopts hn:p: name
do
        case $name in
        p)      opt_pid=1; pid=$OPTARG ;;
        n)      opt_name=1; pname=$OPTARG ;;
        h|?)    cat <<-END >&2
		USAGE: dvmstat [-h] { -p PID | -n name | command }
		           -p PID          # examine this PID
		           -n name         # examine this process name
		  eg,
		       dvmstat -p 1871     # examine PID 1871
		       dvmstat -n tar      # examine processes called "tar"
		       dvmstat df -h       # run and examine "df -h"
		END
                exit 1
        esac
done
shift `expr $OPTIND - 1`


### Option logic
if [ $opt_pid -eq 0 -a $opt_name -eq 0 ]; then
        opt_command=1
        if [ "$*" = "" ]; then
                $0 -h
                exit
        fi
        command="$*"
fi


#################################
# --- Main Program, DTrace ---
#
dtrace='
 #pragma D option quiet

 /*
  * Command line arguments
  */
 inline int OPT_pid      = '$opt_pid';
 inline int OPT_name     = '$opt_name';
 inline int OPT_command  = '$opt_command';
 inline int PID          = '$pid';
 inline string NAME      = "'$pname'";
 inline string COMMAND   = "'$command'";
 inline int SCREEN       = 21;

 /*
  * Initialise variables
  */
 dtrace:::BEGIN
 {
	epi = 0; epo = 0; api = 0; apo = 0; fpi = 0; fpo = 0;
	re = 0; mf = 0; maj = 0; fr = 0; sy = 0;
	lines = SCREEN + 1;
	header = 0;
 }

 /*
  * Print header
  */
 dtrace:::BEGIN,
 dtrace:::END,
 profile:::tick-1sec
 /(OPT_command && probename == "END") || 
  (!(OPT_command && probename == "BEGIN") && lines++ > SCREEN)/
 {
	printf("%6s %5s %5s %4s %4s %4s %4s %4s %4s %4s %6s\n",
	    "re", "maj", "mf", "fr", "epi", "epo", "api", "apo", 
	    "fpi", "fpo", "sy");
	lines = 0;
 }

 /*
  * Probe events
  *
  * this intentionally does not use an associative array for storing data,
  * for reasons of performance.
  */

 vminfo:::execpgin
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { epi += arg0; }

 vminfo:::execpgout
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { epo += arg0; }

 vminfo:::anonpgin
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { api += arg0; }

 vminfo:::anonpgout
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { apo += arg0; }

 vminfo:::fspgin
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { fpi += arg0; }

 vminfo:::fspgout
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { fpo += arg0; }

 vminfo:::pgrec
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { re += arg0; }

 vminfo:::as_fault
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { mf += arg0; }

 vminfo:::maj_fault
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { maj += arg0; }

 vminfo:::dfree
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { fr += arg0; }

 syscall:::entry
 /(OPT_pid && pid == PID) ||
  (OPT_name && execname == NAME) ||
  (OPT_command && pid == $target)/
 { sy++; }

 /* 
  * Print output line
  */
 profile:::tick-1sec,
 dtrace:::END
 {
	/* convert to Kbytes */
	re  *= `_pagesize / 1024;
	maj *= `_pagesize / 1024;
	mf  *= `_pagesize / 1024;
	fr  *= `_pagesize / 1024;
	epi *= `_pagesize / 1024;
	epo *= `_pagesize / 1024;
	api *= `_pagesize / 1024;
	apo *= `_pagesize / 1024;
	fpi *= `_pagesize / 1024;
	fpo *= `_pagesize / 1024;

	/* print line */
	printf("%6d %5d %5d %4d %4d %4d %4d %4d %4d %4d %6d\n",
	    re, maj, mf, fr, epi, epo, api, apo, fpi, fpo, sy);

	/* clear counters */
	epi = 0; epo = 0; api = 0; apo = 0; fpi = 0; fpo = 0;
	re = 0; mf = 0; maj = 0; fr = 0; sy = 0;
 }
'

### Run DTrace
if [ $opt_command -eq 1 ]; then
        /usr/sbin/dtrace -n "$dtrace" -x evaltime=exec -c "$command" >&2
else
        /usr/sbin/dtrace -n "$dtrace" >&2
fi