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