xref: /original-bsd/usr.bin/systat/swap.c (revision 0842ddeb)
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)swap.c	8.3 (Berkeley) 04/29/95";
10 #endif /* not lint */
11 
12 /*
13  * swapinfo - based on a program of the same name by Kevin Lahey
14  */
15 
16 #include <sys/param.h>
17 #include <sys/buf.h>
18 #include <sys/conf.h>
19 #include <sys/ioctl.h>
20 #include <sys/map.h>
21 #include <sys/stat.h>
22 
23 #include <kvm.h>
24 #include <nlist.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 
29 #include "systat.h"
30 #include "extern.h"
31 
32 extern char *getbsize __P((int *headerlenp, long *blocksizep));
33 void showspace __P((char *header, int hlen, long blocksize));
34 
35 kvm_t	*kd;
36 
37 struct nlist syms[] = {
38 	{ "_swapmap" },	/* list of free swap areas */
39 #define VM_SWAPMAP	0
40 	{ "_nswapmap" },/* size of the swap map */
41 #define VM_NSWAPMAP	1
42 	{ "_swdevt" },	/* list of swap devices and sizes */
43 #define VM_SWDEVT	2
44 	{ "_nswap" },	/* size of largest swap device */
45 #define VM_NSWAP	3
46 	{ "_nswdev" },	/* number of swap devices */
47 #define VM_NSWDEV	4
48 	{ "_dmmax" },	/* maximum size of a swap block */
49 #define VM_DMMAX	5
50 	0
51 };
52 
53 static int nswap, nswdev, dmmax, nswapmap;
54 static struct swdevt *sw;
55 static long *perdev, blocksize;
56 static struct map *swapmap, *kswapmap;
57 static struct mapent *mp;
58 static int nfree, hlen;
59 
60 #define	SVAR(var) __STRING(var)	/* to force expansion */
61 #define	KGET(idx, var) \
62 	KGET1(idx, &var, sizeof(var), SVAR(var))
63 #define	KGET1(idx, p, s, msg) \
64 	KGET2(syms[idx].n_value, p, s, msg)
65 #define	KGET2(addr, p, s, msg) \
66 	if (kvm_read(kd, addr, p, s) != s) { \
67 		error("cannot read %s: %s", msg, kvm_geterr(kd)); \
68 		return (0); \
69 	}
70 
71 WINDOW *
72 openswap()
73 {
74 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
75 }
76 
77 void
78 closeswap(w)
79 	WINDOW *w;
80 {
81 	if (w == NULL)
82 		return;
83 	wclear(w);
84 	wrefresh(w);
85 	delwin(w);
86 }
87 
88 initswap()
89 {
90 	int i;
91 	char msgbuf[BUFSIZ];
92 	static int once = 0;
93 
94 	if (once)
95 		return (1);
96 	if (kvm_nlist(kd, syms)) {
97 		strcpy(msgbuf, "systat: swap: cannot find");
98 		for (i = 0; syms[i].n_name != NULL; i++) {
99 			if (syms[i].n_value == 0) {
100 				strcat(msgbuf, " ");
101 				strcat(msgbuf, syms[i].n_name);
102 			}
103 		}
104 		error(msgbuf);
105 		return (0);
106 	}
107 	KGET(VM_NSWAP, nswap);
108 	KGET(VM_NSWDEV, nswdev);
109 	KGET(VM_DMMAX, dmmax);
110 	KGET(VM_NSWAPMAP, nswapmap);
111 	KGET(VM_SWAPMAP, kswapmap);	/* kernel `swapmap' is a pointer */
112 	if ((sw = malloc(nswdev * sizeof(*sw))) == NULL ||
113 	    (perdev = malloc(nswdev * sizeof(*perdev))) == NULL ||
114 	    (mp = malloc(nswapmap * sizeof(*mp))) == NULL) {
115 		error("swap malloc");
116 		return (0);
117 	}
118 	KGET1(VM_SWDEVT, sw, nswdev * sizeof(*sw), "swdevt");
119 	once = 1;
120 	return (1);
121 }
122 
123 void
124 fetchswap()
125 {
126 	int s, e, i;
127 
128 	s = nswapmap * sizeof(*mp);
129 	if (kvm_read(kd, (long)kswapmap, mp, s) != s)
130 		error("cannot read swapmap: %s", kvm_geterr(kd));
131 
132 	/* first entry in map is `struct map'; rest are mapent's */
133 	swapmap = (struct map *)mp;
134 	if (nswapmap != swapmap->m_limit - (struct mapent *)kswapmap)
135 		error("panic: swap: nswapmap goof");
136 
137 	/*
138 	 * Count up swap space.
139 	 */
140 	nfree = 0;
141 	bzero(perdev, nswdev * sizeof(*perdev));
142 	for (mp++; mp->m_addr != 0; mp++) {
143 		s = mp->m_addr;			/* start of swap region */
144 		e = mp->m_addr + mp->m_size;	/* end of region */
145 		nfree += mp->m_size;
146 
147 		/*
148 		 * Swap space is split up among the configured disks.
149 		 * The first dmmax blocks of swap space some from the
150 		 * first disk, the next dmmax blocks from the next,
151 		 * and so on.  The list of free space joins adjacent
152 		 * free blocks, ignoring device boundries.  If we want
153 		 * to keep track of this information per device, we'll
154 		 * just have to extract it ourselves.
155 		 */
156 
157 		/* calculate first device on which this falls */
158 		i = (s / dmmax) % nswdev;
159 		while (s < e) {		/* XXX this is inefficient */
160 			int bound = roundup(s + 1, dmmax);
161 
162 			if (bound > e)
163 				bound = e;
164 			perdev[i] += bound - s;
165 			if (++i >= nswdev)
166 				i = 0;
167 			s = bound;
168 		}
169 	}
170 }
171 
172 void
173 labelswap()
174 {
175 	char *header, *p;
176 	int row, i;
177 
178 	row = 0;
179 	wmove(wnd, row, 0); wclrtobot(wnd);
180 	header = getbsize(&hlen, &blocksize);
181 	mvwprintw(wnd, row++, 0, "%-5s%*s%9s  %55s",
182 	    "Disk", hlen, header, "Used",
183 	    "/0%  /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
184 	for (i = 0; i < nswdev; i++) {
185 		p = devname(sw[i].sw_dev, S_IFBLK);
186 		mvwprintw(wnd, i + 1, 0, "%-5s", p == NULL ? "??" : p);
187 	}
188 }
189 
190 void
191 showswap()
192 {
193 	int col, row, div, i, j, avail, npfree, used, xsize, xfree;
194 
195 	div = blocksize / 512;
196 	avail = npfree = 0;
197 	for (i = 0; i < nswdev; i++) {
198 		col = 5;
199 		mvwprintw(wnd, i + 1, col, "%*d", hlen, sw[i].sw_nblks / div);
200 		col += hlen;
201 		/*
202 		 * Don't report statistics for partitions which have not
203 		 * yet been activated via swapon(8).
204 		 */
205 		if (!sw[i].sw_freed) {
206 			mvwprintw(wnd, i + 1, col + 8,
207 			    "0  *** not available for swapping ***");
208 			continue;
209 		}
210 		xsize = sw[i].sw_nblks;
211 		xfree = perdev[i];
212 		used = xsize - xfree;
213 		mvwprintw(wnd, i + 1, col, "%9d  ", used / div);
214 		for (j = (100 * used / xsize + 1) / 2; j > 0; j--)
215 			waddch(wnd, 'X');
216 		npfree++;
217 		avail += xsize;
218 	}
219 	/*
220 	 * If only one partition has been set up via swapon(8), we don't
221 	 * need to bother with totals.
222 	 */
223 	if (npfree > 1) {
224 		used = avail - nfree;
225 		mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d  ",
226 		    "Total", hlen, avail / div, used / div);
227 		for (j = (100 * used / avail + 1) / 2; j > 0; j--)
228 			waddch(wnd, 'X');
229 	}
230 }
231