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