1 /* $NetBSD: swaplist.c,v 1.19 2023/12/11 12:47:24 mlelstv 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 #include <sys/cdefs.h> 29 30 #ifndef lint 31 __RCSID("$NetBSD: swaplist.c,v 1.19 2023/12/11 12:47:24 mlelstv Exp $"); 32 #endif 33 34 35 #include <sys/param.h> 36 #include <sys/stat.h> 37 #include <sys/swap.h> 38 39 #include <unistd.h> 40 #include <err.h> 41 #include <errno.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <inttypes.h> 46 #include <util.h> 47 48 #define dbtoqb(b) dbtob((int64_t)(b)) 49 50 /* 51 * NOTE: This file is separate from swapctl.c so that pstat can grab it. 52 */ 53 54 #include "swapctl.h" 55 56 int 57 list_swap(int pri, int kflag, int pflag, int tflag, int dolong, int hflag) 58 { 59 struct swapent *sep, *fsep; 60 long blocksize; 61 char szbuf[5], usbuf[5], avbuf[5]; /* size, used, avail */ 62 const char *header, *suff; 63 size_t l; 64 int hlen, size, inuse, ncounted, pathmax; 65 int rnswap, nswap = swapctl(SWAP_NSWAP, 0, 0), i; 66 int64_t totalsize, totalinuse; 67 68 if (nswap < 1) { 69 puts("no swap devices configured"); 70 return 0; 71 } 72 73 fsep = sep = (struct swapent *)malloc(nswap * sizeof(*sep)); 74 if (sep == NULL) 75 err(1, "malloc"); 76 rnswap = swapctl(SWAP_STATS, (void *)sep, nswap); 77 if (rnswap < 0) 78 err(1, "SWAP_STATS"); 79 if (nswap != rnswap) 80 warnx("SWAP_STATS different to SWAP_NSWAP (%d != %d)", 81 rnswap, nswap); 82 83 pathmax = 11; 84 switch(kflag) { 85 case 1: 86 header = "1K-blocks"; 87 blocksize = 1024; 88 suff = "KBytes"; 89 break; 90 case 2: 91 suff = "MBytes"; 92 header = "1M-blocks"; 93 blocksize = 1024 * 1024; 94 break; 95 case 3: 96 header = "1G-blocks"; 97 blocksize = 1024 * 1024 * 1024; 98 suff = "GBytes"; 99 break; 100 default: 101 suff = "blocks"; 102 if (!hflag) { 103 header = getbsize(&hlen, &blocksize); 104 } else { 105 header = "Size"; 106 blocksize = 1; suff = ""; /* unused */ 107 } 108 break; 109 } 110 if (hflag || kflag) 111 hlen = strlen(header); 112 113 if (dolong && tflag == 0) { 114 for (i = rnswap; i-- > 0; sep++) 115 if ((size_t)pathmax < (l = strlen(sep->se_path))) 116 pathmax = l; 117 sep = fsep; 118 (void)printf("%-*s %*s %8s %8s %8s %s\n", 119 pathmax, "Device", hlen, header, 120 "Used", "Avail", "Capacity", "Priority"); 121 } 122 totalsize = totalinuse = ncounted = 0; 123 for (; rnswap-- > 0; sep++) { 124 if (pflag && sep->se_priority != pri) 125 continue; 126 ncounted++; 127 size = sep->se_nblks; 128 inuse = sep->se_inuse; 129 totalsize += size; 130 totalinuse += inuse; 131 132 if (dolong && tflag == 0) { 133 if (hflag == 0) { 134 (void)printf("%-*s %*ld ", pathmax, sep->se_path, hlen, 135 (long)(dbtoqb(size) / blocksize)); 136 137 (void)printf("%8ld %8ld %5.0f%% %d\n", 138 (long)(dbtoqb(inuse) / blocksize), 139 (long)(dbtoqb(size - inuse) / blocksize), 140 (double)inuse / (double)size * 100.0, 141 sep->se_priority); 142 } else { 143 if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(size)), 144 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 145 err(1, "humanize_number"); 146 if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(inuse)), 147 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 148 err(1, "humanize_number"); 149 if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(size-inuse)), 150 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 151 err(1, "humanize_number"); 152 (void)printf("%-*s %*s ", pathmax, sep->se_path, hlen, szbuf); 153 154 (void)printf("%8s %8s %5.0f%% %d\n", 155 usbuf, avbuf, 156 (double)inuse / (double)size * 100.0, 157 sep->se_priority); 158 } 159 } 160 } 161 if (tflag) { 162 if (hflag) { 163 if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)), 164 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 165 err(1, "humanize_number"); 166 if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)), 167 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 168 err(1, "humanize_number"); 169 (void)printf("%s/%s swap space\n", usbuf, szbuf); 170 } else { 171 (void)printf("%ld/%ld %s swap space\n", 172 (long)(dbtoqb(totalinuse) / blocksize), 173 (long)(dbtoqb(totalsize) / blocksize), 174 suff); 175 } 176 } 177 else if (dolong == 0) { 178 if (hflag) { 179 if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)), 180 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 181 err(1, "humanize_number"); 182 if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)), 183 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 184 err(1, "humanize_number"); 185 if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(totalsize-totalinuse)), 186 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 187 err(1, "humanize_number"); 188 (void)printf("total: %s allocated, %s used, %s available.\n", 189 szbuf, usbuf, avbuf); 190 } else { 191 printf("total: %ld %s allocated, %ld %s used, " 192 "%ld %s available\n", 193 (long)(dbtoqb(totalsize) / blocksize), suff, 194 (long)(dbtoqb(totalinuse) / blocksize), suff, 195 (long)(dbtoqb(totalsize - totalinuse) / blocksize), suff); 196 } 197 } else if (ncounted > 1) { 198 if (hflag) { 199 if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)), 200 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 201 err(1, "humanize_number"); 202 if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)), 203 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 204 err(1, "humanize_number"); 205 if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(totalsize-totalinuse)), 206 "", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 207 err(1, "humanize_number"); 208 (void)printf("%-*s %*s %8s %8s %5.0f%%\n", pathmax, "Total", 209 hlen, szbuf, usbuf, avbuf, 210 (double)(totalinuse) / (double)totalsize * 100.0); 211 } else { 212 (void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax, "Total", 213 hlen, 214 (long)(dbtoqb(totalsize) / blocksize), 215 (long)(dbtoqb(totalinuse) / blocksize), 216 (long)(dbtoqb(totalsize - totalinuse) / blocksize), 217 (double)(totalinuse) / (double)totalsize * 100.0); 218 } 219 } 220 if (fsep) 221 (void)free(fsep); 222 223 return 1; 224 } 225