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