swap.c revision 43698
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#ifndef lint
35#if 0
36static char sccsid[] = "@(#)swap.c	8.3 (Berkeley) 4/29/95";
37#endif
38static const char rcsid[] =
39	"$Id: swap.c,v 1.9 1999/01/22 10:57:50 dillon Exp $";
40#endif /* not lint */
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/buf.h>
48#include <sys/conf.h>
49#include <sys/ioctl.h>
50#include <sys/stat.h>
51#include <sys/rlist.h>
52
53#include <kvm.h>
54#include <nlist.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <unistd.h>
58#include <string.h>
59#include <err.h>
60
61#include "systat.h"
62#include "extern.h"
63
64extern char *getbsize __P((int *headerlenp, long *blocksizep));
65void showspace __P((char *header, int hlen, long blocksize));
66
67kvm_t	*kd;
68
69static long blocksize;
70static int hlen;
71
72WINDOW *
73openswap()
74{
75	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
76}
77
78void
79closeswap(w)
80	WINDOW *w;
81{
82	if (w == NULL)
83		return;
84	wclear(w);
85	wrefresh(w);
86	delwin(w);
87}
88
89/*
90 * The meat of all the swap stuff is stolen from pstat(8)'s
91 * swapmode(), which is based on a program called swapinfo written by
92 * Kevin Lahey <kml@rokkaku.atl.ga.us>.
93 */
94
95int
96initswap()
97{
98	int i;
99	char msgbuf[BUFSIZ];
100	char *cp;
101	static int once = 0;
102	u_long ptr;
103	struct kvm_swap dummy;
104
105	if (once)
106		return (1);
107
108	if (kvm_getswapinfo(kd, &dummy, 1, 0) < 0) {
109		snprintf(msgbuf, sizeof(msgbuf), "systat: kvm_getswapinfo failed");
110		error(msgbuf);
111		return (0);
112	}
113
114	once = 1;
115	return (1);
116}
117
118static struct kvm_swap	kvmsw[16];
119static int kvnsw;
120
121void
122fetchswap()
123{
124	kvnsw = kvm_getswapinfo(kd, kvmsw, 16, 0);
125}
126
127void
128labelswap()
129{
130	char *header, *p;
131	int row, i;
132
133	fetchswap();
134
135	row = 0;
136	wmove(wnd, row, 0); wclrtobot(wnd);
137	header = getbsize(&hlen, &blocksize);
138	mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
139	    "Disk", hlen, header, "Used",
140	    "/0%  /10% /20% /30% /40% /50% /60% /70% /80% /90% /100");
141
142	for (i = 0; i < kvnsw; ++i) {
143		mvwprintw(wnd, i + 1, 0, "%-5s", kvmsw[i].ksw_devname);
144	}
145}
146
147void
148showswap()
149{
150	int i;
151	int pagesize = getpagesize();
152
153#define CONVERT(v)      ((int)((quad_t)(v) * pagesize / blocksize))
154
155	for (i = 0; i <= kvnsw; ++i) {
156		int col = 5;
157		int count;
158
159		if (i == kvnsw) {
160			if (kvnsw == 1)
161				break;
162			mvwprintw(
163			    wnd,
164			    i + 1,
165			    col,
166			    "%-5s",
167			    "Total"
168			);
169			col += 5;
170		}
171		if (kvmsw[i].ksw_total == 0) {
172			mvwprintw(
173			    wnd,
174			    i + 1,
175			    col + 5,
176			    "(swap not configured)"
177			);
178			continue;
179		}
180
181		mvwprintw(
182		    wnd,
183		    i + 1,
184		    col,
185		    "%*d",
186		    hlen,
187		    CONVERT(kvmsw[i].ksw_total)
188		);
189		col += hlen;
190
191		mvwprintw(
192		    wnd,
193		    i + 1,
194		    col,
195		    "%9d  ",
196		    CONVERT(kvmsw[i].ksw_used)
197		);
198
199		count = (int)((double)kvmsw[i].ksw_used * 49.999 /
200		    (double)kvmsw[i].ksw_total);
201
202		while (count >= 0) {
203			waddch(wnd, 'X');
204			--count;
205		}
206	}
207}
208