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