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 once = 1; 107 108 return (1); 109 } 110 111 void 112 fetchswap(void) 113 { 114 115 okvnsw = kvnsw; 116 if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) { 117 error("systat: kvm_getswapinfo failed"); 118 return; 119 } 120 121 struct devinfo *tmp_dinfo; 122 123 tmp_dinfo = last_dev.dinfo; 124 last_dev.dinfo = cur_dev.dinfo; 125 cur_dev.dinfo = tmp_dinfo; 126 127 last_dev.snap_time = cur_dev.snap_time; 128 dsgetinfo( &cur_dev ); 129 } 130 131 void 132 labelswap(void) 133 { 134 const char *name; 135 int i; 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 for (i = 0; i <= kvnsw; ++i) { 151 name = i == kvnsw ? "Total" : kvmsw[i].ksw_devname; 152 mvwprintw(wnd, 1 + i, 0, "%-*.*s", pathlen, pathlen - 1, name); 153 } 154 } 155 156 void 157 showswap(void) 158 { 159 int count; 160 int i; 161 162 if (kvnsw != okvnsw) 163 labelswap(); 164 165 dsshow(12, 0, LINES - DISKHIGHT - 1, &cur_dev, &last_dev); 166 167 if (kvnsw <= 0) 168 return; 169 170 for (i = 0; i <= kvnsw; ++i) { 171 sysputpage(wnd, i + 1, pathlen, 5, kvmsw[i].ksw_total, 0); 172 sysputpage(wnd, i + 1, pathlen + 5 + 1, 5, kvmsw[i].ksw_used, 173 0); 174 175 if (kvmsw[i].ksw_used > 0) { 176 count = 50 * kvmsw[i].ksw_used / kvmsw[i].ksw_total; 177 sysputXs(wnd, i + 1, pathlen + 5 + 1 + 5 + 1, count); 178 } 179 wclrtoeol(wnd); 180 } 181 } 182