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