xref: /openbsd/sbin/swapctl/swaplist.c (revision cca36db2)
1 /*	$OpenBSD: swaplist.c,v 1.8 2011/06/24 21:02:09 jasper Exp $	*/
2 /*	$NetBSD: swaplist.c,v 1.8 1998/10/08 10:00:31 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Matthew R. Green
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/swap.h>
34 
35 #include <err.h>
36 #include <inttypes.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include "swapctl.h"
43 
44 #define	dbtoqb(b) dbtob((int64_t)(b))
45 
46 void
47 list_swap(int pri, int kflag, int pflag, int dolong)
48 {
49 	struct	swapent *sep, *fsep;
50 	long	blocksize;
51 	char	*header;
52 	size_t	l;
53 	int	hlen, totalsize, size, totalinuse, inuse, ncounted, pathmax;
54 	int	rnswap, nswap, i;
55 
56 	nswap = swapctl(SWAP_NSWAP, 0, 0);
57 	if (nswap < 1)
58 		errx(1, "no swap devices configured");
59 
60 	fsep = sep = (struct swapent *)calloc(nswap, sizeof(*sep));
61 	if (sep == NULL)
62 		err(1, "calloc");
63 	rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
64 	if (rnswap < 0)
65 		err(1, "SWAP_STATS");
66 	if (nswap != rnswap)
67 		warnx("SWAP_STATS different to SWAP_NSWAP (%d != %d)",
68 		    rnswap, nswap);
69 
70 	pathmax = 11;
71 	if (kflag) {
72 		header = "1K-blocks";
73 		blocksize = 1024;
74 		hlen = strlen(header);
75 	} else
76 		header = getbsize(&hlen, &blocksize);
77 
78 	if (dolong) {
79 		for (i = rnswap; i-- > 0; sep++)
80 			if (pathmax < (l = strlen(sep->se_path)))
81 				pathmax = l;
82 		sep = fsep;
83 		(void)printf("%-*s %*s %8s %8s %8s  %s\n",
84 		    pathmax, "Device", hlen, header,
85 		    "Used", "Avail", "Capacity", "Priority");
86 	}
87 	totalsize = totalinuse = ncounted = 0;
88 	for (; rnswap-- > 0; sep++) {
89 		if (pflag && sep->se_priority != pri)
90 			continue;
91 		ncounted++;
92 		size = sep->se_nblks;
93 		inuse = sep->se_inuse;
94 		totalsize += size;
95 		totalinuse += inuse;
96 
97 		if (dolong) {
98 			(void)printf("%-*s %*ld ", pathmax, sep->se_path, hlen,
99 			    (long)(dbtoqb(size) / blocksize));
100 
101 			(void)printf("%8ld %8ld %5.0f%%    %d\n",
102 			    (long)(dbtoqb(inuse) / blocksize),
103 			    (long)(dbtoqb(size - inuse) / blocksize),
104 			    (double)inuse / (double)size * 100.0,
105 			    sep->se_priority);
106 		}
107 	}
108 	if (dolong == 0)
109 		printf("total: %ld %*s allocated, %ld used, "
110 		    "%ld available\n",
111 		    (long)(dbtoqb(totalsize) / blocksize),
112 		    hlen, header,
113 		    (long)(dbtoqb(totalinuse) / blocksize),
114 		    (long)(dbtoqb(totalsize - totalinuse) / blocksize));
115 	else if (ncounted > 1)
116 		(void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax, "Total",
117 		    hlen,
118 		    (long)(dbtoqb(totalsize) / blocksize),
119 		    (long)(dbtoqb(totalinuse) / blocksize),
120 		    (long)(dbtoqb(totalsize - totalinuse) / blocksize),
121 		    (double)(totalinuse) / (double)totalsize * 100.0);
122 	if (fsep)
123 		free(fsep);
124 }
125