2003-10-13 18:22:20 +04:00
|
|
|
/* $NetBSD: swap.c,v 1.17 2003/10/13 14:22:20 agc Exp $ */
|
1995-01-20 11:51:48 +03:00
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
/*-
|
2003-10-13 18:22:20 +04:00
|
|
|
* Copyright (c) 1997 Matthew R. Green. All rights reserved.
|
1995-01-20 11:30:50 +03: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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. 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.
|
|
|
|
*/
|
|
|
|
|
1997-07-21 11:03:15 +04:00
|
|
|
#include <sys/cdefs.h>
|
1995-01-20 11:30:50 +03:00
|
|
|
#ifndef lint
|
1995-01-20 11:51:48 +03:00
|
|
|
#if 0
|
1995-09-01 02:11:37 +04:00
|
|
|
static char sccsid[] = "@(#)swap.c 8.3 (Berkeley) 4/29/95";
|
1995-01-20 11:51:48 +03:00
|
|
|
#endif
|
2003-10-13 18:22:20 +04:00
|
|
|
__RCSID("$NetBSD: swap.c,v 1.17 2003/10/13 14:22:20 agc Exp $");
|
1995-01-20 11:30:50 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2000-04-13 12:29:56 +04:00
|
|
|
#include <sys/swap.h>
|
1997-07-21 11:03:15 +04:00
|
|
|
|
2000-12-01 05:19:43 +03:00
|
|
|
#include <errno.h>
|
1995-01-20 11:30:50 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1995-04-29 09:54:42 +04:00
|
|
|
#include <string.h>
|
1995-01-20 11:30:50 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "systat.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
2000-07-05 15:03:20 +04:00
|
|
|
void showspace(char *header, int hlen, long blocksize);
|
1995-01-20 11:30:50 +03:00
|
|
|
|
1997-07-21 11:03:15 +04:00
|
|
|
static long blocksize;
|
|
|
|
static int hlen, nswap, rnswap;
|
|
|
|
static int first = 1;
|
|
|
|
static struct swapent *swap_devices;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
WINDOW *
|
2000-07-05 15:03:20 +04:00
|
|
|
openswap(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-07-21 11:03:15 +04:00
|
|
|
|
2003-02-24 13:10:00 +03:00
|
|
|
return (subwin(stdscr, -1, 0, 5, 0));
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
closeswap(WINDOW *w)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-07-21 11:03:15 +04:00
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
if (w == NULL)
|
|
|
|
return;
|
|
|
|
wclear(w);
|
|
|
|
wrefresh(w);
|
|
|
|
delwin(w);
|
|
|
|
}
|
|
|
|
|
1997-07-21 11:03:15 +04:00
|
|
|
/* do nothing */
|
|
|
|
int
|
2000-07-05 15:03:20 +04:00
|
|
|
initswap(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
fetchswap(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1997-07-21 11:03:15 +04:00
|
|
|
int update_label = 0;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
1997-07-21 11:03:15 +04:00
|
|
|
first = 0;
|
|
|
|
nswap = swapctl(SWAP_NSWAP, 0, 0);
|
|
|
|
if (nswap < 0)
|
|
|
|
error("error: %s", strerror(errno));
|
|
|
|
if (nswap == 0)
|
|
|
|
return;
|
|
|
|
update_label = (nswap != rnswap);
|
|
|
|
|
|
|
|
if (swap_devices)
|
|
|
|
(void)free(swap_devices);
|
1999-12-21 02:11:50 +03:00
|
|
|
if ((swap_devices = malloc(nswap * sizeof(*swap_devices))) == NULL) {
|
|
|
|
error("malloc failed");
|
|
|
|
die(0);
|
|
|
|
}
|
1997-07-21 11:03:15 +04:00
|
|
|
|
1999-12-21 02:11:50 +03:00
|
|
|
if ((rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap)) != nswap) {
|
|
|
|
error("swapctl failed");
|
|
|
|
die(0);
|
|
|
|
}
|
|
|
|
|
1997-07-21 11:03:15 +04:00
|
|
|
if (update_label)
|
|
|
|
labelswap();
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
labelswap(void)
|
1995-01-20 11:30:50 +03:00
|
|
|
{
|
1998-12-26 10:05:08 +03:00
|
|
|
char *header;
|
|
|
|
int row;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
row = 0;
|
1997-07-21 11:03:15 +04:00
|
|
|
wmove(wnd, row, 0);
|
|
|
|
wclrtobot(wnd);
|
|
|
|
if (first)
|
|
|
|
fetchswap();
|
|
|
|
if (nswap == 0) {
|
|
|
|
mvwprintw(wnd, row++, 0, "No swap");
|
|
|
|
return;
|
|
|
|
}
|
1995-01-20 11:30:50 +03:00
|
|
|
header = getbsize(&hlen, &blocksize);
|
|
|
|
mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s",
|
|
|
|
"Disk", hlen, header, "Used",
|
|
|
|
"/0% /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-05 15:03:20 +04:00
|
|
|
showswap(void)
|
|
|
|
{
|
2000-06-04 22:29:13 +04:00
|
|
|
int col, div, i, avail, used, xsize, free;
|
1997-07-21 11:03:15 +04:00
|
|
|
struct swapent *sep;
|
1998-12-26 10:05:08 +03:00
|
|
|
char *p;
|
1995-01-20 11:30:50 +03:00
|
|
|
|
|
|
|
div = blocksize / 512;
|
1997-07-21 11:03:15 +04:00
|
|
|
free = avail = 0;
|
|
|
|
for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
|
|
|
|
if (sep == NULL)
|
|
|
|
continue;
|
1998-12-26 10:05:08 +03:00
|
|
|
|
|
|
|
p = strrchr(sep->se_path, '/');
|
|
|
|
p = p ? p+1 : sep->se_path;
|
|
|
|
|
|
|
|
mvwprintw(wnd, i + 1, 0, "%-5s", p);
|
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
col = 5;
|
1997-07-21 11:03:15 +04:00
|
|
|
mvwprintw(wnd, i + 1, col, "%*d", hlen, sep->se_nblks / div);
|
|
|
|
|
1995-01-20 11:30:50 +03:00
|
|
|
col += hlen;
|
1997-07-21 11:03:15 +04:00
|
|
|
xsize = sep->se_nblks;
|
|
|
|
used = sep->se_inuse;
|
|
|
|
avail += xsize;
|
|
|
|
free += xsize - used;
|
1995-01-20 11:30:50 +03:00
|
|
|
mvwprintw(wnd, i + 1, col, "%9d ", used / div);
|
1997-04-03 10:07:40 +04:00
|
|
|
wclrtoeol(wnd);
|
2000-06-04 22:29:13 +04:00
|
|
|
whline(wnd, 'X', (100 * used / xsize + 1) / 2);
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
1997-07-21 11:03:15 +04:00
|
|
|
/* do total if necessary */
|
|
|
|
if (nswap > 1) {
|
|
|
|
used = avail - free;
|
1995-01-20 11:30:50 +03:00
|
|
|
mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d ",
|
|
|
|
"Total", hlen, avail / div, used / div);
|
1997-04-03 10:07:40 +04:00
|
|
|
wclrtoeol(wnd);
|
2000-06-04 22:29:13 +04:00
|
|
|
whline(wnd, 'X', (100 * used / avail + 1) / 2);
|
1995-01-20 11:30:50 +03:00
|
|
|
}
|
|
|
|
}
|