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 * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
3087715Smarkm#include <sys/cdefs.h>
3187715Smarkm
3287715Smarkm__FBSDID("$FreeBSD$");
3387715Smarkm
3487715Smarkm#ifdef lint
3587715Smarkmstatic const char sccsid[] = "@(#)swap.c	8.3 (Berkeley) 4/29/95";
3627232Sbde#endif
371590Srgrimes
381590Srgrimes/*
391590Srgrimes * swapinfo - based on a program of the same name by Kevin Lahey
401590Srgrimes */
411590Srgrimes
421590Srgrimes#include <sys/param.h>
431590Srgrimes#include <sys/ioctl.h>
441590Srgrimes#include <sys/stat.h>
451590Srgrimes
461590Srgrimes#include <kvm.h>
471590Srgrimes#include <nlist.h>
481590Srgrimes#include <stdio.h>
491590Srgrimes#include <stdlib.h>
501590Srgrimes#include <unistd.h>
5140060Sobrien#include <string.h>
52200462Sdelphij#include <err.h>
531590Srgrimes
541590Srgrimes#include "systat.h"
551590Srgrimes#include "extern.h"
561590Srgrimes
571590Srgrimeskvm_t	*kd;
581590Srgrimes
59165490Syarstatic char *header;
6043047Sdillonstatic long blocksize;
61165501Syarstatic int dlen, odlen;
6243047Sdillonstatic int hlen;
63165490Syarstatic int ulen, oulen;
64165490Syarstatic int pagesize;
651590Srgrimes
661590SrgrimesWINDOW *
67175387Sdelphijopenswap(void)
681590Srgrimes{
69158160Sbde	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
701590Srgrimes}
711590Srgrimes
721590Srgrimesvoid
73175387Sdelphijcloseswap(WINDOW *w)
741590Srgrimes{
751590Srgrimes	if (w == NULL)
761590Srgrimes		return;
771590Srgrimes	wclear(w);
781590Srgrimes	wrefresh(w);
791590Srgrimes	delwin(w);
801590Srgrimes}
811590Srgrimes
8221617Sjoerg/*
8321617Sjoerg * The meat of all the swap stuff is stolen from pstat(8)'s
8421617Sjoerg * swapmode(), which is based on a program called swapinfo written by
8521617Sjoerg * Kevin Lahey <kml@rokkaku.atl.ga.us>.
8621617Sjoerg */
8721617Sjoerg
88165501Syar#define NSWAP	16
89165501Syar
90165501Syarstatic struct kvm_swap kvmsw[NSWAP];
91165501Syarstatic int kvnsw, okvnsw;
92165501Syar
93165506Syarstatic void calclens(void);
94165506Syar
95165501Syar#define CONVERT(v)	((int)((int64_t)(v) * pagesize / blocksize))
96165501Syar
97165501Syarstatic void
98175387Sdelphijcalclens(void)
99165501Syar{
100165501Syar	int i, n;
101165501Syar	int len;
102165501Syar
103165501Syar	dlen = sizeof("Disk");
104165501Syar	for (i = 0; i < kvnsw; ++i) {
105165501Syar		len = strlen(kvmsw[i].ksw_devname);
106165501Syar		if (dlen < len)
107165501Syar			dlen = len;
108165501Syar	}
109165501Syar
110165501Syar	ulen = sizeof("Used");
111165501Syar	for (n = CONVERT(kvmsw[kvnsw].ksw_used), len = 2; n /= 10; ++len);
112165501Syar	if (ulen < len)
113165501Syar		ulen = len;
114165501Syar}
115165501Syar
11640060Sobrienint
117175387Sdelphijinitswap(void)
1181590Srgrimes{
1191590Srgrimes	static int once = 0;
1201590Srgrimes
1211590Srgrimes	if (once)
1221590Srgrimes		return (1);
12343047Sdillon
124165490Syar	header = getbsize(&hlen, &blocksize);
125165490Syar	pagesize = getpagesize();
126165490Syar
127165501Syar	if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) {
128165501Syar		error("systat: kvm_getswapinfo failed");
1291590Srgrimes		return (0);
1301590Srgrimes	}
131165501Syar	okvnsw = kvnsw;
13243698Sdillon
133165501Syar	calclens();
134165501Syar	odlen = dlen;
135165501Syar	oulen = ulen;
136165501Syar
1371590Srgrimes	once = 1;
1381590Srgrimes	return (1);
1391590Srgrimes}
1401590Srgrimes
1411590Srgrimesvoid
142175387Sdelphijfetchswap(void)
1431590Srgrimes{
144165490Syar
145165490Syar	okvnsw = kvnsw;
146165501Syar	if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) {
147165501Syar		error("systat: kvm_getswapinfo failed");
148165501Syar		return;
149165501Syar	}
150165490Syar
151165501Syar	odlen = dlen;
152165490Syar	oulen = ulen;
153165501Syar	calclens();
1541590Srgrimes}
1551590Srgrimes
1561590Srgrimesvoid
157175387Sdelphijlabelswap(void)
1581590Srgrimes{
159165506Syar	const char *name;
160165492Syar	int i;
1611590Srgrimes
16243050Sdillon	fetchswap();
16343050Sdillon
164165492Syar	werase(wnd);
165165491Syar
166165501Syar	mvwprintw(wnd, 0, 0, "%*s%*s%*s %s",
167165501Syar	    -dlen, "Disk", hlen, header, ulen, "Used",
168164689Syar	    "/0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
16943047Sdillon
170165489Syar	for (i = 0; i <= kvnsw; ++i) {
171165489Syar		if (i == kvnsw) {
172165489Syar			if (kvnsw == 1)
173165489Syar				break;
174165489Syar			name = "Total";
175165489Syar		} else
176165489Syar			name = kvmsw[i].ksw_devname;
177165501Syar		mvwprintw(wnd, i + 1, 0, "%*s", -dlen, name);
1781590Srgrimes	}
1791590Srgrimes}
1801590Srgrimes
1811590Srgrimesvoid
182175387Sdelphijshowswap(void)
1831590Srgrimes{
184165495Syar	int count;
18543047Sdillon	int i;
1861590Srgrimes
187165501Syar	if (kvnsw != okvnsw || dlen != odlen || ulen != oulen)
188165490Syar		labelswap();
18921617Sjoerg
19043047Sdillon	for (i = 0; i <= kvnsw; ++i) {
19143047Sdillon		if (i == kvnsw) {
19243047Sdillon			if (kvnsw == 1)
19343047Sdillon				break;
19443047Sdillon		}
195165491Syar
19643698Sdillon		if (kvmsw[i].ksw_total == 0) {
19743698Sdillon			mvwprintw(
19843698Sdillon			    wnd,
19943698Sdillon			    i + 1,
200165501Syar			    dlen + hlen + ulen + 1,
20143698Sdillon			    "(swap not configured)"
20243698Sdillon			);
20343698Sdillon			continue;
20443698Sdillon		}
20543047Sdillon
206165501Syar		wmove(wnd, i + 1, dlen);
207165492Syar
208165495Syar		wprintw(wnd, "%*d", hlen, CONVERT(kvmsw[i].ksw_total));
209165495Syar		wprintw(wnd, "%*d", ulen, CONVERT(kvmsw[i].ksw_used));
21021617Sjoerg
211165498Syar		count = 50.0 * kvmsw[i].ksw_used / kvmsw[i].ksw_total + 1;
21243047Sdillon
213165495Syar		waddch(wnd, ' ');
214165495Syar		while (count--)
2151590Srgrimes			waddch(wnd, 'X');
216165499Syar		wclrtoeol(wnd);
2171590Srgrimes	}
2181590Srgrimes}
219