properly initialize ncpus for UP
[dragonfly.git] / sys / vm / vm_meter.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94
34 * $FreeBSD: src/sys/vm/vm_meter.c,v 1.34.2.7 2002/10/10 19:28:22 dillon Exp $
12e4aaff 35 * $DragonFly: src/sys/vm/vm_meter.c,v 1.4 2003/07/03 17:24:04 dillon Exp $
984263bc
MD
36 */
37
38#include <sys/param.h>
39#include <sys/proc.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/resource.h>
43#include <sys/vmmeter.h>
44
45#include <vm/vm.h>
46#include <vm/vm_page.h>
47#include <vm/vm_extern.h>
48#include <vm/vm_param.h>
49#include <sys/lock.h>
50#include <vm/pmap.h>
51#include <vm/vm_map.h>
52#include <vm/vm_object.h>
53#include <sys/sysctl.h>
54
12e4aaff 55struct vmstats vmstats;
984263bc
MD
56
57static int maxslp = MAXSLP;
58
59SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
12e4aaff 60 CTLFLAG_RW, &vmstats.v_free_min, 0, "");
984263bc 61SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
12e4aaff 62 CTLFLAG_RW, &vmstats.v_free_target, 0, "");
984263bc 63SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
12e4aaff 64 CTLFLAG_RW, &vmstats.v_free_reserved, 0, "");
984263bc 65SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
12e4aaff 66 CTLFLAG_RW, &vmstats.v_inactive_target, 0, "");
984263bc 67SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
12e4aaff 68 CTLFLAG_RW, &vmstats.v_cache_min, 0, "");
984263bc 69SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
12e4aaff 70 CTLFLAG_RW, &vmstats.v_cache_max, 0, "");
984263bc 71SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
12e4aaff 72 CTLFLAG_RW, &vmstats.v_pageout_free_min, 0, "");
984263bc 73SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
12e4aaff 74 CTLFLAG_RW, &vmstats.v_free_severe, 0, "");
984263bc
MD
75
76SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD,
77 &averunnable, loadavg, "Machine loadaverage history");
78
79static int
80vmtotal(SYSCTL_HANDLER_ARGS)
81{
82 struct proc *p;
83 struct vmtotal total, *totalp;
84 vm_map_entry_t entry;
85 vm_object_t object;
86 vm_map_t map;
87 int paging;
88
89 totalp = &total;
90 bzero(totalp, sizeof *totalp);
91 /*
92 * Mark all objects as inactive.
93 */
94 for (object = TAILQ_FIRST(&vm_object_list);
95 object != NULL;
96 object = TAILQ_NEXT(object,object_list))
97 vm_object_clear_flag(object, OBJ_ACTIVE);
98 /*
99 * Calculate process statistics.
100 */
101 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
102 if (p->p_flag & P_SYSTEM)
103 continue;
104 switch (p->p_stat) {
105 case 0:
106 continue;
107
108 case SSLEEP:
109 case SSTOP:
110 if (p->p_flag & P_INMEM) {
26a0694b 111 /* YYY p_priority */
984263bc
MD
112 if (p->p_priority <= PZERO)
113 totalp->t_dw++;
114 else if (p->p_slptime < maxslp)
115 totalp->t_sl++;
116 } else if (p->p_slptime < maxslp)
117 totalp->t_sw++;
118 if (p->p_slptime >= maxslp)
119 continue;
120 break;
121
122 case SRUN:
123 case SIDL:
124 if (p->p_flag & P_INMEM)
125 totalp->t_rq++;
126 else
127 totalp->t_sw++;
128 if (p->p_stat == SIDL)
129 continue;
130 break;
131 }
132 /*
133 * Note active objects.
134 */
135 paging = 0;
136 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
137 entry != &map->header; entry = entry->next) {
138 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
139 entry->object.vm_object == NULL)
140 continue;
141 vm_object_set_flag(entry->object.vm_object, OBJ_ACTIVE);
142 paging |= entry->object.vm_object->paging_in_progress;
143 }
144 if (paging)
145 totalp->t_pw++;
146 }
147 /*
148 * Calculate object memory usage statistics.
149 */
150 for (object = TAILQ_FIRST(&vm_object_list);
151 object != NULL;
152 object = TAILQ_NEXT(object, object_list)) {
153 /*
154 * devices, like /dev/mem, will badly skew our totals
155 */
156 if (object->type == OBJT_DEVICE)
157 continue;
158 totalp->t_vm += object->size;
159 totalp->t_rm += object->resident_page_count;
160 if (object->flags & OBJ_ACTIVE) {
161 totalp->t_avm += object->size;
162 totalp->t_arm += object->resident_page_count;
163 }
164 if (object->shadow_count > 1) {
165 /* shared object */
166 totalp->t_vmshr += object->size;
167 totalp->t_rmshr += object->resident_page_count;
168 if (object->flags & OBJ_ACTIVE) {
169 totalp->t_avmshr += object->size;
170 totalp->t_armshr += object->resident_page_count;
171 }
172 }
173 }
12e4aaff 174 totalp->t_free = vmstats.v_free_count + vmstats.v_cache_count;
984263bc
MD
175 return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
176}
177
12e4aaff
MD
178/*
179 * vcnt() - accumulate statistics from the cnt structure for each cpu
180 *
181 * The vmmeter structure is now per-cpu as well as global. Those
182 * statistics which can be kept on a per-cpu basis (to avoid cache
183 * stalls between cpus) can be moved to the per-cpu vmmeter. Remaining
184 * statistics, such as v_free_reserved, are left in the global
185 * structure.
186 *
187 * (sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
188 */
189static int
190vcnt(SYSCTL_HANDLER_ARGS)
191{
192 int i;
193 int count = 0;
194 int offset = (int)arg1;
195
196 for (i = 0; i < ncpus; ++i) {
197 struct globaldata *gd = globaldata_find(i);
198 count += *(int *)((char *)&gd->gd_cnt + offset);
199 }
200 return(SYSCTL_OUT(req, &count, sizeof(int)));
201}
202
203#define VMMETEROFF(var) (void *)offsetof(struct vmmeter, var)
204
984263bc
MD
205SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
206 0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
207 "System virtual memory statistics");
208SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
209SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, "VM meter sys stats");
210SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, "VM meter vm stats");
211SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
12e4aaff
MD
212
213SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_swtch, CTLTYPE_UINT|CTLFLAG_RD,
214 VMMETEROFF(v_swtch), 0, vcnt, "IU", "Context switches");
215SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intrans_coll, CTLTYPE_UINT|CTLFLAG_RW,
216 VMMETEROFF(v_intrans_coll), 0, vcnt, "IU", "");
217SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intrans_wait, CTLTYPE_UINT|CTLFLAG_RW,
218 VMMETEROFF(v_intrans_wait), 0, vcnt, "IU", "");
219SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_trap, CTLTYPE_UINT|CTLFLAG_RD,
220 VMMETEROFF(v_trap), 0, vcnt, "IU", "Traps");
221SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_syscall, CTLTYPE_UINT|CTLFLAG_RD,
222 VMMETEROFF(v_syscall), 0, vcnt, "IU", "Syscalls");
223SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intr, CTLTYPE_UINT|CTLFLAG_RD,
224 VMMETEROFF(v_intr), 0, vcnt, "IU", "Hardware interrupts");
225SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_soft, CTLTYPE_UINT|CTLFLAG_RD,
226 VMMETEROFF(v_soft), 0, vcnt, "IU", "Software interrupts");
227SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vm_faults, CTLTYPE_UINT|CTLFLAG_RD,
228 VMMETEROFF(v_vm_faults), 0, vcnt, "IU", "VM faults");
229SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_faults, CTLTYPE_UINT|CTLFLAG_RD,
230 VMMETEROFF(v_cow_faults), 0, vcnt, "IU", "COW faults");
231SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_optim, CTLTYPE_UINT|CTLFLAG_RD,
232 VMMETEROFF(v_cow_optim), 0, vcnt, "IU", "Optimized COW faults");
233SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_zfod, CTLTYPE_UINT|CTLFLAG_RD,
234 VMMETEROFF(v_zfod), 0, vcnt, "IU", "Zero fill");
235SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_ozfod, CTLTYPE_UINT|CTLFLAG_RD,
236 VMMETEROFF(v_ozfod), 0, vcnt, "IU", "Optimized zero fill");
237SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapin, CTLTYPE_UINT|CTLFLAG_RD,
238 VMMETEROFF(v_swapin), 0, vcnt, "IU", "Swapin operations");
239SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapout, CTLTYPE_UINT|CTLFLAG_RD,
240 VMMETEROFF(v_swapout), 0, vcnt, "IU", "Swapout operations");
241SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsin, CTLTYPE_UINT|CTLFLAG_RD,
242 VMMETEROFF(v_swappgsin), 0, vcnt, "IU", "Swapin pages");
243SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsout, CTLTYPE_UINT|CTLFLAG_RD,
244 VMMETEROFF(v_swappgsout), 0, vcnt, "IU", "Swapout pages");
245SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodein, CTLTYPE_UINT|CTLFLAG_RD,
246 VMMETEROFF(v_vnodein), 0, vcnt, "IU", "Vnodein operations");
247SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodeout, CTLTYPE_UINT|CTLFLAG_RD,
248 VMMETEROFF(v_vnodeout), 0, vcnt, "IU", "Vnodeout operations");
249SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsin, CTLTYPE_UINT|CTLFLAG_RD,
250 VMMETEROFF(v_vnodepgsin), 0, vcnt, "IU", "Vnodein pages");
251SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsout, CTLTYPE_UINT|CTLFLAG_RD,
252 VMMETEROFF(v_vnodepgsout), 0, vcnt, "IU", "Vnodeout pages");
253SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_intrans, CTLTYPE_UINT|CTLFLAG_RD,
254 VMMETEROFF(v_intrans), 0, vcnt, "IU", "In transit page blocking");
255SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_reactivated, CTLTYPE_UINT|CTLFLAG_RD,
256 VMMETEROFF(v_reactivated), 0, vcnt, "IU", "Reactivated pages");
257SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdwakeups, CTLTYPE_UINT|CTLFLAG_RD,
258 VMMETEROFF(v_pdwakeups), 0, vcnt, "IU", "Pagedaemon wakeups");
259SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdpages, CTLTYPE_UINT|CTLFLAG_RD,
260 VMMETEROFF(v_pdpages), 0, vcnt, "IU", "Pagedaemon page scans");
261SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_dfree, CTLTYPE_UINT|CTLFLAG_RD,
262 VMMETEROFF(v_dfree), 0, vcnt, "IU", "");
263SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pfree, CTLTYPE_UINT|CTLFLAG_RD,
264 VMMETEROFF(v_pfree), 0, vcnt, "IU", "");
265SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_tfree, CTLTYPE_UINT|CTLFLAG_RD,
266 VMMETEROFF(v_tfree), 0, vcnt, "IU", "");
267SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forks, CTLTYPE_UINT|CTLFLAG_RD,
268 VMMETEROFF(v_forks), 0, vcnt, "IU", "Number of fork() calls");
269SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforks, CTLTYPE_UINT|CTLFLAG_RD,
270 VMMETEROFF(v_vforks), 0, vcnt, "IU", "Number of vfork() calls");
271SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforks, CTLTYPE_UINT|CTLFLAG_RD,
272 VMMETEROFF(v_rforks), 0, vcnt, "IU", "Number of rfork() calls");
273SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreads, CTLTYPE_UINT|CTLFLAG_RD,
274 VMMETEROFF(v_kthreads), 0, vcnt, "IU", "Number of fork() calls by kernel");
275SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forkpages, CTLTYPE_UINT|CTLFLAG_RD,
276 VMMETEROFF(v_forkpages), 0, vcnt, "IU", "VM pages affected by fork()");
277SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforkpages, CTLTYPE_UINT|CTLFLAG_RD,
278 VMMETEROFF(v_vforkpages), 0, vcnt, "IU", "VM pages affected by vfork()");
279SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforkpages, CTLTYPE_UINT|CTLFLAG_RD,
280 VMMETEROFF(v_rforkpages), 0, vcnt, "IU", "VM pages affected by rfork()");
281SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreadpages, CTLTYPE_UINT|CTLFLAG_RD,
282 VMMETEROFF(v_kthreadpages), 0, vcnt, "IU", "VM pages affected by fork() by kernel");
283
984263bc 284SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 285 v_page_size, CTLFLAG_RD, &vmstats.v_page_size, 0, "");
984263bc 286SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 287 v_page_count, CTLFLAG_RD, &vmstats.v_page_count, 0, "");
984263bc 288SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 289 v_free_reserved, CTLFLAG_RD, &vmstats.v_free_reserved, 0, "");
984263bc 290SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 291 v_free_target, CTLFLAG_RD, &vmstats.v_free_target, 0, "");
984263bc 292SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 293 v_free_min, CTLFLAG_RD, &vmstats.v_free_min, 0, "");
984263bc 294SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 295 v_free_count, CTLFLAG_RD, &vmstats.v_free_count, 0, "");
984263bc 296SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 297 v_wire_count, CTLFLAG_RD, &vmstats.v_wire_count, 0, "");
984263bc 298SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 299 v_active_count, CTLFLAG_RD, &vmstats.v_active_count, 0, "");
984263bc 300SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 301 v_inactive_target, CTLFLAG_RD, &vmstats.v_inactive_target, 0, "");
984263bc 302SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 303 v_inactive_count, CTLFLAG_RD, &vmstats.v_inactive_count, 0, "");
984263bc 304SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 305 v_cache_count, CTLFLAG_RD, &vmstats.v_cache_count, 0, "");
984263bc 306SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 307 v_cache_min, CTLFLAG_RD, &vmstats.v_cache_min, 0, "");
984263bc 308SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 309 v_cache_max, CTLFLAG_RD, &vmstats.v_cache_max, 0, "");
984263bc 310SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 311 v_pageout_free_min, CTLFLAG_RD, &vmstats.v_pageout_free_min, 0, "");
984263bc 312SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
12e4aaff 313 v_interrupt_free_min, CTLFLAG_RD, &vmstats.v_interrupt_free_min, 0, "");
984263bc
MD
314SYSCTL_INT(_vm_stats_misc, OID_AUTO,
315 zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0, "");