Home | History | Annotate | Line # | Download | only in systat
swap.c revision 1.11
      1 /*	$NetBSD: swap.c,v 1.11 2000/04/13 08:29:56 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 Matthew R. Green.  All rights reserved.
      5  * Copyright (c) 1980, 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)swap.c	8.3 (Berkeley) 4/29/95";
     41 #endif
     42 __RCSID("$NetBSD: swap.c,v 1.11 2000/04/13 08:29:56 mrg Exp $");
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/buf.h>
     47 #include <sys/conf.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/stat.h>
     50 #include <sys/swap.h>
     51 
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <errno.h>
     56 #include <unistd.h>
     57 
     58 #include "systat.h"
     59 #include "extern.h"
     60 
     61 void showspace __P((char *header, int hlen, long blocksize));
     62 
     63 static	long blocksize;
     64 static	int hlen, nswap, rnswap;
     65 static	int first = 1;
     66 static	struct swapent *swap_devices;
     67 
     68 WINDOW *
     69 openswap()
     70 {
     71 
     72 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
     73 }
     74 
     75 void
     76 closeswap(w)
     77 	WINDOW *w;
     78 {
     79 
     80 	if (w == NULL)
     81 		return;
     82 	wclear(w);
     83 	wrefresh(w);
     84 	delwin(w);
     85 }
     86 
     87 /* do nothing */
     88 int
     89 initswap()
     90 {
     91 
     92 	return (1);
     93 }
     94 
     95 void
     96 fetchswap()
     97 {
     98 	int	update_label = 0;
     99 
    100 	first = 0;
    101 	nswap = swapctl(SWAP_NSWAP, 0, 0);
    102 	if (nswap < 0)
    103 		error("error: %s", strerror(errno));
    104 	if (nswap == 0)
    105 		return;
    106 	update_label = (nswap != rnswap);
    107 
    108 	if (swap_devices)
    109 		(void)free(swap_devices);
    110 	if ((swap_devices = malloc(nswap * sizeof(*swap_devices))) == NULL) {
    111 		error("malloc failed");
    112 		die(0);
    113 	}
    114 
    115 	if ((rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap)) != nswap) {
    116 		error("swapctl failed");
    117 		die(0);
    118 	}
    119 
    120 	if (update_label)
    121 		labelswap();
    122 }
    123 
    124 void
    125 labelswap()
    126 {
    127 	char	*header;
    128 	int	row;
    129 
    130 	row = 0;
    131 	wmove(wnd, row, 0);
    132 	wclrtobot(wnd);
    133 	if (first)
    134 		fetchswap();
    135 	if (nswap == 0) {
    136 		mvwprintw(wnd, row++, 0, "No swap");
    137 		return;
    138 	}
    139 	header = getbsize(&hlen, &blocksize);
    140 	mvwprintw(wnd, row++, 0, "%-5s%*s%9s  %55s",
    141 	    "Disk", hlen, header, "Used",
    142 	    "/0%  /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
    143 }
    144 
    145 void
    146 showswap() {
    147 	int	col, div, i, j, avail, used, xsize, free;
    148 	struct	swapent *sep;
    149 	char	*p;
    150 
    151 	div = blocksize / 512;
    152 	free = avail = 0;
    153 	for (sep = swap_devices, i = 0; i < nswap; i++, sep++) {
    154 		if (sep == NULL)
    155 			continue;
    156 
    157 		p = strrchr(sep->se_path, '/');
    158 		p = p ? p+1 : sep->se_path;
    159 
    160 		mvwprintw(wnd, i + 1, 0, "%-5s", p);
    161 
    162 		col = 5;
    163 		mvwprintw(wnd, i + 1, col, "%*d", hlen, sep->se_nblks / div);
    164 
    165 		col += hlen;
    166 		xsize = sep->se_nblks;
    167 		used = sep->se_inuse;
    168 		avail += xsize;
    169 		free += xsize - used;
    170 		mvwprintw(wnd, i + 1, col, "%9d  ", used / div);
    171 		for (j = (100 * used / xsize + 1) / 2; j > 0; j--)
    172 			waddch(wnd, 'X');
    173 		wclrtoeol(wnd);
    174 	}
    175 	/* do total if necessary */
    176 	if (nswap > 1) {
    177 		used = avail - free;
    178 		mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d  ",
    179 		    "Total", hlen, avail / div, used / div);
    180 		for (j = (100 * used / avail + 1) / 2; j > 0; j--)
    181 			waddch(wnd, 'X');
    182 		wclrtoeol(wnd);
    183 	}
    184 }
    185