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>
76 RB_ENTRY(mbtrack) rb_node;
81 RB_HEAD(mbuf_rb_tree, mbtrack);
82 RB_PROTOTYPE2(mbuf_rb_tree, mbtrack, rb_node, mbtrack_cmp, struct mbuf *);
85 { "_mbuf_track_root" },
89 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
90 static void dumpmb(kvm_t *kd, struct mbtrack *mtp);
91 static void dumpmbdata(kvm_t *kd, char *data, int len);
93 struct mbuf_rb_tree tree;
100 int count_buffer_space;
105 main(int ac, char **av)
110 const char *corefile = NULL;
111 const char *sysfile = NULL;
113 while ((ch = getopt(ac, av, "vM:N:")) != -1) {
125 fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
130 if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
134 if (kvm_nlist(kd, Nl) != 0) {
138 kkread(kd, Nl[0].n_value, &tree, sizeof(tree));
140 dumpmb(kd, tree.rbh_root);
142 printf("Histogram:\n");
143 for (i = 0; i < 256; ++i) {
145 printf("%d\t%d\n", i, tracks[i]);
148 printf("clusters: %d\n", count_cluster);
149 printf("normal: %d\n", count_noncluster);
150 printf("external: %d\n", count_ext);
151 printf("data: %d\n", count_data_bytes);
152 printf("bufspace: %d\n", count_buffer_space);
157 dumpmb(kvm_t *kd, struct mbtrack *mtp)
162 kkread(kd, (long)mtp, &mt, sizeof(mt));
164 if (mt.rb_node.rbe_left)
165 dumpmb(kd, mt.rb_node.rbe_left);
168 printf("mbuf %p track %d\n", mt.m, mt.trackid);
169 if (mt.trackid >= 0 && mt.trackid < 256)
170 ++tracks[mt.trackid];
172 kkread(kd, (long)mt.m, &mb, sizeof(mb));
173 if (mb.m_flags & M_EXT_CLUSTER)
175 else if (mb.m_flags & M_EXT)
179 count_data_bytes += mb.m_len;
180 if (mb.m_flags & M_EXT) {
181 count_buffer_space += mb.m_ext.ext_size;
183 count_buffer_space += MLEN;
186 dumpmbdata(kd, mb.m_data, mb.m_len);
190 if (mt.rb_node.rbe_right)
191 dumpmb(kd, mt.rb_node.rbe_right);
195 dumpmbdata(kvm_t *kd, char *data, int len)
203 for (n = 0; n < len; n += count) {
205 if (count > sizeof(buf))
207 kkread(kd, (long)data + n, buf, count);
208 for (i = 0; i < count; ++i) {
209 if ((n + i) % 16 == 0) {
210 printf(" %04x ", n + i);
212 printf(" %02x", (unsigned char)buf[i]);
213 if ((n + i) % 16 == 15 || i + 1 == count) {
215 for (j = i & ~15; j <= i; ++j)
216 printf("%c", isprint(buf[j]) ? buf[j] : '.');
225 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
227 if (kvm_read(kd, addr, buf, nbytes) != nbytes) {