xref: /netbsd-src/sbin/swapctl/swaplist.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: swaplist.c,v 1.13 2005/02/09 05:51:38 xtraeme 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.13 2005/02/09 05:51:38 xtraeme 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(int pri, int kflag, int pflag, int tflag, int dolong, int hflag)
60 {
61 	struct	swapent *sep, *fsep;
62 	long	blocksize;
63 	char	szbuf[5], usbuf[5], avbuf[5]; /* size, used, avail */
64 	const	char *header, *suff;
65 	size_t	l;
66 	int	hlen, totalsize, size, totalinuse, inuse, ncounted, pathmax;
67 	int	rnswap, nswap = swapctl(SWAP_NSWAP, 0, 0), i;
68 
69 	if (nswap < 1) {
70 		puts("no swap devices configured");
71 		exit(0);
72 	}
73 
74 	fsep = sep = (struct swapent *)malloc(nswap * sizeof(*sep));
75 	if (sep == NULL)
76 		err(1, "malloc");
77 	rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
78 	if (rnswap < 0)
79 		err(1, "SWAP_STATS");
80 	if (nswap != rnswap)
81 		warnx("SWAP_STATS different to SWAP_NSWAP (%d != %d)",
82 		    rnswap, nswap);
83 
84 	pathmax = 11;
85 	switch(kflag) {
86 		case 1:
87 			header = "1K-blocks";
88 			blocksize = 1024;
89 			suff = "KBytes";
90 			break;
91 		case 2:
92 			suff = "MBytes";
93 			header = "1M-blocks";
94 			blocksize = 1024 * 1024;
95 			break;
96 		case 3:
97 			header = "1G-blocks";
98 			blocksize = 1024 * 1024 * 1024;
99 			suff = "GBytes";
100 			break;
101 		default:
102 			suff = "blocks";
103 			if (!hflag) {
104 				header = getbsize(&hlen, &blocksize);
105 			} else {
106 				header = "Size";
107 				blocksize = 1; suff = ""; /* unused */
108 			}
109 			break;
110 	}
111 	if (hflag || kflag)
112 		hlen = strlen(header);
113 
114 	if (dolong && tflag == 0) {
115 		for (i = rnswap; i-- > 0; sep++)
116 			if (pathmax < (l = strlen(sep->se_path)))
117 				pathmax = l;
118 		sep = fsep;
119 		(void)printf("%-*s %*s %8s %8s %8s  %s\n",
120 		    pathmax, "Device", hlen, header,
121 		    "Used", "Avail", "Capacity", "Priority");
122 	}
123 	totalsize = totalinuse = ncounted = 0;
124 	for (; rnswap-- > 0; sep++) {
125 		if (pflag && sep->se_priority != pri)
126 			continue;
127 		ncounted++;
128 		size = sep->se_nblks;
129 		inuse = sep->se_inuse;
130 		totalsize += size;
131 		totalinuse += inuse;
132 
133 		if (dolong && tflag == 0) {
134 			if (hflag == 0) {
135 				(void)printf("%-*s %*ld ", pathmax, sep->se_path, hlen,
136 				    (long)(dbtoqb(size) / blocksize));
137 
138 				(void)printf("%8ld %8ld %5.0f%%    %d\n",
139 				    (long)(dbtoqb(inuse) / blocksize),
140 				    (long)(dbtoqb(size - inuse) / blocksize),
141 				    (double)inuse / (double)size * 100.0,
142 				    sep->se_priority);
143 			} else {
144 				if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(size)),
145 					"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
146 					err(1, "humanize_number");
147 				if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(inuse)),
148 					"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
149 					err(1, "humanize_number");
150 				if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(size-inuse)),
151 					"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
152 					err(1, "humanize_number");
153 				(void)printf("%-*s %*s ", pathmax, sep->se_path, hlen, szbuf);
154 
155 				(void)printf("%8s %8s %5.0f%%    %d\n",
156 				    usbuf, avbuf,
157 				    (double)inuse / (double)size * 100.0,
158 				    sep->se_priority);
159 			}
160 		}
161 	}
162 	if (tflag) {
163 		if (hflag) {
164 			if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)),
165 				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
166 				err(1, "humanize_number");
167 			if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)),
168 				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
169 				err(1, "humanize_number");
170 			(void)printf("%s/%s swap space\n", usbuf, szbuf);
171 		} else {
172 			(void)printf("%ld/%ld %s swap space\n",
173 				(long)(dbtoqb(totalinuse) / blocksize),
174 				(long)(dbtoqb(totalsize) / blocksize),
175 				suff);
176 		}
177 	}
178 	else if (dolong == 0) {
179 		if (hflag) {
180 			if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)),
181 				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
182 				err(1, "humanize_number");
183 			if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)),
184 				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
185 				err(1, "humanize_number");
186 			if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(totalsize-totalinuse)),
187 				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
188 				err(1, "humanize_number");
189 			(void)printf("total: %s allocated = %s used, %s available.\n",
190 				szbuf, usbuf, avbuf);
191 		} else {
192 		    printf("total: %ld %s allocated = %ld %s used, "
193 			   "%ld %s available\n",
194 		    (long)(dbtoqb(totalsize) / blocksize), suff,
195 		    (long)(dbtoqb(totalinuse) / blocksize), suff,
196 		    (long)(dbtoqb(totalsize - totalinuse) / blocksize), suff);
197 		}
198 	} else if (ncounted > 1) {
199 		if (hflag) {
200 			if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)),
201 				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
202 				err(1, "humanize_number");
203 			if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)),
204 				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
205 				err(1, "humanize_number");
206 			if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(totalsize-totalinuse)),
207 				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
208 				err(1, "humanize_number");
209 			(void)printf("%-*s %*s %8s %8s %5.0f%%\n", pathmax, "Total",
210 				hlen, szbuf, usbuf, avbuf,
211 			        (double)(totalinuse) / (double)totalsize * 100.0);
212 		} else {
213 			(void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax, "Total",
214 			    hlen,
215 			    (long)(dbtoqb(totalsize) / blocksize),
216 			    (long)(dbtoqb(totalinuse) / blocksize),
217 			    (long)(dbtoqb(totalsize - totalinuse) / blocksize),
218 			    (double)(totalinuse) / (double)totalsize * 100.0);
219 		}
220 	}
221 	if (fsep)
222 		(void)free(fsep);
223 }
224