swap.c revision 1.8 1 1.8 mrg /* $NetBSD: swap.c,v 1.8 1997/07/21 07:03:15 mrg Exp $ */
2 1.2 jtc
3 1.1 jtc /*-
4 1.8 mrg * Copyright (c) 1997 Matthew R. Green. All rights reserved.
5 1.1 jtc * Copyright (c) 1980, 1992, 1993
6 1.1 jtc * The Regents of the University of California. All rights reserved.
7 1.1 jtc *
8 1.1 jtc * Redistribution and use in source and binary forms, with or without
9 1.1 jtc * modification, are permitted provided that the following conditions
10 1.1 jtc * are met:
11 1.1 jtc * 1. Redistributions of source code must retain the above copyright
12 1.1 jtc * notice, this list of conditions and the following disclaimer.
13 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 jtc * notice, this list of conditions and the following disclaimer in the
15 1.1 jtc * documentation and/or other materials provided with the distribution.
16 1.1 jtc * 3. All advertising materials mentioning features or use of this software
17 1.1 jtc * must display the following acknowledgement:
18 1.1 jtc * This product includes software developed by the University of
19 1.1 jtc * California, Berkeley and its contributors.
20 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
21 1.1 jtc * may be used to endorse or promote products derived from this software
22 1.1 jtc * without specific prior written permission.
23 1.1 jtc *
24 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 jtc * SUCH DAMAGE.
35 1.1 jtc */
36 1.1 jtc
37 1.8 mrg #include <sys/cdefs.h>
38 1.1 jtc #ifndef lint
39 1.2 jtc #if 0
40 1.4 jtc static char sccsid[] = "@(#)swap.c 8.3 (Berkeley) 4/29/95";
41 1.2 jtc #endif
42 1.8 mrg __RCSID("$NetBSD: swap.c,v 1.8 1997/07/21 07:03:15 mrg Exp $");
43 1.1 jtc #endif /* not lint */
44 1.1 jtc
45 1.1 jtc #include <sys/param.h>
46 1.1 jtc #include <sys/buf.h>
47 1.1 jtc #include <sys/conf.h>
48 1.1 jtc #include <sys/ioctl.h>
49 1.1 jtc #include <sys/stat.h>
50 1.1 jtc
51 1.8 mrg #include <vm/vm_swap.h>
52 1.8 mrg
53 1.1 jtc #include <stdio.h>
54 1.1 jtc #include <stdlib.h>
55 1.3 cgd #include <string.h>
56 1.8 mrg #include <errno.h>
57 1.1 jtc #include <unistd.h>
58 1.1 jtc
59 1.1 jtc #include "systat.h"
60 1.1 jtc #include "extern.h"
61 1.1 jtc
62 1.1 jtc void showspace __P((char *header, int hlen, long blocksize));
63 1.1 jtc
64 1.8 mrg static long blocksize;
65 1.8 mrg static int hlen, nswap, rnswap;
66 1.8 mrg static int first = 1;
67 1.8 mrg static struct swapent *swap_devices;
68 1.1 jtc
69 1.1 jtc WINDOW *
70 1.1 jtc openswap()
71 1.1 jtc {
72 1.8 mrg
73 1.1 jtc return (subwin(stdscr, LINES-5-1, 0, 5, 0));
74 1.1 jtc }
75 1.1 jtc
76 1.1 jtc void
77 1.1 jtc closeswap(w)
78 1.1 jtc WINDOW *w;
79 1.1 jtc {
80 1.8 mrg
81 1.1 jtc if (w == NULL)
82 1.1 jtc return;
83 1.1 jtc wclear(w);
84 1.1 jtc wrefresh(w);
85 1.1 jtc delwin(w);
86 1.1 jtc }
87 1.1 jtc
88 1.8 mrg /* do nothing */
89 1.8 mrg int
90 1.1 jtc initswap()
91 1.1 jtc {
92 1.8 mrg
93 1.1 jtc return (1);
94 1.1 jtc }
95 1.1 jtc
96 1.1 jtc void
97 1.1 jtc fetchswap()
98 1.1 jtc {
99 1.8 mrg int update_label = 0;
100 1.8 mrg
101 1.8 mrg first = 0;
102 1.8 mrg nswap = swapctl(SWAP_NSWAP, 0, 0);
103 1.8 mrg if (nswap < 0)
104 1.8 mrg error("error: %s", strerror(errno));
105 1.8 mrg if (nswap == 0)
106 1.8 mrg return;
107 1.8 mrg update_label = (nswap != rnswap);
108 1.1 jtc
109 1.8 mrg if (swap_devices)
110 1.8 mrg (void)free(swap_devices);
111 1.8 mrg swap_devices = (struct swapent *)malloc(nswap * sizeof(*swap_devices));
112 1.8 mrg if (swap_devices == NULL)
113 1.8 mrg /* XXX */ ; /* XXX systat doesn't do errors! */
114 1.8 mrg
115 1.8 mrg rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap);
116 1.8 mrg if (nswap < 0)
117 1.8 mrg /* XXX */ ; /* XXX systat doesn't do errors! */
118 1.8 mrg if (nswap != rnswap)
119 1.8 mrg /* XXX */ ; /* XXX systat doesn't do errors! */
120 1.8 mrg if (update_label)
121 1.8 mrg labelswap();
122 1.1 jtc }
123 1.1 jtc
124 1.1 jtc void
125 1.1 jtc labelswap()
126 1.1 jtc {
127 1.8 mrg struct swapent *sep;
128 1.8 mrg char *header, *p;
129 1.8 mrg int row, i;
130 1.1 jtc
131 1.1 jtc row = 0;
132 1.8 mrg wmove(wnd, row, 0);
133 1.8 mrg wclrtobot(wnd);
134 1.8 mrg if (first)
135 1.8 mrg fetchswap();
136 1.8 mrg if (nswap == 0) {
137 1.8 mrg mvwprintw(wnd, row++, 0, "No swap");
138 1.8 mrg return;
139 1.8 mrg }
140 1.1 jtc header = getbsize(&hlen, &blocksize);
141 1.1 jtc mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
142 1.1 jtc "Disk", hlen, header, "Used",
143 1.1 jtc "/0% /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
144 1.8 mrg for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
145 1.8 mrg if (sep == NULL)
146 1.8 mrg continue;
147 1.8 mrg p = sep ? devname(sep->se_dev, S_IFBLK) : "swfl";
148 1.8 mrg mvwprintw(wnd, i + 1, 0, "%-5s", p ? p : "??");
149 1.1 jtc }
150 1.1 jtc }
151 1.1 jtc
152 1.1 jtc void
153 1.8 mrg showswap() {
154 1.8 mrg int col, div, i, j, avail, used, xsize, free;
155 1.8 mrg struct swapent *sep;
156 1.1 jtc
157 1.1 jtc div = blocksize / 512;
158 1.8 mrg free = avail = 0;
159 1.8 mrg for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
160 1.8 mrg if (sep == NULL)
161 1.8 mrg continue;
162 1.1 jtc col = 5;
163 1.8 mrg mvwprintw(wnd, i + 1, col, "%*d", hlen, sep->se_nblks / div);
164 1.8 mrg
165 1.1 jtc col += hlen;
166 1.8 mrg xsize = sep->se_nblks;
167 1.8 mrg used = sep->se_inuse;
168 1.8 mrg avail += xsize;
169 1.8 mrg free += xsize - used;
170 1.1 jtc mvwprintw(wnd, i + 1, col, "%9d ", used / div);
171 1.1 jtc for (j = (100 * used / xsize + 1) / 2; j > 0; j--)
172 1.1 jtc waddch(wnd, 'X');
173 1.7 mikel wclrtoeol(wnd);
174 1.1 jtc }
175 1.8 mrg /* do total if necessary */
176 1.8 mrg if (nswap > 1) {
177 1.8 mrg used = avail - free;
178 1.1 jtc mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d ",
179 1.1 jtc "Total", hlen, avail / div, used / div);
180 1.1 jtc for (j = (100 * used / avail + 1) / 2; j > 0; j--)
181 1.1 jtc waddch(wnd, 'X');
182 1.7 mikel wclrtoeol(wnd);
183 1.1 jtc }
184 1.1 jtc }
185