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