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