1235368Sgnn#!/usr/sbin/dtrace -s
2235368Sgnn/*
3235368Sgnn * vmstat-p.d - vmstat -p demo in DTrace.
4235368Sgnn *              Written using DTrace (Solaris 10 3/05).
5235368Sgnn *
6235368Sgnn * This has been written to demonstrate fetching similar data as vmstat
7235368Sgnn * from DTrace. This program is intended as a starting point for other
8235368Sgnn * DTrace scripts, by beginning with familiar statistics.
9235368Sgnn *
10235368Sgnn * $Id: vmstat-p.d 3 2007-08-01 10:50:08Z brendan $
11235368Sgnn *
12235368Sgnn * USAGE:	vmstat-p.d
13235368Sgnn *
14235368Sgnn * FIELDS:
15235368Sgnn *		swap	virtual memory free	Kbytes
16235368Sgnn *		free	free RAM		Kbytes
17235368Sgnn *		re	page reclaims		Kbytes
18235368Sgnn *		mf	minor faults		Kbytes
19235368Sgnn *		sr	scan rate		pages
20235368Sgnn *		epi	executable page ins	Kbytes
21235368Sgnn *		epo	executable page outs	Kbytes
22235368Sgnn *		epf	executable frees	Kbytes
23235368Sgnn *		api	anonymous page ins	Kbytes
24235368Sgnn *		apo	anonymous page outs	Kbytes
25235368Sgnn *		apf	anonymous frees		Kbytes
26235368Sgnn *		fpi	filesystem page ins	Kbytes
27235368Sgnn *		fpo	filesystem page outs	Kbytes
28235368Sgnn *		fpf	filesystem frees	Kbytes
29235368Sgnn *
30235368Sgnn * NOTES:
31235368Sgnn *	Most of the statistics are in units of kilobytes, unlike the
32235368Sgnn *	original vmstat command which sometimes uses page counts.
33235368Sgnn *	As this program does not use Kstat, there is no summary since
34235368Sgnn *	boot line. Free RAM is both free free + cache free.
35235368Sgnn *
36235368Sgnn * SEE ALSO:	vmstat(1M)
37235368Sgnn *
38235368Sgnn * COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
39235368Sgnn *
40235368Sgnn * CDDL HEADER START
41235368Sgnn *
42235368Sgnn *  The contents of this file are subject to the terms of the
43235368Sgnn *  Common Development and Distribution License, Version 1.0 only
44235368Sgnn *  (the "License").  You may not use this file except in compliance
45235368Sgnn *  with the License.
46235368Sgnn *
47235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
48235368Sgnn *  or http://www.opensolaris.org/os/licensing.
49235368Sgnn *  See the License for the specific language governing permissions
50235368Sgnn *  and limitations under the License.
51235368Sgnn *
52235368Sgnn * CDDL HEADER END
53235368Sgnn *
54235368Sgnn * 11-Jun-2005  Brendan Gregg   Created this.
55235368Sgnn * 08-Jan-2006	   "      "	Last update.
56235368Sgnn */
57235368Sgnn
58235368Sgnn#pragma D option quiet
59235368Sgnn
60235368Sgnninline int SCREEN = 21;
61235368Sgnn
62235368Sgnn/*
63235368Sgnn * Initialise variables
64235368Sgnn */
65235368Sgnndtrace:::BEGIN
66235368Sgnn{
67235368Sgnn	pi = 0; po = 0; re = 0; sr = 0; mf = 0; fr = 0;
68235368Sgnn	sy = 0; in = 0; cs = 0; maj = 0; cow = 0; pro = 0;
69235368Sgnn	epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0;
70235368Sgnn	fpi = 0; fpo = 0; fpf = 0;
71235368Sgnn	lines = SCREEN + 1;
72235368Sgnn}
73235368Sgnn
74235368Sgnn/*
75235368Sgnn * Print header
76235368Sgnn */
77235368Sgnndtrace:::BEGIN,
78235368Sgnntick-1sec
79235368Sgnn/lines++ > SCREEN/
80235368Sgnn{
81235368Sgnn	printf("%14s %13s %16s %14s %13s\n",
82235368Sgnn	    "memory", "page", "executable", "anonymous", "filesystem");
83235368Sgnn	printf("%9s %7s %5s %4s %3s ",
84235368Sgnn	    "swap", "free", "re", "mf", "sr");
85235368Sgnn	printf("%4s %4s %4s %4s %4s %4s %4s %4s %4s\n",
86235368Sgnn	    "epi", "epo", "epf", "api", "apo", "apf", "fpi", "fpo", "fpf");
87235368Sgnn	lines = 0;
88235368Sgnn}
89235368Sgnn
90235368Sgnn/*
91235368Sgnn * Probe events
92235368Sgnn */
93235368Sgnnvminfo:::pgrec	   { re += arg0; }
94235368Sgnnvminfo:::scan	   { sr += arg0; }
95235368Sgnnvminfo:::as_fault  { mf += arg0; }
96235368Sgnnvminfo:::execpgin  { epi += arg0; }
97235368Sgnnvminfo:::execpgout { epo += arg0; }
98235368Sgnnvminfo:::execfree  { epf += arg0; }
99235368Sgnnvminfo:::anonpgin  { api += arg0; }
100235368Sgnnvminfo:::anonpgout { apo += arg0; }
101235368Sgnnvminfo:::anonfree  { apf += arg0; }
102235368Sgnnvminfo:::fspgin    { fpi += arg0; }
103235368Sgnnvminfo:::fspgout   { fpo += arg0; }
104235368Sgnnvminfo:::fsfree    { fpf += arg0; }
105235368Sgnn
106235368Sgnn/*
107235368Sgnn * Print output line
108235368Sgnn */
109235368Sgnnprofile:::tick-1sec
110235368Sgnn{
111235368Sgnn	/* fetch free mem */
112235368Sgnn	this->free = `freemem;
113235368Sgnn
114235368Sgnn	/*
115235368Sgnn	 * fetch free swap
116235368Sgnn	 *
117235368Sgnn	 * free swap is described in /usr/include/vm/anon.h as,
118235368Sgnn	 * MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
119235368Sgnn	 */
120235368Sgnn	this->ani_max = `k_anoninfo.ani_max;
121235368Sgnn	this->ani_resv = `k_anoninfo.ani_phys_resv + `k_anoninfo.ani_mem_resv;
122235368Sgnn	this->swap = (this->ani_max - this->ani_resv > 0 ?
123235368Sgnn	    this->ani_max - this->ani_resv : 0) + `availrmem - `swapfs_minfree;
124235368Sgnn
125235368Sgnn	/* fetch w */
126235368Sgnn	this->w = `nswapped;
127235368Sgnn
128235368Sgnn	/* convert to Kbytes */
129235368Sgnn	epi *= `_pagesize / 1024;
130235368Sgnn	epo *= `_pagesize / 1024;
131235368Sgnn	epf *= `_pagesize / 1024;
132235368Sgnn	api *= `_pagesize / 1024;
133235368Sgnn	apo *= `_pagesize / 1024;
134235368Sgnn	apf *= `_pagesize / 1024;
135235368Sgnn	fpi *= `_pagesize / 1024;
136235368Sgnn	fpo *= `_pagesize / 1024;
137235368Sgnn	fpf *= `_pagesize / 1024;
138235368Sgnn	re  *= `_pagesize / 1024;
139235368Sgnn	sr  *= `_pagesize / 1024;
140235368Sgnn	mf  *= `_pagesize / 1024;
141235368Sgnn	this->swap *= `_pagesize / 1024;
142235368Sgnn	this->free *= `_pagesize / 1024;
143235368Sgnn
144235368Sgnn	/* print line */
145235368Sgnn	printf("%9d %7d %5d %4d %3d ",
146235368Sgnn	    this->swap, this->free, re, mf, sr);
147235368Sgnn	printf("%4d %4d %4d %4d %4d %4d %4d %4d %4d\n",
148235368Sgnn	    epi, epo, epf, api, apo, apf, fpi, fpo, fpf);
149235368Sgnn
150235368Sgnn	/* clear counters */
151235368Sgnn	pi = 0; po = 0; re = 0; sr = 0; mf = 0; fr = 0;
152235368Sgnn	sy = 0; in = 0; cs = 0; maj = 0; cow = 0; pro = 0;
153235368Sgnn	epi = 0; epo = 0; epf = 0; api = 0; apo = 0; apf = 0;
154235368Sgnn	fpi = 0; fpo = 0; fpf = 0;
155235368Sgnn}
156