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 static char sccsid[] = "@(#)swap.c 8.2 (Berkeley) 2/21/94"; 36 #endif /* not lint */ 37 38 /* 39 * swapinfo - based on a program of the same name by Kevin Lahey 40 */ 41 42 #include <sys/param.h> 43 #include <sys/buf.h> 44 #include <sys/conf.h> 45 #include <sys/ioctl.h> 46 #include <sys/stat.h> 47 #include <sys/rlist.h> 48 49 #include <kvm.h> 50 #include <nlist.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <unistd.h> 54 55 #include "systat.h" 56 #include "extern.h" 57 58 extern char *devname __P((int, int)); 59 extern char *getbsize __P((int *headerlenp, long *blocksizep)); 60 void showspace __P((char *header, int hlen, long blocksize)); 61 62 kvm_t *kd; 63 64 struct nlist syms[] = { 65 { "_swaplist" },/* list of free swap areas */ 66 #define VM_SWAPLIST 0 67 { "_swdevt" }, /* list of swap devices and sizes */ 68 #define VM_SWDEVT 1 69 { "_nswap" }, /* size of largest swap device */ 70 #define VM_NSWAP 2 71 { "_nswdev" }, /* number of swap devices */ 72 #define VM_NSWDEV 3 73 { "_dmmax" }, /* maximum size of a swap block */ 74 #define VM_DMMAX 4 75 0 76 }; 77 78 static int nswap, nswdev, dmmax; 79 static struct swdevt *sw; 80 static long *perdev, blocksize; 81 static int nfree, hlen; 82 static struct rlisthdr swaplist; 83 84 #define SVAR(var) __STRING(var) /* to force expansion */ 85 #define KGET(idx, var) \ 86 KGET1(idx, &var, sizeof(var), SVAR(var), (0)) 87 #define KGET1(idx, p, s, msg, rv) \ 88 KGET2(syms[idx].n_value, p, s, msg, rv) 89 #define KGET2(addr, p, s, msg, rv) \ 90 if (kvm_read(kd, addr, p, s) != s) { \ 91 error("cannot read %s: %s", msg, kvm_geterr(kd)); \ 92 return rv; \ 93 } 94 95 WINDOW * 96 openswap() 97 { 98 return (subwin(stdscr, LINES-5-1, 0, 5, 0)); 99 } 100 101 void 102 closeswap(w) 103 WINDOW *w; 104 { 105 if (w == NULL) 106 return; 107 wclear(w); 108 wrefresh(w); 109 delwin(w); 110 } 111 112 /* 113 * The meat of all the swap stuff is stolen from pstat(8)'s 114 * swapmode(), which is based on a program called swapinfo written by 115 * Kevin Lahey <kml@rokkaku.atl.ga.us>. 116 */ 117 118 initswap() 119 { 120 int i; 121 char msgbuf[BUFSIZ]; 122 static int once = 0; 123 u_long ptr; 124 125 if (once) 126 return (1); 127 if (kvm_nlist(kd, syms)) { 128 strcpy(msgbuf, "systat: swap: cannot find"); 129 for (i = 0; syms[i].n_name != NULL; i++) { 130 if (syms[i].n_value == 0) { 131 strcat(msgbuf, " "); 132 strcat(msgbuf, syms[i].n_name); 133 } 134 } 135 error(msgbuf); 136 return (0); 137 } 138 KGET(VM_NSWAP, nswap); 139 KGET(VM_NSWDEV, nswdev); 140 KGET(VM_DMMAX, dmmax); 141 if ((sw = malloc(nswdev * sizeof(*sw))) == NULL || 142 (perdev = malloc(nswdev * sizeof(*perdev))) == NULL) 143 err(1, "malloc"); 144 KGET1(VM_SWDEVT, &ptr, sizeof ptr, "swdevt", (0)); 145 KGET2(ptr, sw, nswdev * sizeof(*sw), "*swdevt", (0)); 146 once = 1; 147 return (1); 148 } 149 150 void 151 fetchswap() 152 { 153 struct rlist head; 154 struct rlist *swapptr; 155 156 /* Count up swap space. */ 157 nfree = 0; 158 memset(perdev, 0, nswdev * sizeof(*perdev)); 159 KGET1(VM_SWAPLIST, &swaplist, sizeof swaplist, "swaplist", /* none */); 160 swapptr = swaplist.rlh_list; 161 while (swapptr) { 162 int top, bottom, next_block; 163 164 KGET2((unsigned long)swapptr, &head, 165 sizeof(struct rlist), "swapptr", /* none */); 166 167 top = head.rl_end; 168 bottom = head.rl_start; 169 170 nfree += top - bottom + 1; 171 172 /* 173 * Swap space is split up among the configured disks. 174 * 175 * For interleaved swap devices, the first dmmax blocks 176 * of swap space some from the first disk, the next dmmax 177 * blocks from the next, and so on up to nswap blocks. 178 * 179 * The list of free space joins adjacent free blocks, 180 * ignoring device boundries. If we want to keep track 181 * of this information per device, we'll just have to 182 * extract it ourselves. 183 */ 184 while (top / dmmax != bottom / dmmax) { 185 next_block = ((bottom + dmmax) / dmmax); 186 perdev[(bottom / dmmax) % nswdev] += 187 next_block * dmmax - bottom; 188 bottom = next_block * dmmax; 189 } 190 perdev[(bottom / dmmax) % nswdev] += 191 top - bottom + 1; 192 193 swapptr = head.rl_next; 194 } 195 196 } 197 198 void 199 labelswap() 200 { 201 char *header; 202 int row, i; 203 204 row = 0; 205 wmove(wnd, row, 0); wclrtobot(wnd); 206 header = getbsize(&hlen, &blocksize); 207 mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s", 208 "Disk", hlen, header, "Used", 209 "/0% /10% /20% /30% /40% /50% /60% /70% /80% /90% /100"); 210 for (i = 0; i < nswdev; i++) { 211 if (!sw[i].sw_freed) 212 continue; 213 mvwprintw(wnd, i + 1, 0, "%-5s", 214 sw[i].sw_dev != NODEV? 215 devname(sw[i].sw_dev, S_IFBLK): "[NFS]"); 216 } 217 } 218 219 void 220 showswap() 221 { 222 int col, row, div, i, j, k, avail, npfree, used, xsize, xfree; 223 224 div = blocksize / 512; 225 avail = npfree = 0; 226 for (i = k = 0; i < nswdev; i++) { 227 /* 228 * Don't report statistics for partitions which have not 229 * yet been activated via swapon(8). 230 */ 231 if (!sw[i].sw_freed) 232 continue; 233 234 col = 5; 235 mvwprintw(wnd, i + 1, col, "%*d", hlen, sw[i].sw_nblks / div); 236 col += hlen; 237 238 /* 239 * The first dmmax is never allocated to avoid trashing of 240 * disklabels 241 */ 242 xsize = sw[i].sw_nblks - dmmax; 243 xfree = perdev[i]; 244 used = xsize - xfree; 245 mvwprintw(wnd, i + 1, col, "%9d ", used / div); 246 for (j = (100 * used / xsize + 1) / 2; j > 0; j--) 247 waddch(wnd, 'X'); 248 npfree++; 249 avail += xsize; 250 k++; 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, k + 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