4 * cc -I/usr/src/sys slabinfo.c -o /usr/local/bin/slabinfo -lkvm
8 * dump kernel slab allocator pcpu data and chains
10 * Copyright (c) 2012 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/signalvar.h>
48 #include <sys/vnode.h>
49 #include <sys/namecache.h>
50 #include <sys/globaldata.h>
51 #include <machine/globaldata.h>
52 #include <sys/slaballoc.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_object.h>
59 #include <vm/swap_pager.h>
60 #include <vm/vnode_pager.h>
79 int slzonedump(kvm_t *kd, SLZone *kslz);
80 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;
96 while ((ch = getopt(ac, av, "M:N:dv")) != -1) {
111 fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
118 if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
122 if (kvm_nlist(kd, Nl) != 0) {
127 kkread(kd, Nl[1].n_value, &ncpus, sizeof(int));
130 for (i = 0; i < ncpus; ++i) {
131 kkread(kd, Nl[0].n_value + i * sizeof(struct privatespace), &gd, sizeof(gd));
132 printf("CPU %02d (NFreeZones=%d) {\n",
133 i, gd.gd_slab.NFreeZones);
134 totalfree += gd.gd_slab.NFreeZones;
136 for (j = 0; j < NZONES; ++j) {
137 printf(" Zone %02d {\n", j);
138 totalzones += slzonedump(kd, gd.gd_slab.ZoneAry[j]);
142 printf(" FreeZone {\n");
143 totalzones += slzonedump(kd, gd.gd_slab.FreeZones);
146 printf(" FreeOVZon {\n");
147 totalzones += slzonedump(kd, gd.gd_slab.FreeOvZones);
152 printf("TotalZones %d x 131072 = %jd\n",
153 totalzones, (intmax_t)totalzones * 131072LL);
154 printf("TotalFree %d x 131072 = %jd\n",
155 totalfree, (intmax_t)totalfree * 131072LL);
160 slzonedump(kvm_t *kd, SLZone *kslz)
166 kkread(kd, (u_long)kslz, &slz, sizeof(slz));
167 printf("\t{ magic=%08x cpu=%d chunking=%d NFree=%d/%d RCnt=%d}\n",
168 slz.z_Magic, slz.z_Cpu, slz.z_ChunkSize,
169 slz.z_NFree, slz.z_NMax, slz.z_RCount);
177 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
179 if (kvm_read(kd, addr, buf, nbytes) != nbytes) {