4 * cc -I/usr/src/sys kmapinfo.c -o ~/bin/kmapinfo -lkvm
8 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
10 * This code is derived from software contributed to The DragonFly Project
11 * by Matthew Dillon <dillon@backplane.com>
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
23 * 3. Neither the name of The DragonFly Project nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific, prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
35 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
37 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #define _KERNEL_STRUCTURES_
42 #include <sys/param.h>
44 #include <sys/malloc.h>
45 #include <sys/signalvar.h>
46 #include <sys/vnode.h>
47 #include <sys/namecache.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_kern.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_map.h>
55 #include <vm/swap_pager.h>
56 #include <vm/vnode_pager.h>
74 vm_offset_t total_empty
;
75 vm_offset_t total_used
;
76 vm_offset_t total_used_byid
[VM_SUBSYS_LIMIT
];
78 static const char *formatnum(int64_t value
);
79 static const char *entryid(vm_subsys_t id
);
80 static void kkread(kvm_t
*kd
, u_long addr
, void *buf
, size_t nbytes
);
81 static void mapscan(kvm_t
*kd
, vm_map_entry_t entry
,
85 main(int ac
, char **av
)
87 const char *corefile
= NULL
;
88 const char *sysfile
= NULL
;
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) {
124 kkread(kd
, Nl
[0].n_value
, &kmap
, sizeof(kmap
));
125 last
= kmap
.header
.start
;
126 mapscan(kd
, kmap
.rb_root
.rbh_root
, &last
);
128 printf("%4ldM 0x%016jx %08lx-%08lx (%6s) EMPTY\n",
129 total_used
/ 1024 / 1024,
131 last
, kmap
.header
.end
,
132 formatnum(kmap
.header
.end
- last
));
133 total_empty
+= kmap
.header
.end
- last
;
135 printf("-----------------------------------------------\n");
136 for (i
= 0; i
< VM_SUBSYS_LIMIT
; ++i
)
137 printf("Total-id: %9s %s\n", entryid(i
), formatnum(total_used_byid
[i
]));
139 printf("-----------------------------------------------\n");
140 printf("Total empty space: %s\n", formatnum(total_empty
));
141 printf("Total used space: %s\n", formatnum(total_used
));
145 formatnum(int64_t value
)
158 if (value
< 100000) {
161 } else if (value
< 1 * 1024 * 1024) {
164 } else if (value
< 1024 * 1024 * 1024) {
167 } else if (value
< 1024LL * 1024 * 1024 * 1024) {
168 div
= 1024 * 1024 * 1024;
171 div
= 1024LL * 1024 * 1024 * 1024;
175 snprintf(buf
, sizeof(buf
), "");
176 } else if (div
== 1) {
177 snprintf(buf
, sizeof(buf
), "%s%7.0f%s",
179 (double)value
/ (double)div
,
182 snprintf(buf
, sizeof(buf
), "%s%6.2f%s",
184 (double)value
/ (double)div
,
191 mapscan(kvm_t
*kd
, vm_map_entry_t entryp
, vm_offset_t
*lastp
)
193 struct vm_map_entry entry
;
197 kkread(kd
, (u_long
)entryp
, &entry
, sizeof(entry
));
198 mapscan(kd
, entry
.rb_entry
.rbe_left
, lastp
);
199 if (*lastp
!= entry
.start
) {
200 printf("%4ldM %p %08lx-%08lx (%s) EMPTY\n",
201 total_used
/ 1024 / 1024,
204 formatnum(entry
.start
- *lastp
));
205 total_empty
+= entry
.start
- *lastp
;
207 printf("%4ldM %p %08lx-%08lx (%6ldK) id=%-8s object=%p\n",
208 total_used
/ 1024 / 1024,
210 entry
.start
, entry
.end
,
211 (entry
.end
- entry
.start
) / 1024,
213 entry
.object
.map_object
);
214 total_used
+= entry
.end
- entry
.start
;
215 if (entry
.id
< VM_SUBSYS_LIMIT
)
216 total_used_byid
[entry
.id
] += entry
.end
- entry
.start
;
218 total_used_byid
[0] += entry
.end
- entry
.start
;
220 mapscan(kd
, entry
.rb_entry
.rbe_right
, lastp
);
225 entryid(vm_subsys_t id
)
230 case VM_SUBSYS_UNKNOWN
:
232 case VM_SUBSYS_KMALLOC
:
234 case VM_SUBSYS_STACK
:
236 case VM_SUBSYS_IMGACT
:
240 case VM_SUBSYS_RESERVED
:
248 case VM_SUBSYS_SHMEM
:
250 case VM_SUBSYS_SYSMAP
:
256 case VM_SUBSYS_BOGUS
:
260 case VM_SUBSYS_BUFDATA
:
266 case VM_SUBSYS_PVENTRY
:
270 case VM_SUBSYS_MAPDEV
:
272 case VM_SUBSYS_ZALLOC
:
277 case VM_SUBSYS_CONTIG
:
281 case VM_SUBSYS_DRM_GEM
:
283 case VM_SUBSYS_DRM_SCAT
:
285 case VM_SUBSYS_DRM_VMAP
:
287 case VM_SUBSYS_DRM_TTM
:
289 case VM_SUBSYS_HAMMER
:
294 snprintf(buf
, sizeof(buf
), "%d", (int)id
);
301 kkread(kvm_t
*kd
, u_long addr
, void *buf
, size_t nbytes
)
303 if (kvm_read(kd
, addr
, buf
, nbytes
) != nbytes
) {