xref: /dragonfly/test/debug/mbufinfo.c (revision 0db87cb7)
1 /*
2  * MBUFINFO.C
3  *
4  * cc -I/usr/src/sys mbufinfo.c -o /usr/local/bin/mbufinfo -lkvm
5  *
6  * mbufinfo
7  *
8  * Dump the MBUF_DEBUG mtrack tree.
9  *
10  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
11  *
12  * This code is derived from software contributed to The DragonFly Project
13  * by Matthew Dillon <dillon@backplane.com>
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  *
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
24  *    distribution.
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.
28  *
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
40  * SUCH DAMAGE.
41  */
42 
43 #define _KERNEL_STRUCTURES
44 #define MBUF_DEBUG
45 #include <sys/param.h>
46 #include <sys/user.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>
52 #include <sys/buf.h>
53 #include <sys/mbuf.h>
54 #include <sys/tree.h>
55 
56 #include <vm/vm.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>
62 
63 #include <vfs/ufs/quota.h>
64 #include <vfs/ufs/inode.h>
65 
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <fcntl.h>
70 #include <kvm.h>
71 #include <nlist.h>
72 #include <getopt.h>
73 
74 struct mbtrack {
75         RB_ENTRY(mbtrack) rb_node;
76 	int trackid;
77 	struct mbuf *m;
78 };
79 
80 RB_HEAD(mbuf_rb_tree, mbtrack);
81 RB_PROTOTYPE2(mbuf_rb_tree, mbtrack, rb_node, mbtrack_cmp, struct mbuf *);
82 
83 struct nlist Nl[] = {
84     { "_mbuf_track_root" },
85     { NULL }
86 };
87 
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);
91 
92 struct mbuf_rb_tree tree;
93 
94 int tracks[256];
95 int count_cluster;
96 int count_ext;
97 int count_noncluster;
98 int count_data_bytes;
99 int count_buffer_space;
100 
101 int VerboseOpt;
102 
103 int
104 main(int ac, char **av)
105 {
106     kvm_t *kd;
107     int i;
108     int ch;
109     const char *corefile = NULL;
110     const char *sysfile = NULL;
111 
112     while ((ch = getopt(ac, av, "vM:N:")) != -1) {
113 	switch(ch) {
114 	case 'M':
115 	    corefile = optarg;
116 	    break;
117 	case 'N':
118 	    sysfile = optarg;
119 	    break;
120 	case 'v':
121 	    ++VerboseOpt;
122 	    break;
123 	default:
124 	    fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
125 	    exit(1);
126 	}
127     }
128 
129     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
130 	perror("kvm_open");
131 	exit(1);
132     }
133     if (kvm_nlist(kd, Nl) != 0) {
134 	perror("kvm_nlist");
135 	exit(1);
136     }
137     kkread(kd, Nl[0].n_value, &tree, sizeof(tree));
138     if (tree.rbh_root)
139 	dumpmb(kd, tree.rbh_root);
140 
141     printf("Histogram:\n");
142     for (i = 0; i < 256; ++i) {
143 	if (tracks[i]) {
144 		printf("%d\t%d\n", i, tracks[i]);
145 	}
146     }
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);
152     return(0);
153 }
154 
155 static void
156 dumpmb(kvm_t *kd, struct mbtrack *mtp)
157 {
158     struct mbtrack mt;
159     struct mbuf mb;
160 
161     kkread(kd, (long)mtp, &mt, sizeof(mt));
162 
163     if (mt.rb_node.rbe_left)
164 	dumpmb(kd, mt.rb_node.rbe_left);
165 
166     if (VerboseOpt)
167 	    printf("mbuf %p track %d\n", mt.m, mt.trackid);
168     if (mt.trackid >= 0 && mt.trackid < 256)
169 	++tracks[mt.trackid];
170     if (mt.m) {
171 	kkread(kd, (long)mt.m, &mb, sizeof(mb));
172 	if (mb.m_flags & M_EXT_CLUSTER)
173 		++count_cluster;
174 	else if (mb.m_flags & M_EXT)
175 		++count_ext;
176 	else
177 		++count_noncluster;
178 	count_data_bytes += mb.m_len;
179 	if (mb.m_flags & M_EXT) {
180 		count_buffer_space += mb.m_ext.ext_size;
181 	} else {
182 		count_buffer_space += MLEN;
183 	}
184 	if (VerboseOpt) {
185 		dumpmbdata(kd, mb.m_data, mb.m_len);
186 	}
187     }
188 
189     if (mt.rb_node.rbe_right)
190 	dumpmb(kd, mt.rb_node.rbe_right);
191 }
192 
193 static void
194 dumpmbdata(kvm_t *kd, char *data, int len)
195 {
196     char buf[256];
197     int i;
198     int j;
199     int n;
200     int count;
201 
202     for (n = 0; n < len; n += count) {
203 	count = len - n;
204 	if (count > sizeof(buf))
205 		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);
210 	    }
211 	    printf(" %02x", (unsigned char)buf[i]);
212 	    if ((n + i) % 16 == 15 || i + 1 == count) {
213 		printf(" ");
214 		for (j = i & ~15; j <= i; ++j)
215 		    printf("%c", isprint(buf[j]) ? buf[j] : '.');
216 		printf("\n");
217 	    }
218 	}
219 	printf("\n");
220     }
221 }
222 
223 static void
224 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
225 {
226     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
227         perror("kvm_read");
228         exit(1);
229     }
230 }
231