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