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