kernel - Fix overflows and races in zalloc()
[dragonfly.git] / sys / vm / vm_meter.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      @(#)vm_meter.c  8.4 (Berkeley) 1/4/94
36  * $FreeBSD: src/sys/vm/vm_meter.c,v 1.34.2.7 2002/10/10 19:28:22 dillon Exp $
37  * $DragonFly: src/sys/vm/vm_meter.c,v 1.15 2008/04/28 18:04:08 dillon Exp $
38  */
39
40 #include <sys/param.h>
41 #include <sys/proc.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/resource.h>
45 #include <sys/vmmeter.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_param.h>
51 #include <sys/lock.h>
52 #include <vm/pmap.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_object.h>
55 #include <sys/sysctl.h>
56
57 struct vmstats vmstats;
58
59 static int maxslp = MAXSLP;
60
61 SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
62         CTLFLAG_RW, &vmstats.v_free_min, 0,
63         "Minimum number of pages desired free");
64 SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
65         CTLFLAG_RW, &vmstats.v_free_target, 0,
66         "Number of pages desired free");
67 SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
68         CTLFLAG_RW, &vmstats.v_free_reserved, 0,
69         "Number of pages reserved for deadlock");
70 SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
71         CTLFLAG_RW, &vmstats.v_inactive_target, 0,
72         "Number of pages desired inactive");
73 SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
74         CTLFLAG_RW, &vmstats.v_cache_min, 0,
75         "Min number of pages desired on cache queue");
76 SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
77         CTLFLAG_RW, &vmstats.v_cache_max, 0,
78         "Max number of pages in cached obj");
79 SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
80         CTLFLAG_RW, &vmstats.v_pageout_free_min, 0,
81         "Min number pages reserved for kernel");
82 SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
83         CTLFLAG_RW, &vmstats.v_free_severe, 0, "");
84
85 SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD, 
86     &averunnable, loadavg, "Machine loadaverage history");
87
88 static int do_vmtotal_callback(struct proc *p, void *data);
89
90 /*
91  * No requirements.
92  */
93 static int
94 do_vmtotal(SYSCTL_HANDLER_ARGS)
95 {
96         struct vmtotal total;
97         struct vmtotal *totalp;
98         vm_object_t object;
99
100         bzero(&total, sizeof(total));
101         totalp = &total;
102
103         /*
104          * Mark all objects as inactive.
105          */
106         lwkt_gettoken(&vmobj_token);
107         for (object = TAILQ_FIRST(&vm_object_list);
108             object != NULL;
109             object = TAILQ_NEXT(object,object_list)) {
110                 if (object->type == OBJT_MARKER)
111                         continue;
112                 vm_object_clear_flag(object, OBJ_ACTIVE);
113         }
114         lwkt_reltoken(&vmobj_token);
115
116         /*
117          * Calculate process statistics.
118          */
119         allproc_scan(do_vmtotal_callback, totalp);
120
121         /*
122          * Calculate object memory usage statistics.
123          */
124         lwkt_gettoken(&vmobj_token);
125         for (object = TAILQ_FIRST(&vm_object_list);
126             object != NULL;
127             object = TAILQ_NEXT(object, object_list)) {
128                 /*
129                  * devices, like /dev/mem, will badly skew our totals.
130                  * markers aren't real objects.
131                  */
132                 if (object->type == OBJT_MARKER)
133                         continue;
134                 if (object->type == OBJT_DEVICE)
135                         continue;
136                 if (object->size >= 0x7FFFFFFF) {
137                         /*
138                          * Probably unbounded anonymous memory (really
139                          * bounded by related vm_map_entry structures which
140                          * we do not have access to in this loop).
141                          */
142                         totalp->t_vm += object->resident_page_count;
143                 } else {
144                         /*
145                          * It's questionable how useful this is but...
146                          */
147                         totalp->t_vm += object->size;
148                 }
149                 totalp->t_rm += object->resident_page_count;
150                 if (object->flags & OBJ_ACTIVE) {
151                         totalp->t_avm += object->size;
152                         totalp->t_arm += object->resident_page_count;
153                 }
154                 if (object->shadow_count > 1) {
155                         /* shared object */
156                         totalp->t_vmshr += object->size;
157                         totalp->t_rmshr += object->resident_page_count;
158                         if (object->flags & OBJ_ACTIVE) {
159                                 totalp->t_avmshr += object->size;
160                                 totalp->t_armshr += object->resident_page_count;
161                         }
162                 }
163         }
164         lwkt_reltoken(&vmobj_token);
165         totalp->t_free = vmstats.v_free_count + vmstats.v_cache_count;
166
167         return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
168 }
169
170 /*
171  * The caller must hold proc_token.
172  */
173 static int
174 do_vmtotal_callback(struct proc *p, void *data)
175 {
176         struct vmtotal *totalp = data;
177         struct lwp *lp;
178         vm_map_entry_t entry;
179         vm_map_t map;
180         int paging;
181
182         if (p->p_flag & P_SYSTEM)
183                 return(0);
184
185         FOREACH_LWP_IN_PROC(lp, p) {
186                 switch (lp->lwp_stat) {
187                 case LSSTOP:
188                 case LSSLEEP:
189                         if ((p->p_flag & P_SWAPPEDOUT) == 0) {
190                                 if ((lp->lwp_flag & LWP_SINTR) == 0)
191                                         totalp->t_dw++;
192                                 else if (lp->lwp_slptime < maxslp)
193                                         totalp->t_sl++;
194                         } else if (lp->lwp_slptime < maxslp) {
195                                 totalp->t_sw++;
196                         }
197                         if (lp->lwp_slptime >= maxslp)
198                                 return(0);
199                         break;
200
201                 case LSRUN:
202                         if (p->p_flag & P_SWAPPEDOUT)
203                                 totalp->t_sw++;
204                         else
205                                 totalp->t_rq++;
206                         if (p->p_stat == SIDL)
207                                 return(0);
208                         break;
209
210                 default:
211                         return (0);
212                 }
213         }
214
215         /*
216          * Note active objects.
217          */
218         paging = 0;
219         lwkt_gettoken(&p->p_token);
220         if (p->p_vmspace) {
221                 map = &p->p_vmspace->vm_map;
222                 vm_map_lock_read(map);
223                 for (entry = map->header.next;
224                      entry != &map->header; entry = entry->next) {
225                         if (entry->maptype != VM_MAPTYPE_NORMAL &&
226                             entry->maptype != VM_MAPTYPE_VPAGETABLE) {
227                                 continue;
228                         }
229                         if (entry->object.vm_object == NULL)
230                                 continue;
231                         vm_object_set_flag(entry->object.vm_object, OBJ_ACTIVE);
232                         paging |= entry->object.vm_object->paging_in_progress;
233                 }
234                 vm_map_unlock_read(map);
235         }
236         lwkt_reltoken(&p->p_token);
237         if (paging)
238                 totalp->t_pw++;
239         return(0);
240 }
241
242 /*
243  * No requirements.
244  */
245 static int
246 do_vmstats(SYSCTL_HANDLER_ARGS)
247 {
248         struct vmstats vms = vmstats;
249         return (sysctl_handle_opaque(oidp, &vms, sizeof(vms), req));
250 }
251
252 /*
253  * No requirements.
254  */
255 static int
256 do_vmmeter(SYSCTL_HANDLER_ARGS)
257 {
258         int boffset = offsetof(struct vmmeter, vmmeter_uint_begin);
259         int eoffset = offsetof(struct vmmeter, vmmeter_uint_end);
260         struct vmmeter vmm;
261         int i;
262
263         bzero(&vmm, sizeof(vmm));
264         for (i = 0; i < ncpus; ++i) {
265                 int off;
266                 struct globaldata *gd = globaldata_find(i);
267
268                 for (off = boffset; off <= eoffset; off += sizeof(u_int)) {
269                         *(u_int *)((char *)&vmm + off) +=
270                                 *(u_int *)((char *)&gd->gd_cnt + off);
271                 }
272                 
273         }
274         vmm.v_intr += vmm.v_ipi + vmm.v_timer;
275         return (sysctl_handle_opaque(oidp, &vmm, sizeof(vmm), req));
276 }
277
278 /*
279  * vcnt() -     accumulate statistics from the cnt structure for each cpu
280  *
281  *      The vmmeter structure is now per-cpu as well as global.  Those
282  *      statistics which can be kept on a per-cpu basis (to avoid cache
283  *      stalls between cpus) can be moved to the per-cpu vmmeter.  Remaining
284  *      statistics, such as v_free_reserved, are left in the global
285  *      structure.
286  *
287  * (sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
288  *
289  * No requirements.
290  */
291 static int
292 vcnt(SYSCTL_HANDLER_ARGS)
293 {
294         int i;
295         int count = 0;
296         int offset = arg2;
297
298         for (i = 0; i < ncpus; ++i) {
299                 struct globaldata *gd = globaldata_find(i);
300                 count += *(int *)((char *)&gd->gd_cnt + offset);
301         }
302         return(SYSCTL_OUT(req, &count, sizeof(int)));
303 }
304
305 /*
306  * No requirements.
307  */
308 static int
309 vcnt_intr(SYSCTL_HANDLER_ARGS)
310 {
311         int i;
312         int count = 0;
313
314         for (i = 0; i < ncpus; ++i) {
315                 struct globaldata *gd = globaldata_find(i);
316
317                 count += gd->gd_cnt.v_intr + gd->gd_cnt.v_ipi +
318                          gd->gd_cnt.v_timer;
319         }
320         return(SYSCTL_OUT(req, &count, sizeof(int)));
321 }
322
323 #define VMMETEROFF(var) offsetof(struct vmmeter, var)
324
325 SYSCTL_PROC(_vm, OID_AUTO, vmtotal, CTLTYPE_OPAQUE|CTLFLAG_RD,
326     0, sizeof(struct vmtotal), do_vmtotal, "S,vmtotal", 
327     "System virtual memory aggregate");
328 SYSCTL_PROC(_vm, OID_AUTO, vmstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
329     0, sizeof(struct vmstats), do_vmstats, "S,vmstats", 
330     "System virtual memory statistics");
331 SYSCTL_PROC(_vm, OID_AUTO, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
332     0, sizeof(struct vmmeter), do_vmmeter, "S,vmmeter", 
333     "System statistics");
334 SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
335 SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, "VM meter sys stats");
336 SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, "VM meter vm stats");
337 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
338
339 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_swtch, CTLTYPE_UINT|CTLFLAG_RD,
340         0, VMMETEROFF(v_swtch), vcnt, "IU", "Context switches");
341 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intrans_coll, CTLTYPE_UINT|CTLFLAG_RD,
342         0, VMMETEROFF(v_intrans_coll), vcnt, "IU", "Intransit map collisions (total)");
343 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intrans_wait, CTLTYPE_UINT|CTLFLAG_RD,
344         0, VMMETEROFF(v_intrans_wait), vcnt, "IU", "Intransit map collisions which blocked");
345 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_forwarded_ints, CTLTYPE_UINT|CTLFLAG_RD,
346         0, VMMETEROFF(v_forwarded_ints), vcnt, "IU", "Forwarded interrupts due to MP lock");
347 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_forwarded_hits, CTLTYPE_UINT|CTLFLAG_RD,
348         0, VMMETEROFF(v_forwarded_hits), vcnt, "IU", "Forwarded hits due to MP lock");
349 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_forwarded_misses, CTLTYPE_UINT|CTLFLAG_RD,
350         0, VMMETEROFF(v_forwarded_misses), vcnt, "IU", "Forwarded misses due to MP lock");
351 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_trap, CTLTYPE_UINT|CTLFLAG_RD,
352         0, VMMETEROFF(v_trap), vcnt, "IU", "Traps");
353 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_syscall, CTLTYPE_UINT|CTLFLAG_RD,
354         0, VMMETEROFF(v_syscall), vcnt, "IU", "Syscalls");
355 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intr, CTLTYPE_UINT|CTLFLAG_RD,
356         0, VMMETEROFF(v_intr), vcnt_intr, "IU", "Hardware interrupts");
357 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_ipi, CTLTYPE_UINT|CTLFLAG_RD,
358         0, VMMETEROFF(v_ipi), vcnt, "IU", "Inter-processor interrupts");
359 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_timer, CTLTYPE_UINT|CTLFLAG_RD,
360         0, VMMETEROFF(v_timer), vcnt, "IU", "LAPIC timer interrupts");
361 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_soft, CTLTYPE_UINT|CTLFLAG_RD,
362         0, VMMETEROFF(v_soft), vcnt, "IU", "Software interrupts");
363 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vm_faults, CTLTYPE_UINT|CTLFLAG_RD,
364         0, VMMETEROFF(v_vm_faults), vcnt, "IU", "VM faults");
365 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_faults, CTLTYPE_UINT|CTLFLAG_RD,
366         0, VMMETEROFF(v_cow_faults), vcnt, "IU", "COW faults");
367 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_optim, CTLTYPE_UINT|CTLFLAG_RD,
368         0, VMMETEROFF(v_cow_optim), vcnt, "IU", "Optimized COW faults");
369 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_zfod, CTLTYPE_UINT|CTLFLAG_RD,
370         0, VMMETEROFF(v_zfod), vcnt, "IU", "Zero fill");
371 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_ozfod, CTLTYPE_UINT|CTLFLAG_RD,
372         0, VMMETEROFF(v_ozfod), vcnt, "IU", "Optimized zero fill");
373 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapin, CTLTYPE_UINT|CTLFLAG_RD,
374         0, VMMETEROFF(v_swapin), vcnt, "IU", "Swapin operations");
375 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapout, CTLTYPE_UINT|CTLFLAG_RD,
376         0, VMMETEROFF(v_swapout), vcnt, "IU", "Swapout operations");
377 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsin, CTLTYPE_UINT|CTLFLAG_RD,
378         0, VMMETEROFF(v_swappgsin), vcnt, "IU", "Swapin pages");
379 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsout, CTLTYPE_UINT|CTLFLAG_RD,
380         0, VMMETEROFF(v_swappgsout), vcnt, "IU", "Swapout pages");
381 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodein, CTLTYPE_UINT|CTLFLAG_RD,
382         0, VMMETEROFF(v_vnodein), vcnt, "IU", "Vnodein operations");
383 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodeout, CTLTYPE_UINT|CTLFLAG_RD,
384         0, VMMETEROFF(v_vnodeout), vcnt, "IU", "Vnodeout operations");
385 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsin, CTLTYPE_UINT|CTLFLAG_RD,
386         0, VMMETEROFF(v_vnodepgsin), vcnt, "IU", "Vnodein pages");
387 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsout, CTLTYPE_UINT|CTLFLAG_RD,
388         0, VMMETEROFF(v_vnodepgsout), vcnt, "IU", "Vnodeout pages");
389 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_intrans, CTLTYPE_UINT|CTLFLAG_RD,
390         0, VMMETEROFF(v_intrans), vcnt, "IU", "In transit page blocking");
391 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_reactivated, CTLTYPE_UINT|CTLFLAG_RD,
392         0, VMMETEROFF(v_reactivated), vcnt, "IU", "Reactivated pages");
393 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdwakeups, CTLTYPE_UINT|CTLFLAG_RD,
394         0, VMMETEROFF(v_pdwakeups), vcnt, "IU", "Pagedaemon wakeups");
395 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_ppwakeups, CTLTYPE_UINT|CTLFLAG_RD,
396         0, VMMETEROFF(v_ppwakeups), vcnt, "IU", "vm_wait wakeups");
397 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdpages, CTLTYPE_UINT|CTLFLAG_RD,
398         0, VMMETEROFF(v_pdpages), vcnt, "IU", "Pagedaemon page scans");
399 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_dfree, CTLTYPE_UINT|CTLFLAG_RD,
400         0, VMMETEROFF(v_dfree), vcnt, "IU", "Pages freed by daemon");
401 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pfree, CTLTYPE_UINT|CTLFLAG_RD,
402         0, VMMETEROFF(v_pfree), vcnt, "IU", "Pages freed by exiting processes");
403 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_tfree, CTLTYPE_UINT|CTLFLAG_RD,
404         0, VMMETEROFF(v_tfree), vcnt, "IU", "Total pages freed");
405 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forks, CTLTYPE_UINT|CTLFLAG_RD,
406         0, VMMETEROFF(v_forks), vcnt, "IU", "Number of fork() calls");
407 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforks, CTLTYPE_UINT|CTLFLAG_RD,
408         0, VMMETEROFF(v_vforks), vcnt, "IU", "Number of vfork() calls");
409 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforks, CTLTYPE_UINT|CTLFLAG_RD,
410         0, VMMETEROFF(v_rforks), vcnt, "IU", "Number of rfork() calls");
411 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreads, CTLTYPE_UINT|CTLFLAG_RD,
412         0, VMMETEROFF(v_kthreads), vcnt, "IU", "Number of fork() calls by kernel");
413 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forkpages, CTLTYPE_UINT|CTLFLAG_RD,
414         0, VMMETEROFF(v_forkpages), vcnt, "IU", "VM pages affected by fork()");
415 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforkpages, CTLTYPE_UINT|CTLFLAG_RD,
416         0, VMMETEROFF(v_vforkpages), vcnt, "IU", "VM pages affected by vfork()");
417 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforkpages, CTLTYPE_UINT|CTLFLAG_RD,
418         0, VMMETEROFF(v_rforkpages), vcnt, "IU", "VM pages affected by rfork()");
419 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreadpages, CTLTYPE_UINT|CTLFLAG_RD,
420         0, VMMETEROFF(v_kthreadpages), vcnt, "IU", "VM pages affected by fork() by kernel");
421
422 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
423         v_page_size, CTLFLAG_RD, &vmstats.v_page_size, 0,
424         "Page size in bytes");
425 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
426         v_page_count, CTLFLAG_RD, &vmstats.v_page_count, 0, 
427         "Total number of pages in system");
428 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
429         v_free_reserved, CTLFLAG_RD, &vmstats.v_free_reserved, 0,
430         "Number of pages reserved for deadlock");
431 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
432         v_free_target, CTLFLAG_RD, &vmstats.v_free_target, 0,
433         "Number of pages desired free");
434 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
435         v_free_min, CTLFLAG_RD, &vmstats.v_free_min, 0,
436         "Minimum number of pages desired free");
437 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
438         v_free_count, CTLFLAG_RD, &vmstats.v_free_count, 0,
439         "Number of pages free");
440 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
441         v_wire_count, CTLFLAG_RD, &vmstats.v_wire_count, 0,
442         "Number of pages wired down");
443 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
444         v_active_count, CTLFLAG_RD, &vmstats.v_active_count, 0,
445         "Number of pages active");
446 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
447         v_inactive_target, CTLFLAG_RD, &vmstats.v_inactive_target, 0,
448         "Number of pages desired inactive");
449 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
450         v_inactive_count, CTLFLAG_RD, &vmstats.v_inactive_count, 0,
451         "Number of pages inactive");
452 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
453         v_cache_count, CTLFLAG_RD, &vmstats.v_cache_count, 0,
454         "Number of pages on buffer cache queue");
455 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
456         v_cache_min, CTLFLAG_RD, &vmstats.v_cache_min, 0,
457         "Min number of pages desired on cache queue");
458 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
459         v_cache_max, CTLFLAG_RD, &vmstats.v_cache_max, 0,
460         "Max number of pages in cached obj");
461 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
462         v_pageout_free_min, CTLFLAG_RD, &vmstats.v_pageout_free_min, 0,
463         "Min number pages reserved for kernel");
464 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
465         v_interrupt_free_min, CTLFLAG_RD, &vmstats.v_interrupt_free_min, 0,
466         "Reserved number of pages for int code");
467 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
468         zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0,
469         "Number of zeroing pages");
470
471 /*
472  * No requirements.
473  */
474 static int
475 do_vmmeter_pcpu(SYSCTL_HANDLER_ARGS)
476 {
477         int boffset = offsetof(struct vmmeter, vmmeter_uint_begin);
478         int eoffset = offsetof(struct vmmeter, vmmeter_uint_end);
479         struct globaldata *gd = arg1;
480         struct vmmeter vmm;
481         int off;
482
483         bzero(&vmm, sizeof(vmm));
484         for (off = boffset; off <= eoffset; off += sizeof(u_int)) {
485                 *(u_int *)((char *)&vmm + off) +=
486                         *(u_int *)((char *)&gd->gd_cnt + off);
487         }
488         vmm.v_intr += vmm.v_ipi + vmm.v_timer;
489         return (sysctl_handle_opaque(oidp, &vmm, sizeof(vmm), req));
490 }
491
492 /*
493  * Called from the low level boot code only.
494  */
495 static void
496 vmmeter_init(void *dummy __unused)
497 {
498         int i;
499
500         for (i = 0; i < ncpus; ++i) {
501                 struct sysctl_ctx_list *ctx;
502                 struct sysctl_oid *oid;
503                 struct globaldata *gd;
504                 char name[32];
505
506                 ksnprintf(name, sizeof(name), "cpu%d", i);
507
508                 ctx = kmalloc(sizeof(*ctx), M_TEMP, M_WAITOK);
509                 sysctl_ctx_init(ctx);
510                 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_STATIC_CHILDREN(_vm),
511                                       OID_AUTO, name, CTLFLAG_RD, 0, "");
512
513                 gd = globaldata_find(i);
514                 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
515                                 "vmmeter", CTLTYPE_OPAQUE|CTLFLAG_RD,
516                                 gd, sizeof(struct vmmeter), do_vmmeter_pcpu,
517                                 "S,vmmeter", "System per-cpu statistics");
518         }
519 }
520 SYSINIT(vmmeter, SI_SUB_PSEUDO, SI_ORDER_ANY, vmmeter_init, 0);