Home | History | Annotate | Line # | Download | only in systat
swap.c revision 1.3
      1 /*	$NetBSD: swap.c,v 1.3 1995/04/29 05:54:53 cgd 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)swap.c	8.2 (Berkeley) 2/21/94";
     39 #endif
     40 static char rcsid[] = "$NetBSD: swap.c,v 1.3 1995/04/29 05:54:53 cgd Exp $";
     41 #endif /* not lint */
     42 
     43 /*
     44  * swapinfo - based on a program of the same name by Kevin Lahey
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/buf.h>
     49 #include <sys/conf.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/map.h>
     52 #include <sys/stat.h>
     53 
     54 #include <kvm.h>
     55 #include <nlist.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 
     61 #include "systat.h"
     62 #include "extern.h"
     63 
     64 extern char *devname __P((int, int));
     65 extern char *getbsize __P((int *headerlenp, long *blocksizep));
     66 void showspace __P((char *header, int hlen, long blocksize));
     67 
     68 kvm_t	*kd;
     69 
     70 struct nlist syms[] = {
     71 	{ "_swapmap" },	/* list of free swap areas */
     72 #define VM_SWAPMAP	0
     73 	{ "_nswapmap" },/* size of the swap map */
     74 #define VM_NSWAPMAP	1
     75 	{ "_swdevt" },	/* list of swap devices and sizes */
     76 #define VM_SWDEVT	2
     77 	{ "_nswap" },	/* size of largest swap device */
     78 #define VM_NSWAP	3
     79 	{ "_nswdev" },	/* number of swap devices */
     80 #define VM_NSWDEV	4
     81 	{ "_dmmax" },	/* maximum size of a swap block */
     82 #define VM_DMMAX	5
     83 	0
     84 };
     85 
     86 static int nswap, nswdev, dmmax, nswapmap;
     87 static struct swdevt *sw;
     88 static long *perdev, blocksize;
     89 static struct map *swapmap, *kswapmap;
     90 static struct mapent *mp;
     91 static int nfree, hlen;
     92 
     93 #define	SVAR(var) __STRING(var)	/* to force expansion */
     94 #define	KGET(idx, var) \
     95 	KGET1(idx, &var, sizeof(var), SVAR(var))
     96 #define	KGET1(idx, p, s, msg) \
     97 	KGET2(syms[idx].n_value, p, s, msg)
     98 #define	KGET2(addr, p, s, msg) \
     99 	if (kvm_read(kd, addr, p, s) != s) { \
    100 		error("cannot read %s: %s", msg, kvm_geterr(kd)); \
    101 		return (0); \
    102 	}
    103 
    104 WINDOW *
    105 openswap()
    106 {
    107 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
    108 }
    109 
    110 void
    111 closeswap(w)
    112 	WINDOW *w;
    113 {
    114 	if (w == NULL)
    115 		return;
    116 	wclear(w);
    117 	wrefresh(w);
    118 	delwin(w);
    119 }
    120 
    121 initswap()
    122 {
    123 	int i;
    124 	char msgbuf[BUFSIZ];
    125 	static int once = 0;
    126 
    127 	if (once)
    128 		return (1);
    129 	if (kvm_nlist(kd, syms)) {
    130 		strcpy(msgbuf, "systat: swap: cannot find");
    131 		for (i = 0; syms[i].n_name != NULL; i++) {
    132 			if (syms[i].n_value == 0) {
    133 				strcat(msgbuf, " ");
    134 				strcat(msgbuf, syms[i].n_name);
    135 			}
    136 		}
    137 		error(msgbuf);
    138 		return (0);
    139 	}
    140 	KGET(VM_NSWAP, nswap);
    141 	KGET(VM_NSWDEV, nswdev);
    142 	KGET(VM_DMMAX, dmmax);
    143 	KGET(VM_NSWAPMAP, nswapmap);
    144 	KGET(VM_SWAPMAP, kswapmap);	/* kernel `swapmap' is a pointer */
    145 	if ((sw = malloc(nswdev * sizeof(*sw))) == NULL ||
    146 	    (perdev = malloc(nswdev * sizeof(*perdev))) == NULL ||
    147 	    (mp = malloc(nswapmap * sizeof(*mp))) == NULL) {
    148 		error("swap malloc");
    149 		return (0);
    150 	}
    151 	KGET1(VM_SWDEVT, sw, nswdev * sizeof(*sw), "swdevt");
    152 	once = 1;
    153 	return (1);
    154 }
    155 
    156 void
    157 fetchswap()
    158 {
    159 	int s, e, i;
    160 
    161 	s = nswapmap * sizeof(*mp);
    162 	if (kvm_read(kd, (long)kswapmap, mp, s) != s)
    163 		error("cannot read swapmap: %s", kvm_geterr(kd));
    164 
    165 	/* first entry in map is `struct map'; rest are mapent's */
    166 	swapmap = (struct map *)mp;
    167 	if (nswapmap != swapmap->m_limit - (struct mapent *)kswapmap)
    168 		error("panic: swap: nswapmap goof");
    169 
    170 	/*
    171 	 * Count up swap space.
    172 	 */
    173 	nfree = 0;
    174 	bzero(perdev, nswdev * sizeof(*perdev));
    175 	for (mp++; mp->m_addr != 0; mp++) {
    176 		s = mp->m_addr;			/* start of swap region */
    177 		e = mp->m_addr + mp->m_size;	/* end of region */
    178 		nfree += mp->m_size;
    179 
    180 		/*
    181 		 * Swap space is split up among the configured disks.
    182 		 * The first dmmax blocks of swap space some from the
    183 		 * first disk, the next dmmax blocks from the next,
    184 		 * and so on.  The list of free space joins adjacent
    185 		 * free blocks, ignoring device boundries.  If we want
    186 		 * to keep track of this information per device, we'll
    187 		 * just have to extract it ourselves.
    188 		 */
    189 
    190 		/* calculate first device on which this falls */
    191 		i = (s / dmmax) % nswdev;
    192 		while (s < e) {		/* XXX this is inefficient */
    193 			int bound = roundup(s + 1, dmmax);
    194 
    195 			if (bound > e)
    196 				bound = e;
    197 			perdev[i] += bound - s;
    198 			if (++i >= nswdev)
    199 				i = 0;
    200 			s = bound;
    201 		}
    202 	}
    203 }
    204 
    205 void
    206 labelswap()
    207 {
    208 	char *header;
    209 	int row, i;
    210 
    211 	row = 0;
    212 	wmove(wnd, row, 0); wclrtobot(wnd);
    213 	header = getbsize(&hlen, &blocksize);
    214 	mvwprintw(wnd, row++, 0, "%-5s%*s%9s  %55s",
    215 	    "Disk", hlen, header, "Used",
    216 	    "/0%  /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%");
    217 	for (i = 0; i < nswdev; i++) {
    218 		mvwprintw(wnd, i + 1, 0, "%-5s",
    219 		    devname(sw[i].sw_dev, S_IFBLK));
    220 	}
    221 }
    222 
    223 void
    224 showswap()
    225 {
    226 	int col, row, div, i, j, avail, npfree, used, xsize, xfree;
    227 
    228 	div = blocksize / 512;
    229 	avail = npfree = 0;
    230 	for (i = 0; i < nswdev; i++) {
    231 		col = 5;
    232 		mvwprintw(wnd, i + 1, col, "%*d", hlen, sw[i].sw_nblks / div);
    233 		col += hlen;
    234 		/*
    235 		 * Don't report statistics for partitions which have not
    236 		 * yet been activated via swapon(8).
    237 		 */
    238 		if (!sw[i].sw_freed) {
    239 			mvwprintw(wnd, i + 1, col + 8,
    240 			    "0  *** not available for swapping ***");
    241 			continue;
    242 		}
    243 		xsize = sw[i].sw_nblks;
    244 		xfree = perdev[i];
    245 		used = xsize - xfree;
    246 		mvwprintw(wnd, i + 1, col, "%9d  ", used / div);
    247 		for (j = (100 * used / xsize + 1) / 2; j > 0; j--)
    248 			waddch(wnd, 'X');
    249 		npfree++;
    250 		avail += xsize;
    251 	}
    252 	/*
    253 	 * If only one partition has been set up via swapon(8), we don't
    254 	 * need to bother with totals.
    255 	 */
    256 	if (npfree > 1) {
    257 		used = avail - nfree;
    258 		mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d  ",
    259 		    "Total", hlen, avail / div, used / div);
    260 		for (j = (100 * used / avail + 1) / 2; j > 0; j--)
    261 			waddch(wnd, 'X');
    262 	}
    263 }
    264