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