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