1#!/usr/sbin/dtrace -s
2/*
3 * swapinfo.d - print virtual memory info (swap).
4 *              Written using DTrace (Solaris 10 3/05)
5 *
6 * Prints swap usage details for RAM and disk based swap.
7 * This script is UNDER CONSTRUCTION, check for newer versions.
8 *
9 * 24-Apr-2006, ver 0.84
10 *
11 * USAGE:       swapinfo.d	(check for newer versions)
12 *
13 * FIELDS:
14 *              RAM Total       Total RAM installed
15 *              RAM Unusable    RAM consumed by the OBP and TSBs
16 *              RAM Kernel      Kernel resident in RAM (and usually locked)
17 *              RAM Locked      Locked memory pages from swap (Anon)
18 *              RAM Used        anon + exec + file pages used
19 *              RAM Free        free memory + page cache free
20 *              Disk Total      Total disk swap configured
21 *              Disk Resv       Disk swap allocated + reserved
22 *              Disk Avail      Disk swap available for reservation
23 *              Swap Total      Total Virtual Memory usable
24 *              Swap Resv       VM allocated + reserved
25 *              Swap Avail      VM available for reservation
26 *              Swap MinFree    VM kept free from reservations
27 *
28 * SEE ALSO: swapinfo - K9Toolkit, http://www.brendangregg.com/k9toolkit.html
29 *           vmstat 1 2; swap -s; echo ::memstat | mdb -k
30 *           RMCmem - The MemTool Package
31 *           RICHPse - The SE Toolkit
32 *           "Clearing up swap space confusion" Unix Insider, Adrian Cockcroft
33 *           "Solaris Internals", Jim Mauro, Richard McDougall
34 *           /usr/include/vm/anon.h, /usr/include/sys/systm.h
35 *
36 * COPYRIGHT: Copyright (c) 2005, 2006 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 * Author: Brendan Gregg  [Sydney, Australia]
53 *
54 * 11-Jun-2005  Brendan Gregg   Created this.
55 * 24-Apr-2006	   "	  "	Improved disk measurements; changed terms.
56 */
57
58#pragma D option quiet
59#pragma D option bufsize=16k
60
61inline int DEBUG = 0;
62
63dtrace:::BEGIN
64{
65	/* Debug stats */
66	this->ani_max = `k_anoninfo.ani_max;
67	this->ani_phys_resv = `k_anoninfo.ani_phys_resv;
68	this->ani_mem_resv = `k_anoninfo.ani_mem_resv;
69	this->ani_locked = `k_anoninfo.ani_locked_swap;
70	this->availrmem = `availrmem;
71
72	/* RAM stats */
73	this->ram_total = `physinstalled;
74	this->unusable  = `physinstalled - `physmem;
75	this->locked    = `pages_locked;
76	this->ram_used  = `availrmem - `freemem;
77	this->freemem   = `freemem;
78	this->kernel    = `physmem - `pages_locked - `availrmem;
79
80	/* Disk stats */
81	this->disk_total = `k_anoninfo.ani_max;
82	this->disk_resv = `k_anoninfo.ani_phys_resv;
83	this->disk_avail = this->disk_total - this->disk_resv;
84
85	/* Total Swap stats */
86	this->minfree = `swapfs_minfree;
87	this->reserve = `swapfs_reserve;
88	/* this is TOTAL_AVAILABLE_SWAP from /usr/include/vm/anon.h, */
89	this->swap_total = `k_anoninfo.ani_max +
90	    (`availrmem - `swapfs_minfree > 0 ?
91	    `availrmem - `swapfs_minfree : 0);
92	/* this is CURRENT_TOTAL_AVAILABLE_SWAP from /usr/include/vm/anon.h, */
93	this->swap_avail = `k_anoninfo.ani_max - `k_anoninfo.ani_phys_resv +
94	    (`availrmem - `swapfs_minfree > 0 ?
95	    `availrmem - `swapfs_minfree : 0);
96	this->swap_resv = this->swap_total - this->swap_avail;
97
98	/* Convert to Mbytes */
99	this->ani_phys_resv *= `_pagesize;  this->ani_phys_resv /= 1048576;
100	this->ani_mem_resv *= `_pagesize;  this->ani_mem_resv /= 1048576;
101	this->ani_locked *= `_pagesize;  this->ani_locked /= 1048576;
102	this->ani_max	*= `_pagesize;  this->ani_max	/= 1048576;
103	this->availrmem	*= `_pagesize;  this->availrmem	/= 1048576;
104	this->ram_total	*= `_pagesize;  this->ram_total	/= 1048576;
105	this->unusable	*= `_pagesize;  this->unusable	/= 1048576;
106	this->kernel	*= `_pagesize;  this->kernel	/= 1048576;
107	this->locked	*= `_pagesize;  this->locked	/= 1048576;
108	this->ram_used	*= `_pagesize;  this->ram_used	/= 1048576;
109	this->freemem	*= `_pagesize;  this->freemem	/= 1048576;
110	this->disk_total *= `_pagesize; this->disk_total /= 1048576;
111	this->disk_resv	*= `_pagesize;  this->disk_resv	/= 1048576;
112	this->disk_avail *= `_pagesize;  this->disk_avail /= 1048576;
113	this->swap_total *= `_pagesize; this->swap_total /= 1048576;
114	this->swap_avail *= `_pagesize;  this->swap_avail /= 1048576;
115	this->swap_resv	*= `_pagesize;  this->swap_resv	/= 1048576;
116	this->minfree	*= `_pagesize;  this->minfree	/= 1048576;
117	this->reserve	*= `_pagesize;  this->reserve	/= 1048576;
118
119	/* Print debug */
120	DEBUG ? printf("DEBUG   availrmem %5d MB\n", this->availrmem) : 1;
121	DEBUG ? printf("DEBUG     freemem %5d MB\n", this->freemem) : 1;
122	DEBUG ? printf("DEBUG     ani_max %5d MB\n", this->ani_max) : 1;
123	DEBUG ? printf("DEBUG ani_phys_re %5d MB\n", this->ani_phys_resv) : 1;
124	DEBUG ? printf("DEBUG  ani_mem_re %5d MB\n", this->ani_mem_resv) : 1;
125	DEBUG ? printf("DEBUG  ani_locked %5d MB\n", this->ani_locked) : 1;
126	DEBUG ? printf("DEBUG     reserve %5d MB\n", this->reserve) : 1;
127	DEBUG ? printf("\n") : 1;
128
129	/* Print report */
130	printf("RAM  _______Total %5d MB\n", this->ram_total);
131	printf("RAM      Unusable %5d MB\n", this->unusable);
132	printf("RAM        Kernel %5d MB\n", this->kernel);
133	printf("RAM        Locked %5d MB\n", this->locked);
134	printf("RAM          Used %5d MB\n", this->ram_used);
135	printf("RAM          Free %5d MB\n", this->freemem);
136	printf("\n");
137	printf("Disk _______Total %5d MB\n", this->disk_total);
138	printf("Disk         Resv %5d MB\n", this->disk_resv);
139	printf("Disk        Avail %5d MB\n", this->disk_avail);
140	printf("\n");
141	printf("Swap _______Total %5d MB\n", this->swap_total);
142	printf("Swap         Resv %5d MB\n", this->swap_resv);
143	printf("Swap        Avail %5d MB\n", this->swap_avail);
144	printf("Swap    (Minfree) %5d MB\n", this->minfree);
145
146	DEBUG ? printf("\nNow run other commands for confirmation.\n") : 1;
147	! DEBUG ? exit(0) : 1;
148}
149