1 /* $NetBSD: swaplist.c,v 1.12 2003/12/20 13:31:43 mrg 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 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 #include <sys/cdefs.h> 31 32 #ifndef lint 33 __RCSID("$NetBSD: swaplist.c,v 1.12 2003/12/20 13:31:43 mrg Exp $"); 34 #endif 35 36 37 #include <sys/param.h> 38 #include <sys/stat.h> 39 #include <sys/swap.h> 40 41 #include <unistd.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <inttypes.h> 48 #include <util.h> 49 50 #define dbtoqb(b) dbtob((int64_t)(b)) 51 52 /* 53 * NOTE: This file is separate from swapctl.c so that pstat can grab it. 54 */ 55 56 #include "swapctl.h" 57 58 void 59 list_swap(pri, kflag, pflag, tflag, dolong, hflag) 60 int pri; 61 int kflag; 62 int pflag; 63 int tflag; 64 int dolong; 65 int hflag; 66 { 67 struct swapent *sep, *fsep; 68 long blocksize; 69 char *header, *suff; 70 char szbuf[5], usbuf[5], avbuf[5]; /* size, used, avail */ 71 size_t l; 72 int hlen, totalsize, size, totalinuse, inuse, ncounted, pathmax; 73 int rnswap, nswap = swapctl(SWAP_NSWAP, 0, 0), i; 74 75 if (nswap < 1) { 76 puts("no swap devices configured"); 77 exit(0); 78 } 79 80 fsep = sep = (struct swapent *)malloc(nswap * sizeof(*sep)); 81 if (sep == NULL) 82 err(1, "malloc"); 83 rnswap = swapctl(SWAP_STATS, (void *)sep, nswap); 84 if (rnswap < 0) 85 err(1, "SWAP_STATS"); 86 if (nswap != rnswap) 87 warnx("SWAP_STATS different to SWAP_NSWAP (%d != %d)", 88 rnswap, nswap); 89 90 pathmax = 11; 91 switch(kflag) { 92 case 1: 93 header = "1K-blocks"; 94 blocksize = 1024; 95 suff = "KBytes"; 96 break; 97 case 2: 98 suff = "MBytes"; 99 header = "1M-blocks"; 100 blocksize = 1024 * 1024; 101 break; 102 case 3: 103 header = "1G-blocks"; 104 blocksize = 1024 * 1024 * 1024; 105 suff = "GBytes"; 106 break; 107 default: 108 suff = "blocks"; 109 if (!hflag) { 110 header = getbsize(&hlen, &blocksize); 111 } else { 112 header = "Size"; 113 blocksize = 1; suff = ""; /* unused */ 114 } 115 break; 116 } 117 if (hflag || kflag) 118 hlen = strlen(header); 119 120 if (dolong && tflag == 0) { 121 for (i = rnswap; i-- > 0; sep++) 122 if (pathmax < (l = strlen(sep->se_path))) 123 pathmax = l; 124 sep = fsep; 125 (void)printf("%-*s %*s %8s %8s %8s %s\n", 126 pathmax, "Device", hlen, header, 127 "Used", "Avail", "Capacity", "Priority"); 128 } 129 totalsize = totalinuse = ncounted = 0; 130 for (; rnswap-- > 0; sep++) { 131 if (pflag && sep->se_priority != pri) 132 continue; 133 ncounted++; 134 size = sep->se_nblks; 135 inuse = sep->se_inuse; 136 totalsize += size; 137 totalinuse += inuse; 138 139 if (dolong && tflag == 0) { 140 if (hflag == 0) { 141 (void)printf("%-*s %*ld ", pathmax, sep->se_path, hlen, 142 (long)(dbtoqb(size) / blocksize)); 143 144 (void)printf("%8ld %8ld %5.0f%% %d\n", 145 (long)(dbtoqb(inuse) / blocksize), 146 (long)(dbtoqb(size - inuse) / blocksize), 147 (double)inuse / (double)size * 100.0, 148 sep->se_priority); 149 } else { 150 if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(size)), 151 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 152 err(1, "humanize_number"); 153 if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(inuse)), 154 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 155 err(1, "humanize_number"); 156 if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(size-inuse)), 157 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 158 err(1, "humanize_number"); 159 (void)printf("%-*s %*s ", pathmax, sep->se_path, hlen, szbuf); 160 161 (void)printf("%8s %8s %5.0f%% %d\n", 162 usbuf, avbuf, 163 (double)inuse / (double)size * 100.0, 164 sep->se_priority); 165 } 166 } 167 } 168 if (tflag) { 169 if (hflag) { 170 if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)), 171 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 172 err(1, "humanize_number"); 173 if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)), 174 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 175 err(1, "humanize_number"); 176 (void)printf("%s/%s swap space\n", usbuf, szbuf); 177 } else { 178 (void)printf("%ld/%ld %s swap space\n", 179 (long)(dbtoqb(totalinuse) / blocksize), 180 (long)(dbtoqb(totalsize) / blocksize), 181 suff); 182 } 183 } 184 else if (dolong == 0) { 185 if (hflag) { 186 if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)), 187 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 188 err(1, "humanize_number"); 189 if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)), 190 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 191 err(1, "humanize_number"); 192 if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(totalsize-totalinuse)), 193 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 194 err(1, "humanize_number"); 195 (void)printf("total: %s allocated = %s used, %s available.\n", 196 szbuf, usbuf, avbuf); 197 } else { 198 printf("total: %ld %s allocated = %ld %s used, " 199 "%ld %s available\n", 200 (long)(dbtoqb(totalsize) / blocksize), suff, 201 (long)(dbtoqb(totalinuse) / blocksize), suff, 202 (long)(dbtoqb(totalsize - totalinuse) / blocksize), suff); 203 } 204 } else if (ncounted > 1) { 205 if (hflag) { 206 if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)), 207 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 208 err(1, "humanize_number"); 209 if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)), 210 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 211 err(1, "humanize_number"); 212 if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(totalsize-totalinuse)), 213 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 214 err(1, "humanize_number"); 215 (void)printf("%-*s %*s %8s %8s %5.0f%%\n", pathmax, "Total", 216 hlen, szbuf, usbuf, avbuf, 217 (double)(totalinuse) / (double)totalsize * 100.0); 218 } else { 219 (void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax, "Total", 220 hlen, 221 (long)(dbtoqb(totalsize) / blocksize), 222 (long)(dbtoqb(totalinuse) / blocksize), 223 (long)(dbtoqb(totalsize - totalinuse) / blocksize), 224 (double)(totalinuse) / (double)totalsize * 100.0); 225 } 226 } 227 if (fsep) 228 (void)free(fsep); 229 } 230