xref: /freebsd/usr.bin/systat/swap.c (revision 4d846d26)
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  * Copyright (c) 2017, 2020 Yoshihiro Ota
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 
35 __FBSDID("$FreeBSD$");
36 
37 #ifdef lint
38 static const char sccsid[] = "@(#)swap.c	8.3 (Berkeley) 4/29/95";
39 #endif
40 
41 /*
42  * swapinfo - based on a program of the same name by Kevin Lahey
43  */
44 
45 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/stat.h>
48 
49 #include <kvm.h>
50 #include <nlist.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <string.h>
55 #include <err.h>
56 
57 #include "systat.h"
58 #include "extern.h"
59 #include "devs.h"
60 
61 static int pathlen;
62 
63 WINDOW *
64 openswap(void)
65 {
66 
67 	return (subwin(stdscr, LINES - 3 - 1, 0, MAINWIN_ROW, 0));
68 }
69 
70 void
71 closeswap(WINDOW *w)
72 {
73 
74 	if (w == NULL)
75 		return;
76 	wclear(w);
77 	wrefresh(w);
78 	delwin(w);
79 }
80 
81 /*
82  * The meat of all the swap stuff is stolen from pstat(8)'s
83  * swapmode(), which is based on a program called swapinfo written by
84  * Kevin Lahey <kml@rokkaku.atl.ga.us>.
85  */
86 
87 #define NSWAP	16
88 
89 static struct kvm_swap kvmsw[NSWAP];
90 static int kvnsw, okvnsw;
91 
92 int
93 initswap(void)
94 {
95 	static int once = 0;
96 
97 	if (once)
98 		return (1);
99 
100 	if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) {
101 		error("systat: kvm_getswapinfo failed");
102 		return (0);
103 	}
104 	pathlen = 80 - 50 /* % */ - 5 /* Used */ - 5 /* Size */ - 3 /* space */;
105 	dsinit(12);
106 	procinit();
107 	once = 1;
108 
109 	return (1);
110 }
111 
112 void
113 fetchswap(void)
114 {
115 
116 	okvnsw = kvnsw;
117 	if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) {
118 		error("systat: kvm_getswapinfo failed");
119 		return;
120 	}
121 
122 	struct devinfo *tmp_dinfo;
123 
124 	tmp_dinfo = last_dev.dinfo;
125 	last_dev.dinfo = cur_dev.dinfo;
126 	cur_dev.dinfo = tmp_dinfo;
127 
128 	last_dev.snap_time = cur_dev.snap_time;
129 	dsgetinfo(&cur_dev);
130 	procgetinfo();
131 }
132 
133 void
134 labelswap(void)
135 {
136 
137 	werase(wnd);
138 
139 	dslabel(12, 0, LINES - DISKHIGHT - 1);
140 
141 	if (kvnsw <= 0) {
142 		mvwprintw(wnd, 0, 0, "(swap not configured)");
143 		return;
144 	}
145 
146 	mvwprintw(wnd, 0, 0, "%*s%5s %5s %s",
147 	    -pathlen, "Device/Path", "Size", "Used",
148 	    "|0%  /10  /20  /30  /40  / 60\\  70\\  80\\  90\\ 100|");
149 }
150 
151 void
152 showswap(void)
153 {
154 	const char *name;
155 	int count, i;
156 
157 	if (kvnsw != okvnsw)
158 		labelswap();
159 
160 	dsshow(12, 0, LINES - DISKHIGHT - 1, &cur_dev, &last_dev);
161 
162 	if (kvnsw <= 0)
163 		return;
164 
165 	for (i = (kvnsw == 1 ? 0 : kvnsw); i >= 0; i--) {
166 		name = i == kvnsw ? "Total" : kvmsw[i].ksw_devname;
167 		mvwprintw(wnd, 1 + i, 0, "%-*.*s", pathlen, pathlen - 1, name);
168 
169 		sysputpage(wnd, i + 1, pathlen, 5, kvmsw[i].ksw_total, 0);
170 		sysputpage(wnd, i + 1, pathlen + 5 + 1, 5, kvmsw[i].ksw_used,
171 		    0);
172 
173 		if (kvmsw[i].ksw_used > 0) {
174 			count = 50 * kvmsw[i].ksw_used / kvmsw[i].ksw_total;
175 			sysputXs(wnd, i + 1, pathlen + 5 + 1 + 5 + 1, count);
176 		}
177 		wclrtoeol(wnd);
178 	}
179 	count = kvnsw == 1 ? 2 : 3;
180 	proclabel(kvnsw + count);
181 	procshow(kvnsw + count, LINES - 5 - kvnsw + 3 - DISKHIGHT + 1,
182 	    kvmsw[kvnsw].ksw_total);
183 }
184