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