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