xref: /openbsd-src/usr.bin/systat/swap.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: swap.c,v 1.22 2008/12/07 02:56:06 canacar 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. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/buf.h>
37 #include <sys/conf.h>
38 #include <sys/ioctl.h>
39 #include <sys/stat.h>
40 #include <sys/swap.h>
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <errno.h>
46 #include <unistd.h>
47 
48 #include "systat.h"
49 
50 
51 static	long blocksize;
52 static	int hlen, nswap, rnswap;
53 static	struct swapent *swap_devices;
54 
55 void print_sw(void);
56 int read_sw(void);
57 int select_sw(void);
58 static void showswap(int i);
59 static void showtotal(void);
60 
61 
62 field_def fields_sw[] = {
63 	{"DISK", 6, 16, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
64 	{"BLOCKS", 5, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
65 	{"USED", 5, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
66 	{"", 40, 80, 1, FLD_ALIGN_BAR, -1, 0, 0, 100},
67 };
68 
69 #define FIELD_ADDR(x) (&fields_sw[x])
70 
71 #define FLD_SW_NAME	FIELD_ADDR(0)
72 #define FLD_SW_BLOCKS	FIELD_ADDR(1)
73 #define FLD_SW_USED	FIELD_ADDR(2)
74 #define FLD_SW_BAR	FIELD_ADDR(3)
75 
76 /* Define views */
77 field_def *view_sw_0[] = {
78 	FLD_SW_NAME, FLD_SW_BLOCKS, FLD_SW_USED, FLD_SW_BAR, NULL
79 };
80 
81 
82 /* Define view managers */
83 struct view_manager swap_mgr = {
84 	"Swap", select_sw, read_sw, NULL, print_header,
85 	print_sw, keyboard_callback, NULL, NULL
86 };
87 
88 field_view views_sw[] = {
89 	{view_sw_0, "swap", '6', &swap_mgr},
90 	{NULL, NULL, 0, NULL}
91 };
92 
93 
94 int
95 select_sw(void)
96 {
97 	if (swap_devices == NULL || nswap == 0)
98 		num_disp = 1;
99 	else
100 		num_disp = nswap;
101 	if (nswap > 1)
102 		num_disp++;
103 	return (0);
104 }
105 
106 int
107 read_sw(void)
108 {
109 	num_disp = 1;
110 
111 	nswap = swapctl(SWAP_NSWAP, 0, 0);
112 
113 	if (nswap < 0)
114 		error("error: %s", strerror(errno));
115 	if (nswap == 0)
116 		return 0;
117 
118 	if (swap_devices)
119 		(void)free(swap_devices);
120 
121 	swap_devices = (struct swapent *)calloc(nswap, sizeof(*swap_devices));
122 	if (swap_devices == NULL)
123 		return 0;
124 
125 	rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap);
126 	if (rnswap < 0 || nswap != rnswap)
127 		return 0;
128 
129 	num_disp = nswap;
130 	if (nswap > 1)
131 		num_disp++;
132 
133 	return 0;
134 }
135 
136 
137 void
138 print_sw(void)
139 {
140 	int n, count = 0;
141 
142 	if (swap_devices == NULL || nswap == 0) {
143 		print_fld_str(FLD_SW_BAR, "No swap devices");
144 		return;
145 	}
146 
147 
148 	for (n = dispstart; n < num_disp; n++) {
149 		if (n >= nswap)
150 			showtotal();
151 		else
152 			showswap(n);
153 		count++;
154 		if (maxprint > 0 && count >= maxprint)
155 			break;
156 	}
157 
158 }
159 
160 int
161 initswap(void)
162 {
163 	field_view *v;
164 
165 	char *bs = getbsize(&hlen, &blocksize);
166 
167 	FLD_SW_BLOCKS->title = strdup(bs);
168 
169 	for (v = views_sw; v->name != NULL; v++)
170 		add_view(v);
171 
172 	return(1);
173 }
174 
175 
176 static void
177 showswap(int i)
178 {
179 	int d, used, xsize;
180 	struct	swapent *sep;
181 	char	*p;
182 
183 	d = blocksize / 512;
184 
185 	sep = &swap_devices[i];
186 
187 	p = strrchr(sep->se_path, '/');
188 	p = p ? p+1 : sep->se_path;
189 
190 	print_fld_str(FLD_SW_NAME, p);
191 
192 	xsize = sep->se_nblks;
193 	used = sep->se_inuse;
194 
195 	print_fld_uint(FLD_SW_BLOCKS, xsize / d);
196 	print_fld_uint(FLD_SW_USED, used / d);
197 	print_fld_bar(FLD_SW_BAR, 100 * used / xsize);
198 
199 	end_line();
200 }
201 
202 static void
203 showtotal(void)
204 {
205 	struct	swapent *sep;
206 	int	d, i, avail, used, xsize, mfree;
207 
208 	d = blocksize / 512;
209 	mfree = avail = 0;
210 
211 	for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
212 		xsize = sep->se_nblks;
213 		used = sep->se_inuse;
214 		avail += xsize;
215 		mfree += xsize - used;
216 	}
217 	used = avail - mfree;
218 
219 	print_fld_str(FLD_SW_NAME, "Total");
220 	print_fld_uint(FLD_SW_BLOCKS, avail / d);
221 	print_fld_uint(FLD_SW_USED, used / d);
222 	print_fld_bar(FLD_SW_BAR, 100 * used / avail);
223 
224 	end_line();
225 }
226