swap.c revision 1.15 1 1.15 dsl /* $NetBSD: swap.c,v 1.15 2003/02/24 10:10:00 dsl 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.15 dsl __RCSID("$NetBSD: swap.c,v 1.15 2003/02/24 10:10:00 dsl Exp $");
43 1.1 jtc #endif /* not lint */
44 1.1 jtc
45 1.1 jtc #include <sys/param.h>
46 1.11 mrg #include <sys/swap.h>
47 1.8 mrg
48 1.14 simonb #include <errno.h>
49 1.1 jtc #include <stdio.h>
50 1.1 jtc #include <stdlib.h>
51 1.3 cgd #include <string.h>
52 1.1 jtc #include <unistd.h>
53 1.1 jtc
54 1.1 jtc #include "systat.h"
55 1.1 jtc #include "extern.h"
56 1.1 jtc
57 1.13 ad void showspace(char *header, int hlen, long blocksize);
58 1.1 jtc
59 1.8 mrg static long blocksize;
60 1.8 mrg static int hlen, nswap, rnswap;
61 1.8 mrg static int first = 1;
62 1.8 mrg static struct swapent *swap_devices;
63 1.1 jtc
64 1.1 jtc WINDOW *
65 1.13 ad openswap(void)
66 1.1 jtc {
67 1.8 mrg
68 1.15 dsl return (subwin(stdscr, -1, 0, 5, 0));
69 1.1 jtc }
70 1.1 jtc
71 1.1 jtc void
72 1.13 ad closeswap(WINDOW *w)
73 1.1 jtc {
74 1.8 mrg
75 1.1 jtc if (w == NULL)
76 1.1 jtc return;
77 1.1 jtc wclear(w);
78 1.1 jtc wrefresh(w);
79 1.1 jtc delwin(w);
80 1.1 jtc }
81 1.1 jtc
82 1.8 mrg /* do nothing */
83 1.8 mrg int
84 1.13 ad initswap(void)
85 1.1 jtc {
86 1.8 mrg
87 1.1 jtc return (1);
88 1.1 jtc }
89 1.1 jtc
90 1.1 jtc void
91 1.13 ad fetchswap(void)
92 1.1 jtc {
93 1.8 mrg int update_label = 0;
94 1.8 mrg
95 1.8 mrg first = 0;
96 1.8 mrg nswap = swapctl(SWAP_NSWAP, 0, 0);
97 1.8 mrg if (nswap < 0)
98 1.8 mrg error("error: %s", strerror(errno));
99 1.8 mrg if (nswap == 0)
100 1.8 mrg return;
101 1.8 mrg update_label = (nswap != rnswap);
102 1.1 jtc
103 1.8 mrg if (swap_devices)
104 1.8 mrg (void)free(swap_devices);
105 1.10 jwise if ((swap_devices = malloc(nswap * sizeof(*swap_devices))) == NULL) {
106 1.10 jwise error("malloc failed");
107 1.10 jwise die(0);
108 1.10 jwise }
109 1.10 jwise
110 1.10 jwise if ((rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap)) != nswap) {
111 1.10 jwise error("swapctl failed");
112 1.10 jwise die(0);
113 1.10 jwise }
114 1.10 jwise
115 1.8 mrg if (update_label)
116 1.8 mrg labelswap();
117 1.1 jtc }
118 1.1 jtc
119 1.1 jtc void
120 1.13 ad labelswap(void)
121 1.1 jtc {
122 1.9 marc char *header;
123 1.9 marc int row;
124 1.1 jtc
125 1.1 jtc row = 0;
126 1.8 mrg wmove(wnd, row, 0);
127 1.8 mrg wclrtobot(wnd);
128 1.8 mrg if (first)
129 1.8 mrg fetchswap();
130 1.8 mrg if (nswap == 0) {
131 1.8 mrg mvwprintw(wnd, row++, 0, "No swap");
132 1.8 mrg return;
133 1.8 mrg }
134 1.1 jtc header = getbsize(&hlen, &blocksize);
135 1.1 jtc mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
136 1.1 jtc "Disk", hlen, header, "Used",
137 1.1 jtc "/0% /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
138 1.1 jtc }
139 1.1 jtc
140 1.1 jtc void
141 1.13 ad showswap(void)
142 1.13 ad {
143 1.12 mycroft int col, div, i, avail, used, xsize, free;
144 1.8 mrg struct swapent *sep;
145 1.9 marc char *p;
146 1.1 jtc
147 1.1 jtc div = blocksize / 512;
148 1.8 mrg free = avail = 0;
149 1.8 mrg for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
150 1.8 mrg if (sep == NULL)
151 1.8 mrg continue;
152 1.9 marc
153 1.9 marc p = strrchr(sep->se_path, '/');
154 1.9 marc p = p ? p+1 : sep->se_path;
155 1.9 marc
156 1.9 marc mvwprintw(wnd, i + 1, 0, "%-5s", p);
157 1.9 marc
158 1.1 jtc col = 5;
159 1.8 mrg mvwprintw(wnd, i + 1, col, "%*d", hlen, sep->se_nblks / div);
160 1.8 mrg
161 1.1 jtc col += hlen;
162 1.8 mrg xsize = sep->se_nblks;
163 1.8 mrg used = sep->se_inuse;
164 1.8 mrg avail += xsize;
165 1.8 mrg free += xsize - used;
166 1.1 jtc mvwprintw(wnd, i + 1, col, "%9d ", used / div);
167 1.7 mikel wclrtoeol(wnd);
168 1.12 mycroft whline(wnd, 'X', (100 * used / xsize + 1) / 2);
169 1.1 jtc }
170 1.8 mrg /* do total if necessary */
171 1.8 mrg if (nswap > 1) {
172 1.8 mrg used = avail - free;
173 1.1 jtc mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d ",
174 1.1 jtc "Total", hlen, avail / div, used / div);
175 1.7 mikel wclrtoeol(wnd);
176 1.12 mycroft whline(wnd, 'X', (100 * used / avail + 1) / 2);
177 1.1 jtc }
178 1.1 jtc }
179