swap.c revision 165498
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1980, 1992, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 3487715Smarkm#include <sys/cdefs.h> 3587715Smarkm 3687715Smarkm__FBSDID("$FreeBSD: head/usr.bin/systat/swap.c 165498 2006-12-23 17:46:32Z yar $"); 3787715Smarkm 3887715Smarkm#ifdef lint 3987715Smarkmstatic const char sccsid[] = "@(#)swap.c 8.3 (Berkeley) 4/29/95"; 4027232Sbde#endif 411590Srgrimes 421590Srgrimes/* 431590Srgrimes * swapinfo - based on a program of the same name by Kevin Lahey 441590Srgrimes */ 451590Srgrimes 461590Srgrimes#include <sys/param.h> 471590Srgrimes#include <sys/ioctl.h> 481590Srgrimes#include <sys/stat.h> 491590Srgrimes 501590Srgrimes#include <kvm.h> 511590Srgrimes#include <nlist.h> 521590Srgrimes#include <stdio.h> 531590Srgrimes#include <stdlib.h> 541590Srgrimes#include <unistd.h> 5540060Sobrien#include <string.h> 5640060Sobrien#include <err.h> 571590Srgrimes 581590Srgrimes#include "systat.h" 591590Srgrimes#include "extern.h" 601590Srgrimes 611590Srgrimeskvm_t *kd; 621590Srgrimes 63165490Syarstatic char *header; 6443047Sdillonstatic long blocksize; 6543047Sdillonstatic int hlen; 66165490Syarstatic int ulen, oulen; 67165490Syarstatic int pagesize; 681590Srgrimes 691590SrgrimesWINDOW * 701590Srgrimesopenswap() 711590Srgrimes{ 72158160Sbde return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0)); 731590Srgrimes} 741590Srgrimes 751590Srgrimesvoid 761590Srgrimescloseswap(w) 771590Srgrimes WINDOW *w; 781590Srgrimes{ 791590Srgrimes if (w == NULL) 801590Srgrimes return; 811590Srgrimes wclear(w); 821590Srgrimes wrefresh(w); 831590Srgrimes delwin(w); 841590Srgrimes} 851590Srgrimes 8621617Sjoerg/* 8721617Sjoerg * The meat of all the swap stuff is stolen from pstat(8)'s 8821617Sjoerg * swapmode(), which is based on a program called swapinfo written by 8921617Sjoerg * Kevin Lahey <kml@rokkaku.atl.ga.us>. 9021617Sjoerg */ 9121617Sjoerg 9240060Sobrienint 931590Srgrimesinitswap() 941590Srgrimes{ 951590Srgrimes char msgbuf[BUFSIZ]; 961590Srgrimes static int once = 0; 9743047Sdillon struct kvm_swap dummy; 981590Srgrimes 991590Srgrimes if (once) 1001590Srgrimes return (1); 10143047Sdillon 102165490Syar header = getbsize(&hlen, &blocksize); 103165490Syar pagesize = getpagesize(); 104165490Syar 10543047Sdillon if (kvm_getswapinfo(kd, &dummy, 1, 0) < 0) { 10643047Sdillon snprintf(msgbuf, sizeof(msgbuf), "systat: kvm_getswapinfo failed"); 10777206Skris error("%s", msgbuf); 1081590Srgrimes return (0); 1091590Srgrimes } 11043698Sdillon 1111590Srgrimes once = 1; 1121590Srgrimes return (1); 1131590Srgrimes} 1141590Srgrimes 11543047Sdillonstatic struct kvm_swap kvmsw[16]; 116165490Syarstatic int kvnsw, okvnsw; 11743047Sdillon 118165490Syar#define CONVERT(v) ((int)((int64_t)(v) * pagesize / blocksize)) 119165490Syar 1201590Srgrimesvoid 1211590Srgrimesfetchswap() 1221590Srgrimes{ 123165490Syar int n; 124165490Syar 125165490Syar okvnsw = kvnsw; 12643047Sdillon kvnsw = kvm_getswapinfo(kd, kvmsw, 16, 0); 127165490Syar 128165490Syar oulen = ulen; 129165490Syar for (n = CONVERT(kvmsw[kvnsw].ksw_used), ulen = 2; n /= 10; ++ulen); 130165490Syar if (ulen < sizeof("Used")) 131165490Syar ulen = sizeof("Used"); 1321590Srgrimes} 1331590Srgrimes 1341590Srgrimesvoid 1351590Srgrimeslabelswap() 1361590Srgrimes{ 137165490Syar char *name; 138165492Syar int i; 1391590Srgrimes 14043050Sdillon fetchswap(); 14143050Sdillon 142165492Syar werase(wnd); 143165491Syar 144165492Syar mvwprintw(wnd, 0, 0, "%-5s%*s%*s %s", 145165490Syar "Disk", hlen, header, ulen, "Used", 146164689Syar "/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 14743047Sdillon 148165489Syar for (i = 0; i <= kvnsw; ++i) { 149165489Syar if (i == kvnsw) { 150165489Syar if (kvnsw == 1) 151165489Syar break; 152165489Syar name = "Total"; 153165489Syar } else 154165489Syar name = kvmsw[i].ksw_devname; 155165489Syar mvwprintw(wnd, i + 1, 0, "%-5s", name); 1561590Srgrimes } 1571590Srgrimes} 1581590Srgrimes 1591590Srgrimesvoid 1601590Srgrimesshowswap() 1611590Srgrimes{ 162165495Syar int count; 16343047Sdillon int i; 1641590Srgrimes 165165490Syar if (kvnsw != okvnsw || ulen != oulen) 166165490Syar labelswap(); 16721617Sjoerg 16843047Sdillon for (i = 0; i <= kvnsw; ++i) { 16943047Sdillon if (i == kvnsw) { 17043047Sdillon if (kvnsw == 1) 17143047Sdillon break; 17243047Sdillon } 173165491Syar 17443698Sdillon if (kvmsw[i].ksw_total == 0) { 17543698Sdillon mvwprintw( 17643698Sdillon wnd, 17743698Sdillon i + 1, 178165493Syar 5 + hlen + ulen + 1, 17943698Sdillon "(swap not configured)" 18043698Sdillon ); 18143698Sdillon continue; 18243698Sdillon } 18343047Sdillon 184165492Syar wmove(wnd, i + 1, 5); 185165492Syar 186165495Syar wprintw(wnd, "%*d", hlen, CONVERT(kvmsw[i].ksw_total)); 187165495Syar wprintw(wnd, "%*d", ulen, CONVERT(kvmsw[i].ksw_used)); 18821617Sjoerg 189165498Syar count = 50.0 * kvmsw[i].ksw_used / kvmsw[i].ksw_total + 1; 19043047Sdillon 191165495Syar waddch(wnd, ' '); 192165495Syar while (count--) 1931590Srgrimes waddch(wnd, 'X'); 1941590Srgrimes } 1951590Srgrimes} 196