1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-07-06 04:37:22 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)swap.c 8.3 (Berkeley) 4/29/95";
|
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* swapinfo - based on a program of the same name by Kevin Lahey
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
1994-10-09 07:37:18 +00:00
|
|
|
#include <sys/rlist.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <kvm.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "systat.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
extern char *getbsize __P((int *headerlenp, long *blocksizep));
|
|
|
|
void showspace __P((char *header, int hlen, long blocksize));
|
|
|
|
|
|
|
|
kvm_t *kd;
|
|
|
|
|
|
|
|
struct nlist syms[] = {
|
1997-01-12 19:38:32 +00:00
|
|
|
{ "_swaplist" },/* list of free swap areas */
|
|
|
|
#define VM_SWAPLIST 0
|
1994-05-27 12:33:43 +00:00
|
|
|
{ "_swdevt" }, /* list of swap devices and sizes */
|
1997-01-12 19:38:32 +00:00
|
|
|
#define VM_SWDEVT 1
|
1994-05-27 12:33:43 +00:00
|
|
|
{ "_nswap" }, /* size of largest swap device */
|
1997-01-12 19:38:32 +00:00
|
|
|
#define VM_NSWAP 2
|
1994-05-27 12:33:43 +00:00
|
|
|
{ "_nswdev" }, /* number of swap devices */
|
1997-01-12 19:38:32 +00:00
|
|
|
#define VM_NSWDEV 3
|
1994-05-27 12:33:43 +00:00
|
|
|
{ "_dmmax" }, /* maximum size of a swap block */
|
1997-01-12 19:38:32 +00:00
|
|
|
#define VM_DMMAX 4
|
1994-05-27 12:33:43 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
1997-01-12 19:38:32 +00:00
|
|
|
static int nswap, nswdev, dmmax;
|
1994-05-27 12:33:43 +00:00
|
|
|
static struct swdevt *sw;
|
|
|
|
static long *perdev, blocksize;
|
|
|
|
static int nfree, hlen;
|
1997-01-12 19:38:32 +00:00
|
|
|
static struct rlisthdr swaplist;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#define SVAR(var) __STRING(var) /* to force expansion */
|
|
|
|
#define KGET(idx, var) \
|
1997-01-12 19:38:32 +00:00
|
|
|
KGET1(idx, &var, sizeof(var), SVAR(var), (0))
|
|
|
|
#define KGET1(idx, p, s, msg, rv) \
|
|
|
|
KGET2(syms[idx].n_value, p, s, msg, rv)
|
|
|
|
#define KGET2(addr, p, s, msg, rv) \
|
1994-05-27 12:33:43 +00:00
|
|
|
if (kvm_read(kd, addr, p, s) != s) { \
|
|
|
|
error("cannot read %s: %s", msg, kvm_geterr(kd)); \
|
1997-01-12 19:38:32 +00:00
|
|
|
return rv; \
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WINDOW *
|
|
|
|
openswap()
|
|
|
|
{
|
|
|
|
return (subwin(stdscr, LINES-5-1, 0, 5, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
closeswap(w)
|
|
|
|
WINDOW *w;
|
|
|
|
{
|
|
|
|
if (w == NULL)
|
|
|
|
return;
|
|
|
|
wclear(w);
|
|
|
|
wrefresh(w);
|
|
|
|
delwin(w);
|
|
|
|
}
|
|
|
|
|
1997-01-12 19:38:32 +00:00
|
|
|
/*
|
|
|
|
* The meat of all the swap stuff is stolen from pstat(8)'s
|
|
|
|
* swapmode(), which is based on a program called swapinfo written by
|
|
|
|
* Kevin Lahey <kml@rokkaku.atl.ga.us>.
|
|
|
|
*/
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
initswap()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char msgbuf[BUFSIZ];
|
|
|
|
static int once = 0;
|
1997-01-12 19:38:32 +00:00
|
|
|
u_long ptr;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (once)
|
|
|
|
return (1);
|
|
|
|
if (kvm_nlist(kd, syms)) {
|
|
|
|
strcpy(msgbuf, "systat: swap: cannot find");
|
|
|
|
for (i = 0; syms[i].n_name != NULL; i++) {
|
|
|
|
if (syms[i].n_value == 0) {
|
|
|
|
strcat(msgbuf, " ");
|
|
|
|
strcat(msgbuf, syms[i].n_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error(msgbuf);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
KGET(VM_NSWAP, nswap);
|
|
|
|
KGET(VM_NSWDEV, nswdev);
|
|
|
|
KGET(VM_DMMAX, dmmax);
|
|
|
|
if ((sw = malloc(nswdev * sizeof(*sw))) == NULL ||
|
1997-01-12 19:38:32 +00:00
|
|
|
(perdev = malloc(nswdev * sizeof(*perdev))) == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
KGET1(VM_SWDEVT, &ptr, sizeof ptr, "swdevt", (0));
|
|
|
|
KGET2(ptr, sw, nswdev * sizeof(*sw), "*swdevt", (0));
|
1994-05-27 12:33:43 +00:00
|
|
|
once = 1;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fetchswap()
|
|
|
|
{
|
1997-01-12 19:38:32 +00:00
|
|
|
struct rlist head;
|
|
|
|
struct rlist *swapptr;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1997-01-12 19:38:32 +00:00
|
|
|
/* Count up swap space. */
|
|
|
|
nfree = 0;
|
|
|
|
memset(perdev, 0, nswdev * sizeof(*perdev));
|
|
|
|
KGET1(VM_SWAPLIST, &swaplist, sizeof swaplist, "swaplist", /* none */);
|
|
|
|
swapptr = swaplist.rlh_list;
|
|
|
|
while (swapptr) {
|
|
|
|
int top, bottom, next_block;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1997-01-12 19:38:32 +00:00
|
|
|
KGET2((unsigned long)swapptr, &head,
|
|
|
|
sizeof(struct rlist), "swapptr", /* none */);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1997-01-12 19:38:32 +00:00
|
|
|
top = head.rl_end;
|
|
|
|
bottom = head.rl_start;
|
|
|
|
|
|
|
|
nfree += top - bottom + 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Swap space is split up among the configured disks.
|
1997-01-12 19:38:32 +00:00
|
|
|
*
|
|
|
|
* For interleaved swap devices, the first dmmax blocks
|
|
|
|
* of swap space some from the first disk, the next dmmax
|
|
|
|
* blocks from the next, and so on up to nswap blocks.
|
|
|
|
*
|
|
|
|
* The list of free space joins adjacent free blocks,
|
|
|
|
* ignoring device boundries. If we want to keep track
|
|
|
|
* of this information per device, we'll just have to
|
|
|
|
* extract it ourselves.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
1997-01-12 19:38:32 +00:00
|
|
|
while (top / dmmax != bottom / dmmax) {
|
|
|
|
next_block = ((bottom + dmmax) / dmmax);
|
|
|
|
perdev[(bottom / dmmax) % nswdev] +=
|
|
|
|
next_block * dmmax - bottom;
|
|
|
|
bottom = next_block * dmmax;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1997-01-12 19:38:32 +00:00
|
|
|
perdev[(bottom / dmmax) % nswdev] +=
|
|
|
|
top - bottom + 1;
|
|
|
|
|
|
|
|
swapptr = head.rl_next;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1997-01-12 19:38:32 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
labelswap()
|
|
|
|
{
|
1997-07-06 04:37:22 +00:00
|
|
|
char *header, *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
int row, i;
|
|
|
|
|
|
|
|
row = 0;
|
|
|
|
wmove(wnd, row, 0); wclrtobot(wnd);
|
|
|
|
header = getbsize(&hlen, &blocksize);
|
1997-01-12 19:38:32 +00:00
|
|
|
mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
|
1994-05-27 12:33:43 +00:00
|
|
|
"Disk", hlen, header, "Used",
|
1997-01-12 19:38:32 +00:00
|
|
|
"/0% /10% /20% /30% /40% /50% /60% /70% /80% /90% /100");
|
1994-05-27 12:33:43 +00:00
|
|
|
for (i = 0; i < nswdev; i++) {
|
1997-01-12 19:38:32 +00:00
|
|
|
if (!sw[i].sw_freed)
|
|
|
|
continue;
|
1997-07-06 04:37:22 +00:00
|
|
|
p = devname(sw[i].sw_dev, S_IFBLK);
|
1994-05-27 12:33:43 +00:00
|
|
|
mvwprintw(wnd, i + 1, 0, "%-5s",
|
1997-07-06 04:37:22 +00:00
|
|
|
sw[i].sw_dev == NODEV ? "[NFS]" :
|
|
|
|
p == NULL ? "??" : p);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
showswap()
|
|
|
|
{
|
1997-01-12 19:38:32 +00:00
|
|
|
int col, row, div, i, j, k, avail, npfree, used, xsize, xfree;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
div = blocksize / 512;
|
|
|
|
avail = npfree = 0;
|
1997-01-12 19:38:32 +00:00
|
|
|
for (i = k = 0; i < nswdev; i++) {
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Don't report statistics for partitions which have not
|
|
|
|
* yet been activated via swapon(8).
|
|
|
|
*/
|
1997-01-12 19:38:32 +00:00
|
|
|
if (!sw[i].sw_freed)
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
1997-01-12 19:38:32 +00:00
|
|
|
|
|
|
|
col = 5;
|
|
|
|
mvwprintw(wnd, i + 1, col, "%*d", hlen, sw[i].sw_nblks / div);
|
|
|
|
col += hlen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The first dmmax is never allocated to avoid trashing of
|
|
|
|
* disklabels
|
|
|
|
*/
|
|
|
|
xsize = sw[i].sw_nblks - dmmax;
|
1994-05-27 12:33:43 +00:00
|
|
|
xfree = perdev[i];
|
|
|
|
used = xsize - xfree;
|
|
|
|
mvwprintw(wnd, i + 1, col, "%9d ", used / div);
|
|
|
|
for (j = (100 * used / xsize + 1) / 2; j > 0; j--)
|
|
|
|
waddch(wnd, 'X');
|
|
|
|
npfree++;
|
|
|
|
avail += xsize;
|
1997-01-12 19:38:32 +00:00
|
|
|
k++;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
/*
|
1994-05-27 12:33:43 +00:00
|
|
|
* If only one partition has been set up via swapon(8), we don't
|
|
|
|
* need to bother with totals.
|
|
|
|
*/
|
|
|
|
if (npfree > 1) {
|
|
|
|
used = avail - nfree;
|
1997-01-12 19:38:32 +00:00
|
|
|
mvwprintw(wnd, k + 1, 0, "%-5s%*d%9d ",
|
1994-05-27 12:33:43 +00:00
|
|
|
"Total", hlen, avail / div, used / div);
|
|
|
|
for (j = (100 * used / avail + 1) / 2; j > 0; j--)
|
|
|
|
waddch(wnd, 'X');
|
|
|
|
}
|
|
|
|
}
|