GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/swapctl/swaplist.c Lines: 0 53 0.0 %
Date: 2017-11-07 Branches: 0 28 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: swaplist.c,v 1.12 2015/12/10 17:27:00 mmcc 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>	/* dbtob */
33
#include <sys/types.h>
34
#include <sys/swap.h>
35
36
#include <err.h>
37
#include <stdint.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <unistd.h>
42
43
#include "swapctl.h"
44
45
#define	dbtoqb(b) dbtob((int64_t)(b))
46
47
void
48
list_swap(int pri, int kflag, int pflag, int dolong)
49
{
50
	struct	swapent *sep, *fsep;
51
	long	blocksize;
52
	char	*header;
53
	size_t	l;
54
	int	hlen, totalsize, size, totalinuse, inuse, ncounted, pathmax;
55
	int	rnswap, nswap, i;
56
57
	nswap = swapctl(SWAP_NSWAP, 0, 0);
58
	if (nswap < 1)
59
		errx(1, "no swap devices configured");
60
61
	fsep = sep = calloc(nswap, sizeof(*sep));
62
	if (sep == NULL)
63
		err(1, "calloc");
64
	rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
65
	if (rnswap < 0)
66
		err(1, "SWAP_STATS");
67
	if (nswap != rnswap)
68
		warnx("SWAP_STATS different to SWAP_NSWAP (%d != %d)",
69
		    rnswap, nswap);
70
71
	pathmax = 11;
72
	if (kflag) {
73
		header = "1K-blocks";
74
		blocksize = 1024;
75
		hlen = strlen(header);
76
	} else
77
		header = getbsize(&hlen, &blocksize);
78
79
	if (dolong) {
80
		for (i = rnswap; i-- > 0; sep++)
81
			if (pathmax < (l = strlen(sep->se_path)))
82
				pathmax = l;
83
		sep = fsep;
84
		(void)printf("%-*s %*s %8s %8s %8s  %s\n",
85
		    pathmax, "Device", hlen, header,
86
		    "Used", "Avail", "Capacity", "Priority");
87
	}
88
	totalsize = totalinuse = ncounted = 0;
89
	for (; rnswap-- > 0; sep++) {
90
		if (pflag && sep->se_priority != pri)
91
			continue;
92
		ncounted++;
93
		size = sep->se_nblks;
94
		inuse = sep->se_inuse;
95
		totalsize += size;
96
		totalinuse += inuse;
97
98
		if (dolong) {
99
			(void)printf("%-*s %*ld ", pathmax, sep->se_path, hlen,
100
			    (long)(dbtoqb(size) / blocksize));
101
102
			(void)printf("%8ld %8ld %5.0f%%    %d\n",
103
			    (long)(dbtoqb(inuse) / blocksize),
104
			    (long)(dbtoqb(size - inuse) / blocksize),
105
			    (double)inuse / (double)size * 100.0,
106
			    sep->se_priority);
107
		}
108
	}
109
	if (dolong == 0)
110
		printf("total: %ld %*s allocated, %ld used, "
111
		    "%ld available\n",
112
		    (long)(dbtoqb(totalsize) / blocksize),
113
		    hlen, header,
114
		    (long)(dbtoqb(totalinuse) / blocksize),
115
		    (long)(dbtoqb(totalsize - totalinuse) / blocksize));
116
	else if (ncounted > 1)
117
		(void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax, "Total",
118
		    hlen,
119
		    (long)(dbtoqb(totalsize) / blocksize),
120
		    (long)(dbtoqb(totalinuse) / blocksize),
121
		    (long)(dbtoqb(totalsize - totalinuse) / blocksize),
122
		    (double)(totalinuse) / (double)totalsize * 100.0);
123
	free(fsep);
124
}