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