1#!/usr/bin/sh
2#
3# xvmstat - extended vmstat demo in DTrace.
4#           Written using DTrace (Solaris 10 3/05).
5#
6# This has been written to demonstrate fetching similar data as vmstat
7# from DTrace, with a few extra fields.
8#
9# 01-Mar-2006, ver 0.85
10#
11# USAGE:	xvmstat [interval [count]]
12#
13# FIELDS: 
14#		w	swapped out LWPs	number
15#		swap	virtual memory free	Mbytes
16#		free	free RAM		Mbytes
17#		re	page reclaims		pages/sec
18#		maj	major faults		pages/sec
19#		mf	minor faults		pages/sec
20#		cow	copy-on-write faults	pages/sec
21#		pro	protection faults	pages/sec
22#		sr	scan rate		pages/sec
23#		epi	executable page ins	pages/sec
24#		epo	executable page outs	pages/sec
25#		epf	executable frees	pages/sec
26#		api	anonymous page ins	pages/sec
27#		apo	anonymous page outs	pages/sec
28#		apf	anonymous frees		pages/sec
29#		fpi	filesystem page ins	pages/sec
30#		fpo	filesystem page outs	pages/sec
31#		fpf	filesystem frees	pages/sec
32#
33# NOTES: 
34# - Most of the statistics are in units of pages, unlike the
35#   original vmstat command which sometimes uses kilobytes. 
36# - As this program does not use Kstat, there is no summary since boot line.
37# - Free RAM is both free free + cache free.
38#
39# SEE ALSO:	vmstat(1M)
40#
41# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
42#
43# CDDL HEADER START
44#
45#  The contents of this file are subject to the terms of the
46#  Common Development and Distribution License, Version 1.0 only
47#  (the "License").  You may not use this file except in compliance
48#  with the License.
49#
50#  You can obtain a copy of the license at Docs/cddl1.txt
51#  or http://www.opensolaris.org/os/licensing.
52#  See the License for the specific language governing permissions
53#  and limitations under the License.
54#
55# CDDL HEADER END
56#
57# 12-Jun-2005  Brendan Gregg   Created this.
58#
59
60##############################
61# --- Process Arguments ---
62#
63
64### default values
65interval=1; count=-1
66
67### check arguments
68if [ "$1" = "-h" -o "$1" = "--help" ]; then
69	cat <<-END >&2
70	USAGE: xvmstat [interval [count]]
71	       xvmstat          # 1 second samples, infinite
72	  eg,
73	       xvmstat 1        # print every 1 second
74	       xvmstat 5 6      # print every 5 seconds, 6 times
75	END
76	exit 1
77fi
78
79### argument logic
80if [ "$1" -gt 0 ]; then
81        interval=$1; count=-1; shift
82fi
83if [ "$1" -gt 0 ]; then
84        count=$1; shift
85fi
86if [ $interval -eq 0 ]; then
87	interval=1
88fi
89
90
91#################################
92# --- Main Program, DTrace ---
93#
94/usr/sbin/dtrace -n '
95 #pragma D option quiet
96
97 /*
98  * Command line arguments
99  */
100 inline int INTERVAL   = '$interval';
101 inline int COUNTER    = '$count';
102 inline int SCREEN = 21;
103
104 /*
105  * Initialise variables
106  */
107 dtrace:::BEGIN
108 {
109	re = 0; sr = 0; mf = 0; maj = 0; cow = 0; pro = 0;
110	epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0;
111	fpi = 0; fpo = 0; fpf = 0;
112	lines = SCREEN + 1;
113	counts = COUNTER;
114	secs = INTERVAL;
115	first = 1;
116 }
117
118 profile:::tick-1sec
119 {
120        secs--;
121 }
122
123 /*
124  * Print header
125  */
126 dtrace:::BEGIN,
127 profile:::tick-1sec
128 /first || (secs == 0 && lines > SCREEN)/
129 {
130	printf("%2s %6s %5s %5s %3s %4s %3s %3s %3s ",
131	    "w", "swap", "free", "re", "maj", "mf", "cow", "pro", "sr");
132	printf("%3s %3s %3s %3s %3s %3s %3s %3s %3s\n",
133	    "epi", "epo", "epf", "api", "apo", "apf", "fpi", "fpo", "fpf");
134	lines = 0;
135	first = 0;
136 }
137
138 /*
139  * Probe events
140  */
141 vminfo:::pgrec      { re += arg0; }
142 vminfo:::scan       { sr += arg0; }
143 vminfo:::as_fault   { mf += arg0; }
144 vminfo:::execpgin   { epi += arg0; }
145 vminfo:::execpgout  { epo += arg0; }
146 vminfo:::execfree   { epf += arg0; }
147 vminfo:::anonpgin   { api += arg0; }
148 vminfo:::anonpgout  { apo += arg0; }
149 vminfo:::anonfree   { apf += arg0; }
150 vminfo:::fspgin     { fpi += arg0; }
151 vminfo:::fspgout    { fpo += arg0; }
152 vminfo:::fsfree     { fpf += arg0; }
153 vminfo:::maj_fault  { maj += arg0; }
154 vminfo:::cow_fault  { cow += arg0; }
155 vminfo:::prot_fault { pro += arg0; }
156
157 /* 
158  * Print output line
159  */
160 profile:::tick-1sec
161 /secs == 0/
162 {
163	/* fetch free mem */
164	this->free = `freemem;
165
166	/*
167	 * fetch free swap
168	 *
169	 * free swap is described in /usr/include/vm/anon.h as,
170	 * MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
171	 */
172	this->ani_max = `k_anoninfo.ani_max;
173	this->ani_resv = `k_anoninfo.ani_phys_resv + `k_anoninfo.ani_mem_resv;
174	this->swap = (this->ani_max - this->ani_resv > 0 ?
175	    this->ani_max - this->ani_resv : 0) + `availrmem - `swapfs_minfree;
176
177	/* fetch w */
178	this->w = `nswapped;
179
180	/* convert to Mbytes */
181	this->swap *= `_pagesize; this->swap /= 1048576;
182	this->free *= `_pagesize; this->free /= 1048576;
183
184	/* convert to per second values */
185	re  /= INTERVAL; maj /= INTERVAL; mf  /= INTERVAL;
186	cow /= INTERVAL; pro /= INTERVAL; sr  /= INTERVAL;
187	epi /= INTERVAL; epo /= INTERVAL; epf /= INTERVAL;
188	api /= INTERVAL; apo /= INTERVAL; apf /= INTERVAL;
189	fpi /= INTERVAL; fpo /= INTERVAL; fpf /= INTERVAL;
190
191	/* print line */
192	printf("%2d %6d %5d %5d %3d %4d %3d %3d %3d ",
193	    this->w, this->swap, this->free, re, maj, mf, cow, pro, sr);
194	printf("%3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
195	    epi, epo, epf, api, apo, apf, fpi, fpo, fpf);
196
197	/* clear counters */
198	re = 0; sr = 0; mf = 0; maj = 0; cow = 0; pro = 0;
199	epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0;
200	fpi = 0; fpo = 0; fpf = 0;
201
202	/* process counts */
203	secs = INTERVAL;
204	counts--;
205	lines++;
206 }
207
208 /*
209  * End
210  */
211 profile:::tick-1sec
212 /counts == 0/
213 {
214	exit(0);
215 }
216'
217