4 * cc -I/usr/src/sys mbufinfo.c -o /usr/local/bin/mbufinfo -lkvm
8 * Dump the MBUF_DEBUG mtrack tree.
10 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
12 * This code is derived from software contributed to The DragonFly Project
13 * by Matthew Dillon <dillon@backplane.com>
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in
23 * the documentation and/or other materials provided with the
25 * 3. Neither the name of The DragonFly Project nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific, prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
32 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
33 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
35 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
37 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
38 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
39 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 #define _KERNEL_STRUCTURES
45 #include <sys/param.h>
47 #include <sys/malloc.h>
48 #include <sys/signalvar.h>
49 #include <sys/namecache.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_object.h>
60 #include <vm/swap_pager.h>
61 #include <vm/vnode_pager.h>
63 #include <vfs/ufs/quota.h>
64 #include <vfs/ufs/inode.h>
75 RB_ENTRY(mbtrack) rb_node;
80 RB_HEAD(mbuf_rb_tree, mbtrack);
81 RB_PROTOTYPE2(mbuf_rb_tree, mbtrack, rb_node, mbtrack_cmp, struct mbuf *);
84 { "_mbuf_track_root" },
88 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
89 static void dumpmb(kvm_t *kd, struct mbtrack *mtp);
90 static void dumpmbdata(kvm_t *kd, char *data, int len);
92 struct mbuf_rb_tree tree;
99 int count_buffer_space;
104 main(int ac, char **av)
109 const char *corefile = NULL;
110 const char *sysfile = NULL;
112 while ((ch = getopt(ac, av, "vM:N:")) != -1) {
124 fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
129 if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
133 if (kvm_nlist(kd, Nl) != 0) {
137 kkread(kd, Nl[0].n_value, &tree, sizeof(tree));
139 dumpmb(kd, tree.rbh_root);
141 printf("Histogram:\n");
142 for (i = 0; i < 256; ++i) {
144 printf("%d\t%d\n", i, tracks[i]);
147 printf("clusters: %d\n", count_cluster);
148 printf("normal: %d\n", count_noncluster);
149 printf("external: %d\n", count_ext);
150 printf("data: %d\n", count_data_bytes);
151 printf("bufspace: %d\n", count_buffer_space);
156 dumpmb(kvm_t *kd, struct mbtrack *mtp)
161 kkread(kd, (long)mtp, &mt, sizeof(mt));
163 if (mt.rb_node.rbe_left)
164 dumpmb(kd, mt.rb_node.rbe_left);
167 printf("mbuf %p track %d\n", mt.m, mt.trackid);
168 if (mt.trackid >= 0 && mt.trackid < 256)
169 ++tracks[mt.trackid];
171 kkread(kd, (long)mt.m, &mb, sizeof(mb));
172 if (mb.m_flags & M_EXT_CLUSTER)
174 else if (mb.m_flags & M_EXT)
178 count_data_bytes += mb.m_len;
179 if (mb.m_flags & M_EXT) {
180 count_buffer_space += mb.m_ext.ext_size;
182 count_buffer_space += MLEN;
185 dumpmbdata(kd, mb.m_data, mb.m_len);
189 if (mt.rb_node.rbe_right)
190 dumpmb(kd, mt.rb_node.rbe_right);
194 dumpmbdata(kvm_t *kd, char *data, int len)
202 for (n = 0; n < len; n += count) {
204 if (count > sizeof(buf))
206 kkread(kd, (long)data + n, buf, count);
207 for (i = 0; i < count; ++i) {
208 if ((n + i) % 16 == 0) {
209 printf(" %04x ", n + i);
211 printf(" %02x", (unsigned char)buf[i]);
212 if ((n + i) % 16 == 15 || i + 1 == count) {
214 for (j = i & ~15; j <= i; ++j)
215 printf("%c", isprint(buf[j]) ? buf[j] : '.');
224 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
226 if (kvm_read(kd, addr, buf, nbytes) != nbytes) {