4 * cc -I/usr/src/sys zallocinfo.c -o /usr/local/bin/zallocinfo -lkvm
8 * Print the slab structure and chains for all cpus.
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_
44 #include <sys/param.h>
46 #include <sys/malloc.h>
47 #include <sys/slaballoc.h>
48 #include <sys/signalvar.h>
49 #include <sys/globaldata.h>
50 #include <machine/globaldata.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_object.h>
57 #include <vm/swap_pager.h>
58 #include <vm/vnode_pager.h>
79 static void dumpslab(kvm_t
*kd
, int cpu
, struct SLGlobalData
*slab
);
80 static void kkread(kvm_t
*kd
, u_long addr
, void *buf
, size_t nbytes
);
83 main(int ac
, char **av
)
85 const char *corefile
= NULL
;
86 const char *sysfile
= NULL
;
87 struct SLGlobalData slab
;
94 while ((ch
= getopt(ac
, av
, "M:N:dv")) != -1) {
109 fprintf(stderr
, "%s [-M core] [-N system]\n", av
[0]);
116 if ((kd
= kvm_open(sysfile
, corefile
, NULL
, O_RDONLY
, "kvm:")) == NULL
) {
120 if (kvm_nlist(kd
, Nl
) != 0) {
125 kkread(kd
, Nl
[1].n_value
, &ncpus
, sizeof(ncpus
));
127 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
129 * Get the CPU_prvspace base pointer, that is the address to
130 * CPU_prvspace[i] from where other addresses can be built
132 kkread(kd
, Nl
[0].n_value
+ cpu
* sizeof(baseptr
),
133 &baseptr
, sizeof(baseptr
));
134 /* Now get globaldata struct for the current cpu */
135 kkread(kd
, baseptr
+ offsetof(struct mdglobaldata
, mi
.gd_slab
),
136 &slab
, sizeof(slab
));
137 dumpslab(kd
, cpu
, &slab
);
144 dumpslab(kvm_t
*kd
, int cpu
, struct SLGlobalData
*slab
)
146 struct SLZone
*zonep
;
158 printf("cpu %d NFreeZones=%d JunkIndex=%d\n", cpu
, slab
->NFreeZones
,
161 for (z
= 0; z
< NZONES
; z
++) {
162 for (pass
= 1; pass
<= 2; ++pass
) {
163 zonep
= TAILQ_FIRST(&slab
->ZoneAry
[z
]);
168 kkread(kd
, (u_long
)zonep
, &zone
, sizeof(zone
));
172 printf(" zone %2d", z
);
173 printf(" chunk=%-5d elms=%-4d ",
174 zone
.z_ChunkSize
, zone
.z_NMax
);
176 } else if (pass
== 2 && first
== 0) {
182 printf(" %d", zone
.z_NFree
);
183 extra
+= zone
.z_NFree
* zone
.z_ChunkSize
;
185 chunkp
= zone
.z_RChunks
;
188 kkread(kd
, (u_long
)chunkp
, &chunk
, sizeof(chunk
));
189 chunkp
= chunk
.c_Next
;
195 printf(" [rc=%d", xcount
);
196 extra
+= xcount
* zone
.z_ChunkSize
;
198 chunkp
= zone
.z_LChunks
;
201 kkread(kd
, (u_long
)chunkp
, &chunk
, sizeof(chunk
));
202 chunkp
= chunk
.c_Next
;
211 printf("lc=%d", rcount
);
213 extra
+= rcount
* zone
.z_ChunkSize
;
215 if (rcount
|| xcount
) {
219 zonep
= TAILQ_NEXT(&zone
, z_Entry
);
221 if (first
== 0 && pass
== 1)
222 printf(" %6jdK free:", (intmax_t)(extra
- save
) / 1024);
223 if (first
== 0 && pass
== 2)
227 printf(" TotalUnused %jd.%03dM\n",
228 (intmax_t)extra
/ 1024 / 1024,
229 (int)(extra
% 1024) * 999 / 1024);
233 kkread(kvm_t
*kd
, u_long addr
, void *buf
, size_t nbytes
)
235 if (kvm_read(kd
, addr
, buf
, nbytes
) != nbytes
) {