boot/efi: Bring in a bunch of additional TianoCore EDK II headers.
[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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      @(#)vm_meter.c  8.4 (Berkeley) 1/4/94
32  * $FreeBSD: src/sys/vm/vm_meter.c,v 1.34.2.7 2002/10/10 19:28:22 dillon Exp $
33  * $DragonFly: src/sys/vm/vm_meter.c,v 1.15 2008/04/28 18:04:08 dillon Exp $
34  */
35
36 #include <sys/param.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/resource.h>
41 #include <sys/vmmeter.h>
42 #include <sys/kcollect.h>
43
44 #include <vm/vm.h>
45 #include <vm/vm_page.h>
46 #include <vm/vm_extern.h>
47 #include <vm/vm_param.h>
48 #include <sys/lock.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_object.h>
52 #include <sys/sysctl.h>
53
54 /*
55  * WARNING: vmstats represents the final say, but individual cpu's may
56  *          accumulative adjustments in gd->gd_vmstats_adj.  These are
57  *          synchronized to the global vmstats in hardclock.
58  *
59  *          In addition, most individual cpus check vmstats using a local
60  *          copy of the global vmstats in gd->gd_vmstats.  Hardclock also
61  *          sychronizes the copy.  The pageout code and vm_page_alloc will
62  *          also synchronize their local copies as necessary.
63  *
64  *          Other consumers should not expect perfect values.
65  */
66 __cachealign struct vmstats vmstats;
67
68 static int maxslp = MAXSLP;
69
70 SYSCTL_ULONG(_vm, VM_V_FREE_MIN, v_free_min,
71         CTLFLAG_RW, &vmstats.v_free_min, 0,
72         "Minimum number of pages desired free");
73 SYSCTL_ULONG(_vm, VM_V_FREE_TARGET, v_free_target,
74         CTLFLAG_RW, &vmstats.v_free_target, 0,
75         "Number of pages desired free");
76 SYSCTL_ULONG(_vm, VM_V_FREE_RESERVED, v_free_reserved,
77         CTLFLAG_RW, &vmstats.v_free_reserved, 0,
78         "Number of pages reserved for deadlock");
79 SYSCTL_ULONG(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
80         CTLFLAG_RW, &vmstats.v_inactive_target, 0,
81         "Number of pages desired inactive");
82 SYSCTL_ULONG(_vm, VM_V_CACHE_MIN, v_cache_min,
83         CTLFLAG_RW, &vmstats.v_cache_min, 0,
84         "Min number of pages desired on cache queue");
85 SYSCTL_ULONG(_vm, VM_V_CACHE_MAX, v_cache_max,
86         CTLFLAG_RW, &vmstats.v_cache_max, 0,
87         "Max number of pages in cached obj");
88 SYSCTL_ULONG(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
89         CTLFLAG_RW, &vmstats.v_pageout_free_min, 0,
90         "Min number pages reserved for kernel");
91 SYSCTL_ULONG(_vm, OID_AUTO, v_free_severe,
92         CTLFLAG_RW, &vmstats.v_free_severe, 0, "");
93
94 SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg,
95         CTLFLAG_RD | CTLFLAG_NOLOCK,
96         &averunnable, loadavg, "Machine loadaverage history");
97
98 static int do_vmtotal_callback(struct proc *p, void *data);
99
100 /*
101  * No requirements.
102  */
103 static int
104 do_vmtotal(SYSCTL_HANDLER_ARGS)
105 {
106         struct vmtotal total;
107         globaldata_t gd;
108         int n;
109
110         bzero(&total, sizeof(total));
111         for (n = 0; n < ncpus; ++n) {
112                 gd = globaldata_find(n);
113
114                 /* total.t_rq calculated separately */
115                 /* total.t_dw calculated separately */
116                 /* total.t_pw calculated separately */
117                 /* total.t_sl calculated separately */
118                 /* total.t_sw calculated separately */
119                 total.t_vm += gd->gd_vmtotal.t_vm;
120                 total.t_avm += gd->gd_vmtotal.t_avm;
121                 total.t_rm += gd->gd_vmtotal.t_rm;
122                 total.t_arm += gd->gd_vmtotal.t_arm;
123                 total.t_vmshr += gd->gd_vmtotal.t_vmshr;
124                 total.t_avmshr += gd->gd_vmtotal.t_avmshr;
125                 total.t_rmshr += gd->gd_vmtotal.t_rmshr;
126                 total.t_armshr += gd->gd_vmtotal.t_armshr;
127                 /* total.t_free calculated separately */
128         }
129
130         /*
131          * Calculate process statistics.
132          */
133         allproc_scan(do_vmtotal_callback, &total, 0);
134
135         /*
136          * Adjust for sysctl return.  Add real memory into virtual memory.
137          * Set t_free.
138          *
139          * t_rm - Real memory
140          * t_vm - Virtual memory (real + swap)
141          */
142         total.t_vm += total.t_rm;
143         total.t_free = vmstats.v_free_count + vmstats.v_cache_count;
144
145         return (sysctl_handle_opaque(oidp, &total, sizeof(total), req));
146 }
147
148 static int
149 do_vmtotal_callback(struct proc *p, void *data)
150 {
151         struct vmtotal *totalp = data;
152         struct lwp *lp;
153
154         if (p->p_flags & P_SYSTEM)
155                 return(0);
156
157         lwkt_gettoken_shared(&p->p_token);
158
159         FOREACH_LWP_IN_PROC(lp, p) {
160                 switch (lp->lwp_stat) {
161                 case LSSTOP:
162                 case LSSLEEP:
163                         if ((p->p_flags & P_SWAPPEDOUT) == 0) {
164                                 if ((lp->lwp_flags & LWP_SINTR) == 0)
165                                         totalp->t_dw++;
166                                 else if (lp->lwp_slptime < maxslp)
167                                         totalp->t_sl++;
168                         } else if (lp->lwp_slptime < maxslp) {
169                                 totalp->t_sw++;
170                         }
171                         if (lp->lwp_slptime >= maxslp)
172                                 goto out;
173                         break;
174
175                 case LSRUN:
176                         if (p->p_flags & P_SWAPPEDOUT)
177                                 totalp->t_sw++;
178                         else
179                                 totalp->t_rq++;
180                         if (p->p_stat == SIDL)
181                                 goto out;
182                         break;
183
184                 default:
185                         goto out;
186                 }
187
188                 /*
189                  * Set while in vm_fault()
190                  */
191                 if (lp->lwp_flags & LWP_PAGING)
192                         totalp->t_pw++;
193         }
194 out:
195         lwkt_reltoken(&p->p_token);
196         return(0);
197 }
198
199 /*
200  * No requirements.
201  */
202 static int
203 do_vmstats(SYSCTL_HANDLER_ARGS)
204 {
205         struct vmstats vms = vmstats;
206         return (sysctl_handle_opaque(oidp, &vms, sizeof(vms), req));
207 }
208
209 /*
210  * No requirements.
211  */
212 static int
213 do_vmmeter(SYSCTL_HANDLER_ARGS)
214 {
215         int boffset = offsetof(struct vmmeter, vmmeter_uint_begin);
216         int eoffset = offsetof(struct vmmeter, vmmeter_uint_end);
217         struct vmmeter vmm;
218         int i;
219
220         bzero(&vmm, sizeof(vmm));
221         for (i = 0; i < ncpus; ++i) {
222                 int off;
223                 struct globaldata *gd = globaldata_find(i);
224
225                 for (off = boffset; off <= eoffset; off += sizeof(u_int)) {
226                         *(u_int *)((char *)&vmm + off) +=
227                                 *(u_int *)((char *)&gd->gd_cnt + off);
228                 }
229                 
230         }
231         vmm.v_intr += vmm.v_ipi + vmm.v_timer;
232         return (sysctl_handle_opaque(oidp, &vmm, sizeof(vmm), req));
233 }
234
235 /*
236  * vcnt() -     accumulate statistics from the cnt structure for each cpu
237  *
238  *      The vmmeter structure is now per-cpu as well as global.  Those
239  *      statistics which can be kept on a per-cpu basis (to avoid cache
240  *      stalls between cpus) can be moved to the per-cpu vmmeter.  Remaining
241  *      statistics, such as v_free_reserved, are left in the global
242  *      structure.
243  *
244  * (sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
245  *
246  * No requirements.
247  */
248 static int
249 vcnt(SYSCTL_HANDLER_ARGS)
250 {
251         int i;
252         int count = 0;
253         int offset = arg2;
254
255         for (i = 0; i < ncpus; ++i) {
256                 struct globaldata *gd = globaldata_find(i);
257                 count += *(int *)((char *)&gd->gd_cnt + offset);
258         }
259         return(SYSCTL_OUT(req, &count, sizeof(int)));
260 }
261
262 /*
263  * No requirements.
264  */
265 static int
266 vcnt_intr(SYSCTL_HANDLER_ARGS)
267 {
268         int i;
269         int count = 0;
270
271         for (i = 0; i < ncpus; ++i) {
272                 struct globaldata *gd = globaldata_find(i);
273
274                 count += gd->gd_cnt.v_intr + gd->gd_cnt.v_ipi +
275                          gd->gd_cnt.v_timer;
276         }
277         return(SYSCTL_OUT(req, &count, sizeof(int)));
278 }
279
280 #define VMMETEROFF(var) offsetof(struct vmmeter, var)
281
282 SYSCTL_PROC(_vm, OID_AUTO, vmtotal, CTLTYPE_OPAQUE|CTLFLAG_RD,
283     0, sizeof(struct vmtotal), do_vmtotal, "S,vmtotal", 
284     "System virtual memory aggregate");
285 SYSCTL_PROC(_vm, OID_AUTO, vmstats, CTLTYPE_OPAQUE|CTLFLAG_RD,
286     0, sizeof(struct vmstats), do_vmstats, "S,vmstats", 
287     "System virtual memory statistics");
288 SYSCTL_PROC(_vm, OID_AUTO, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
289     0, sizeof(struct vmmeter), do_vmmeter, "S,vmmeter", 
290     "System statistics");
291 SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
292 SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, "VM meter sys stats");
293 SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, "VM meter vm stats");
294 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
295
296 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_swtch, CTLTYPE_UINT|CTLFLAG_RD,
297         0, VMMETEROFF(v_swtch), vcnt, "IU", "Context switches");
298 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intrans_coll, CTLTYPE_UINT|CTLFLAG_RD,
299         0, VMMETEROFF(v_intrans_coll), vcnt, "IU", "Intransit map collisions (total)");
300 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intrans_wait, CTLTYPE_UINT|CTLFLAG_RD,
301         0, VMMETEROFF(v_intrans_wait), vcnt, "IU", "Intransit map collisions which blocked");
302 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_forwarded_ints, CTLTYPE_UINT|CTLFLAG_RD,
303         0, VMMETEROFF(v_forwarded_ints), vcnt, "IU", "Forwarded interrupts due to MP lock");
304 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_forwarded_hits, CTLTYPE_UINT|CTLFLAG_RD,
305         0, VMMETEROFF(v_forwarded_hits), vcnt, "IU", "Forwarded hits due to MP lock");
306 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_forwarded_misses, CTLTYPE_UINT|CTLFLAG_RD,
307         0, VMMETEROFF(v_forwarded_misses), vcnt, "IU", "Forwarded misses due to MP lock");
308 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_trap, CTLTYPE_UINT|CTLFLAG_RD,
309         0, VMMETEROFF(v_trap), vcnt, "IU", "Traps");
310 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_syscall, CTLTYPE_UINT|CTLFLAG_RD,
311         0, VMMETEROFF(v_syscall), vcnt, "IU", "Syscalls");
312 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intr, CTLTYPE_UINT|CTLFLAG_RD,
313         0, VMMETEROFF(v_intr), vcnt_intr, "IU", "Hardware interrupts");
314 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_ipi, CTLTYPE_UINT|CTLFLAG_RD,
315         0, VMMETEROFF(v_ipi), vcnt, "IU", "Inter-processor interrupts");
316 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_timer, CTLTYPE_UINT|CTLFLAG_RD,
317         0, VMMETEROFF(v_timer), vcnt, "IU", "LAPIC timer interrupts");
318 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_soft, CTLTYPE_UINT|CTLFLAG_RD,
319         0, VMMETEROFF(v_soft), vcnt, "IU", "Software interrupts");
320 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vm_faults, CTLTYPE_UINT|CTLFLAG_RD,
321         0, VMMETEROFF(v_vm_faults), vcnt, "IU", "VM faults");
322 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_faults, CTLTYPE_UINT|CTLFLAG_RD,
323         0, VMMETEROFF(v_cow_faults), vcnt, "IU", "COW faults");
324 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_optim, CTLTYPE_UINT|CTLFLAG_RD,
325         0, VMMETEROFF(v_cow_optim), vcnt, "IU", "Optimized COW faults");
326 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_zfod, CTLTYPE_UINT|CTLFLAG_RD,
327         0, VMMETEROFF(v_zfod), vcnt, "IU", "Zero fill");
328 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_ozfod, CTLTYPE_UINT|CTLFLAG_RD,
329         0, VMMETEROFF(v_ozfod), vcnt, "IU", "Optimized zero fill");
330 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapin, CTLTYPE_UINT|CTLFLAG_RD,
331         0, VMMETEROFF(v_swapin), vcnt, "IU", "Swapin operations");
332 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapout, CTLTYPE_UINT|CTLFLAG_RD,
333         0, VMMETEROFF(v_swapout), vcnt, "IU", "Swapout operations");
334 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsin, CTLTYPE_UINT|CTLFLAG_RD,
335         0, VMMETEROFF(v_swappgsin), vcnt, "IU", "Swapin pages");
336 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsout, CTLTYPE_UINT|CTLFLAG_RD,
337         0, VMMETEROFF(v_swappgsout), vcnt, "IU", "Swapout pages");
338 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodein, CTLTYPE_UINT|CTLFLAG_RD,
339         0, VMMETEROFF(v_vnodein), vcnt, "IU", "Vnodein operations");
340 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodeout, CTLTYPE_UINT|CTLFLAG_RD,
341         0, VMMETEROFF(v_vnodeout), vcnt, "IU", "Vnodeout operations");
342 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsin, CTLTYPE_UINT|CTLFLAG_RD,
343         0, VMMETEROFF(v_vnodepgsin), vcnt, "IU", "Vnodein pages");
344 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsout, CTLTYPE_UINT|CTLFLAG_RD,
345         0, VMMETEROFF(v_vnodepgsout), vcnt, "IU", "Vnodeout pages");
346 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_intrans, CTLTYPE_UINT|CTLFLAG_RD,
347         0, VMMETEROFF(v_intrans), vcnt, "IU", "In transit page blocking");
348 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_reactivated, CTLTYPE_UINT|CTLFLAG_RD,
349         0, VMMETEROFF(v_reactivated), vcnt, "IU", "Reactivated pages");
350 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdwakeups, CTLTYPE_UINT|CTLFLAG_RD,
351         0, VMMETEROFF(v_pdwakeups), vcnt, "IU", "Pagedaemon wakeups");
352 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_ppwakeups, CTLTYPE_UINT|CTLFLAG_RD,
353         0, VMMETEROFF(v_ppwakeups), vcnt, "IU", "vm_wait wakeups");
354 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdpages, CTLTYPE_UINT|CTLFLAG_RD,
355         0, VMMETEROFF(v_pdpages), vcnt, "IU", "Pagedaemon page scans");
356 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_dfree, CTLTYPE_UINT|CTLFLAG_RD,
357         0, VMMETEROFF(v_dfree), vcnt, "IU", "Pages freed by daemon");
358 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pfree, CTLTYPE_UINT|CTLFLAG_RD,
359         0, VMMETEROFF(v_pfree), vcnt, "IU", "Pages freed by exiting processes");
360 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_tfree, CTLTYPE_UINT|CTLFLAG_RD,
361         0, VMMETEROFF(v_tfree), vcnt, "IU", "Total pages freed");
362 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forks, CTLTYPE_UINT|CTLFLAG_RD,
363         0, VMMETEROFF(v_forks), vcnt, "IU", "Number of fork() calls");
364 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforks, CTLTYPE_UINT|CTLFLAG_RD,
365         0, VMMETEROFF(v_vforks), vcnt, "IU", "Number of vfork() calls");
366 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforks, CTLTYPE_UINT|CTLFLAG_RD,
367         0, VMMETEROFF(v_rforks), vcnt, "IU", "Number of rfork() calls");
368 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreads, CTLTYPE_UINT|CTLFLAG_RD,
369         0, VMMETEROFF(v_kthreads), vcnt, "IU", "Number of fork() calls by kernel");
370 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forkpages, CTLTYPE_UINT|CTLFLAG_RD,
371         0, VMMETEROFF(v_forkpages), vcnt, "IU", "VM pages affected by fork()");
372 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforkpages, CTLTYPE_UINT|CTLFLAG_RD,
373         0, VMMETEROFF(v_vforkpages), vcnt, "IU", "VM pages affected by vfork()");
374 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforkpages, CTLTYPE_UINT|CTLFLAG_RD,
375         0, VMMETEROFF(v_rforkpages), vcnt, "IU", "VM pages affected by rfork()");
376 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreadpages, CTLTYPE_UINT|CTLFLAG_RD,
377         0, VMMETEROFF(v_kthreadpages), vcnt, "IU", "VM pages affected by fork() by kernel");
378
379 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
380         v_page_size, CTLFLAG_RD, &vmstats.v_page_size, 0,
381         "Page size in bytes");
382 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
383         v_page_count, CTLFLAG_RD, &vmstats.v_page_count, 0, 
384         "Total number of pages in system");
385 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
386         v_free_reserved, CTLFLAG_RD, &vmstats.v_free_reserved, 0,
387         "Number of pages reserved for deadlock");
388 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
389         v_free_target, CTLFLAG_RD, &vmstats.v_free_target, 0,
390         "Number of pages desired free");
391 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
392         v_free_min, CTLFLAG_RD, &vmstats.v_free_min, 0,
393         "Minimum number of pages desired free");
394 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
395         v_free_count, CTLFLAG_RD, &vmstats.v_free_count, 0,
396         "Number of pages free");
397 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
398         v_wire_count, CTLFLAG_RD, &vmstats.v_wire_count, 0,
399         "Number of pages wired down");
400 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
401         v_active_count, CTLFLAG_RD, &vmstats.v_active_count, 0,
402         "Number of pages active");
403 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
404         v_inactive_target, CTLFLAG_RD, &vmstats.v_inactive_target, 0,
405         "Number of pages desired inactive");
406 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
407         v_inactive_count, CTLFLAG_RD, &vmstats.v_inactive_count, 0,
408         "Number of pages inactive");
409 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
410         v_cache_count, CTLFLAG_RD, &vmstats.v_cache_count, 0,
411         "Number of pages on buffer cache queue");
412 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
413         v_cache_min, CTLFLAG_RD, &vmstats.v_cache_min, 0,
414         "Min number of pages desired on cache queue");
415 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
416         v_cache_max, CTLFLAG_RD, &vmstats.v_cache_max, 0,
417         "Max number of pages in cached obj");
418 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
419         v_pageout_free_min, CTLFLAG_RD, &vmstats.v_pageout_free_min, 0,
420         "Min number pages reserved for kernel");
421 SYSCTL_ULONG(_vm_stats_vm, OID_AUTO,
422         v_interrupt_free_min, CTLFLAG_RD, &vmstats.v_interrupt_free_min, 0,
423         "Reserved number of pages for int code");
424
425 /*
426  * No requirements.
427  */
428 static int
429 do_vmmeter_pcpu(SYSCTL_HANDLER_ARGS)
430 {
431         int boffset = offsetof(struct vmmeter, vmmeter_uint_begin);
432         int eoffset = offsetof(struct vmmeter, vmmeter_uint_end);
433         struct globaldata *gd = arg1;
434         struct vmmeter vmm;
435         int off;
436
437         bzero(&vmm, sizeof(vmm));
438         for (off = boffset; off <= eoffset; off += sizeof(u_int)) {
439                 *(u_int *)((char *)&vmm + off) +=
440                         *(u_int *)((char *)&gd->gd_cnt + off);
441         }
442         vmm.v_intr += vmm.v_ipi + vmm.v_timer;
443         return (sysctl_handle_opaque(oidp, &vmm, sizeof(vmm), req));
444 }
445
446 /*
447  * Callback for long-term slow data collection on 10-second interval.
448  *
449  * Return faults, set data for other entries.
450  */
451 #define PTOB(value)     ((uint64_t)(value) << PAGE_SHIFT)
452
453 static uint64_t
454 collect_vmstats_callback(int n)
455 {
456         static struct vmmeter last_vmm;
457         struct vmmeter cur_vmm;
458         const int boffset = offsetof(struct vmmeter, vmmeter_uint_begin);
459         const int eoffset = offsetof(struct vmmeter, vmmeter_uint_end);
460         uint64_t total;
461
462         /*
463          * The hardclock already rolls up vmstats for us.
464          */
465         kcollect_setvalue(KCOLLECT_MEMFRE, PTOB(vmstats.v_free_count));
466         kcollect_setvalue(KCOLLECT_MEMCAC, PTOB(vmstats.v_cache_count));
467         kcollect_setvalue(KCOLLECT_MEMINA, PTOB(vmstats.v_inactive_count));
468         kcollect_setvalue(KCOLLECT_MEMACT, PTOB(vmstats.v_active_count));
469         kcollect_setvalue(KCOLLECT_MEMWIR, PTOB(vmstats.v_wire_count));
470
471         /*
472          * Collect pcpu statistics for things like faults.
473          */
474         bzero(&cur_vmm, sizeof(cur_vmm));
475         for (n = 0; n < ncpus; ++n) {
476                 struct globaldata *gd = globaldata_find(n);
477                 int off;
478
479                 for (off = boffset; off <= eoffset; off += sizeof(u_int)) {
480                         *(u_int *)((char *)&cur_vmm + off) +=
481                                 *(u_int *)((char *)&gd->gd_cnt + off);
482                 }
483
484         }
485
486         total = cur_vmm.v_cow_faults - last_vmm.v_cow_faults;
487         last_vmm.v_cow_faults = cur_vmm.v_cow_faults;
488         kcollect_setvalue(KCOLLECT_COWFAULT, total);
489
490         total = cur_vmm.v_zfod - last_vmm.v_zfod;
491         last_vmm.v_zfod = cur_vmm.v_zfod;
492         kcollect_setvalue(KCOLLECT_ZFILL, total);
493
494         total = cur_vmm.v_syscall - last_vmm.v_syscall;
495         last_vmm.v_syscall = cur_vmm.v_syscall;
496         kcollect_setvalue(KCOLLECT_SYSCALLS, total);
497
498         total = cur_vmm.v_intr - last_vmm.v_intr;
499         last_vmm.v_intr = cur_vmm.v_intr;
500         kcollect_setvalue(KCOLLECT_INTR, total);
501
502         total = cur_vmm.v_ipi - last_vmm.v_ipi;
503         last_vmm.v_ipi = cur_vmm.v_ipi;
504         kcollect_setvalue(KCOLLECT_IPI, total);
505
506         total = cur_vmm.v_timer - last_vmm.v_timer;
507         last_vmm.v_timer = cur_vmm.v_timer;
508         kcollect_setvalue(KCOLLECT_TIMER, total);
509
510         total = cur_vmm.v_vm_faults - last_vmm.v_vm_faults;
511         last_vmm.v_vm_faults = cur_vmm.v_vm_faults;
512
513         return total;
514 }
515
516 /*
517  * Called from the low level boot code only.
518  */
519 static void
520 vmmeter_init(void *dummy __unused)
521 {
522         int i;
523
524         for (i = 0; i < ncpus; ++i) {
525                 struct sysctl_ctx_list *ctx;
526                 struct sysctl_oid *oid;
527                 struct globaldata *gd;
528                 char name[32];
529
530                 ksnprintf(name, sizeof(name), "cpu%d", i);
531
532                 ctx = kmalloc(sizeof(*ctx), M_TEMP, M_WAITOK);
533                 sysctl_ctx_init(ctx);
534                 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_STATIC_CHILDREN(_vm),
535                                       OID_AUTO, name, CTLFLAG_RD, 0, "");
536
537                 gd = globaldata_find(i);
538                 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
539                                 "vmmeter", CTLTYPE_OPAQUE|CTLFLAG_RD,
540                                 gd, sizeof(struct vmmeter), do_vmmeter_pcpu,
541                                 "S,vmmeter", "System per-cpu statistics");
542         }
543         kcollect_register(KCOLLECT_VMFAULT, "fault", collect_vmstats_callback,
544                           KCOLLECT_SCALE(KCOLLECT_VMFAULT_FORMAT, 0));
545         kcollect_register(KCOLLECT_COWFAULT, "cow", NULL,
546                           KCOLLECT_SCALE(KCOLLECT_COWFAULT_FORMAT, 0));
547         kcollect_register(KCOLLECT_ZFILL, "zfill", NULL,
548                           KCOLLECT_SCALE(KCOLLECT_ZFILL_FORMAT, 0));
549
550         kcollect_register(KCOLLECT_MEMFRE, "free", NULL,
551                           KCOLLECT_SCALE(KCOLLECT_MEMFRE_FORMAT,
552                                          PTOB(vmstats.v_page_count)));
553         kcollect_register(KCOLLECT_MEMCAC, "cache", NULL,
554                           KCOLLECT_SCALE(KCOLLECT_MEMCAC_FORMAT,
555                                          PTOB(vmstats.v_page_count)));
556         kcollect_register(KCOLLECT_MEMINA, "inact", NULL,
557                           KCOLLECT_SCALE(KCOLLECT_MEMINA_FORMAT,
558                                          PTOB(vmstats.v_page_count)));
559         kcollect_register(KCOLLECT_MEMACT, "act", NULL,
560                           KCOLLECT_SCALE(KCOLLECT_MEMACT_FORMAT,
561                                          PTOB(vmstats.v_page_count)));
562         kcollect_register(KCOLLECT_MEMWIR, "wired", NULL,
563                           KCOLLECT_SCALE(KCOLLECT_MEMWIR_FORMAT,
564                                          PTOB(vmstats.v_page_count)));
565
566         kcollect_register(KCOLLECT_SYSCALLS, "syscalls", NULL,
567                           KCOLLECT_SCALE(KCOLLECT_SYSCALLS_FORMAT, 0));
568
569         kcollect_register(KCOLLECT_INTR, "intr", NULL,
570                           KCOLLECT_SCALE(KCOLLECT_INTR_FORMAT, 0));
571         kcollect_register(KCOLLECT_IPI, "ipi", NULL,
572                           KCOLLECT_SCALE(KCOLLECT_IPI_FORMAT, 0));
573         kcollect_register(KCOLLECT_TIMER, "timer", NULL,
574                           KCOLLECT_SCALE(KCOLLECT_TIMER_FORMAT, 0));
575 }
576 SYSINIT(vmmeter, SI_SUB_PSEUDO, SI_ORDER_ANY, vmmeter_init, 0);
577
578 /*
579  * Rolls up accumulated pcpu adjustments to vmstats counts into the global
580  * structure, copy the global structure into our pcpu structure.  Critical
581  * path checks will use our pcpu structure.
582  *
583  * This is somewhat expensive and only called when needed, and by the
584  * hardclock.
585  */
586 void
587 vmstats_rollup(void)
588 {
589         int cpu;
590
591         for (cpu = 0; cpu < ncpus; ++cpu) {
592                 vmstats_rollup_cpu(globaldata_find(cpu));
593         }
594         mycpu->gd_vmstats = vmstats;
595 }
596
597 void
598 vmstats_rollup_cpu(globaldata_t gd)
599 {
600         long value;
601
602         if (gd->gd_vmstats_adj.v_free_count) {
603                 value = atomic_swap_long(&gd->gd_vmstats_adj.v_free_count, 0);
604                 atomic_add_long(&vmstats.v_free_count, value);
605         }
606         if (gd->gd_vmstats_adj.v_cache_count) {
607                 value = atomic_swap_long(&gd->gd_vmstats_adj.v_cache_count, 0);
608                 atomic_add_long(&vmstats.v_cache_count, value);
609         }
610         if (gd->gd_vmstats_adj.v_inactive_count) {
611                 value=atomic_swap_long(&gd->gd_vmstats_adj.v_inactive_count, 0);
612                 atomic_add_long(&vmstats.v_inactive_count, value);
613         }
614         if (gd->gd_vmstats_adj.v_active_count) {
615                 value = atomic_swap_long(&gd->gd_vmstats_adj.v_active_count, 0);
616                 atomic_add_long(&vmstats.v_active_count, value);
617         }
618         if (gd->gd_vmstats_adj.v_wire_count) {
619                 value = atomic_swap_long(&gd->gd_vmstats_adj.v_wire_count, 0);
620                 atomic_add_long(&vmstats.v_wire_count, value);
621         }
622 }