2 * Copyright (c) 1999 Matthew Dillon. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided under the terms of the BSD
6 * Copyright as found in /usr/src/COPYRIGHT in the FreeBSD source tree.
8 * @(#) Copyright (c) 1999
9 * $FreeBSD: src/lib/libkvm/kvm_getswapinfo.c,v 1.10.2.4 2003/01/12 09:23:13 dillon Exp $
10 * $DragonFly: src/lib/libkvm/kvm_getswapinfo.c,v 1.4 2004/06/28 02:57:10 drhodus Exp $
13 #define _KERNEL_STRUCTURES
15 #include <sys/param.h>
17 #include <sys/ucred.h>
20 #include <sys/blist.h>
32 static struct nlist kvm_swap_nl[] = {
33 { "_swapblist" }, /* new radix swap list */
34 { "_swdevt" }, /* list of swap devices and sizes */
35 { "_nswdev" }, /* number of swap devices */
36 { "_dmmax" }, /* maximum size of a swap block */
40 #define NL_SWAPBLIST 0
45 static int kvm_swap_nl_cached = 0;
50 static void getswapinfo_radix(kvm_t *kd, struct kvm_swap *swap_ary,
51 int swap_max, int flags);
53 #define SVAR(var) __STRING(var) /* to force expansion */
54 #define KGET(idx, var) \
55 KGET1(idx, &var, sizeof(var), SVAR(var))
56 #define KGET1(idx, p, s, msg) \
57 KGET2(kvm_swap_nl[idx].n_value, p, s, msg)
58 #define KGET2(addr, p, s, msg) \
59 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
60 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
61 #define KGETN(idx, var) \
62 KGET1N(idx, &var, sizeof(var), SVAR(var))
63 #define KGET1N(idx, p, s, msg) \
64 KGET2N(kvm_swap_nl[idx].n_value, p, s, msg)
65 #define KGET2N(addr, p, s, msg) \
66 ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
67 #define KGETRET(addr, p, s, msg) \
68 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
69 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
76 struct kvm_swap *swap_ary,
86 kvm_swap_nl_cached = 0;
93 if (kvm_swap_nl_cached == 0) {
96 if (kvm_nlist(kd, kvm_swap_nl) < 0)
104 kvm_swap_nl[NL_SWDEVT].n_value == 0 ||
105 kvm_swap_nl[NL_NSWDEV].n_value == 0 ||
106 kvm_swap_nl[NL_DMMAX].n_value == 0 ||
107 kvm_swap_nl[NL_SWAPBLIST].n_type == 0
113 * get globals, type of swap
116 KGET(NL_NSWDEV, nswdev);
117 KGET(NL_DMMAX, dmmax);
120 * figure out how many actual swap devices are enabled
124 for (unswdev = nswdev - 1; unswdev >= 0; --unswdev) {
125 struct swdevt swinfo;
127 KGET2(&sw[unswdev], &swinfo, sizeof(swinfo), "swinfo");
133 kvm_swap_nl_cached = 1;
146 bzero(swap_ary, sizeof(struct kvm_swap) * (ti + 1));
149 for (i = 0; i < unswdev; ++i) {
150 struct swdevt swinfo;
153 KGET2(&sw[i], &swinfo, sizeof(swinfo), "swinfo");
156 * old style: everything in DEV_BSIZE'd chunks,
159 * new style: swinfo in DEV_BSIZE'd chunks but dmmax
162 * The first dmmax is never allocating to avoid
163 * trashing the disklabels
166 ttl = swinfo.sw_nblks - dmmax;
172 swap_ary[i].ksw_total = ttl;
173 swap_ary[i].ksw_used = ttl;
174 swap_ary[i].ksw_flags = swinfo.sw_flags;
175 if (swinfo.sw_dev == NODEV) {
177 swap_ary[i].ksw_devname,
178 sizeof(swap_ary[i].ksw_devname),
184 swap_ary[i].ksw_devname,
185 sizeof(swap_ary[i].ksw_devname),
187 ((flags & SWIF_DEV_PREFIX) ? _PATH_DEV : ""),
188 devname(swinfo.sw_dev, S_IFCHR)
193 swap_ary[ti].ksw_total += ttl;
194 swap_ary[ti].ksw_used += ttl;
199 getswapinfo_radix(kd, swap_ary, swap_max, flags);
204 * scanradix() - support routine for radix scanner
207 #define TABME tab, tab, ""
219 struct kvm_swap *swap_ary,
225 int ti = (unswdev >= swap_max) ? swap_max - 1 : unswdev;
227 KGET2(scan, &meta, sizeof(meta), "blmeta_t");
232 if (meta.bm_bighint == (daddr_t)-1) {
233 if (flags & SWIF_DUMP_TREE) {
234 printf("%*.*s(0x%06x,%d) Terminator\n",
243 if (radix == BLIST_BMAP_RADIX) {
249 if (flags & SWIF_DUMP_TREE) {
250 printf("%*.*s(0x%06x,%d) Bitmap %08x big=%d\n",
254 (int)meta.u.bmu_bitmap,
260 * If not all allocated, count.
262 if (meta.u.bmu_bitmap != 0) {
263 for (i = 0; i < BLIST_BMAP_RADIX && i < count; ++i) {
265 * A 0 bit means allocated
267 if ((meta.u.bmu_bitmap & (1 << i))) {
271 t = (blk + i) / dmmax % nswdev;
273 --swap_ary[t].ksw_used;
275 --swap_ary[ti].ksw_used;
279 } else if (meta.u.bmu_avail == radix) {
281 * Meta node if all free
283 if (flags & SWIF_DUMP_TREE) {
284 printf("%*.*s(0x%06x,%d) Submap ALL-FREE {\n",
291 * Note: both dmmax and radix are powers of 2. However, dmmax
292 * may be larger then radix so use a smaller increment if
302 for (t = blk; t < blk + radix; t += tinc) {
303 int u = (nswdev) ? (t / dmmax % nswdev) : 0;
306 swap_ary[u].ksw_used -= tinc;
308 swap_ary[ti].ksw_used -= tinc;
311 } else if (meta.u.bmu_avail == 0) {
313 * Meta node if all used
315 if (flags & SWIF_DUMP_TREE) {
316 printf("%*.*s(0x%06x,%d) Submap ALL-ALLOCATED\n",
324 * Meta node if not all free
329 if (flags & SWIF_DUMP_TREE) {
330 printf("%*.*s(0x%06x,%d) Submap avail=%d big=%d {\n",
334 (int)meta.u.bmu_avail,
339 radix /= BLIST_META_RADIX;
340 next_skip = skip / BLIST_META_RADIX;
342 for (i = 1; i <= skip; i += next_skip) {
344 daddr_t vcount = (count > radix) ? radix : count;
364 if (flags & SWIF_DUMP_TREE) {
365 printf("%*.*s}\n", TABME);
372 getswapinfo_radix(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max, int flags)
374 struct blist *swapblist = NULL;
375 struct blist blcopy = { 0 };
377 KGET(NL_SWAPBLIST, swapblist);
379 if (swapblist == NULL) {
380 if (flags & SWIF_DUMP_TREE)
381 printf("radix tree: NULL - no swap in system\n");
385 KGET2(swapblist, &blcopy, sizeof(blcopy), "*swapblist");
387 if (flags & SWIF_DUMP_TREE) {
388 printf("radix tree: %d/%d/%d blocks, %dK wired\n",
392 (int)((blcopy.bl_rootblks * sizeof(blmeta_t) + 1023)/