2 * Copyright (c) 1999, Matthew Dillon. All Rights Reserved.
3 * Copyright (c) 2001, Thomas Moestl. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <sys/blist.h>
34 #include <sys/sysctl.h>
36 #include <vm/vm_param.h>
50 #include "kvm_private.h"
52 #define NL_SWAPBLIST 0
57 static int kvm_swap_nl_cached = 0;
58 static int unswdev; /* number of found swap dev's */
61 static int kvm_getswapinfo_sysctl(kvm_t *, struct kvm_swap *, int, int);
62 static int getsysctl(kvm_t *, char *, void *, size_t);
64 #define GETSWDEVNAME(dev, str, flags) \
66 strlcpy(str, "[NFS swap]", sizeof(str)); \
69 str, sizeof(str),"%s%s", \
70 ((flags & SWIF_DEV_PREFIX) ? _PATH_DEV : ""), \
71 devname(dev, S_IFCHR) \
78 struct kvm_swap *swap_ary,
87 kvm_swap_nl_cached = 0;
92 return kvm_getswapinfo_sysctl(kd, swap_ary, swap_max, flags);
98 #define GETSYSCTL(kd, name, var) \
99 getsysctl(kd, name, &(var), sizeof(var))
101 /* The maximum MIB length for vm.swap_info and an additional device number */
105 kvm_getswapinfo_sysctl(
107 struct kvm_swap *swap_ary,
113 int soid[SWI_MAXMIB];
117 if (!GETSYSCTL(kd, "vm.dmmax", dmmax))
120 mibi = SWI_MAXMIB - 1;
121 if (sysctlnametomib("vm.swap_info", soid, &mibi) == -1) {
122 _kvm_err(kd, kd->program, "sysctlnametomib failed: %s",
126 bzero(&tot, sizeof(tot));
127 for (unswdev = 0;; unswdev++) {
128 soid[mibi] = unswdev;
130 if (sysctl(soid, mibi + 1, &xsd, &len, NULL, 0) == -1) {
133 _kvm_err(kd, kd->program, "cannot read sysctl: %s.",
137 if (len != sizeof(xsd)) {
138 _kvm_err(kd, kd->program, "struct xswdev has unexpected "
139 "size; kernel and libkvm out of sync?");
142 if (xsd.xsw_version != XSWDEV_VERSION) {
143 _kvm_err(kd, kd->program, "struct xswdev version "
144 "mismatch; kernel and libkvm out of sync?");
148 ttl = xsd.xsw_nblks - dmmax;
149 if (unswdev < swap_max - 1) {
150 bzero(&swap_ary[unswdev], sizeof(swap_ary[unswdev]));
151 swap_ary[unswdev].ksw_total = ttl;
152 swap_ary[unswdev].ksw_used = xsd.xsw_used;
153 swap_ary[unswdev].ksw_flags = xsd.xsw_flags;
154 GETSWDEVNAME(xsd.xsw_dev, swap_ary[unswdev].ksw_devname,
157 tot.ksw_total += ttl;
158 tot.ksw_used += xsd.xsw_used;
178 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
179 _kvm_err(kd, kd->program, "cannot read sysctl %s:%s", name,
184 _kvm_err(kd, kd->program, "sysctl %s has unexpected size", name);