1235368Sgnn#!/usr/sbin/dtrace -s
2235368Sgnn/*
3235368Sgnn * swapinfo.d - print virtual memory info (swap).
4235368Sgnn *              Written using DTrace (Solaris 10 3/05)
5235368Sgnn *
6235368Sgnn * Prints swap usage details for RAM and disk based swap.
7235368Sgnn * This script is UNDER CONSTRUCTION, check for newer versions.
8235368Sgnn *
9235368Sgnn * $Id: swapinfo.d 3 2007-08-01 10:50:08Z brendan $
10235368Sgnn *
11235368Sgnn * USAGE:       swapinfo.d	(check for newer versions)
12235368Sgnn *
13235368Sgnn * FIELDS:
14235368Sgnn *              RAM Total       Total RAM installed
15235368Sgnn *              RAM Unusable    RAM consumed by the OBP and TSBs
16235368Sgnn *              RAM Kernel      Kernel resident in RAM (and usually locked)
17235368Sgnn *              RAM Locked      Locked memory pages from swap (Anon)
18235368Sgnn *              RAM Used        anon + exec + file pages used
19235368Sgnn *              RAM Free        free memory + page cache free
20235368Sgnn *              Disk Total      Total disk swap configured
21235368Sgnn *              Disk Resv       Disk swap allocated + reserved
22235368Sgnn *              Disk Avail      Disk swap available for reservation
23235368Sgnn *              Swap Total      Total Virtual Memory usable
24235368Sgnn *              Swap Resv       VM allocated + reserved
25235368Sgnn *              Swap Avail      VM available for reservation
26235368Sgnn *              Swap MinFree    VM kept free from reservations
27235368Sgnn *
28235368Sgnn * SEE ALSO: swapinfo - K9Toolkit, http://www.brendangregg.com/k9toolkit.html
29235368Sgnn *           vmstat 1 2; swap -s; echo ::memstat | mdb -k
30235368Sgnn *           RMCmem - The MemTool Package
31235368Sgnn *           RICHPse - The SE Toolkit
32235368Sgnn *           "Clearing up swap space confusion" Unix Insider, Adrian Cockcroft
33235368Sgnn *           "Solaris Internals", Jim Mauro, Richard McDougall
34235368Sgnn *           /usr/include/vm/anon.h, /usr/include/sys/systm.h
35235368Sgnn *
36235368Sgnn * COPYRIGHT: Copyright (c) 2005, 2006 Brendan Gregg.
37235368Sgnn *
38235368Sgnn * CDDL HEADER START
39235368Sgnn *
40235368Sgnn *  The contents of this file are subject to the terms of the
41235368Sgnn *  Common Development and Distribution License, Version 1.0 only
42235368Sgnn *  (the "License").  You may not use this file except in compliance
43235368Sgnn *  with the License.
44235368Sgnn *
45235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
46235368Sgnn *  or http://www.opensolaris.org/os/licensing.
47235368Sgnn *  See the License for the specific language governing permissions
48235368Sgnn *  and limitations under the License.
49235368Sgnn *
50235368Sgnn * CDDL HEADER END
51235368Sgnn *
52235368Sgnn * Author: Brendan Gregg  [Sydney, Australia]
53235368Sgnn *
54235368Sgnn * 11-Jun-2005  Brendan Gregg   Created this.
55235368Sgnn * 24-Apr-2006	   "	  "	Improved disk measurements; changed terms.
56235368Sgnn * 24-Apr-2006	   "	  "	Last update.
57235368Sgnn */
58235368Sgnn
59235368Sgnn#pragma D option quiet
60235368Sgnn#pragma D option bufsize=16k
61235368Sgnn
62235368Sgnninline int DEBUG = 0;
63235368Sgnn
64235368Sgnndtrace:::BEGIN
65235368Sgnn{
66235368Sgnn	/* Debug stats */
67235368Sgnn	this->ani_max = `k_anoninfo.ani_max;
68235368Sgnn	this->ani_phys_resv = `k_anoninfo.ani_phys_resv;
69235368Sgnn	this->ani_mem_resv = `k_anoninfo.ani_mem_resv;
70235368Sgnn	this->ani_locked = `k_anoninfo.ani_locked_swap;
71235368Sgnn	this->availrmem = `availrmem;
72235368Sgnn
73235368Sgnn	/* RAM stats */
74235368Sgnn	this->ram_total = `physinstalled;
75235368Sgnn	this->unusable  = `physinstalled - `physmem;
76235368Sgnn	this->locked    = `pages_locked;
77235368Sgnn	this->ram_used  = `availrmem - `freemem;
78235368Sgnn	this->freemem   = `freemem;
79235368Sgnn	this->kernel    = `physmem - `pages_locked - `availrmem;
80235368Sgnn
81235368Sgnn	/* Disk stats */
82235368Sgnn	this->disk_total = `k_anoninfo.ani_max;
83235368Sgnn	this->disk_resv = `k_anoninfo.ani_phys_resv;
84235368Sgnn	this->disk_avail = this->disk_total - this->disk_resv;
85235368Sgnn
86235368Sgnn	/* Total Swap stats */
87235368Sgnn	this->minfree = `swapfs_minfree;
88235368Sgnn	this->reserve = `swapfs_reserve;
89235368Sgnn	/* this is TOTAL_AVAILABLE_SWAP from /usr/include/vm/anon.h, */
90235368Sgnn	this->swap_total = `k_anoninfo.ani_max +
91235368Sgnn	    (`availrmem - `swapfs_minfree > 0 ?
92235368Sgnn	    `availrmem - `swapfs_minfree : 0);
93235368Sgnn	/* this is CURRENT_TOTAL_AVAILABLE_SWAP from /usr/include/vm/anon.h, */
94235368Sgnn	this->swap_avail = `k_anoninfo.ani_max - `k_anoninfo.ani_phys_resv +
95235368Sgnn	    (`availrmem - `swapfs_minfree > 0 ?
96235368Sgnn	    `availrmem - `swapfs_minfree : 0);
97235368Sgnn	this->swap_resv = this->swap_total - this->swap_avail;
98235368Sgnn
99235368Sgnn	/* Convert to Mbytes */
100235368Sgnn	this->ani_phys_resv *= `_pagesize;  this->ani_phys_resv /= 1048576;
101235368Sgnn	this->ani_mem_resv *= `_pagesize;  this->ani_mem_resv /= 1048576;
102235368Sgnn	this->ani_locked *= `_pagesize;  this->ani_locked /= 1048576;
103235368Sgnn	this->ani_max	*= `_pagesize;  this->ani_max	/= 1048576;
104235368Sgnn	this->availrmem	*= `_pagesize;  this->availrmem	/= 1048576;
105235368Sgnn	this->ram_total	*= `_pagesize;  this->ram_total	/= 1048576;
106235368Sgnn	this->unusable	*= `_pagesize;  this->unusable	/= 1048576;
107235368Sgnn	this->kernel	*= `_pagesize;  this->kernel	/= 1048576;
108235368Sgnn	this->locked	*= `_pagesize;  this->locked	/= 1048576;
109235368Sgnn	this->ram_used	*= `_pagesize;  this->ram_used	/= 1048576;
110235368Sgnn	this->freemem	*= `_pagesize;  this->freemem	/= 1048576;
111235368Sgnn	this->disk_total *= `_pagesize; this->disk_total /= 1048576;
112235368Sgnn	this->disk_resv	*= `_pagesize;  this->disk_resv	/= 1048576;
113235368Sgnn	this->disk_avail *= `_pagesize;  this->disk_avail /= 1048576;
114235368Sgnn	this->swap_total *= `_pagesize; this->swap_total /= 1048576;
115235368Sgnn	this->swap_avail *= `_pagesize;  this->swap_avail /= 1048576;
116235368Sgnn	this->swap_resv	*= `_pagesize;  this->swap_resv	/= 1048576;
117235368Sgnn	this->minfree	*= `_pagesize;  this->minfree	/= 1048576;
118235368Sgnn	this->reserve	*= `_pagesize;  this->reserve	/= 1048576;
119235368Sgnn
120235368Sgnn	/* Print debug */
121235368Sgnn	DEBUG ? printf("DEBUG   availrmem %5d MB\n", this->availrmem) : 1;
122235368Sgnn	DEBUG ? printf("DEBUG     freemem %5d MB\n", this->freemem) : 1;
123235368Sgnn	DEBUG ? printf("DEBUG     ani_max %5d MB\n", this->ani_max) : 1;
124235368Sgnn	DEBUG ? printf("DEBUG ani_phys_re %5d MB\n", this->ani_phys_resv) : 1;
125235368Sgnn	DEBUG ? printf("DEBUG  ani_mem_re %5d MB\n", this->ani_mem_resv) : 1;
126235368Sgnn	DEBUG ? printf("DEBUG  ani_locked %5d MB\n", this->ani_locked) : 1;
127235368Sgnn	DEBUG ? printf("DEBUG     reserve %5d MB\n", this->reserve) : 1;
128235368Sgnn	DEBUG ? printf("\n") : 1;
129235368Sgnn
130235368Sgnn	/* Print report */
131235368Sgnn	printf("RAM  _______Total %5d MB\n", this->ram_total);
132235368Sgnn	printf("RAM      Unusable %5d MB\n", this->unusable);
133235368Sgnn	printf("RAM        Kernel %5d MB\n", this->kernel);
134235368Sgnn	printf("RAM        Locked %5d MB\n", this->locked);
135235368Sgnn	printf("RAM          Used %5d MB\n", this->ram_used);
136235368Sgnn	printf("RAM          Free %5d MB\n", this->freemem);
137235368Sgnn	printf("\n");
138235368Sgnn	printf("Disk _______Total %5d MB\n", this->disk_total);
139235368Sgnn	printf("Disk         Resv %5d MB\n", this->disk_resv);
140235368Sgnn	printf("Disk        Avail %5d MB\n", this->disk_avail);
141235368Sgnn	printf("\n");
142235368Sgnn	printf("Swap _______Total %5d MB\n", this->swap_total);
143235368Sgnn	printf("Swap         Resv %5d MB\n", this->swap_resv);
144235368Sgnn	printf("Swap        Avail %5d MB\n", this->swap_avail);
145235368Sgnn	printf("Swap    (Minfree) %5d MB\n", this->minfree);
146235368Sgnn
147235368Sgnn	DEBUG ? printf("\nNow run other commands for confirmation.\n") : 1;
148235368Sgnn	! DEBUG ? exit(0) : 1;
149235368Sgnn}
150