4 * cc -I/usr/src/sys vmpageinfo.c -o /usr/local/bin/vmpageinfo -lkvm
8 * Validate the vm_page_buckets[] hash array against the vm_page_array
11 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
13 * This code is derived from software contributed to The DragonFly Project
14 * by Matthew Dillon <dillon@backplane.com>
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in
24 * the documentation and/or other materials provided with the
26 * 3. Neither the name of The DragonFly Project nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific, prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
36 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
38 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
39 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
40 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * $DragonFly: src/test/debug/vmpageinfo.c,v 1.2 2006/05/23 03:00:05 dillon Exp $
46 #define _KERNEL_STRUCTURES_
47 #include <sys/param.h>
49 #include <sys/malloc.h>
50 #include <sys/signalvar.h>
51 #include <sys/vnode.h>
52 #include <sys/namecache.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>
71 { "_vm_page_buckets" },
72 { "_vm_page_hash_mask" },
74 { "_vm_page_array_size" },
80 struct vm_page
**vm_page_buckets
;
81 int vm_page_hash_mask
;
82 struct vm_page
*vm_page_array
;
83 int vm_page_array_size
;
85 void checkpage(kvm_t
*kd
, vm_page_t mptr
, vm_page_t m
, struct vm_object
*obj
);
86 void kkread(kvm_t
*kd
, u_long addr
, void *buf
, size_t nbytes
);
89 main(int ac
, char **av
)
91 const char *corefile
= NULL
;
92 const char *sysfile
= NULL
;
103 while ((ch
= getopt(ac
, av
, "M:N:dv")) != -1) {
118 fprintf(stderr
, "%s [-M core] [-N system]\n", av
[0]);
125 if ((kd
= kvm_open(sysfile
, corefile
, NULL
, O_RDONLY
, "kvm:")) == NULL
) {
129 if (kvm_nlist(kd
, Nl
) != 0) {
134 kkread(kd
, Nl
[0].n_value
, &vm_page_buckets
, sizeof(vm_page_buckets
));
135 kkread(kd
, Nl
[1].n_value
, &vm_page_hash_mask
, sizeof(vm_page_hash_mask
));
136 kkread(kd
, Nl
[2].n_value
, &vm_page_array
, sizeof(vm_page_array
));
137 kkread(kd
, Nl
[3].n_value
, &vm_page_array_size
, sizeof(vm_page_array_size
));
140 * Scan the vm_page_array validating all pages with associated objects
142 for (i
= 0; i
< vm_page_array_size
; ++i
) {
144 printf("page %d\r", i
);
147 kkread(kd
, (u_long
)&vm_page_array
[i
], &m
, sizeof(m
));
149 kkread(kd
, (u_long
)m
.object
, &obj
, sizeof(obj
));
150 checkpage(kd
, &vm_page_array
[i
], &m
, &obj
);
153 if (m
.queue
>= PQ_HOLD
) {
155 } else if (m
.queue
>= PQ_CACHE
) {
157 } else if (m
.queue
>= PQ_ACTIVE
) {
159 } else if (m
.queue
>= PQ_INACTIVE
) {
161 } else if (m
.queue
>= PQ_FREE
) {
166 printf("page %p val=%02x dty=%02x hold=%d wired=%-2d active=%-3d busy=%d/%d %s",
174 ((m
.flags
& PG_BUSY
) ? 1 : 0),
201 if (m
.object
&& verboseopt
> 1) {
202 printf("\tobj=%p type=%s\n", m
.object
, ostr
);
208 if (debugopt
|| verboseopt
)
212 * Scan the vm_page_buckets array validating all pages found
214 for (i
= 0; i
<= vm_page_hash_mask
; ++i
) {
216 printf("index %d\r", i
);
219 kkread(kd
, (u_long
)&vm_page_buckets
[i
], &mptr
, sizeof(mptr
));
221 kkread(kd
, (u_long
)mptr
, &m
, sizeof(m
));
223 kkread(kd
, (u_long
)m
.object
, &obj
, sizeof(obj
));
224 hv
= ((uintptr_t)m
.object
+ m
.pindex
) ^ obj
.hash_rand
;
225 hv
&= vm_page_hash_mask
;
227 printf("vm_page_buckets[%d] ((struct vm_page *)%p)"
228 " should be in bucket %d\n", i
, mptr
, hv
);
229 checkpage(kd
, mptr
, &m
, &obj
);
231 printf("vm_page_buckets[%d] ((struct vm_page *)%p)"
232 " has no object\n", i
, mptr
);
243 * A page with an object.
246 checkpage(kvm_t
*kd
, vm_page_t mptr
, vm_page_t m
, struct vm_object
*obj
)
252 hv
= ((uintptr_t)m
->object
+ m
->pindex
) ^ obj
->hash_rand
;
253 hv
&= vm_page_hash_mask
;
254 kkread(kd
, (u_long
)&vm_page_buckets
[hv
], &scanptr
, sizeof(scanptr
));
258 kkread(kd
, (u_long
)scanptr
, &scan
, sizeof(scan
));
259 scanptr
= scan
.hnext
;
263 printf("good checkpage %p bucket %d\n", mptr
, hv
);
265 printf("vm_page_buckets[%d] ((struct vm_page *)%p)"
266 " page not found in bucket list\n", hv
, mptr
);
271 kkread(kvm_t
*kd
, u_long addr
, void *buf
, size_t nbytes
)
273 if (kvm_read(kd
, addr
, buf
, nbytes
) != nbytes
) {