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