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