FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_meter.c
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: releng/6.0/sys/vm/vm_meter.c 146017 2005-05-08 23:56:16Z marcel $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/resource.h>
42 #include <sys/sx.h>
43 #include <sys/vmmeter.h>
44 #include <sys/smp.h>
45
46 #include <vm/vm.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_extern.h>
49 #include <vm/vm_param.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_object.h>
53 #include <sys/sysctl.h>
54
55 /*
56 * Virtual memory MPSAFE temporary workarounds.
57 */
58 #if !defined(__arm__) && !defined(__powerpc__)
59 int debug_mpsafevm = 1;
60 #else
61 int debug_mpsafevm;
62 #endif
63 TUNABLE_INT("debug.mpsafevm", &debug_mpsafevm);
64 SYSCTL_INT(_debug, OID_AUTO, mpsafevm, CTLFLAG_RD, &debug_mpsafevm, 0,
65 "Enable/disable MPSAFE virtual memory support");
66
67 struct vmmeter cnt;
68
69 int maxslp = MAXSLP;
70
71 SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
72 CTLFLAG_RW, &cnt.v_free_min, 0, "");
73 SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
74 CTLFLAG_RW, &cnt.v_free_target, 0, "");
75 SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
76 CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
77 SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
78 CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
79 SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
80 CTLFLAG_RW, &cnt.v_cache_min, 0, "");
81 SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
82 CTLFLAG_RW, &cnt.v_cache_max, 0, "");
83 SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
84 CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
85 SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
86 CTLFLAG_RW, &cnt.v_free_severe, 0, "");
87
88 static int
89 sysctl_vm_loadavg(SYSCTL_HANDLER_ARGS)
90 {
91 #ifdef SCTL_MASK32
92 u_int32_t la[4];
93
94 if (req->flags & SCTL_MASK32) {
95 la[0] = averunnable.ldavg[0];
96 la[1] = averunnable.ldavg[1];
97 la[2] = averunnable.ldavg[2];
98 la[3] = averunnable.fscale;
99 return SYSCTL_OUT(req, la, sizeof(la));
100 } else
101 #endif
102 return SYSCTL_OUT(req, &averunnable, sizeof(averunnable));
103 }
104 SYSCTL_PROC(_vm, VM_LOADAVG, loadavg, CTLTYPE_STRUCT|CTLFLAG_RD,
105 NULL, 0, sysctl_vm_loadavg, "S,loadavg", "Machine loadaverage history");
106
107 static int
108 vmtotal(SYSCTL_HANDLER_ARGS)
109 {
110 /* XXXKSE almost completely broken */
111 struct proc *p;
112 struct vmtotal total, *totalp;
113 vm_map_entry_t entry;
114 vm_object_t object;
115 vm_map_t map;
116 int paging;
117 struct thread *td;
118
119 totalp = &total;
120 bzero(totalp, sizeof *totalp);
121 /*
122 * Mark all objects as inactive.
123 */
124 GIANT_REQUIRED;
125 mtx_lock(&vm_object_list_mtx);
126 TAILQ_FOREACH(object, &vm_object_list, object_list) {
127 if (!VM_OBJECT_TRYLOCK(object)) {
128 /*
129 * Avoid a lock-order reversal. Consequently,
130 * the reported number of active pages may be
131 * greater than the actual number.
132 */
133 continue;
134 }
135 vm_object_clear_flag(object, OBJ_ACTIVE);
136 VM_OBJECT_UNLOCK(object);
137 }
138 mtx_unlock(&vm_object_list_mtx);
139 /*
140 * Calculate process statistics.
141 */
142 sx_slock(&allproc_lock);
143 FOREACH_PROC_IN_SYSTEM(p) {
144 if (p->p_flag & P_SYSTEM)
145 continue;
146 mtx_lock_spin(&sched_lock);
147 switch (p->p_state) {
148 case PRS_NEW:
149 mtx_unlock_spin(&sched_lock);
150 continue;
151 break;
152 default:
153 FOREACH_THREAD_IN_PROC(p, td) {
154 /* Need new statistics XXX */
155 switch (td->td_state) {
156 case TDS_INHIBITED:
157 if (TD_ON_LOCK(td) ||
158 (td->td_inhibitors ==
159 TDI_SWAPPED)) {
160 totalp->t_sw++;
161 } else if (TD_IS_SLEEPING(td) ||
162 TD_AWAITING_INTR(td) ||
163 TD_IS_SUSPENDED(td)) {
164 if (td->td_priority <= PZERO)
165 totalp->t_dw++;
166 else
167 totalp->t_sl++;
168 }
169 break;
170
171 case TDS_CAN_RUN:
172 totalp->t_sw++;
173 break;
174 case TDS_RUNQ:
175 case TDS_RUNNING:
176 totalp->t_rq++;
177 continue;
178 default:
179 break;
180 }
181 }
182 }
183 mtx_unlock_spin(&sched_lock);
184 /*
185 * Note active objects.
186 */
187 paging = 0;
188 map = &p->p_vmspace->vm_map;
189 vm_map_lock_read(map);
190 for (entry = map->header.next;
191 entry != &map->header; entry = entry->next) {
192 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
193 (object = entry->object.vm_object) == NULL)
194 continue;
195 VM_OBJECT_LOCK(object);
196 vm_object_set_flag(object, OBJ_ACTIVE);
197 paging |= object->paging_in_progress;
198 VM_OBJECT_UNLOCK(object);
199 }
200 vm_map_unlock_read(map);
201 if (paging)
202 totalp->t_pw++;
203 }
204 sx_sunlock(&allproc_lock);
205 /*
206 * Calculate object memory usage statistics.
207 */
208 mtx_lock(&vm_object_list_mtx);
209 TAILQ_FOREACH(object, &vm_object_list, object_list) {
210 /*
211 * Perform unsynchronized reads on the object to avoid
212 * a lock-order reversal. In this case, the lack of
213 * synchronization should not impair the accuracy of
214 * the reported statistics.
215 */
216 if (object->type == OBJT_DEVICE) {
217 /*
218 * Devices, like /dev/mem, will badly skew our totals.
219 */
220 continue;
221 }
222 totalp->t_vm += object->size;
223 totalp->t_rm += object->resident_page_count;
224 if (object->flags & OBJ_ACTIVE) {
225 totalp->t_avm += object->size;
226 totalp->t_arm += object->resident_page_count;
227 }
228 if (object->shadow_count > 1) {
229 /* shared object */
230 totalp->t_vmshr += object->size;
231 totalp->t_rmshr += object->resident_page_count;
232 if (object->flags & OBJ_ACTIVE) {
233 totalp->t_avmshr += object->size;
234 totalp->t_armshr += object->resident_page_count;
235 }
236 }
237 }
238 mtx_unlock(&vm_object_list_mtx);
239 totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
240 return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
241 }
242
243 /*
244 * vcnt() - accumulate statistics from all cpus and the global cnt
245 * structure.
246 *
247 * The vmmeter structure is now per-cpu as well as global. Those
248 * statistics which can be kept on a per-cpu basis (to avoid cache
249 * stalls between cpus) can be moved to the per-cpu vmmeter. Remaining
250 * statistics, such as v_free_reserved, are left in the global
251 * structure.
252 *
253 * (sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
254 */
255 static int
256 vcnt(SYSCTL_HANDLER_ARGS)
257 {
258 int count = *(int *)arg1;
259 int offset = (char *)arg1 - (char *)&cnt;
260 #ifdef SMP
261 int i;
262
263 for (i = 0; i < mp_ncpus; ++i) {
264 struct pcpu *pcpu = pcpu_find(i);
265 count += *(int *)((char *)&pcpu->pc_cnt + offset);
266 }
267 #else
268 count += *(int *)((char *)PCPU_PTR(cnt) + offset);
269 #endif
270 return (SYSCTL_OUT(req, &count, sizeof(int)));
271 }
272
273 SYSCTL_PROC(_vm, VM_TOTAL, vmtotal, CTLTYPE_OPAQUE|CTLFLAG_RD,
274 0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
275 "System virtual memory statistics");
276 SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
277 static SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0,
278 "VM meter sys stats");
279 static SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0,
280 "VM meter vm stats");
281 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
282
283 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_swtch, CTLTYPE_UINT|CTLFLAG_RD,
284 &cnt.v_swtch, 0, vcnt, "IU", "Context switches");
285 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_trap, CTLTYPE_UINT|CTLFLAG_RD,
286 &cnt.v_trap, 0, vcnt, "IU", "Traps");
287 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_syscall, CTLTYPE_UINT|CTLFLAG_RD,
288 &cnt.v_syscall, 0, vcnt, "IU", "Syscalls");
289 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intr, CTLTYPE_UINT|CTLFLAG_RD,
290 &cnt.v_intr, 0, vcnt, "IU", "Hardware interrupts");
291 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_soft, CTLTYPE_UINT|CTLFLAG_RD,
292 &cnt.v_soft, 0, vcnt, "IU", "Software interrupts");
293 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vm_faults, CTLTYPE_UINT|CTLFLAG_RD,
294 &cnt.v_vm_faults, 0, vcnt, "IU", "VM faults");
295 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_faults, CTLTYPE_UINT|CTLFLAG_RD,
296 &cnt.v_cow_faults, 0, vcnt, "IU", "COW faults");
297 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_optim, CTLTYPE_UINT|CTLFLAG_RD,
298 &cnt.v_cow_optim, 0, vcnt, "IU", "Optimized COW faults");
299 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_zfod, CTLTYPE_UINT|CTLFLAG_RD,
300 &cnt.v_zfod, 0, vcnt, "IU", "Zero fill");
301 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_ozfod, CTLTYPE_UINT|CTLFLAG_RD,
302 &cnt.v_ozfod, 0, vcnt, "IU", "Optimized zero fill");
303 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapin, CTLTYPE_UINT|CTLFLAG_RD,
304 &cnt.v_swapin, 0, vcnt, "IU", "Swapin operations");
305 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapout, CTLTYPE_UINT|CTLFLAG_RD,
306 &cnt.v_swapout, 0, vcnt, "IU", "Swapout operations");
307 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsin, CTLTYPE_UINT|CTLFLAG_RD,
308 &cnt.v_swappgsin, 0, vcnt, "IU", "Swapin pages");
309 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsout, CTLTYPE_UINT|CTLFLAG_RD,
310 &cnt.v_swappgsout, 0, vcnt, "IU", "Swapout pages");
311 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodein, CTLTYPE_UINT|CTLFLAG_RD,
312 &cnt.v_vnodein, 0, vcnt, "IU", "Vnodein operations");
313 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodeout, CTLTYPE_UINT|CTLFLAG_RD,
314 &cnt.v_vnodeout, 0, vcnt, "IU", "Vnodeout operations");
315 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsin, CTLTYPE_UINT|CTLFLAG_RD,
316 &cnt.v_vnodepgsin, 0, vcnt, "IU", "Vnodein pages");
317 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsout, CTLTYPE_UINT|CTLFLAG_RD,
318 &cnt.v_vnodepgsout, 0, vcnt, "IU", "Vnodeout pages");
319 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_intrans, CTLTYPE_UINT|CTLFLAG_RD,
320 &cnt.v_intrans, 0, vcnt, "IU", "In transit page blocking");
321 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_reactivated, CTLTYPE_UINT|CTLFLAG_RD,
322 &cnt.v_reactivated, 0, vcnt, "IU", "Reactivated pages");
323 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdwakeups, CTLTYPE_UINT|CTLFLAG_RD,
324 &cnt.v_pdwakeups, 0, vcnt, "IU", "Pagedaemon wakeups");
325 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdpages, CTLTYPE_UINT|CTLFLAG_RD,
326 &cnt.v_pdpages, 0, vcnt, "IU", "Pagedaemon page scans");
327 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_dfree, CTLTYPE_UINT|CTLFLAG_RD,
328 &cnt.v_dfree, 0, vcnt, "IU", "");
329 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pfree, CTLTYPE_UINT|CTLFLAG_RD,
330 &cnt.v_pfree, 0, vcnt, "IU", "");
331 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_tfree, CTLTYPE_UINT|CTLFLAG_RD,
332 &cnt.v_tfree, 0, vcnt, "IU", "");
333 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_page_size, CTLTYPE_UINT|CTLFLAG_RD,
334 &cnt.v_page_size, 0, vcnt, "IU", "");
335 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_page_count, CTLTYPE_UINT|CTLFLAG_RD,
336 &cnt.v_page_count, 0, vcnt, "IU", "");
337 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_reserved, CTLTYPE_UINT|CTLFLAG_RD,
338 &cnt.v_free_reserved, 0, vcnt, "IU", "");
339 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_target, CTLTYPE_UINT|CTLFLAG_RD,
340 &cnt.v_free_target, 0, vcnt, "IU", "");
341 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_min, CTLTYPE_UINT|CTLFLAG_RD,
342 &cnt.v_free_min, 0, vcnt, "IU", "");
343 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_count, CTLTYPE_UINT|CTLFLAG_RD,
344 &cnt.v_free_count, 0, vcnt, "IU", "");
345 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_wire_count, CTLTYPE_UINT|CTLFLAG_RD,
346 &cnt.v_wire_count, 0, vcnt, "IU", "");
347 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_active_count, CTLTYPE_UINT|CTLFLAG_RD,
348 &cnt.v_active_count, 0, vcnt, "IU", "");
349 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_inactive_target, CTLTYPE_UINT|CTLFLAG_RD,
350 &cnt.v_inactive_target, 0, vcnt, "IU", "");
351 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_inactive_count, CTLTYPE_UINT|CTLFLAG_RD,
352 &cnt.v_inactive_count, 0, vcnt, "IU", "");
353 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cache_count, CTLTYPE_UINT|CTLFLAG_RD,
354 &cnt.v_cache_count, 0, vcnt, "IU", "");
355 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cache_min, CTLTYPE_UINT|CTLFLAG_RD,
356 &cnt.v_cache_min, 0, vcnt, "IU", "");
357 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cache_max, CTLTYPE_UINT|CTLFLAG_RD,
358 &cnt.v_cache_max, 0, vcnt, "IU", "");
359 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pageout_free_min, CTLTYPE_UINT|CTLFLAG_RD,
360 &cnt.v_pageout_free_min, 0, vcnt, "IU", "");
361 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_interrupt_free_min, CTLTYPE_UINT|CTLFLAG_RD,
362 &cnt.v_interrupt_free_min, 0, vcnt, "IU", "");
363 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forks, CTLTYPE_UINT|CTLFLAG_RD,
364 &cnt.v_forks, 0, vcnt, "IU", "Number of fork() calls");
365 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforks, CTLTYPE_UINT|CTLFLAG_RD,
366 &cnt.v_vforks, 0, vcnt, "IU", "Number of vfork() calls");
367 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforks, CTLTYPE_UINT|CTLFLAG_RD,
368 &cnt.v_rforks, 0, vcnt, "IU", "Number of rfork() calls");
369 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreads, CTLTYPE_UINT|CTLFLAG_RD,
370 &cnt.v_kthreads, 0, vcnt, "IU", "Number of fork() calls by kernel");
371 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forkpages, CTLTYPE_UINT|CTLFLAG_RD,
372 &cnt.v_forkpages, 0, vcnt, "IU", "VM pages affected by fork()");
373 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforkpages, CTLTYPE_UINT|CTLFLAG_RD,
374 &cnt.v_vforkpages, 0, vcnt, "IU", "VM pages affected by vfork()");
375 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforkpages, CTLTYPE_UINT|CTLFLAG_RD,
376 &cnt.v_rforkpages, 0, vcnt, "IU", "VM pages affected by rfork()");
377 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreadpages, CTLTYPE_UINT|CTLFLAG_RD,
378 &cnt.v_kthreadpages, 0, vcnt, "IU", "VM pages affected by fork() by kernel");
379
380 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
381 zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0, "");
382 #if 0
383 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
384 page_mask, CTLFLAG_RD, &page_mask, 0, "");
385 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
386 page_shift, CTLFLAG_RD, &page_shift, 0, "");
387 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
388 first_page, CTLFLAG_RD, &first_page, 0, "");
389 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
390 last_page, CTLFLAG_RD, &last_page, 0, "");
391 #endif
Cache object: 23632f6af5de615c1493a0a1c1b69dd6
|