kvm_getswapinfo.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1999, Matthew Dillon.  All Rights Reserved.
5 * Copyright (c) 2001, Thomas Moestl.  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 *
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, 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
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/11/lib/libkvm/kvm_getswapinfo.c 330897 2018-03-14 03:19:51Z eadler $");
31
32#include <sys/param.h>
33#include <sys/time.h>
34#include <sys/stat.h>
35#include <sys/blist.h>
36#include <sys/sysctl.h>
37
38#include <vm/swap_pager.h>
39#include <vm/vm_param.h>
40
41#include <err.h>
42#include <errno.h>
43#include <fcntl.h>
44#include <kvm.h>
45#include <nlist.h>
46#include <paths.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <unistd.h>
51#include <limits.h>
52
53#include "kvm_private.h"
54
55static struct nlist kvm_swap_nl[] = {
56	{ .n_name = "_swtailq" },	/* list of swap devices and sizes */
57	{ .n_name = "_dmmax" },		/* maximum size of a swap block */
58	{ .n_name = NULL }
59};
60
61#define NL_SWTAILQ	0
62#define NL_DMMAX	1
63
64static int kvm_swap_nl_cached = 0;
65static int unswdev;  /* number of found swap dev's */
66static int dmmax;
67
68static int  kvm_getswapinfo_kvm(kvm_t *, struct kvm_swap *, int, int);
69static int  kvm_getswapinfo_sysctl(kvm_t *, struct kvm_swap *, int, int);
70static int  nlist_init(kvm_t *);
71static int  getsysctl(kvm_t *, const char *, void *, size_t);
72
73#define KREAD(kd, addr, obj) \
74	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
75#define	KGET(idx, var)							\
76	KGET2(kvm_swap_nl[(idx)].n_value, var, kvm_swap_nl[(idx)].n_name)
77#define KGET2(addr, var, msg)						\
78	if (KREAD(kd, (u_long)(addr), (var))) {				\
79		_kvm_err(kd, kd->program, "cannot read %s", msg);	\
80		return (-1);						\
81	}
82
83#define GETSWDEVNAME(dev, str, flags)					\
84	if (dev == NODEV) {						\
85		strlcpy(str, "[NFS swap]", sizeof(str));		\
86	} else {							\
87		snprintf(						\
88		    str, sizeof(str),"%s%s",				\
89		    ((flags & SWIF_DEV_PREFIX) ? _PATH_DEV : ""),	\
90		    devname(dev, S_IFCHR)				\
91		);							\
92	}
93
94int
95kvm_getswapinfo(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max, int flags)
96{
97
98	/*
99	 * clear cache
100	 */
101	if (kd == NULL) {
102		kvm_swap_nl_cached = 0;
103		return(0);
104	}
105
106	if (ISALIVE(kd)) {
107		return kvm_getswapinfo_sysctl(kd, swap_ary, swap_max, flags);
108	} else {
109		return kvm_getswapinfo_kvm(kd, swap_ary, swap_max, flags);
110	}
111}
112
113int
114kvm_getswapinfo_kvm(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max,
115    int flags)
116{
117	int i;
118	swblk_t ttl;
119	TAILQ_HEAD(, swdevt) swtailq;
120	struct swdevt *sp, swinfo;
121	struct kvm_swap tot;
122
123	if (!kd->arch->ka_native(kd)) {
124		_kvm_err(kd, kd->program,
125		    "cannot read swapinfo from non-native core");
126		return (-1);
127	}
128
129	if (!nlist_init(kd))
130		return (-1);
131
132	bzero(&tot, sizeof(tot));
133	KGET(NL_SWTAILQ, &swtailq);
134	sp = TAILQ_FIRST(&swtailq);
135	for (i = 0; sp != NULL; i++) {
136		KGET2(sp, &swinfo, "swinfo");
137		ttl = swinfo.sw_nblks - dmmax;
138		if (i < swap_max - 1) {
139			bzero(&swap_ary[i], sizeof(swap_ary[i]));
140			swap_ary[i].ksw_total = ttl;
141			swap_ary[i].ksw_used = swinfo.sw_used;
142			swap_ary[i].ksw_flags = swinfo.sw_flags;
143			GETSWDEVNAME(swinfo.sw_dev, swap_ary[i].ksw_devname,
144			     flags);
145		}
146		tot.ksw_total += ttl;
147		tot.ksw_used += swinfo.sw_used;
148		sp = TAILQ_NEXT(&swinfo, sw_list);
149	}
150
151	if (i >= swap_max)
152		i = swap_max - 1;
153	if (i >= 0)
154		swap_ary[i] = tot;
155
156        return(i);
157}
158
159#define	GETSYSCTL(kd, name, var)					\
160	    getsysctl(kd, name, &(var), sizeof(var))
161
162/* The maximum MIB length for vm.swap_info and an additional device number */
163#define	SWI_MAXMIB	3
164
165int
166kvm_getswapinfo_sysctl(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max,
167    int flags)
168{
169	int ti;
170	swblk_t ttl;
171	size_t mibi, len;
172	int soid[SWI_MAXMIB];
173	struct xswdev xsd;
174	struct kvm_swap tot;
175
176	if (!GETSYSCTL(kd, "vm.dmmax", dmmax))
177		return -1;
178
179	mibi = SWI_MAXMIB - 1;
180	if (sysctlnametomib("vm.swap_info", soid, &mibi) == -1) {
181		_kvm_err(kd, kd->program, "sysctlnametomib failed: %s",
182		    strerror(errno));
183		return -1;
184	}
185	bzero(&tot, sizeof(tot));
186	for (unswdev = 0;; unswdev++) {
187		soid[mibi] = unswdev;
188		len = sizeof(xsd);
189		if (sysctl(soid, mibi + 1, &xsd, &len, NULL, 0) == -1) {
190			if (errno == ENOENT)
191				break;
192			_kvm_err(kd, kd->program, "cannot read sysctl: %s.",
193			    strerror(errno));
194			return -1;
195		}
196		if (len != sizeof(xsd)) {
197			_kvm_err(kd, kd->program, "struct xswdev has unexpected "
198			    "size;  kernel and libkvm out of sync?");
199			return -1;
200		}
201		if (xsd.xsw_version != XSWDEV_VERSION) {
202			_kvm_err(kd, kd->program, "struct xswdev version "
203			    "mismatch; kernel and libkvm out of sync?");
204			return -1;
205		}
206
207		ttl = xsd.xsw_nblks - dmmax;
208		if (unswdev < swap_max - 1) {
209			bzero(&swap_ary[unswdev], sizeof(swap_ary[unswdev]));
210			swap_ary[unswdev].ksw_total = ttl;
211			swap_ary[unswdev].ksw_used = xsd.xsw_used;
212			swap_ary[unswdev].ksw_flags = xsd.xsw_flags;
213			GETSWDEVNAME(xsd.xsw_dev, swap_ary[unswdev].ksw_devname,
214			     flags);
215		}
216		tot.ksw_total += ttl;
217		tot.ksw_used += xsd.xsw_used;
218	}
219
220	ti = unswdev;
221	if (ti >= swap_max)
222		ti = swap_max - 1;
223	if (ti >= 0)
224		swap_ary[ti] = tot;
225
226        return(ti);
227}
228
229static int
230nlist_init(kvm_t *kd)
231{
232
233	if (kvm_swap_nl_cached)
234		return (1);
235
236	if (kvm_nlist(kd, kvm_swap_nl) < 0)
237		return (0);
238
239	/* Required entries */
240	if (kvm_swap_nl[NL_SWTAILQ].n_value == 0) {
241		_kvm_err(kd, kd->program, "unable to find swtailq");
242		return (0);
243	}
244
245	if (kvm_swap_nl[NL_DMMAX].n_value == 0) {
246		_kvm_err(kd, kd->program, "unable to find dmmax");
247		return (0);
248	}
249
250	/* Get globals, type of swap */
251	KGET(NL_DMMAX, &dmmax);
252
253	kvm_swap_nl_cached = 1;
254	return (1);
255}
256
257static int
258getsysctl(kvm_t *kd, const char *name, void *ptr, size_t len)
259{
260	size_t nlen = len;
261	if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
262		_kvm_err(kd, kd->program, "cannot read sysctl %s:%s", name,
263		    strerror(errno));
264		return (0);
265	}
266	if (nlen != len) {
267		_kvm_err(kd, kd->program, "sysctl %s has unexpected size", name);
268		return (0);
269	}
270	return (1);
271}
272