1 /* $NetBSD: swaplist.c,v 1.15 2008/05/29 14:51:25 mrg Exp $ */
4 * Copyright (c) 1997 Matthew R. Green
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: swaplist.c,v 1.15 2008/05/29 14:51:25 mrg Exp $");
35 #include <sys/param.h>
48 #define dbtoqb(b) dbtob((int64_t)(b))
51 * NOTE: This file is separate from swapctl.c so that pstat can grab it.
57 list_swap(int pri
, int kflag
, int pflag
, int tflag
, int dolong
, int hflag
)
59 struct swapent
*sep
, *fsep
;
61 char szbuf
[5], usbuf
[5], avbuf
[5]; /* size, used, avail */
62 const char *header
, *suff
;
64 int hlen
, totalsize
, size
, totalinuse
, inuse
, ncounted
, pathmax
;
65 int rnswap
, nswap
= swapctl(SWAP_NSWAP
, 0, 0), i
;
68 puts("no swap devices configured");
72 fsep
= sep
= (struct swapent
*)malloc(nswap
* sizeof(*sep
));
75 rnswap
= swapctl(SWAP_STATS
, (void *)sep
, nswap
);
79 warnx("SWAP_STATS different to SWAP_NSWAP (%d != %d)",
92 blocksize
= 1024 * 1024;
96 blocksize
= 1024 * 1024 * 1024;
102 header
= getbsize(&hlen
, &blocksize
);
105 blocksize
= 1; suff
= ""; /* unused */
110 hlen
= strlen(header
);
112 if (dolong
&& tflag
== 0) {
113 for (i
= rnswap
; i
-- > 0; sep
++)
114 if ((size_t)pathmax
< (l
= strlen(sep
->se_path
)))
117 (void)printf("%-*s %*s %8s %8s %8s %s\n",
118 pathmax
, "Device", hlen
, header
,
119 "Used", "Avail", "Capacity", "Priority");
121 totalsize
= totalinuse
= ncounted
= 0;
122 for (; rnswap
-- > 0; sep
++) {
123 if (pflag
&& sep
->se_priority
!= pri
)
126 size
= sep
->se_nblks
;
127 inuse
= sep
->se_inuse
;
131 if (dolong
&& tflag
== 0) {
133 (void)printf("%-*s %*ld ", pathmax
, sep
->se_path
, hlen
,
134 (long)(dbtoqb(size
) / blocksize
));
136 (void)printf("%8ld %8ld %5.0f%% %d\n",
137 (long)(dbtoqb(inuse
) / blocksize
),
138 (long)(dbtoqb(size
- inuse
) / blocksize
),
139 (double)inuse
/ (double)size
* 100.0,
142 if ((humanize_number(szbuf
, sizeof(szbuf
), (dbtoqb(size
)),
143 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
144 err(1, "humanize_number");
145 if ((humanize_number(usbuf
, sizeof(usbuf
), (dbtoqb(inuse
)),
146 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
147 err(1, "humanize_number");
148 if ((humanize_number(avbuf
, sizeof(avbuf
), (dbtoqb(size
-inuse
)),
149 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
150 err(1, "humanize_number");
151 (void)printf("%-*s %*s ", pathmax
, sep
->se_path
, hlen
, szbuf
);
153 (void)printf("%8s %8s %5.0f%% %d\n",
155 (double)inuse
/ (double)size
* 100.0,
162 if ((humanize_number(usbuf
, sizeof(usbuf
), (dbtoqb(totalinuse
)),
163 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
164 err(1, "humanize_number");
165 if ((humanize_number(szbuf
, sizeof(szbuf
), (dbtoqb(totalsize
)),
166 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
167 err(1, "humanize_number");
168 (void)printf("%s/%s swap space\n", usbuf
, szbuf
);
170 (void)printf("%ld/%ld %s swap space\n",
171 (long)(dbtoqb(totalinuse
) / blocksize
),
172 (long)(dbtoqb(totalsize
) / blocksize
),
176 else if (dolong
== 0) {
178 if ((humanize_number(szbuf
, sizeof(szbuf
), (dbtoqb(totalsize
)),
179 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
180 err(1, "humanize_number");
181 if ((humanize_number(usbuf
, sizeof(usbuf
), (dbtoqb(totalinuse
)),
182 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
183 err(1, "humanize_number");
184 if ((humanize_number(avbuf
, sizeof(avbuf
), (dbtoqb(totalsize
-totalinuse
)),
185 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
186 err(1, "humanize_number");
187 (void)printf("total: %s allocated = %s used, %s available.\n",
188 szbuf
, usbuf
, avbuf
);
190 printf("total: %ld %s allocated = %ld %s used, "
191 "%ld %s available\n",
192 (long)(dbtoqb(totalsize
) / blocksize
), suff
,
193 (long)(dbtoqb(totalinuse
) / blocksize
), suff
,
194 (long)(dbtoqb(totalsize
- totalinuse
) / blocksize
), suff
);
196 } else if (ncounted
> 1) {
198 if ((humanize_number(szbuf
, sizeof(szbuf
), (dbtoqb(totalsize
)),
199 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
200 err(1, "humanize_number");
201 if ((humanize_number(usbuf
, sizeof(usbuf
), (dbtoqb(totalinuse
)),
202 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
203 err(1, "humanize_number");
204 if ((humanize_number(avbuf
, sizeof(avbuf
), (dbtoqb(totalsize
-totalinuse
)),
205 "", HN_AUTOSCALE
, (HN_DECIMAL
| HN_B
| HN_NOSPACE
))) == -1)
206 err(1, "humanize_number");
207 (void)printf("%-*s %*s %8s %8s %5.0f%%\n", pathmax
, "Total",
208 hlen
, szbuf
, usbuf
, avbuf
,
209 (double)(totalinuse
) / (double)totalsize
* 100.0);
211 (void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax
, "Total",
213 (long)(dbtoqb(totalsize
) / blocksize
),
214 (long)(dbtoqb(totalinuse
) / blocksize
),
215 (long)(dbtoqb(totalsize
- totalinuse
) / blocksize
),
216 (double)(totalinuse
) / (double)totalsize
* 100.0);