xref: /freebsd/usr.bin/systat/swap.c (revision 13ea0450a9c8742119d36f3bf8f47accdce46e54)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 
34 __FBSDID("$FreeBSD$");
35 
36 #ifdef lint
37 static const char sccsid[] = "@(#)swap.c	8.3 (Berkeley) 4/29/95";
38 #endif
39 
40 /*
41  * swapinfo - based on a program of the same name by Kevin Lahey
42  */
43 
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 
48 #include <kvm.h>
49 #include <nlist.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <string.h>
54 #include <err.h>
55 
56 #include "systat.h"
57 #include "extern.h"
58 
59 kvm_t	*kd;
60 
61 static char *header;
62 static long blocksize;
63 static int dlen, odlen;
64 static int hlen;
65 static int ulen, oulen;
66 static int pagesize;
67 
68 WINDOW *
69 openswap(void)
70 {
71 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
72 }
73 
74 void
75 closeswap(WINDOW *w)
76 {
77 	if (w == NULL)
78 		return;
79 	wclear(w);
80 	wrefresh(w);
81 	delwin(w);
82 }
83 
84 /*
85  * The meat of all the swap stuff is stolen from pstat(8)'s
86  * swapmode(), which is based on a program called swapinfo written by
87  * Kevin Lahey <kml@rokkaku.atl.ga.us>.
88  */
89 
90 #define NSWAP	16
91 
92 static struct kvm_swap kvmsw[NSWAP];
93 static int kvnsw, okvnsw;
94 
95 static void calclens(void);
96 
97 #define CONVERT(v)	((int)((int64_t)(v) * pagesize / blocksize))
98 
99 static void
100 calclens(void)
101 {
102 	int i, n;
103 	int len;
104 
105 	dlen = sizeof("Disk");
106 	for (i = 0; i < kvnsw; ++i) {
107 		len = strlen(kvmsw[i].ksw_devname);
108 		if (dlen < len)
109 			dlen = len;
110 	}
111 
112 	ulen = sizeof("Used");
113 	for (n = CONVERT(kvmsw[kvnsw].ksw_used), len = 2; n /= 10; ++len);
114 	if (ulen < len)
115 		ulen = len;
116 }
117 
118 int
119 initswap(void)
120 {
121 	static int once = 0;
122 
123 	if (once)
124 		return (1);
125 
126 	header = getbsize(&hlen, &blocksize);
127 	pagesize = getpagesize();
128 
129 	if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) {
130 		error("systat: kvm_getswapinfo failed");
131 		return (0);
132 	}
133 	okvnsw = kvnsw;
134 
135 	calclens();
136 	odlen = dlen;
137 	oulen = ulen;
138 
139 	once = 1;
140 	return (1);
141 }
142 
143 void
144 fetchswap(void)
145 {
146 
147 	okvnsw = kvnsw;
148 	if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) {
149 		error("systat: kvm_getswapinfo failed");
150 		return;
151 	}
152 
153 	odlen = dlen;
154 	oulen = ulen;
155 	calclens();
156 }
157 
158 void
159 labelswap(void)
160 {
161 	const char *name;
162 	int i;
163 
164 	fetchswap();
165 
166 	werase(wnd);
167 
168 	mvwprintw(wnd, 0, 0, "%*s%*s%*s %s",
169 	    -dlen, "Disk", hlen, header, ulen, "Used",
170 	    "/0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
171 
172 	for (i = 0; i <= kvnsw; ++i) {
173 		if (i == kvnsw) {
174 			if (kvnsw == 1)
175 				break;
176 			name = "Total";
177 		} else
178 			name = kvmsw[i].ksw_devname;
179 		mvwprintw(wnd, i + 1, 0, "%*s", -dlen, name);
180 	}
181 }
182 
183 void
184 showswap(void)
185 {
186 	int count;
187 	int i;
188 
189 	if (kvnsw != okvnsw || dlen != odlen || ulen != oulen)
190 		labelswap();
191 
192 	for (i = 0; i <= kvnsw; ++i) {
193 		if (i == kvnsw) {
194 			if (kvnsw == 1)
195 				break;
196 		}
197 
198 		if (kvmsw[i].ksw_total == 0) {
199 			mvwprintw(
200 			    wnd,
201 			    i + 1,
202 			    dlen + hlen + ulen + 1,
203 			    "(swap not configured)"
204 			);
205 			continue;
206 		}
207 
208 		wmove(wnd, i + 1, dlen);
209 
210 		wprintw(wnd, "%*d", hlen, CONVERT(kvmsw[i].ksw_total));
211 		wprintw(wnd, "%*d", ulen, CONVERT(kvmsw[i].ksw_used));
212 
213 		count = 50.0 * kvmsw[i].ksw_used / kvmsw[i].ksw_total + 1;
214 
215 		waddch(wnd, ' ');
216 		while (count--)
217 			waddch(wnd, 'X');
218 		wclrtoeol(wnd);
219 	}
220 }
221