1#!/usr/sbin/dtrace -s
2/*
3 * vmstat.d - vmstat demo in DTrace.
4 *            Written using DTrace (Solaris 10 3/05).
5 *
6 * This has been written to demonstrate fetching the same data as vmstat
7 * from DTrace. This program is intended as a starting point for other
8 * DTrace scripts, by beginning with familiar statistics.
9 *
10 * $Id: vmstat.d 8 2007-08-06 05:55:26Z brendan $
11 *
12 * USAGE:	vmstat.d
13 *
14 * FIELDS:
15 *		w	swapped out LWPs	number
16 *		swap	virtual memory free	Kbytes
17 *		free	free RAM		Kbytes
18 *		re	page reclaims		Kbytes
19 *		mf	minor faults		Kbytes
20 *		pi	page ins		Kbytes
21 *		po	page outs		Kbytes
22 *		fr	pages freed		Kbytes
23 *		sr	scan rate		pages
24 *		in	interrupts		number
25 *		sy	system calls		number
26 *		cs	context switches	number
27 *
28 * NOTES:
29 *  Most of the statistics are in units of kilobytes, unlike the
30 *  original vmstat command which sometimes uses page counts.
31 *  As this program does not use Kstat, there is no summary since boot line.
32 *  Free RAM is both free free + cache free.
33 *
34 * SEE ALSO:	vmstat(1M)
35 *
36 * COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
37 *
38 * CDDL HEADER START
39 *
40 *  The contents of this file are subject to the terms of the
41 *  Common Development and Distribution License, Version 1.0 only
42 *  (the "License").  You may not use this file except in compliance
43 *  with the License.
44 *
45 *  You can obtain a copy of the license at Docs/cddl1.txt
46 *  or http://www.opensolaris.org/os/licensing.
47 *  See the License for the specific language governing permissions
48 *  and limitations under the License.
49 *
50 * CDDL HEADER END
51 *
52 * 11-Jun-2005  Brendan Gregg   Created this.
53 * 08-Jan-2006	   "      "	Last update.
54 */
55
56#pragma D option quiet
57
58inline int SCREEN = 21;
59
60/*
61 * Initialise variables
62 */
63dtrace:::BEGIN
64{
65	pi = 0; po = 0; re = 0; sr = 0; mf = 0; fr = 0;
66	sy = 0; in = 0; cs = 0;
67	lines = SCREEN + 1;
68}
69
70/*
71 * Print header
72 */
73dtrace:::BEGIN,
74profile:::tick-1sec
75/lines++ > SCREEN/
76{
77	printf(" %1s %10s %8s %5s %5s %4s %4s %4s %4s %5s %6s %4s\n",
78	    "w", "swap", "free", "re", "mf", "pi", "po", "fr", "sr",
79	    "in", "sy", "cs");
80	lines = 0;
81}
82
83/*
84 * Probe events
85 */
86vminfo:::pgpgin   { pi += arg0; }
87vminfo:::pgpgout  { po += arg0; }
88vminfo:::pgrec    { re += arg0; }
89vminfo:::scan	  { sr += arg0; }
90vminfo:::as_fault { mf += arg0; }
91vminfo:::dfree    { fr += arg0; }
92
93syscall:::entry		{ sy++; }
94sdt:::interrupt-start	{ in++; }
95sched::resume:on-cpu	{ cs++; }
96
97/*
98 * Print output line
99 */
100profile:::tick-1sec
101{
102	/* fetch free mem */
103	this->free = `freemem;
104
105	/*
106	 * fetch free swap
107	 *
108	 * free swap is described in /usr/include/vm/anon.h as,
109	 * MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
110	 */
111	this->ani_max = `k_anoninfo.ani_max;
112	this->ani_resv = `k_anoninfo.ani_phys_resv + `k_anoninfo.ani_mem_resv;
113	this->swap = (this->ani_max - this->ani_resv > 0 ?
114	    this->ani_max - this->ani_resv : 0) + `availrmem - `swapfs_minfree;
115
116	/* fetch w */
117	this->w = `nswapped;
118
119	/* convert to Kbytes */
120	pi *= `_pagesize / 1024;
121	po *= `_pagesize / 1024;
122	re *= `_pagesize / 1024;
123	sr *= `_pagesize / 1024;
124	mf *= `_pagesize / 1024;
125	fr *= `_pagesize / 1024;
126	this->swap *= `_pagesize / 1024;
127	this->free *= `_pagesize / 1024;
128
129	/* print line */
130	printf(" %1d %10d %8d %5d %5d %4d %4d %4d %4d %5d %6d %4d\n",
131	    this->w, this->swap, this->free, re, mf, pi, po, fr, sr,
132	    in, sy, cs);
133
134	/* clear counters */
135	pi = 0; po = 0; re = 0; sr = 0; mf = 0; fr = 0;
136	sy = 0; in = 0; cs = 0;
137}
138