swap.c revision 165488
1/*- 2 * Copyright (c) 1980, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include <sys/cdefs.h> 35 36__FBSDID("$FreeBSD: head/usr.bin/systat/swap.c 165488 2006-12-23 14:25:17Z 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 long blocksize; 64static int hlen; 65 66WINDOW * 67openswap() 68{ 69 return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); 70} 71 72void 73closeswap(w) 74 WINDOW *w; 75{ 76 if (w == NULL) 77 return; 78 wclear(w); 79 wrefresh(w); 80 delwin(w); 81} 82 83/* 84 * The meat of all the swap stuff is stolen from pstat(8)'s 85 * swapmode(), which is based on a program called swapinfo written by 86 * Kevin Lahey <kml@rokkaku.atl.ga.us>. 87 */ 88 89int 90initswap() 91{ 92 char msgbuf[BUFSIZ]; 93 static int once = 0; 94 struct kvm_swap dummy; 95 96 if (once) 97 return (1); 98 99 if (kvm_getswapinfo(kd, &dummy, 1, 0) < 0) { 100 snprintf(msgbuf, sizeof(msgbuf), "systat: kvm_getswapinfo failed"); 101 error("%s", msgbuf); 102 return (0); 103 } 104 105 once = 1; 106 return (1); 107} 108 109static struct kvm_swap kvmsw[16]; 110static int kvnsw; 111 112void 113fetchswap() 114{ 115 kvnsw = kvm_getswapinfo(kd, kvmsw, 16, 0); 116} 117 118void 119labelswap() 120{ 121 char *header; 122 int row, i; 123 124 fetchswap(); 125 126 row = 0; 127 wmove(wnd, row, 0); wclrtobot(wnd); 128 header = getbsize(&hlen, &blocksize); 129 mvwprintw(wnd, row++, 0, "%-5s%*s%9s %s", 130 "Disk", hlen, header, "Used", 131 "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 132 133 for (i = 0; i < kvnsw; ++i) { 134 mvwprintw(wnd, i + 1, 0, "%-5s", kvmsw[i].ksw_devname); 135 } 136} 137 138void 139showswap() 140{ 141 int i; 142 int pagesize = getpagesize(); 143 144#define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize)) 145 146 for (i = 0; i <= kvnsw; ++i) { 147 int lcol = 5; 148 int count; 149 150 if (i == kvnsw) { 151 if (kvnsw == 1) 152 break; 153 mvwprintw( 154 wnd, 155 i + 1, 156 lcol, 157 "%-5s", 158 "Total" 159 ); 160 lcol += 5; 161 } 162 if (kvmsw[i].ksw_total == 0) { 163 mvwprintw( 164 wnd, 165 i + 1, 166 lcol + 5, 167 "(swap not configured)" 168 ); 169 continue; 170 } 171 172 mvwprintw( 173 wnd, 174 i + 1, 175 lcol, 176 "%*d", 177 hlen, 178 CONVERT(kvmsw[i].ksw_total) 179 ); 180 lcol += hlen; 181 182 mvwprintw( 183 wnd, 184 i + 1, 185 lcol, 186 "%9d ", 187 CONVERT(kvmsw[i].ksw_used) 188 ); 189 190 count = (int)((double)kvmsw[i].ksw_used * 49.999 / 191 (double)kvmsw[i].ksw_total); 192 193 while (count >= 0) { 194 waddch(wnd, 'X'); 195 --count; 196 } 197 } 198} 199