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