xref: /netbsd-src/usr.bin/systat/swap.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: swap.c,v 1.17 2003/10/13 14:22:20 agc 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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)swap.c	8.3 (Berkeley) 4/29/95";
37 #endif
38 __RCSID("$NetBSD: swap.c,v 1.17 2003/10/13 14:22:20 agc Exp $");
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/swap.h>
43 
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include "systat.h"
51 #include "extern.h"
52 
53 void showspace(char *header, int hlen, long blocksize);
54 
55 static	long blocksize;
56 static	int hlen, nswap, rnswap;
57 static	int first = 1;
58 static	struct swapent *swap_devices;
59 
60 WINDOW *
61 openswap(void)
62 {
63 
64 	return (subwin(stdscr, -1, 0, 5, 0));
65 }
66 
67 void
68 closeswap(WINDOW *w)
69 {
70 
71 	if (w == NULL)
72 		return;
73 	wclear(w);
74 	wrefresh(w);
75 	delwin(w);
76 }
77 
78 /* do nothing */
79 int
80 initswap(void)
81 {
82 
83 	return (1);
84 }
85 
86 void
87 fetchswap(void)
88 {
89 	int	update_label = 0;
90 
91 	first = 0;
92 	nswap = swapctl(SWAP_NSWAP, 0, 0);
93 	if (nswap < 0)
94 		error("error: %s", strerror(errno));
95 	if (nswap == 0)
96 		return;
97 	update_label = (nswap != rnswap);
98 
99 	if (swap_devices)
100 		(void)free(swap_devices);
101 	if ((swap_devices = malloc(nswap * sizeof(*swap_devices))) == NULL) {
102 		error("malloc failed");
103 		die(0);
104 	}
105 
106 	if ((rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap)) != nswap) {
107 		error("swapctl failed");
108 		die(0);
109 	}
110 
111 	if (update_label)
112 		labelswap();
113 }
114 
115 void
116 labelswap(void)
117 {
118 	char	*header;
119 	int	row;
120 
121 	row = 0;
122 	wmove(wnd, row, 0);
123 	wclrtobot(wnd);
124 	if (first)
125 		fetchswap();
126 	if (nswap == 0) {
127 		mvwprintw(wnd, row++, 0, "No swap");
128 		return;
129 	}
130 	header = getbsize(&hlen, &blocksize);
131 	mvwprintw(wnd, row++, 0, "%-5s%*s%9s  %55s",
132 	    "Disk", hlen, header, "Used",
133 	    "/0%  /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
134 }
135 
136 void
137 showswap(void)
138 {
139 	int	col, div, i, avail, used, xsize, free;
140 	struct	swapent *sep;
141 	char	*p;
142 
143 	div = blocksize / 512;
144 	free = avail = 0;
145 	for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
146 		if (sep == NULL)
147 			continue;
148 
149 		p = strrchr(sep->se_path, '/');
150 		p = p ? p+1 : sep->se_path;
151 
152 		mvwprintw(wnd, i + 1, 0, "%-5s", p);
153 
154 		col = 5;
155 		mvwprintw(wnd, i + 1, col, "%*d", hlen, sep->se_nblks / div);
156 
157 		col += hlen;
158 		xsize = sep->se_nblks;
159 		used = sep->se_inuse;
160 		avail += xsize;
161 		free += xsize - used;
162 		mvwprintw(wnd, i + 1, col, "%9d  ", used / div);
163 		wclrtoeol(wnd);
164 		whline(wnd, 'X', (100 * used / xsize + 1) / 2);
165 	}
166 	/* do total if necessary */
167 	if (nswap > 1) {
168 		used = avail - free;
169 		mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d  ",
170 		    "Total", hlen, avail / div, used / div);
171 		wclrtoeol(wnd);
172 		whline(wnd, 'X', (100 * used / avail + 1) / 2);
173 	}
174 }
175