xref: /netbsd-src/usr.bin/systat/swap.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: swap.c,v 1.20 2008/05/30 02:29:37 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Matthew R. Green.
5  * All rights reserved.
6  *
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.
15  *
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.
27  */
28 
29 /*-
30  * Copyright (c) 1980, 1992, 1993
31  *	The Regents of the University of California.  All rights reserved.
32  *
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.
44  *
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.
56  */
57 
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.20 2008/05/30 02:29:37 mrg Exp $");
64 #endif /* not lint */
65 
66 #include <sys/param.h>
67 #include <sys/swap.h>
68 
69 #include <errno.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <unistd.h>
74 
75 #include "systat.h"
76 #include "extern.h"
77 
78 void showspace(char *header, int hlen, long blocksize);
79 
80 static	long blocksize;
81 static	int hlen, nswap, rnswap;
82 static	int first = 1;
83 static	struct swapent *swap_devices;
84 
85 WINDOW *
86 openswap(void)
87 {
88 
89 	return (subwin(stdscr, -1, 0, 5, 0));
90 }
91 
92 void
93 closeswap(WINDOW *w)
94 {
95 
96 	if (w == NULL)
97 		return;
98 	wclear(w);
99 	wrefresh(w);
100 	delwin(w);
101 }
102 
103 /* do nothing */
104 int
105 initswap(void)
106 {
107 
108 	return (1);
109 }
110 
111 void
112 fetchswap(void)
113 {
114 	int	update_label = 0;
115 
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);
123 
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);
129 	}
130 
131 	if ((rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap)) != nswap) {
132 		error("swapctl failed");
133 		die(0);
134 	}
135 
136 	if (update_label)
137 		labelswap();
138 }
139 
140 void
141 labelswap(void)
142 {
143 	char	*header;
144 	int	row;
145 
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;
154 	}
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%");
159 }
160 
161 void
162 showswap(void)
163 {
164 	int	col, blk_div, i, avail, used, xsize, swp_free;
165 	struct	swapent *sep;
166 	char	*p;
167 
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;
173 
174 		mvwprintw(wnd, i + 1, 0, "%-5s", p);
175 
176 		col = 5;
177 		mvwprintw(wnd, i + 1, col, "%*d", hlen, sep->se_nblks / blk_div);
178 
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);
187 	}
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);
195 	}
196 }
197