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