1 /* $NetBSD: swaplist.c,v 1.10 2003/06/23 11:53:45 agc 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.10 2003/06/23 11:53:45 agc 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 49 #define dbtoqb(b) dbtob((int64_t)(b)) 50 51 /* 52 * NOTE: This file is separate from swapctl.c so that pstat can grab it. 53 */ 54 55 #include "swapctl.h" 56 57 void 58 list_swap(pri, kflag, pflag, tflag, dolong) 59 int pri; 60 int kflag; 61 int pflag; 62 int tflag; 63 int dolong; 64 { 65 struct swapent *sep, *fsep; 66 long blocksize; 67 char *header; 68 size_t l; 69 int hlen, totalsize, size, totalinuse, inuse, ncounted, pathmax; 70 int rnswap, nswap = swapctl(SWAP_NSWAP, 0, 0), i; 71 72 if (nswap < 1) { 73 puts("no swap devices configured"); 74 exit(0); 75 } 76 77 fsep = sep = (struct swapent *)malloc(nswap * sizeof(*sep)); 78 if (sep == NULL) 79 err(1, "malloc"); 80 rnswap = swapctl(SWAP_STATS, (void *)sep, nswap); 81 if (rnswap < 0) 82 err(1, "SWAP_STATS"); 83 if (nswap != rnswap) 84 warnx("SWAP_STATS different to SWAP_NSWAP (%d != %d)", 85 rnswap, nswap); 86 87 pathmax = 11; 88 if (dolong && tflag == 0) { 89 if (kflag) { 90 header = "1K-blocks"; 91 blocksize = 1024; 92 hlen = strlen(header); 93 } else 94 header = getbsize(&hlen, &blocksize); 95 for (i = rnswap; i-- > 0; sep++) 96 if (pathmax < (l = strlen(sep->se_path))) 97 pathmax = l; 98 sep = fsep; 99 (void)printf("%-*s %*s %8s %8s %8s %s\n", 100 pathmax, "Device", hlen, header, 101 "Used", "Avail", "Capacity", "Priority"); 102 } 103 totalsize = totalinuse = ncounted = 0; 104 for (; rnswap-- > 0; sep++) { 105 if (pflag && sep->se_priority != pri) 106 continue; 107 ncounted++; 108 size = sep->se_nblks; 109 inuse = sep->se_inuse; 110 totalsize += size; 111 totalinuse += inuse; 112 113 if (dolong && tflag == 0) { 114 (void)printf("%-*s %*ld ", pathmax, sep->se_path, hlen, 115 (long)(dbtoqb(size) / blocksize)); 116 117 (void)printf("%8ld %8ld %5.0f%% %d\n", 118 (long)(dbtoqb(inuse) / blocksize), 119 (long)(dbtoqb(size - inuse) / blocksize), 120 (double)inuse / (double)size * 100.0, 121 sep->se_priority); 122 } 123 } 124 if (tflag) 125 (void)printf("%dM/%dM swap space\n", 126 (int)(dbtoqb(totalinuse) / (1024 * 1024)), 127 (int)(dbtoqb(totalsize) / (1024 * 1024))); 128 else if (dolong == 0) 129 printf("total: %ldk bytes allocated = %ldk used, " 130 "%ldk available\n", 131 (long)(dbtoqb(totalsize) / 1024), 132 (long)(dbtoqb(totalinuse) / 1024), 133 (long)(dbtoqb(totalsize - totalinuse) / 1024)); 134 else if (ncounted > 1) 135 (void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax, "Total", 136 hlen, 137 (long)(dbtoqb(totalsize) / blocksize), 138 (long)(dbtoqb(totalinuse) / blocksize), 139 (long)(dbtoqb(totalsize - totalinuse) / blocksize), 140 (double)(totalinuse) / (double)totalsize * 100.0); 141 if (fsep) 142 (void)free(fsep); 143 } 144