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