1 /* $NetBSD: swaplist.c,v 1.2 1997/06/25 07:44:12 mikel 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Matthew R. Green for 18 * The NetBSD Foundation. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/stat.h> 37 38 #include <vm/vm_swap.h> 39 40 #include <unistd.h> 41 #include <err.h> 42 #include <errno.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 47 /* 48 * NOTE: This file is separate from swapctl.c so that pstat can grab it. 49 */ 50 51 #include "swapctl.h" 52 53 void 54 list_swap(pri, kflag, pflag, tflag, dolong) 55 int pri; 56 int kflag; 57 int pflag; 58 int tflag; 59 int dolong; 60 { 61 struct swapent *sep; 62 long blocksize; 63 char *header; 64 int hlen, totalsize, size, totalinuse, inuse, ncounted; 65 int rnswap, nswap = swapctl(SWAP_NSWAP, 0, 0); 66 67 if (nswap < 1) { 68 puts("no swap devices configured"); 69 exit(0); 70 } 71 72 sep = (struct swapent *)malloc(nswap * sizeof(*sep)); 73 if (sep == NULL) 74 err(1, "malloc"); 75 rnswap = swapctl(SWAP_STATS, (void *)sep, nswap); 76 if (nswap < 0) 77 errx(1, "SWAP_STATS"); 78 if (nswap != rnswap) 79 warnx("SWAP_STATS gave different value than SWAP_NSWAP"); 80 81 if (dolong && tflag == 0) { 82 if (kflag) { 83 header = "1K-blocks"; 84 blocksize = 1024; 85 hlen = strlen(header); 86 } else 87 header = getbsize(&hlen, &blocksize); 88 (void)printf("%-11s %*s %8s %8s %8s %s\n", 89 "Device", hlen, header, 90 "Used", "Avail", "Capacity", "Priority"); 91 } 92 totalsize = totalinuse = ncounted = 0; 93 for (; rnswap-- > 0; sep++) { 94 if (pflag && sep->se_priority != pri) 95 continue; 96 ncounted++; 97 size = sep->se_nblks; 98 inuse = sep->se_inuse; 99 totalsize += size; 100 totalinuse += inuse; 101 102 if (dolong && tflag == 0) { 103 /* XXX handle se_dev == NODEV */ 104 (void)printf("/dev/%-6s %*ld ", 105 devname(sep->se_dev, S_IFBLK), 106 hlen, dbtob(size) / blocksize); 107 108 (void)printf("%8ld %8ld %5.0f%% %d\n", 109 dbtob(inuse) / blocksize, 110 dbtob(size - inuse) / blocksize, 111 (double)inuse / (double)size * 100.0, 112 sep->se_priority); 113 } 114 } 115 if (tflag) 116 (void)printf("%dM/%dM swap space\n", 117 dbtob(totalinuse) / (1024 * 1024), 118 dbtob(totalsize) / (1024 * 1024)); 119 else if (dolong == 0) 120 (void)printf("total: %dk bytes allocated = %dk used, %dk available\n", 121 dbtob(totalsize) / 1024, 122 dbtob(totalinuse) / 1024, 123 dbtob(totalsize - totalinuse) / 1024); 124 else if (ncounted > 1) 125 (void)printf("%-11s %*ld %8ld %8ld %5.0f%%\n", "Total", hlen, 126 dbtob(totalsize) / blocksize, 127 dbtob(totalinuse) / blocksize, 128 dbtob(totalsize - totalinuse) / blocksize, 129 (double)(totalinuse) / (double)totalsize * 100.0); 130 } 131