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