nrelease - fix/improve livecd
[dragonfly.git] / test / debug / mbufinfo.c
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 <ctype.h>
72 #include <nlist.h>
73 #include <getopt.h>
74
75 struct mbtrack {
76         RB_ENTRY(mbtrack) rb_node;
77         int trackid;
78         struct mbuf *m;
79 };
80
81 RB_HEAD(mbuf_rb_tree, mbtrack);
82 RB_PROTOTYPE2(mbuf_rb_tree, mbtrack, rb_node, mbtrack_cmp, struct mbuf *);
83
84 struct nlist Nl[] = {
85     { "_mbuf_track_root" },
86     { NULL }
87 };
88
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);
92
93 struct mbuf_rb_tree tree;
94
95 int tracks[256];
96 int count_cluster;
97 int count_ext;
98 int count_noncluster;
99 int count_data_bytes;
100 int count_buffer_space;
101
102 int VerboseOpt;
103
104 int
105 main(int ac, char **av)
106 {
107     kvm_t *kd;
108     int i;
109     int ch;
110     const char *corefile = NULL;
111     const char *sysfile = NULL;
112
113     while ((ch = getopt(ac, av, "vM:N:")) != -1) {
114         switch(ch) {
115         case 'M':
116             corefile = optarg;
117             break;
118         case 'N':
119             sysfile = optarg;
120             break;
121         case 'v':
122             ++VerboseOpt;
123             break;
124         default:
125             fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
126             exit(1);
127         }
128     }
129
130     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
131         perror("kvm_open");
132         exit(1);
133     }
134     if (kvm_nlist(kd, Nl) != 0) {
135         perror("kvm_nlist");
136         exit(1);
137     }
138     kkread(kd, Nl[0].n_value, &tree, sizeof(tree));
139     if (tree.rbh_root)
140         dumpmb(kd, tree.rbh_root);
141
142     printf("Histogram:\n");
143     for (i = 0; i < 256; ++i) {
144         if (tracks[i]) {
145                 printf("%d\t%d\n", i, tracks[i]);
146         }
147     }
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);
153     return(0);
154 }
155
156 static void
157 dumpmb(kvm_t *kd, struct mbtrack *mtp)
158 {
159     struct mbtrack mt;
160     struct mbuf mb;
161
162     kkread(kd, (long)mtp, &mt, sizeof(mt));
163
164     if (mt.rb_node.rbe_left)
165         dumpmb(kd, mt.rb_node.rbe_left);
166
167     if (VerboseOpt)
168             printf("mbuf %p track %d\n", mt.m, mt.trackid);
169     if (mt.trackid >= 0 && mt.trackid < 256)
170         ++tracks[mt.trackid];
171     if (mt.m) {
172         kkread(kd, (long)mt.m, &mb, sizeof(mb));
173         if (mb.m_flags & M_EXT_CLUSTER)
174                 ++count_cluster;
175         else if (mb.m_flags & M_EXT)
176                 ++count_ext;
177         else
178                 ++count_noncluster;
179         count_data_bytes += mb.m_len;
180         if (mb.m_flags & M_EXT) {
181                 count_buffer_space += mb.m_ext.ext_size;
182         } else {
183                 count_buffer_space += MLEN;
184         }
185         if (VerboseOpt) {
186                 dumpmbdata(kd, mb.m_data, mb.m_len);
187         }
188     }
189
190     if (mt.rb_node.rbe_right)
191         dumpmb(kd, mt.rb_node.rbe_right);
192 }
193
194 static void
195 dumpmbdata(kvm_t *kd, char *data, int len)
196 {
197     char buf[256];
198     int i;
199     int j;
200     int n;
201     int count;
202
203     for (n = 0; n < len; n += count) {
204         count = len - n;
205         if (count > sizeof(buf))
206                 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);
211             }
212             printf(" %02x", (unsigned char)buf[i]);
213             if ((n + i) % 16 == 15 || i + 1 == count) {
214                 printf(" ");
215                 for (j = i & ~15; j <= i; ++j)
216                     printf("%c", isprint(buf[j]) ? buf[j] : '.');
217                 printf("\n");
218             }
219         }
220         printf("\n");
221     }
222 }
223
224 static void
225 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
226 {
227     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
228         perror("kvm_read");
229         exit(1);
230     }
231 }