vm_meter.c revision 228449
150472Speter/*-
233975Sjdp * Copyright (c) 1982, 1986, 1989, 1993
333975Sjdp *	The Regents of the University of California.  All rights reserved.
433975Sjdp *
544360Simp * Redistribution and use in source and binary forms, with or without
633975Sjdp * modification, are permitted provided that the following conditions
784902Sobrien * are met:
884902Sobrien * 1. Redistributions of source code must retain the above copyright
984902Sobrien *    notice, this list of conditions and the following disclaimer.
1084902Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1184902Sobrien *    notice, this list of conditions and the following disclaimer in the
1284902Sobrien *    documentation and/or other materials provided with the distribution.
1384902Sobrien * 4. Neither the name of the University nor the names of its contributors
1484902Sobrien *    may be used to endorse or promote products derived from this software
1533975Sjdp *    without specific prior written permission.
1633975Sjdp *
1734498Sjdp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1834498Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1934495Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2033975Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2152924Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2252924Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2352924Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2484902Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2584902Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2684902Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2784902Sobrien * SUCH DAMAGE.
2852924Sjb *
2952924Sjb *	@(#)vm_meter.c	8.4 (Berkeley) 1/4/94
3035711Sjb */
3158953Ssteve
3284902Sobrien#include <sys/cdefs.h>
3358953Ssteve__FBSDID("$FreeBSD: head/sys/vm/vm_meter.c 228449 2011-12-13 00:38:50Z eadler $");
3435711Sjb
3535711Sjb#include <sys/param.h>
3684902Sobrien#include <sys/systm.h>
3735711Sjb#include <sys/kernel.h>
3884902Sobrien#include <sys/lock.h>
3935711Sjb#include <sys/mutex.h>
4035711Sjb#include <sys/proc.h>
4158953Ssteve#include <sys/resource.h>
4284902Sobrien#include <sys/sx.h>
4335711Sjb#include <sys/vmmeter.h>
4435711Sjb#include <sys/smp.h>
4535711Sjb
4635711Sjb#include <vm/vm.h>
4784902Sobrien#include <vm/vm_page.h>
4835711Sjb#include <vm/vm_extern.h>
4984902Sobrien#include <vm/vm_param.h>
5035711Sjb#include <vm/pmap.h>
5135711Sjb#include <vm/vm_map.h>
5284902Sobrien#include <vm/vm_object.h>
5335711Sjb#include <sys/sysctl.h>
5433975Sjdp
5533975Sjdpstruct vmmeter cnt;
5633975Sjdp
5733975SjdpSYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
58	CTLFLAG_RW, &cnt.v_free_min, 0, "Minimum low-free-pages threshold");
59SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
60	CTLFLAG_RW, &cnt.v_free_target, 0, "Desired free pages");
61SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
62	CTLFLAG_RW, &cnt.v_free_reserved, 0, "Pages reserved for deadlock");
63SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
64	CTLFLAG_RW, &cnt.v_inactive_target, 0, "Pages desired inactive");
65SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
66	CTLFLAG_RW, &cnt.v_cache_min, 0, "Min pages on cache queue");
67SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
68	CTLFLAG_RW, &cnt.v_cache_max, 0, "Max pages on cache queue");
69SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
70	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "Min pages reserved for kernel");
71SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
72	CTLFLAG_RW, &cnt.v_free_severe, 0, "Severe page depletion point");
73
74static int
75sysctl_vm_loadavg(SYSCTL_HANDLER_ARGS)
76{
77
78#ifdef SCTL_MASK32
79	u_int32_t la[4];
80
81	if (req->flags & SCTL_MASK32) {
82		la[0] = averunnable.ldavg[0];
83		la[1] = averunnable.ldavg[1];
84		la[2] = averunnable.ldavg[2];
85		la[3] = averunnable.fscale;
86		return SYSCTL_OUT(req, la, sizeof(la));
87	} else
88#endif
89		return SYSCTL_OUT(req, &averunnable, sizeof(averunnable));
90}
91SYSCTL_PROC(_vm, VM_LOADAVG, loadavg, CTLTYPE_STRUCT | CTLFLAG_RD |
92    CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_loadavg, "S,loadavg",
93    "Machine loadaverage history");
94
95static int
96vmtotal(SYSCTL_HANDLER_ARGS)
97{
98	struct proc *p;
99	struct vmtotal total;
100	vm_map_entry_t entry;
101	vm_object_t object;
102	vm_map_t map;
103	int paging;
104	struct thread *td;
105	struct vmspace *vm;
106
107	bzero(&total, sizeof(total));
108	/*
109	 * Mark all objects as inactive.
110	 */
111	mtx_lock(&vm_object_list_mtx);
112	TAILQ_FOREACH(object, &vm_object_list, object_list) {
113		if (!VM_OBJECT_TRYLOCK(object)) {
114			/*
115			 * Avoid a lock-order reversal.  Consequently,
116			 * the reported number of active pages may be
117			 * greater than the actual number.
118			 */
119			continue;
120		}
121		vm_object_clear_flag(object, OBJ_ACTIVE);
122		VM_OBJECT_UNLOCK(object);
123	}
124	mtx_unlock(&vm_object_list_mtx);
125	/*
126	 * Calculate process statistics.
127	 */
128	sx_slock(&allproc_lock);
129	FOREACH_PROC_IN_SYSTEM(p) {
130		if (p->p_flag & P_SYSTEM)
131			continue;
132		PROC_LOCK(p);
133		switch (p->p_state) {
134		case PRS_NEW:
135			PROC_UNLOCK(p);
136			continue;
137			break;
138		default:
139			FOREACH_THREAD_IN_PROC(p, td) {
140				thread_lock(td);
141				switch (td->td_state) {
142				case TDS_INHIBITED:
143					if (TD_IS_SWAPPED(td))
144						total.t_sw++;
145					else if (TD_IS_SLEEPING(td) &&
146					    td->td_priority <= PZERO)
147						total.t_dw++;
148					else
149						total.t_sl++;
150					break;
151
152				case TDS_CAN_RUN:
153					total.t_sw++;
154					break;
155				case TDS_RUNQ:
156				case TDS_RUNNING:
157					total.t_rq++;
158					thread_unlock(td);
159					continue;
160				default:
161					break;
162				}
163				thread_unlock(td);
164			}
165		}
166		PROC_UNLOCK(p);
167		/*
168		 * Note active objects.
169		 */
170		paging = 0;
171		vm = vmspace_acquire_ref(p);
172		if (vm == NULL)
173			continue;
174		map = &vm->vm_map;
175		vm_map_lock_read(map);
176		for (entry = map->header.next;
177		    entry != &map->header; entry = entry->next) {
178			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
179			    (object = entry->object.vm_object) == NULL)
180				continue;
181			VM_OBJECT_LOCK(object);
182			vm_object_set_flag(object, OBJ_ACTIVE);
183			paging |= object->paging_in_progress;
184			VM_OBJECT_UNLOCK(object);
185		}
186		vm_map_unlock_read(map);
187		vmspace_free(vm);
188		if (paging)
189			total.t_pw++;
190	}
191	sx_sunlock(&allproc_lock);
192	/*
193	 * Calculate object memory usage statistics.
194	 */
195	mtx_lock(&vm_object_list_mtx);
196	TAILQ_FOREACH(object, &vm_object_list, object_list) {
197		/*
198		 * Perform unsynchronized reads on the object to avoid
199		 * a lock-order reversal.  In this case, the lack of
200		 * synchronization should not impair the accuracy of
201		 * the reported statistics.
202		 */
203		if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
204			/*
205			 * Devices, like /dev/mem, will badly skew our totals.
206			 */
207			continue;
208		}
209		if (object->ref_count == 0) {
210			/*
211			 * Also skip unreferenced objects, including
212			 * vnodes representing mounted file systems.
213			 */
214			continue;
215		}
216		total.t_vm += object->size;
217		total.t_rm += object->resident_page_count;
218		if (object->flags & OBJ_ACTIVE) {
219			total.t_avm += object->size;
220			total.t_arm += object->resident_page_count;
221		}
222		if (object->shadow_count > 1) {
223			/* shared object */
224			total.t_vmshr += object->size;
225			total.t_rmshr += object->resident_page_count;
226			if (object->flags & OBJ_ACTIVE) {
227				total.t_avmshr += object->size;
228				total.t_armshr += object->resident_page_count;
229			}
230		}
231	}
232	mtx_unlock(&vm_object_list_mtx);
233	total.t_free = cnt.v_free_count + cnt.v_cache_count;
234	return (sysctl_handle_opaque(oidp, &total, sizeof(total), req));
235}
236
237/*
238 * vcnt() -	accumulate statistics from all cpus and the global cnt
239 *		structure.
240 *
241 *	The vmmeter structure is now per-cpu as well as global.  Those
242 *	statistics which can be kept on a per-cpu basis (to avoid cache
243 *	stalls between cpus) can be moved to the per-cpu vmmeter.  Remaining
244 *	statistics, such as v_free_reserved, are left in the global
245 *	structure.
246 *
247 * (sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
248 */
249static int
250vcnt(SYSCTL_HANDLER_ARGS)
251{
252	int count = *(int *)arg1;
253	int offset = (char *)arg1 - (char *)&cnt;
254	int i;
255
256	CPU_FOREACH(i) {
257		struct pcpu *pcpu = pcpu_find(i);
258		count += *(int *)((char *)&pcpu->pc_cnt + offset);
259	}
260	return (SYSCTL_OUT(req, &count, sizeof(int)));
261}
262
263SYSCTL_PROC(_vm, VM_TOTAL, vmtotal, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
264    0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
265    "System virtual memory statistics");
266SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
267static SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0,
268	"VM meter sys stats");
269static SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0,
270	"VM meter vm stats");
271SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
272
273#define VM_STATS_SYS(var, descr) SYSCTL_PROC(_vm_stats_sys, OID_AUTO, var, \
274    (CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE), &cnt.v_swtch, 0, vcnt, \
275    "IU", descr);
276
277VM_STATS_SYS(v_swtch, "Context switches");
278VM_STATS_SYS(v_trap, "Traps");
279VM_STATS_SYS(v_syscall, "System calls");
280VM_STATS_SYS(v_intr, "Device interrupts");
281VM_STATS_SYS(v_soft, "Software interrupts");
282VM_STATS_SYS(v_vm_faults, "Address memory faults");
283VM_STATS_SYS(v_cow_faults, "Copy-on-write faults");
284VM_STATS_SYS(v_cow_optim, "Optimized COW faults")
285VM_STATS_SYS(v_zfod, "Pages zero-filled on demand")
286VM_STATS_SYS(v_ozfod, "Optimized zero fill pages")
287VM_STATS_SYS(v_swapin, "Swap pager pageins")
288VM_STATS_SYS(v_swapout, "Swap pager pageouts")
289VM_STATS_SYS(v_swappgsin, "Swap pages swapped in")
290VM_STATS_SYS(v_swappgsout, "Swap pages swapped out")
291VM_STATS_SYS(v_vnodein, "Vnode pager pageins")
292VM_STATS_SYS(v_vnodeout, "Vnode pager pageouts")
293VM_STATS_SYS(v_vnodepgsin, "Vnode pages paged in")
294VM_STATS_SYS(v_vnodepgsout, "Vnode pages paged out")
295VM_STATS_SYS(v_intrans, "In transit page faults")
296VM_STATS_SYS(v_reactivated, "Pages reactivated from free list")
297VM_STATS_SYS(v_pdwakeups, "Pagedaemon wakeups")
298VM_STATS_SYS(v_pdpages, "Pages analyzed by pagedaemon")
299VM_STATS_SYS(v_tcached, "Total pages cached")
300VM_STATS_SYS(v_dfree, "Pages freed by pagedaemon")
301VM_STATS_SYS(v_pfree, "Pages freed by exiting processes")
302VM_STATS_SYS(v_tfree, "Total pages freed")
303VM_STATS_SYS(v_page_size, "Page size in bytes")
304VM_STATS_SYS(v_page_count, "Total number of pages in system")
305VM_STATS_SYS(v_free_reserved, "Pages reserved for deadlock")
306VM_STATS_SYS(v_free_target, "Pages desired free")
307VM_STATS_SYS(v_free_min, "Minimum low-free-pages threshold")
308VM_STATS_SYS(v_free_count, "Free pages")
309VM_STATS_SYS(v_wire_count, "Wired pages")
310VM_STATS_SYS(v_active_count, "Active pages")
311VM_STATS_SYS(v_inactive_target, "Desired inactive pages")
312VM_STATS_SYS(v_inactive_count, "Inactive pages")
313VM_STATS_SYS(v_cache_count, "Pages on cache queue")
314VM_STATS_SYS(v_cache_min, "Min pages on cache queue")
315VM_STATS_SYS(v_cache_max, "Max pages on cached queue")
316VM_STATS_SYS(v_pageout_free_min, "Min pages reserved for kernel")
317VM_STATS_SYS(v_interrupt_free_min, "Reserved pages for interrupt code")
318VM_STATS_SYS(v_forks, "Number of fork() calls")
319VM_STATS_SYS(v_vforks, "Number of vfork() calls")
320VM_STATS_SYS(v_rforks, "Number of rfork() calls")
321VM_STATS_SYS(v_kthreads, "Number of fork() calls by kernel")
322VM_STATS_SYS(v_forkpages, "VM pages affected by fork()")
323VM_STATS_SYS(v_vforkpages, "VM pages affected by vfork()")
324VM_STATS_SYS(v_rforkpages, "VM pages affected by rfork()")
325VM_STATS_SYS(v_kthreadpages, "VM pages affected by fork() by kernel")
326
327SYSCTL_INT(_vm_stats_misc, OID_AUTO, zero_page_count, CTLFLAG_RD,
328	&vm_page_zero_count, 0, "Number of zero-ed free pages");
329