swap.c revision 164689
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 164689 2006-11-27 20:19:05Z 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
6343047Sdillonstatic long blocksize;
6443047Sdillonstatic int hlen;
651590Srgrimes
661590SrgrimesWINDOW *
671590Srgrimesopenswap()
681590Srgrimes{
69158160Sbde	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
701590Srgrimes}
711590Srgrimes
721590Srgrimesvoid
731590Srgrimescloseswap(w)
741590Srgrimes	WINDOW *w;
751590Srgrimes{
761590Srgrimes	if (w == NULL)
771590Srgrimes		return;
781590Srgrimes	wclear(w);
791590Srgrimes	wrefresh(w);
801590Srgrimes	delwin(w);
811590Srgrimes}
821590Srgrimes
8321617Sjoerg/*
8421617Sjoerg * The meat of all the swap stuff is stolen from pstat(8)'s
8521617Sjoerg * swapmode(), which is based on a program called swapinfo written by
8621617Sjoerg * Kevin Lahey <kml@rokkaku.atl.ga.us>.
8721617Sjoerg */
8821617Sjoerg
8940060Sobrienint
901590Srgrimesinitswap()
911590Srgrimes{
921590Srgrimes	char msgbuf[BUFSIZ];
931590Srgrimes	static int once = 0;
9443047Sdillon	struct kvm_swap dummy;
951590Srgrimes
961590Srgrimes	if (once)
971590Srgrimes		return (1);
9843047Sdillon
9943047Sdillon	if (kvm_getswapinfo(kd, &dummy, 1, 0) < 0) {
10043047Sdillon		snprintf(msgbuf, sizeof(msgbuf), "systat: kvm_getswapinfo failed");
10177206Skris		error("%s", msgbuf);
1021590Srgrimes		return (0);
1031590Srgrimes	}
10443698Sdillon
1051590Srgrimes	once = 1;
1061590Srgrimes	return (1);
1071590Srgrimes}
1081590Srgrimes
10943047Sdillonstatic struct kvm_swap	kvmsw[16];
11043047Sdillonstatic int kvnsw;
11143047Sdillon
1121590Srgrimesvoid
1131590Srgrimesfetchswap()
1141590Srgrimes{
11543047Sdillon	kvnsw = kvm_getswapinfo(kd, kvmsw, 16, 0);
1161590Srgrimes}
1171590Srgrimes
1181590Srgrimesvoid
1191590Srgrimeslabelswap()
1201590Srgrimes{
12174671Stmm	char *header;
1221590Srgrimes	int row, i;
1231590Srgrimes
12443050Sdillon	fetchswap();
12543050Sdillon
1261590Srgrimes	row = 0;
1271590Srgrimes	wmove(wnd, row, 0); wclrtobot(wnd);
128108454Smike	header = getbsize(&hlen, &blocksize);
12921617Sjoerg	mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
1301590Srgrimes	    "Disk", hlen, header, "Used",
131164689Syar	    "/0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
13243047Sdillon
13343047Sdillon	for (i = 0; i < kvnsw; ++i) {
13443047Sdillon		mvwprintw(wnd, i + 1, 0, "%-5s", kvmsw[i].ksw_devname);
1351590Srgrimes	}
1361590Srgrimes}
1371590Srgrimes
1381590Srgrimesvoid
1391590Srgrimesshowswap()
1401590Srgrimes{
14143047Sdillon	int i;
14243047Sdillon	int pagesize = getpagesize();
1431590Srgrimes
14443047Sdillon#define CONVERT(v)      ((int)((quad_t)(v) * pagesize / blocksize))
14521617Sjoerg
14643047Sdillon	for (i = 0; i <= kvnsw; ++i) {
14787715Smarkm		int lcol = 5;
14843047Sdillon		int count;
14943047Sdillon
15043047Sdillon		if (i == kvnsw) {
15143047Sdillon			if (kvnsw == 1)
15243047Sdillon				break;
15343047Sdillon			mvwprintw(
15443047Sdillon			    wnd,
15543047Sdillon			    i + 1,
15687715Smarkm			    lcol,
15743047Sdillon			    "%-5s",
15843047Sdillon			    "Total"
15943047Sdillon			);
16087715Smarkm			lcol += 5;
16143047Sdillon		}
16243698Sdillon		if (kvmsw[i].ksw_total == 0) {
16343698Sdillon			mvwprintw(
16443698Sdillon			    wnd,
16543698Sdillon			    i + 1,
16687715Smarkm			    lcol + 5,
16743698Sdillon			    "(swap not configured)"
16843698Sdillon			);
16943698Sdillon			continue;
17043698Sdillon		}
17143047Sdillon
17243047Sdillon		mvwprintw(
173158161Sbde		    wnd,
174158161Sbde		    i + 1,
17587715Smarkm		    lcol,
17643047Sdillon		    "%*d",
177158161Sbde		    hlen,
17843047Sdillon		    CONVERT(kvmsw[i].ksw_total)
17943047Sdillon		);
18087715Smarkm		lcol += hlen;
18121617Sjoerg
18243047Sdillon		mvwprintw(
18343047Sdillon		    wnd,
18443047Sdillon		    i + 1,
185158161Sbde		    lcol,
18643047Sdillon		    "%9d  ",
18743047Sdillon		    CONVERT(kvmsw[i].ksw_used)
18843047Sdillon		);
18943047Sdillon
19043047Sdillon		count = (int)((double)kvmsw[i].ksw_used * 49.999 /
19143047Sdillon		    (double)kvmsw[i].ksw_total);
19243047Sdillon
19343047Sdillon		while (count >= 0) {
1941590Srgrimes			waddch(wnd, 'X');
19543047Sdillon			--count;
19643047Sdillon		}
1971590Srgrimes	}
1981590Srgrimes}
199