swap.c revision 165492
1193323Sed/*- 2193323Sed * Copyright (c) 1980, 1992, 1993 3193323Sed * The Regents of the University of California. All rights reserved. 4193323Sed * 5193323Sed * Redistribution and use in source and binary forms, with or without 6193323Sed * modification, are permitted provided that the following conditions 7193323Sed * are met: 8193323Sed * 1. Redistributions of source code must retain the above copyright 9193323Sed * notice, this list of conditions and the following disclaimer. 10193323Sed * 2. Redistributions in binary form must reproduce the above copyright 11193323Sed * notice, this list of conditions and the following disclaimer in the 12193323Sed * documentation and/or other materials provided with the distribution. 13193323Sed * 3. All advertising materials mentioning features or use of this software 14193323Sed * must display the following acknowledgement: 15198090Srdivacky * This product includes software developed by the University of 16193323Sed * California, Berkeley and its contributors. 17193323Sed * 4. Neither the name of the University nor the names of its contributors 18193323Sed * may be used to endorse or promote products derived from this software 19193323Sed * without specific prior written permission. 20193323Sed * 21193323Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24193323Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25210299Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26198090Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30198090Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31193323Sed * SUCH DAMAGE. 32193323Sed */ 33193323Sed 34193323Sed#include <sys/cdefs.h> 35193323Sed 36__FBSDID("$FreeBSD: head/usr.bin/systat/swap.c 165492 2006-12-23 16:39:09Z yar $"); 37 38#ifdef lint 39static const char sccsid[] = "@(#)swap.c 8.3 (Berkeley) 4/29/95"; 40#endif 41 42/* 43 * swapinfo - based on a program of the same name by Kevin Lahey 44 */ 45 46#include <sys/param.h> 47#include <sys/ioctl.h> 48#include <sys/stat.h> 49 50#include <kvm.h> 51#include <nlist.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <unistd.h> 55#include <string.h> 56#include <err.h> 57 58#include "systat.h" 59#include "extern.h" 60 61kvm_t *kd; 62 63static char *header; 64static long blocksize; 65static int hlen; 66static int ulen, oulen; 67static int pagesize; 68 69WINDOW * 70openswap() 71{ 72 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); 73} 74 75void 76closeswap(w) 77 WINDOW *w; 78{ 79 if (w == NULL) 80 return; 81 wclear(w); 82 wrefresh(w); 83 delwin(w); 84} 85 86/* 87 * The meat of all the swap stuff is stolen from pstat(8)'s 88 * swapmode(), which is based on a program called swapinfo written by 89 * Kevin Lahey <kml@rokkaku.atl.ga.us>. 90 */ 91 92int 93initswap() 94{ 95 char msgbuf[BUFSIZ]; 96 static int once = 0; 97 struct kvm_swap dummy; 98 99 if (once) 100 return (1); 101 102 header = getbsize(&hlen, &blocksize); 103 pagesize = getpagesize(); 104 105 if (kvm_getswapinfo(kd, &dummy, 1, 0) < 0) { 106 snprintf(msgbuf, sizeof(msgbuf), "systat: kvm_getswapinfo failed"); 107 error("%s", msgbuf); 108 return (0); 109 } 110 111 once = 1; 112 return (1); 113} 114 115static struct kvm_swap kvmsw[16]; 116static int kvnsw, okvnsw; 117 118#define CONVERT(v) ((int)((int64_t)(v) * pagesize / blocksize)) 119 120void 121fetchswap() 122{ 123 int n; 124 125 okvnsw = kvnsw; 126 kvnsw = kvm_getswapinfo(kd, kvmsw, 16, 0); 127 128 oulen = ulen; 129 for (n = CONVERT(kvmsw[kvnsw].ksw_used), ulen = 2; n /= 10; ++ulen); 130 if (ulen < sizeof("Used")) 131 ulen = sizeof("Used"); 132} 133 134void 135labelswap() 136{ 137 char *name; 138 int i; 139 140 fetchswap(); 141 142 werase(wnd); 143 144 mvwprintw(wnd, 0, 0, "%-5s%*s%*s %s", 145 "Disk", hlen, header, ulen, "Used", 146 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 147 148 for (i = 0; i <= kvnsw; ++i) { 149 if (i == kvnsw) { 150 if (kvnsw == 1) 151 break; 152 name = "Total"; 153 } else 154 name = kvmsw[i].ksw_devname; 155 mvwprintw(wnd, i + 1, 0, "%-5s", name); 156 } 157} 158 159void 160showswap() 161{ 162 int i; 163 164 if (kvnsw != okvnsw || ulen != oulen) 165 labelswap(); 166 167 for (i = 0; i <= kvnsw; ++i) { 168 int count; 169 170 if (i == kvnsw) { 171 if (kvnsw == 1) 172 break; 173 } 174 175 if (kvmsw[i].ksw_total == 0) { 176 mvwprintw( 177 wnd, 178 i + 1, 179 5 + 5, 180 "(swap not configured)" 181 ); 182 continue; 183 } 184 185 wmove(wnd, i + 1, 5); 186 187 wprintw( 188 wnd, 189 "%*d", 190 hlen, 191 CONVERT(kvmsw[i].ksw_total) 192 ); 193 194 wprintw( 195 wnd, 196 "%*d ", 197 ulen, 198 CONVERT(kvmsw[i].ksw_used) 199 ); 200 201 count = (int)((double)kvmsw[i].ksw_used * 49.999 / 202 (double)kvmsw[i].ksw_total); 203 204 while (count >= 0) { 205 waddch(wnd, 'X'); 206 --count; 207 } 208 } 209} 210