2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/mutex.h>
41 #include <sys/resource.h>
42 #include <sys/rwlock.h>
44 #include <sys/vmmeter.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_param.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_object.h>
54 #include <sys/sysctl.h>
58 SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
59 CTLFLAG_RW, &cnt.v_free_min, 0, "Minimum low-free-pages threshold");
60 SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
61 CTLFLAG_RW, &cnt.v_free_target, 0, "Desired free pages");
62 SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
63 CTLFLAG_RW, &cnt.v_free_reserved, 0, "Pages reserved for deadlock");
64 SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
65 CTLFLAG_RW, &cnt.v_inactive_target, 0, "Pages desired inactive");
66 SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
67 CTLFLAG_RW, &cnt.v_cache_min, 0, "Min pages on cache queue");
68 SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
69 CTLFLAG_RW, &cnt.v_cache_max, 0, "Max pages on cache queue");
70 SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
71 CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "Min pages reserved for kernel");
72 SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
73 CTLFLAG_RW, &cnt.v_free_severe, 0, "Severe page depletion point");
76 sysctl_vm_loadavg(SYSCTL_HANDLER_ARGS)
82 if (req->flags & SCTL_MASK32) {
83 la[0] = averunnable.ldavg[0];
84 la[1] = averunnable.ldavg[1];
85 la[2] = averunnable.ldavg[2];
86 la[3] = averunnable.fscale;
87 return SYSCTL_OUT(req, la, sizeof(la));
90 return SYSCTL_OUT(req, &averunnable, sizeof(averunnable));
92 SYSCTL_PROC(_vm, VM_LOADAVG, loadavg, CTLTYPE_STRUCT | CTLFLAG_RD |
93 CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_loadavg, "S,loadavg",
94 "Machine loadaverage history");
97 vmtotal(SYSCTL_HANDLER_ARGS)
100 struct vmtotal total;
101 vm_map_entry_t entry;
108 bzero(&total, sizeof(total));
110 * Mark all objects as inactive.
112 mtx_lock(&vm_object_list_mtx);
113 TAILQ_FOREACH(object, &vm_object_list, object_list) {
114 if (!VM_OBJECT_TRYWLOCK(object)) {
116 * Avoid a lock-order reversal. Consequently,
117 * the reported number of active pages may be
118 * greater than the actual number.
122 vm_object_clear_flag(object, OBJ_ACTIVE);
123 VM_OBJECT_WUNLOCK(object);
125 mtx_unlock(&vm_object_list_mtx);
127 * Calculate process statistics.
129 sx_slock(&allproc_lock);
130 FOREACH_PROC_IN_SYSTEM(p) {
131 if (p->p_flag & P_SYSTEM)
134 switch (p->p_state) {
140 FOREACH_THREAD_IN_PROC(p, td) {
142 switch (td->td_state) {
144 if (TD_IS_SWAPPED(td))
146 else if (TD_IS_SLEEPING(td) &&
147 td->td_priority <= PZERO)
169 * Note active objects.
172 vm = vmspace_acquire_ref(p);
176 vm_map_lock_read(map);
177 for (entry = map->header.next;
178 entry != &map->header; entry = entry->next) {
179 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
180 (object = entry->object.vm_object) == NULL)
182 VM_OBJECT_WLOCK(object);
183 vm_object_set_flag(object, OBJ_ACTIVE);
184 paging |= object->paging_in_progress;
185 VM_OBJECT_WUNLOCK(object);
187 vm_map_unlock_read(map);
192 sx_sunlock(&allproc_lock);
194 * Calculate object memory usage statistics.
196 mtx_lock(&vm_object_list_mtx);
197 TAILQ_FOREACH(object, &vm_object_list, object_list) {
199 * Perform unsynchronized reads on the object to avoid
200 * a lock-order reversal. In this case, the lack of
201 * synchronization should not impair the accuracy of
202 * the reported statistics.
204 if ((object->flags & OBJ_FICTITIOUS) != 0) {
206 * Devices, like /dev/mem, will badly skew our totals.
210 if (object->ref_count == 0) {
212 * Also skip unreferenced objects, including
213 * vnodes representing mounted file systems.
217 total.t_vm += object->size;
218 total.t_rm += object->resident_page_count;
219 if (object->flags & OBJ_ACTIVE) {
220 total.t_avm += object->size;
221 total.t_arm += object->resident_page_count;
223 if (object->shadow_count > 1) {
225 total.t_vmshr += object->size;
226 total.t_rmshr += object->resident_page_count;
227 if (object->flags & OBJ_ACTIVE) {
228 total.t_avmshr += object->size;
229 total.t_armshr += object->resident_page_count;
233 mtx_unlock(&vm_object_list_mtx);
234 total.t_free = cnt.v_free_count + cnt.v_cache_count;
235 return (sysctl_handle_opaque(oidp, &total, sizeof(total), req));
239 * vcnt() - accumulate statistics from all cpus and the global cnt
242 * The vmmeter structure is now per-cpu as well as global. Those
243 * statistics which can be kept on a per-cpu basis (to avoid cache
244 * stalls between cpus) can be moved to the per-cpu vmmeter. Remaining
245 * statistics, such as v_free_reserved, are left in the global
248 * (sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
251 vcnt(SYSCTL_HANDLER_ARGS)
253 int count = *(int *)arg1;
254 int offset = (char *)arg1 - (char *)&cnt;
258 struct pcpu *pcpu = pcpu_find(i);
259 count += *(int *)((char *)&pcpu->pc_cnt + offset);
261 return (SYSCTL_OUT(req, &count, sizeof(int)));
264 SYSCTL_PROC(_vm, VM_TOTAL, vmtotal, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
265 0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
266 "System virtual memory statistics");
267 SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
268 static SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0,
269 "VM meter sys stats");
270 static SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0,
271 "VM meter vm stats");
272 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
274 #define VM_STATS(parent, var, descr) \
275 SYSCTL_PROC(parent, OID_AUTO, var, \
276 CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, &cnt.var, 0, vcnt, \
278 #define VM_STATS_VM(var, descr) VM_STATS(_vm_stats_vm, var, descr)
279 #define VM_STATS_SYS(var, descr) VM_STATS(_vm_stats_sys, var, descr)
281 VM_STATS_SYS(v_swtch, "Context switches");
282 VM_STATS_SYS(v_trap, "Traps");
283 VM_STATS_SYS(v_syscall, "System calls");
284 VM_STATS_SYS(v_intr, "Device interrupts");
285 VM_STATS_SYS(v_soft, "Software interrupts");
286 VM_STATS_VM(v_vm_faults, "Address memory faults");
287 VM_STATS_VM(v_io_faults, "Page faults requiring I/O");
288 VM_STATS_VM(v_cow_faults, "Copy-on-write faults");
289 VM_STATS_VM(v_cow_optim, "Optimized COW faults");
290 VM_STATS_VM(v_zfod, "Pages zero-filled on demand");
291 VM_STATS_VM(v_ozfod, "Optimized zero fill pages");
292 VM_STATS_VM(v_swapin, "Swap pager pageins");
293 VM_STATS_VM(v_swapout, "Swap pager pageouts");
294 VM_STATS_VM(v_swappgsin, "Swap pages swapped in");
295 VM_STATS_VM(v_swappgsout, "Swap pages swapped out");
296 VM_STATS_VM(v_vnodein, "Vnode pager pageins");
297 VM_STATS_VM(v_vnodeout, "Vnode pager pageouts");
298 VM_STATS_VM(v_vnodepgsin, "Vnode pages paged in");
299 VM_STATS_VM(v_vnodepgsout, "Vnode pages paged out");
300 VM_STATS_VM(v_intrans, "In transit page faults");
301 VM_STATS_VM(v_reactivated, "Pages reactivated from free list");
302 VM_STATS_VM(v_pdwakeups, "Pagedaemon wakeups");
303 VM_STATS_VM(v_pdpages, "Pages analyzed by pagedaemon");
304 VM_STATS_VM(v_tcached, "Total pages cached");
305 VM_STATS_VM(v_dfree, "Pages freed by pagedaemon");
306 VM_STATS_VM(v_pfree, "Pages freed by exiting processes");
307 VM_STATS_VM(v_tfree, "Total pages freed");
308 VM_STATS_VM(v_page_size, "Page size in bytes");
309 VM_STATS_VM(v_page_count, "Total number of pages in system");
310 VM_STATS_VM(v_free_reserved, "Pages reserved for deadlock");
311 VM_STATS_VM(v_free_target, "Pages desired free");
312 VM_STATS_VM(v_free_min, "Minimum low-free-pages threshold");
313 VM_STATS_VM(v_free_count, "Free pages");
314 VM_STATS_VM(v_wire_count, "Wired pages");
315 VM_STATS_VM(v_active_count, "Active pages");
316 VM_STATS_VM(v_inactive_target, "Desired inactive pages");
317 VM_STATS_VM(v_inactive_count, "Inactive pages");
318 VM_STATS_VM(v_cache_count, "Pages on cache queue");
319 VM_STATS_VM(v_cache_min, "Min pages on cache queue");
320 VM_STATS_VM(v_cache_max, "Max pages on cached queue");
321 VM_STATS_VM(v_pageout_free_min, "Min pages reserved for kernel");
322 VM_STATS_VM(v_interrupt_free_min, "Reserved pages for interrupt code");
323 VM_STATS_VM(v_forks, "Number of fork() calls");
324 VM_STATS_VM(v_vforks, "Number of vfork() calls");
325 VM_STATS_VM(v_rforks, "Number of rfork() calls");
326 VM_STATS_VM(v_kthreads, "Number of fork() calls by kernel");
327 VM_STATS_VM(v_forkpages, "VM pages affected by fork()");
328 VM_STATS_VM(v_vforkpages, "VM pages affected by vfork()");
329 VM_STATS_VM(v_rforkpages, "VM pages affected by rfork()");
330 VM_STATS_VM(v_kthreadpages, "VM pages affected by fork() by kernel");
332 SYSCTL_INT(_vm_stats_misc, OID_AUTO, zero_page_count, CTLFLAG_RD,
333 &vm_page_zero_count, 0, "Number of zero-ed free pages");