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