swap.c revision 36789
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
36static char sccsid[] = "@(#)swap.c	8.3 (Berkeley) 4/29/95";
37#endif
38static const char rcsid[] =
39	"$Id: swap.c,v 1.5 1997/07/06 04:37:22 bde 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
59#include "systat.h"
60#include "extern.h"
61
62extern char *getbsize __P((int *headerlenp, long *blocksizep));
63void showspace __P((char *header, int hlen, long blocksize));
64
65kvm_t	*kd;
66
67struct nlist syms[] = {
68	{ "_swaplist" },/* list of free swap areas */
69#define VM_SWAPLIST	0
70	{ "_swdevt" },	/* list of swap devices and sizes */
71#define VM_SWDEVT	1
72	{ "_nswap" },	/* size of largest swap device */
73#define VM_NSWAP	2
74	{ "_nswdev" },	/* number of swap devices */
75#define VM_NSWDEV	3
76	{ "_dmmax" },	/* maximum size of a swap block */
77#define VM_DMMAX	4
78	0
79};
80
81static int nswap, nswdev, dmmax;
82static struct swdevt *sw;
83static long *perdev, blocksize;
84static int nfree, hlen;
85static struct rlisthdr swaplist;
86
87#define	SVAR(var) __STRING(var)	/* to force expansion */
88#define	KGET(idx, var) \
89	KGET1(idx, &var, sizeof(var), SVAR(var), (0))
90#define	KGET1(idx, p, s, msg, rv) \
91	KGET2(syms[idx].n_value, p, s, msg, rv)
92#define	KGET2(addr, p, s, msg, rv) \
93	if (kvm_read(kd, addr, p, s) != s) { \
94		error("cannot read %s: %s", msg, kvm_geterr(kd)); \
95		return rv; \
96	}
97
98WINDOW *
99openswap()
100{
101	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
102}
103
104void
105closeswap(w)
106	WINDOW *w;
107{
108	if (w == NULL)
109		return;
110	wclear(w);
111	wrefresh(w);
112	delwin(w);
113}
114
115/*
116 * The meat of all the swap stuff is stolen from pstat(8)'s
117 * swapmode(), which is based on a program called swapinfo written by
118 * Kevin Lahey <kml@rokkaku.atl.ga.us>.
119 */
120
121initswap()
122{
123	int i;
124	char msgbuf[BUFSIZ];
125	char *cp;
126	static int once = 0;
127	u_long ptr;
128
129	if (once)
130		return (1);
131	if (kvm_nlist(kd, syms)) {
132		snprintf(msgbuf, sizeof(msgbuf), "systat: swap: cannot find");
133		cp = msgbuf + strlen(msgbuf) + 1;
134		for (i = 0;
135		    syms[i].n_name != NULL && cp - msgbuf < sizeof(msgbuf);
136		    i++) {
137			if (syms[i].n_value == 0) {
138				snprintf(cp, sizeof(msgbuf) - (cp - msgbuf),
139				    " %s", syms[i].n_name);
140				cp += strlen(cp) + 1;
141			}
142		}
143		error(msgbuf);
144		return (0);
145	}
146	KGET(VM_NSWAP, nswap);
147	KGET(VM_NSWDEV, nswdev);
148	KGET(VM_DMMAX, dmmax);
149	if ((sw = malloc(nswdev * sizeof(*sw))) == NULL ||
150	    (perdev = malloc(nswdev * sizeof(*perdev))) == NULL)
151		err(1, "malloc");
152	KGET1(VM_SWDEVT, &ptr, sizeof ptr, "swdevt", (0));
153	KGET2(ptr, sw, nswdev * sizeof(*sw), "*swdevt", (0));
154	once = 1;
155	return (1);
156}
157
158void
159fetchswap()
160{
161	struct rlist head;
162	struct rlist *swapptr;
163
164	/* Count up swap space. */
165	nfree = 0;
166	memset(perdev, 0, nswdev * sizeof(*perdev));
167	KGET1(VM_SWAPLIST, &swaplist, sizeof swaplist, "swaplist", /* none */);
168	swapptr = swaplist.rlh_list;
169	while (swapptr) {
170		int	top, bottom, next_block;
171
172		KGET2((unsigned long)swapptr, &head,
173		      sizeof(struct rlist), "swapptr", /* none */);
174
175		top = head.rl_end;
176		bottom = head.rl_start;
177
178		nfree += top - bottom + 1;
179
180		/*
181		 * Swap space is split up among the configured disks.
182		 *
183		 * For interleaved swap devices, the first dmmax blocks
184		 * of swap space some from the first disk, the next dmmax
185		 * blocks from the next, and so on up to nswap blocks.
186		 *
187		 * The list of free space joins adjacent free blocks,
188		 * ignoring device boundries.  If we want to keep track
189		 * of this information per device, we'll just have to
190		 * extract it ourselves.
191		 */
192		while (top / dmmax != bottom / dmmax) {
193			next_block = ((bottom + dmmax) / dmmax);
194			perdev[(bottom / dmmax) % nswdev] +=
195				next_block * dmmax - bottom;
196			bottom = next_block * dmmax;
197		}
198		perdev[(bottom / dmmax) % nswdev] +=
199			top - bottom + 1;
200
201		swapptr = head.rl_next;
202	}
203
204}
205
206void
207labelswap()
208{
209	char *header, *p;
210	int row, i;
211
212	row = 0;
213	wmove(wnd, row, 0); wclrtobot(wnd);
214	header = getbsize(&hlen, &blocksize);
215	mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
216	    "Disk", hlen, header, "Used",
217	    "/0%  /10% /20% /30% /40% /50% /60% /70% /80% /90% /100");
218	for (i = 0; i < nswdev; i++) {
219		if (!sw[i].sw_freed)
220			continue;
221		p = devname(sw[i].sw_dev, S_IFBLK);
222		mvwprintw(wnd, i + 1, 0, "%-5s",
223			  sw[i].sw_dev == NODEV ? "[NFS]" :
224			  p == NULL ? "??" : p);
225	}
226}
227
228void
229showswap()
230{
231	int col, row, div, i, j, k, avail, npfree, used, xsize, xfree;
232
233	div = blocksize / 512;
234	avail = npfree = 0;
235	for (i = k = 0; i < nswdev; i++) {
236		/*
237		 * Don't report statistics for partitions which have not
238		 * yet been activated via swapon(8).
239		 */
240		if (!sw[i].sw_freed)
241			continue;
242
243		col = 5;
244		mvwprintw(wnd, i + 1, col, "%*d", hlen, sw[i].sw_nblks / div);
245		col += hlen;
246
247		/*
248		 * The first dmmax is never allocated to avoid trashing of
249		 * disklabels
250		 */
251		xsize = sw[i].sw_nblks - dmmax;
252		xfree = perdev[i];
253		used = xsize - xfree;
254		mvwprintw(wnd, i + 1, col, "%9d  ", used / div);
255		for (j = (100 * used / xsize + 1) / 2; j > 0; j--)
256			waddch(wnd, 'X');
257		npfree++;
258		avail += xsize;
259		k++;
260	}
261	/*
262	 * If only one partition has been set up via swapon(8), we don't
263	 * need to bother with totals.
264	 */
265	if (npfree > 1) {
266		used = avail - nfree;
267		mvwprintw(wnd, k + 1, 0, "%-5s%*d%9d  ",
268		    "Total", hlen, avail / div, used / div);
269		for (j = (100 * used / avail + 1) / 2; j > 0; j--)
270			waddch(wnd, 'X');
271	}
272}
273