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_object.h>
53 #include <vm/vm_map.h>
54 #include <vm/swap_pager.h>
55 #include <vm/vnode_pager.h>
73 vm_offset_t total_empty
;
74 vm_offset_t total_used
;
75 vm_offset_t total_real
;
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
, int *realmemp
);
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 kptr
, vm_map_entry_t ken
,
85 main(int ac
, char **av
)
87 const char *corefile
= NULL
;
88 const char *sysfile
= NULL
;
93 struct vm_map_entry entry
;
94 struct vm_map_entry
*kptr
;
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) {
126 kkread(kd
, Nl
[0].n_value
, &kmap
, sizeof(kmap
));
127 last
= kmap
.min_addr
;
129 kptr
= kvm_vm_map_entry_first(kd
, &kmap
, &entry
);
131 mapscan(kd
, kptr
, &entry
, &last
);
132 kptr
= kvm_vm_map_entry_next(kd
, kptr
, &entry
);
135 printf("%4ldM 0x%016jx %08lx-%08lx (%6s) EMPTY\n",
136 total_used
/ 1024 / 1024,
139 formatnum(kmap
.max_addr
- last
));
140 total_empty
+= kmap
.max_addr
- last
;
142 printf("-----------------------------------------------\n");
143 for (i
= 0; i
< VM_SUBSYS_LIMIT
; ++i
) {
145 const char *id
= entryid(i
, &realmem
);
147 printf("Total-id: %9s %s%s\n",
149 formatnum(total_used_byid
[i
]),
150 (realmem
? " (real memory)" : ""));
153 printf("-----------------------------------------------\n");
154 printf("Total empty space: %s\n", formatnum(total_empty
));
155 printf("Total used space: %s\n", formatnum(total_used
));
156 printf("Total real space: %s\n", formatnum(total_real
));
160 formatnum(int64_t value
)
173 if (value
< 100000) {
176 } else if (value
< 1 * 1024 * 1024) {
179 } else if (value
< 1024 * 1024 * 1024) {
182 } else if (value
< 1024LL * 1024 * 1024 * 1024) {
183 div
= 1024 * 1024 * 1024;
186 div
= 1024LL * 1024 * 1024 * 1024;
190 snprintf(buf
, sizeof(buf
), "");
191 } else if (div
== 1) {
192 snprintf(buf
, sizeof(buf
), "%s%7.0f%s",
194 (double)value
/ (double)div
,
197 snprintf(buf
, sizeof(buf
), "%s%6.2f%s",
199 (double)value
/ (double)div
,
206 mapscan(kvm_t
*kd
, vm_map_entry_t kptr
, vm_map_entry_t ken
, vm_offset_t
*lastp
)
210 if (*lastp
!= ken
->ba
.start
) {
211 printf("%4ldM %p %08lx-%08lx (%s) EMPTY\n",
212 total_used
/ 1024 / 1024,
214 *lastp
, ken
->ba
.start
,
215 formatnum(ken
->ba
.start
- *lastp
));
216 total_empty
+= ken
->ba
.start
- *lastp
;
218 printf("%4ldM %p %08lx-%08lx (%6ldK) id=%-8s object=%p\n",
219 total_used
/ 1024 / 1024,
221 ken
->ba
.start
, ken
->ba
.end
,
222 (ken
->ba
.end
- ken
->ba
.start
) / 1024,
223 entryid(ken
->id
, &realmem
),
225 total_used
+= ken
->ba
.end
- ken
->ba
.start
;
227 if (ken
->id
< VM_SUBSYS_LIMIT
)
228 total_used_byid
[ken
->id
] += ken
->ba
.end
- ken
->ba
.start
;
230 total_used_byid
[0] += ken
->ba
.end
- ken
->ba
.start
;
233 total_real
+= ken
->ba
.end
- ken
->ba
.start
;
235 *lastp
= ken
->ba
.end
;
240 entryid(vm_subsys_t id
, int *realmemp
)
244 int *realmem
= (realmemp
? realmemp
: &dummy
);
249 case VM_SUBSYS_UNKNOWN
:
251 case VM_SUBSYS_KMALLOC
:
254 case VM_SUBSYS_STACK
:
257 case VM_SUBSYS_IMGACT
:
261 case VM_SUBSYS_RESERVED
:
270 case VM_SUBSYS_SHMEM
:
272 case VM_SUBSYS_SYSMAP
:
278 case VM_SUBSYS_BOGUS
:
283 case VM_SUBSYS_BUFDATA
:
291 case VM_SUBSYS_PVENTRY
:
296 case VM_SUBSYS_MAPDEV
:
298 case VM_SUBSYS_ZALLOC
:
303 case VM_SUBSYS_CONTIG
:
307 case VM_SUBSYS_DRM_GEM
:
309 case VM_SUBSYS_DRM_SCAT
:
311 case VM_SUBSYS_DRM_VMAP
:
314 case VM_SUBSYS_DRM_TTM
:
316 case VM_SUBSYS_HAMMER
:
318 case VM_SUBSYS_VMPGHASH
:
324 snprintf(buf
, sizeof(buf
), "%d", (int)id
);
331 kkread(kvm_t
*kd
, u_long addr
, void *buf
, size_t nbytes
)
333 if (kvm_read(kd
, addr
, buf
, nbytes
) != nbytes
) {