xref: /freebsd/usr.bin/systat/swap.c (revision 0640d357f29fb1c0daaaffadd0416c5981413afd)
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)swap.c	8.3 (Berkeley) 4/29/95";
37 #endif
38 static const char rcsid[] =
39 	"$Id: swap.c,v 1.6 1998/06/09 04:17:27 imp Exp $";
40 #endif /* not lint */
41 
42 /*
43  * swapinfo - based on a program of the same name by Kevin Lahey
44  */
45 
46 #include <sys/param.h>
47 #include <sys/buf.h>
48 #include <sys/conf.h>
49 #include <sys/ioctl.h>
50 #include <sys/stat.h>
51 #include <sys/rlist.h>
52 
53 #include <kvm.h>
54 #include <nlist.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <string.h>
59 #include <err.h>
60 
61 #include "systat.h"
62 #include "extern.h"
63 
64 extern char *getbsize __P((int *headerlenp, long *blocksizep));
65 void showspace __P((char *header, int hlen, long blocksize));
66 
67 kvm_t	*kd;
68 
69 struct nlist syms[] = {
70 	{ "_swaplist" },/* list of free swap areas */
71 #define VM_SWAPLIST	0
72 	{ "_swdevt" },	/* list of swap devices and sizes */
73 #define VM_SWDEVT	1
74 	{ "_nswap" },	/* size of largest swap device */
75 #define VM_NSWAP	2
76 	{ "_nswdev" },	/* number of swap devices */
77 #define VM_NSWDEV	3
78 	{ "_dmmax" },	/* maximum size of a swap block */
79 #define VM_DMMAX	4
80 	{ 0 }
81 };
82 
83 static int nswap, nswdev, dmmax;
84 static struct swdevt *sw;
85 static long *perdev, blocksize;
86 static int nfree, hlen;
87 static struct rlisthdr swaplist;
88 
89 #define	SVAR(var) __STRING(var)	/* to force expansion */
90 #define	KGET(idx, var) \
91 	KGET1(idx, &var, sizeof(var), SVAR(var), (0))
92 #define	KGET1(idx, p, s, msg, rv) \
93 	KGET2(syms[idx].n_value, p, s, msg, rv)
94 #define	KGET2(addr, p, s, msg, rv) \
95 	if (kvm_read(kd, addr, p, s) != s) { \
96 		error("cannot read %s: %s", msg, kvm_geterr(kd)); \
97 		return rv; \
98 	}
99 
100 WINDOW *
101 openswap()
102 {
103 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
104 }
105 
106 void
107 closeswap(w)
108 	WINDOW *w;
109 {
110 	if (w == NULL)
111 		return;
112 	wclear(w);
113 	wrefresh(w);
114 	delwin(w);
115 }
116 
117 /*
118  * The meat of all the swap stuff is stolen from pstat(8)'s
119  * swapmode(), which is based on a program called swapinfo written by
120  * Kevin Lahey <kml@rokkaku.atl.ga.us>.
121  */
122 
123 int
124 initswap()
125 {
126 	int i;
127 	char msgbuf[BUFSIZ];
128 	char *cp;
129 	static int once = 0;
130 	u_long ptr;
131 
132 	if (once)
133 		return (1);
134 	if (kvm_nlist(kd, syms)) {
135 		snprintf(msgbuf, sizeof(msgbuf), "systat: swap: cannot find");
136 		cp = msgbuf + strlen(msgbuf) + 1;
137 		for (i = 0;
138 		    syms[i].n_name != NULL && cp - msgbuf < sizeof(msgbuf);
139 		    i++) {
140 			if (syms[i].n_value == 0) {
141 				snprintf(cp, sizeof(msgbuf) - (cp - msgbuf),
142 				    " %s", syms[i].n_name);
143 				cp += strlen(cp) + 1;
144 			}
145 		}
146 		error(msgbuf);
147 		return (0);
148 	}
149 	KGET(VM_NSWAP, nswap);
150 	KGET(VM_NSWDEV, nswdev);
151 	KGET(VM_DMMAX, dmmax);
152 	if ((sw = malloc(nswdev * sizeof(*sw))) == NULL ||
153 	    (perdev = malloc(nswdev * sizeof(*perdev))) == NULL)
154 		err(1, "malloc");
155 	KGET1(VM_SWDEVT, &ptr, sizeof ptr, "swdevt", (0));
156 	KGET2(ptr, sw, nswdev * sizeof(*sw), "*swdevt", (0));
157 	once = 1;
158 	return (1);
159 }
160 
161 void
162 fetchswap()
163 {
164 	struct rlist head;
165 	struct rlist *swapptr;
166 
167 	/* Count up swap space. */
168 	nfree = 0;
169 	memset(perdev, 0, nswdev * sizeof(*perdev));
170 	KGET1(VM_SWAPLIST, &swaplist, sizeof swaplist, "swaplist", /* none */);
171 	swapptr = swaplist.rlh_list;
172 	while (swapptr) {
173 		int	top, bottom, next_block;
174 
175 		KGET2((unsigned long)swapptr, &head,
176 		      sizeof(struct rlist), "swapptr", /* none */);
177 
178 		top = head.rl_end;
179 		bottom = head.rl_start;
180 
181 		nfree += top - bottom + 1;
182 
183 		/*
184 		 * Swap space is split up among the configured disks.
185 		 *
186 		 * For interleaved swap devices, the first dmmax blocks
187 		 * of swap space some from the first disk, the next dmmax
188 		 * blocks from the next, and so on up to nswap blocks.
189 		 *
190 		 * The list of free space joins adjacent free blocks,
191 		 * ignoring device boundries.  If we want to keep track
192 		 * of this information per device, we'll just have to
193 		 * extract it ourselves.
194 		 */
195 		while (top / dmmax != bottom / dmmax) {
196 			next_block = ((bottom + dmmax) / dmmax);
197 			perdev[(bottom / dmmax) % nswdev] +=
198 				next_block * dmmax - bottom;
199 			bottom = next_block * dmmax;
200 		}
201 		perdev[(bottom / dmmax) % nswdev] +=
202 			top - bottom + 1;
203 
204 		swapptr = head.rl_next;
205 	}
206 
207 }
208 
209 void
210 labelswap()
211 {
212 	char *header, *p;
213 	int row, i;
214 
215 	row = 0;
216 	wmove(wnd, row, 0); wclrtobot(wnd);
217 	header = getbsize(&hlen, &blocksize);
218 	mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
219 	    "Disk", hlen, header, "Used",
220 	    "/0%  /10% /20% /30% /40% /50% /60% /70% /80% /90% /100");
221 	for (i = 0; i < nswdev; i++) {
222 		if (!sw[i].sw_freed)
223 			continue;
224 		p = devname(sw[i].sw_dev, S_IFBLK);
225 		mvwprintw(wnd, i + 1, 0, "%-5s",
226 			  sw[i].sw_dev == NODEV ? "[NFS]" :
227 			  p == NULL ? "??" : p);
228 	}
229 }
230 
231 void
232 showswap()
233 {
234 	int col, div, i, j, k, avail, npfree, used, xsize, xfree;
235 
236 	div = blocksize / 512;
237 	avail = npfree = 0;
238 	for (i = k = 0; i < nswdev; i++) {
239 		/*
240 		 * Don't report statistics for partitions which have not
241 		 * yet been activated via swapon(8).
242 		 */
243 		if (!sw[i].sw_freed)
244 			continue;
245 
246 		col = 5;
247 		mvwprintw(wnd, i + 1, col, "%*d", hlen, sw[i].sw_nblks / div);
248 		col += hlen;
249 
250 		/*
251 		 * The first dmmax is never allocated to avoid trashing of
252 		 * disklabels
253 		 */
254 		xsize = sw[i].sw_nblks - dmmax;
255 		xfree = perdev[i];
256 		used = xsize - xfree;
257 		mvwprintw(wnd, i + 1, col, "%9d  ", used / div);
258 		for (j = (100 * used / xsize + 1) / 2; j > 0; j--)
259 			waddch(wnd, 'X');
260 		npfree++;
261 		avail += xsize;
262 		k++;
263 	}
264 	/*
265 	 * If only one partition has been set up via swapon(8), we don't
266 	 * need to bother with totals.
267 	 */
268 	if (npfree > 1) {
269 		used = avail - nfree;
270 		mvwprintw(wnd, k + 1, 0, "%-5s%*d%9d  ",
271 		    "Total", hlen, avail / div, used / div);
272 		for (j = (100 * used / avail + 1) / 2; j > 0; j--)
273 			waddch(wnd, 'X');
274 	}
275 }
276