1 /* $NetBSD: swap.c,v 1.14 2000/12/01 02:19:44 simonb 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.14 2000/12/01 02:19:44 simonb Exp $"); 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/swap.h> 47 48 #include <errno.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 #include "systat.h" 55 #include "extern.h" 56 57 void showspace(char *header, int hlen, long blocksize); 58 59 static long blocksize; 60 static int hlen, nswap, rnswap; 61 static int first = 1; 62 static struct swapent *swap_devices; 63 64 WINDOW * 65 openswap(void) 66 { 67 68 return (subwin(stdscr, LINES-5-1, 0, 5, 0)); 69 } 70 71 void 72 closeswap(WINDOW *w) 73 { 74 75 if (w == NULL) 76 return; 77 wclear(w); 78 wrefresh(w); 79 delwin(w); 80 } 81 82 /* do nothing */ 83 int 84 initswap(void) 85 { 86 87 return (1); 88 } 89 90 void 91 fetchswap(void) 92 { 93 int update_label = 0; 94 95 first = 0; 96 nswap = swapctl(SWAP_NSWAP, 0, 0); 97 if (nswap < 0) 98 error("error: %s", strerror(errno)); 99 if (nswap == 0) 100 return; 101 update_label = (nswap != rnswap); 102 103 if (swap_devices) 104 (void)free(swap_devices); 105 if ((swap_devices = malloc(nswap * sizeof(*swap_devices))) == NULL) { 106 error("malloc failed"); 107 die(0); 108 } 109 110 if ((rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap)) != nswap) { 111 error("swapctl failed"); 112 die(0); 113 } 114 115 if (update_label) 116 labelswap(); 117 } 118 119 void 120 labelswap(void) 121 { 122 char *header; 123 int row; 124 125 row = 0; 126 wmove(wnd, row, 0); 127 wclrtobot(wnd); 128 if (first) 129 fetchswap(); 130 if (nswap == 0) { 131 mvwprintw(wnd, row++, 0, "No swap"); 132 return; 133 } 134 header = getbsize(&hlen, &blocksize); 135 mvwprintw(wnd, row++, 0, "%-5s%*s%9s %55s", 136 "Disk", hlen, header, "Used", 137 "/0% /10% /20% /30% /40% /50% /60% /70% /80% /90% /100%"); 138 } 139 140 void 141 showswap(void) 142 { 143 int col, div, i, avail, used, xsize, free; 144 struct swapent *sep; 145 char *p; 146 147 div = blocksize / 512; 148 free = avail = 0; 149 for (sep = swap_devices, i = 0; i < nswap; i++, sep++) { 150 if (sep == NULL) 151 continue; 152 153 p = strrchr(sep->se_path, '/'); 154 p = p ? p+1 : sep->se_path; 155 156 mvwprintw(wnd, i + 1, 0, "%-5s", p); 157 158 col = 5; 159 mvwprintw(wnd, i + 1, col, "%*d", hlen, sep->se_nblks / div); 160 161 col += hlen; 162 xsize = sep->se_nblks; 163 used = sep->se_inuse; 164 avail += xsize; 165 free += xsize - used; 166 mvwprintw(wnd, i + 1, col, "%9d ", used / div); 167 wclrtoeol(wnd); 168 whline(wnd, 'X', (100 * used / xsize + 1) / 2); 169 } 170 /* do total if necessary */ 171 if (nswap > 1) { 172 used = avail - free; 173 mvwprintw(wnd, i + 1, 0, "%-5s%*d%9d ", 174 "Total", hlen, avail / div, used / div); 175 wclrtoeol(wnd); 176 whline(wnd, 'X', (100 * used / avail + 1) / 2); 177 } 178 } 179