1 /* $OpenBSD: swap.c,v 1.23 2011/03/02 06:48:17 jasper Exp $ */ 2 /* $NetBSD: swap.c,v 1.9 1998/12/26 07:05:08 marc Exp $ */ 3 4 /*- 5 * Copyright (c) 1997 Matthew R. Green. All rights reserved. 6 * Copyright (c) 1980, 1992, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. 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 #include <sys/cdefs.h> 35 #include <sys/param.h> 36 #include <sys/buf.h> 37 #include <sys/conf.h> 38 #include <sys/ioctl.h> 39 #include <sys/stat.h> 40 #include <sys/swap.h> 41 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <errno.h> 46 #include <unistd.h> 47 48 #include "systat.h" 49 50 51 static long blocksize; 52 static int hlen, nswap, rnswap; 53 static struct swapent *swap_devices; 54 55 void print_sw(void); 56 int read_sw(void); 57 int select_sw(void); 58 static void showswap(int i); 59 static void showtotal(void); 60 61 62 field_def fields_sw[] = { 63 {"DISK", 6, 16, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0}, 64 {"BLOCKS", 5, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, 65 {"USED", 5, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, 66 {"", 40, 80, 1, FLD_ALIGN_BAR, -1, 0, 0, 100}, 67 }; 68 69 #define FLD_SW_NAME FIELD_ADDR(fields_sw,0) 70 #define FLD_SW_BLOCKS FIELD_ADDR(fields_sw,1) 71 #define FLD_SW_USED FIELD_ADDR(fields_sw,2) 72 #define FLD_SW_BAR FIELD_ADDR(fields_sw,3) 73 74 /* Define views */ 75 field_def *view_sw_0[] = { 76 FLD_SW_NAME, FLD_SW_BLOCKS, FLD_SW_USED, FLD_SW_BAR, NULL 77 }; 78 79 80 /* Define view managers */ 81 struct view_manager swap_mgr = { 82 "Swap", select_sw, read_sw, NULL, print_header, 83 print_sw, keyboard_callback, NULL, NULL 84 }; 85 86 field_view views_sw[] = { 87 {view_sw_0, "swap", '6', &swap_mgr}, 88 {NULL, NULL, 0, NULL} 89 }; 90 91 92 int 93 select_sw(void) 94 { 95 if (swap_devices == NULL || nswap == 0) 96 num_disp = 1; 97 else 98 num_disp = nswap; 99 if (nswap > 1) 100 num_disp++; 101 return (0); 102 } 103 104 int 105 read_sw(void) 106 { 107 num_disp = 1; 108 109 nswap = swapctl(SWAP_NSWAP, 0, 0); 110 111 if (nswap < 0) 112 error("error: %s", strerror(errno)); 113 if (nswap == 0) 114 return 0; 115 116 if (swap_devices) 117 (void)free(swap_devices); 118 119 swap_devices = (struct swapent *)calloc(nswap, sizeof(*swap_devices)); 120 if (swap_devices == NULL) 121 return 0; 122 123 rnswap = swapctl(SWAP_STATS, (void *)swap_devices, nswap); 124 if (rnswap < 0 || nswap != rnswap) 125 return 0; 126 127 num_disp = nswap; 128 if (nswap > 1) 129 num_disp++; 130 131 return 0; 132 } 133 134 135 void 136 print_sw(void) 137 { 138 int n, count = 0; 139 140 if (swap_devices == NULL || nswap == 0) { 141 print_fld_str(FLD_SW_BAR, "No swap devices"); 142 return; 143 } 144 145 146 for (n = dispstart; n < num_disp; n++) { 147 if (n >= nswap) 148 showtotal(); 149 else 150 showswap(n); 151 count++; 152 if (maxprint > 0 && count >= maxprint) 153 break; 154 } 155 156 } 157 158 int 159 initswap(void) 160 { 161 field_view *v; 162 163 char *bs = getbsize(&hlen, &blocksize); 164 165 FLD_SW_BLOCKS->title = strdup(bs); 166 167 for (v = views_sw; v->name != NULL; v++) 168 add_view(v); 169 170 return(1); 171 } 172 173 174 static void 175 showswap(int i) 176 { 177 int d, used, xsize; 178 struct swapent *sep; 179 char *p; 180 181 d = blocksize / 512; 182 183 sep = &swap_devices[i]; 184 185 p = strrchr(sep->se_path, '/'); 186 p = p ? p+1 : sep->se_path; 187 188 print_fld_str(FLD_SW_NAME, p); 189 190 xsize = sep->se_nblks; 191 used = sep->se_inuse; 192 193 print_fld_uint(FLD_SW_BLOCKS, xsize / d); 194 print_fld_uint(FLD_SW_USED, used / d); 195 print_fld_bar(FLD_SW_BAR, 100 * used / xsize); 196 197 end_line(); 198 } 199 200 static void 201 showtotal(void) 202 { 203 struct swapent *sep; 204 int d, i, avail, used, xsize, mfree; 205 206 d = blocksize / 512; 207 mfree = avail = 0; 208 209 for (sep = swap_devices, i = 0; i < nswap; i++, sep++) { 210 xsize = sep->se_nblks; 211 used = sep->se_inuse; 212 avail += xsize; 213 mfree += xsize - used; 214 } 215 used = avail - mfree; 216 217 print_fld_str(FLD_SW_NAME, "Total"); 218 print_fld_uint(FLD_SW_BLOCKS, avail / d); 219 print_fld_uint(FLD_SW_USED, used / d); 220 print_fld_bar(FLD_SW_BAR, 100 * used / avail); 221 222 end_line(); 223 } 224