Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / systat / swap.c
blob2f908113c0fe87256e78d346d32b35bca6a1e9c4
1 /* $NetBSD: swap.c,v 1.19 2006/03/18 11:02:48 dsl Exp $ */
3 /*
4 * Copyright (c) 1997 Matthew R. Green.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 /*-
30 * Copyright (c) 1980, 1992, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
58 #include <sys/cdefs.h>
59 #ifndef lint
60 #if 0
61 static char sccsid[] = "@(#)swap.c 8.3 (Berkeley) 4/29/95";
62 #endif
63 __RCSID("$NetBSD: swap.c,v 1.19 2006/03/18 11:02:48 dsl Exp $");
64 #endif /* not lint */
66 #include <sys/param.h>
67 #include <sys/swap.h>
69 #include <errno.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <unistd.h>
75 #include "systat.h"
76 #include "extern.h"
78 void showspace(char *header, int hlen, long blocksize);
80 static long blocksize;
81 static int hlen, nswap, rnswap;
82 static int first = 1;
83 static struct swapent *swap_devices;
85 WINDOW *
86 openswap(void)
89 return (subwin(stdscr, -1, 0, 5, 0));
92 void
93 closeswap(WINDOW *w)
96 if (w == NULL)
97 return;
98 wclear(w);
99 wrefresh(w);
100 delwin(w);
103 /* do nothing */
105 initswap(void)
108 return (1);
111 void
112 fetchswap(void)
114 int update_label = 0;
116 first = 0;
117 nswap = swapctl(SWAP_NSWAP, 0, 0);
118 if (nswap < 0)
119 error("error: %s", strerror(errno));
120 if (nswap == 0)
121 return;
122 update_label = (nswap != rnswap);
124 if (swap_devices)
125 (void)free(swap_devices);
126 if ((swap_devices = malloc(nswap * sizeof(*swap_devices))) == NULL) {
127 error("malloc failed");
128 die(0);
131 if ((rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap)) != nswap) {
132 error("swapctl failed");
133 die(0);
136 if (update_label)
137 labelswap();
140 void
141 labelswap(void)
143 char *header;
144 int row;
146 row = 0;
147 wmove(wnd, row, 0);
148 wclrtobot(wnd);
149 if (first)
150 fetchswap();
151 if (nswap == 0) {
152 mvwprintw(wnd, row++, 0, "No swap");
153 return;
155 header = getbsize(&hlen, &blocksize);
156 mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
157 "Disk", hlen, header, "Used",
158 "/0% /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
161 void
162 showswap(void)
164 int col, blk_div, i, avail, used, xsize, swp_free;
165 struct swapent *sep;
166 char *p;
168 blk_div = blocksize / 512;
169 swp_free = avail = 0;
170 for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
171 p = strrchr(sep->se_path, '/');
172 p = p ? p+1 : sep->se_path;
174 mvwprintw(wnd, i + 1, 0, "%-5s", p);
176 col = 5;
177 mvwprintw(wnd, i + 1, col, "%*d", hlen, sep->se_nblks / blk_div);
179 col += hlen;
180 xsize = sep->se_nblks;
181 used = sep->se_inuse;
182 avail += xsize;
183 swp_free += xsize - used;
184 mvwprintw(wnd, i + 1, col, "%9d ", used / blk_div);
185 wclrtoeol(wnd);
186 whline(wnd, 'X', (100 * used / xsize + 1) / 2);
188 /* do total if necessary */
189 if (nswap > 1) {
190 used = avail - swp_free;
191 mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d ",
192 "Total", hlen, avail / blk_div, used / blk_div);
193 wclrtoeol(wnd);
194 whline(wnd, 'X', (100 * used / avail + 1) / 2);