aboutsummaryrefslogtreecommitdiff
path: root/Zones/zvmstat
blob: e49f89c8ed541cf0558c9f04082ce75f32f7e47b (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/ksh
#
# zvmstat - print vmstat style info per Zone.
#           This uses DTrace (Solaris 10 3/05).
#
# This program must be run from the global zone as root.
#
# $Id: zvmstat 3 2007-08-01 10:50:08Z brendan $
#
# USAGE: 	zvmstat [-ht] [interval [count]]
#
#		zvmstat         # default output
#			-t      # print times
#  eg,
#		zvmstat 1       # print every 1 second
#		zvmstat 10 5    # print 5 x 10 second samples
#		zvmstat -t 5    # print every 5 seconds with time
#
#
# FIELDS:
#		re		page reclaims
#		mf		minor faults
#		fr		pages freed
#		sr		scan rate
#		epi		executable pages paged in
#		epo		executable pages paged out
#		epf		executable pages freed
#		api		anonymous pages paged in
#		apo		anonymous pages paged out
#		apf		anonymous pages freed
#		fpi		filesystem pages paged in
#		fpo		filesystem pages paged out
#		fpf		filesystem pages freed
#
# NOTES: 
# - Zone status should really be provided by Kstat, which currently
#   provides system wide values, per CPU and per processor set, but not per
#   zone. DTrace can fill this role in the meantime until Kstat supports zones.
# - First output does not contain summary since boot.
#
# SEE ALSO: prstat -Z
#
# 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
#
# BUGS:
# - First output may not contain all zones due to how loops are achieved.
#   Check for newer versions.
#
# Author: Brendan Gregg  [Sydney, Australia]
#
# 11-May-2005   Brendan Gregg   Created this.
# 26-Jul-2005	   "      "	Improved code.
# 08-Jan-2006	   "      "	Last update.
#


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

### default variables
opt_time=0; interval=1; counts=1

### process options
while getopts ht name
do
	case $name in
	t)      opt_time=1 ;;
	h|?)    cat <<-END >&2
		USAGE: zvmstat [-ht] [interval [count]]
		       zvmstat         # default output
		               -t      # print times
		   eg,
		       zvmstat 1       # print every 1 second
		       zvmstat 10 5    # print 5 x 10 second samples
		       zvmstat -t 5    # print every 5 seconds with time
		END
		exit 1
	esac
done
shift $(( OPTIND - 1 ))

### option logic
if (( "0$1" > 0 )); then
        interval=$1; counts=-1; shift
fi
if (( "0$1" > 0 )); then
        counts=$1; shift
fi


#################################
# --- Main Program, DTrace ---
#
dtrace -n '
 #pragma D option quiet
 #pragma D option destructive
 #pragma D option switchrate=10

 /*
  * Command line arguments
  */
 inline int OPT_time   = '$opt_time';
 inline int INTERVAL   = '$interval';
 inline int COUNTER    = '$counts';

 /* 
  * Initialise variables
  */
 dtrace:::BEGIN 
 {
	secs = INTERVAL; 
	counts = COUNTER;
	zonemax = 0;
	listing = 1;
	re[""] = 0; pi[""] = 0; po[""] = 0;
	mf[""] = 0; sr[""] = 0; fr[""] = 0;
	epi[""] = 0; epo[""] = 0; epf[""] = 0;
	api[""] = 0; apo[""] = 0; apf[""] = 0;
	fpi[""] = 0; fpo[""] = 0; fpf[""] = 0;
 }

 /*
  * Build zonelist array
  *
  * Here we want the output of a command to be saved into an array
  * inside dtrace. This is done by running the command, sending the
  * output to /dev/null, and by probing its write syscalls from dtrace. 
  *
  * This is an example of a "scraper".
  */

 /*
  * List zones
  */
 dtrace:::BEGIN 
 {
	/* run zoneadm */
	system("/usr/sbin/zoneadm list > /dev/null; echo END > /dev/null");
 }

 /*
  * Scrape zone listing
  */
 syscall::write:entry
 /listing && (execname == "zoneadm") && 
 (curthread->t_procp->p_parent->p_ppid == $pid)/
 {
	/* read zoneadm output */
	zonelist[zonemax] = stringof(copyin(arg1, arg2 - 1));

	/* increment max number of zones */
	zonemax++;
 }

 /*
  * Finish scraping zones
  */
 syscall::write:entry
 /listing && (execname == "sh") && (ppid == $pid)/
 {
	/*
	 * this end tag lets us know our zonelist has finished.
	 * thanks A. Packer.
	 */
	listing = stringof(copyin(arg1, arg2 - 1)) == "END" ? 0 : 1;
 }

 /*
  * Record vminfo counters
  */
 vminfo:::pgrec      { re[zonename] += arg0; }
 vminfo:::as_fault   { mf[zonename] += arg0; }
 vminfo:::scan       { sr[zonename] += arg0; }
 vminfo:::execpgin   { epi[zonename] += arg0; }
 vminfo:::execpgout  { epo[zonename] += arg0; }
 vminfo:::execfree   { epf[zonename] += arg0; fr[zonename] += arg0; }
 vminfo:::anonpgin   { api[zonename] += arg0; }
 vminfo:::anonpgout  { apo[zonename] += arg0; }
 vminfo:::anonfree   { apf[zonename] += arg0; fr[zonename] += arg0; }
 vminfo:::fspgin     { fpi[zonename] += arg0; }
 vminfo:::fspgout    { fpo[zonename] += arg0; }
 vminfo:::fsfree     { fpf[zonename] += arg0; fr[zonename] += arg0; }

 /*
  * Timer
  */
 profile:::tick-1sec
 {
	secs--;
 }

 /*
  * Check for exit
  */
 profile:::tick-1sec
 /counts == 0/
 {
	exit(0);
 }

 /*
  * Print header line
  */
 profile:::tick-1sec
 /secs == 0/
 {
	/* set counters */
	secs = INTERVAL;
	counts--;
	zonei = 0;

	/* print time */
	OPT_time ? printf("\n%Y,\n",walltimestamp) : 1;

	/* print output line */
	printf("%10s %4s %5s %4s %5s %4s %4s %4s %4s %4s %4s %4s %4s %4s\n",
	    "ZONE", "re", "mf", "fr", "sr", "epi", "epo", "epf", "api", "apo",
	    "apf", "fpi", "fpo", "fpf");

	/* ensure zone writes are triggered */
	printf(" \b");
 }

 /*
  * Print zone status line
  *
  * This is a fairly interesting function in that it loops over the keys in 
  * an associative array and prints out the values. DTrace cant really do 
  * loops, and generally doesnt need to. We "cheat" by generating writes
  * in the above probe which in turn trigger the probe below which 
  * contains the contents of each loop. Dont do this at home! We are
  * supposed to use aggreagations instead, wherever possible.
  *
  * This is an example of a "feedback loop".
  */
 syscall::write:return
 /pid == $pid && zonei < zonemax/
 {
	/* fetch zonename */
	self->zone = zonelist[zonei];

	/* print output */
	printf("%10s %4d %5d %4d %5d %4d %4d %4d %4d %4d %4d %4d %4d %4d\n",
	    self->zone, re[self->zone], mf[self->zone], fr[self->zone],
	    sr[self->zone], epi[self->zone], epo[self->zone],
	    epf[self->zone], api[self->zone], apo[self->zone],
	    apf[self->zone], fpi[self->zone], fpo[self->zone],
	    fpf[self->zone]);
	
	/* clear values */
	re[self->zone] = 0; mf[self->zone] = 0; fr[self->zone] = 0;
	sr[self->zone] = 0; epi[self->zone] = 0; epo[self->zone] = 0;
	epf[self->zone] = 0; api[self->zone] = 0; apo[self->zone] = 0;
	apf[self->zone] = 0; fpi[self->zone] = 0; fpo[self->zone] = 0;
	fpf[self->zone] = 0;
	self->zone = 0;
	
	/* go to next zone */
	zonei++;
 }
'