vm_meter.c revision 282660
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: head/sys/vm/vm_meter.c 282660 2015-05-08 19:43:37Z jhb $");
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/rwlock.h>
43#include <sys/sx.h>
44#include <sys/vmmeter.h>
45#include <sys/smp.h>
46
47#include <vm/vm.h>
48#include <vm/vm_page.h>
49#include <vm/vm_extern.h>
50#include <vm/vm_param.h>
51#include <vm/pmap.h>
52#include <vm/vm_map.h>
53#include <vm/vm_object.h>
54#include <sys/sysctl.h>
55
56struct vmmeter vm_cnt;
57
58SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
59	CTLFLAG_RW, &vm_cnt.v_free_min, 0, "Minimum low-free-pages threshold");
60SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
61	CTLFLAG_RW, &vm_cnt.v_free_target, 0, "Desired free pages");
62SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
63	CTLFLAG_RW, &vm_cnt.v_free_reserved, 0, "Pages reserved for deadlock");
64SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
65	CTLFLAG_RW, &vm_cnt.v_inactive_target, 0, "Pages desired inactive");
66SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
67	CTLFLAG_RW, &vm_cnt.v_cache_min, 0, "Min pages on cache queue");
68SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
69	CTLFLAG_RW, &vm_cnt.v_cache_max, 0, "Max pages on cache queue");
70SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
71	CTLFLAG_RW, &vm_cnt.v_pageout_free_min, 0, "Min pages reserved for kernel");
72SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
73	CTLFLAG_RW, &vm_cnt.v_free_severe, 0, "Severe page depletion point");
74
75static int
76sysctl_vm_loadavg(SYSCTL_HANDLER_ARGS)
77{
78
79#ifdef SCTL_MASK32
80	u_int32_t la[4];
81
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));
88	} else
89#endif
90		return SYSCTL_OUT(req, &averunnable, sizeof(averunnable));
91}
92SYSCTL_PROC(_vm, VM_LOADAVG, loadavg, CTLTYPE_STRUCT | CTLFLAG_RD |
93    CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_loadavg, "S,loadavg",
94    "Machine loadaverage history");
95
96static int
97vmtotal(SYSCTL_HANDLER_ARGS)
98{
99	struct proc *p;
100	struct vmtotal total;
101	vm_map_entry_t entry;
102	vm_object_t object;
103	vm_map_t map;
104	int paging;
105	struct thread *td;
106	struct vmspace *vm;
107
108	bzero(&total, sizeof(total));
109	/*
110	 * Mark all objects as inactive.
111	 */
112	mtx_lock(&vm_object_list_mtx);
113	TAILQ_FOREACH(object, &vm_object_list, object_list) {
114		VM_OBJECT_WLOCK(object);
115		vm_object_clear_flag(object, OBJ_ACTIVE);
116		VM_OBJECT_WUNLOCK(object);
117	}
118	mtx_unlock(&vm_object_list_mtx);
119	/*
120	 * Calculate process statistics.
121	 */
122	sx_slock(&allproc_lock);
123	FOREACH_PROC_IN_SYSTEM(p) {
124		if (p->p_flag & P_SYSTEM)
125			continue;
126		PROC_LOCK(p);
127		switch (p->p_state) {
128		case PRS_NEW:
129			PROC_UNLOCK(p);
130			continue;
131			break;
132		default:
133			FOREACH_THREAD_IN_PROC(p, td) {
134				thread_lock(td);
135				switch (td->td_state) {
136				case TDS_INHIBITED:
137					if (TD_IS_SWAPPED(td))
138						total.t_sw++;
139					else if (TD_IS_SLEEPING(td) &&
140					    td->td_priority <= PZERO)
141						total.t_dw++;
142					else
143						total.t_sl++;
144					break;
145
146				case TDS_CAN_RUN:
147					total.t_sw++;
148					break;
149				case TDS_RUNQ:
150				case TDS_RUNNING:
151					total.t_rq++;
152					thread_unlock(td);
153					continue;
154				default:
155					break;
156				}
157				thread_unlock(td);
158			}
159		}
160		PROC_UNLOCK(p);
161		/*
162		 * Note active objects.
163		 */
164		paging = 0;
165		vm = vmspace_acquire_ref(p);
166		if (vm == NULL)
167			continue;
168		map = &vm->vm_map;
169		vm_map_lock_read(map);
170		for (entry = map->header.next;
171		    entry != &map->header; entry = entry->next) {
172			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
173			    (object = entry->object.vm_object) == NULL)
174				continue;
175			VM_OBJECT_WLOCK(object);
176			vm_object_set_flag(object, OBJ_ACTIVE);
177			paging |= object->paging_in_progress;
178			VM_OBJECT_WUNLOCK(object);
179		}
180		vm_map_unlock_read(map);
181		vmspace_free(vm);
182		if (paging)
183			total.t_pw++;
184	}
185	sx_sunlock(&allproc_lock);
186	/*
187	 * Calculate object memory usage statistics.
188	 */
189	mtx_lock(&vm_object_list_mtx);
190	TAILQ_FOREACH(object, &vm_object_list, object_list) {
191		/*
192		 * Perform unsynchronized reads on the object.  In
193		 * this case, the lack of synchronization should not
194		 * impair the accuracy of the reported statistics.
195		 */
196		if ((object->flags & OBJ_FICTITIOUS) != 0) {
197			/*
198			 * Devices, like /dev/mem, will badly skew our totals.
199			 */
200			continue;
201		}
202		if (object->ref_count == 0) {
203			/*
204			 * Also skip unreferenced objects, including
205			 * vnodes representing mounted file systems.
206			 */
207			continue;
208		}
209		total.t_vm += object->size;
210		total.t_rm += object->resident_page_count;
211		if (object->flags & OBJ_ACTIVE) {
212			total.t_avm += object->size;
213			total.t_arm += object->resident_page_count;
214		}
215		if (object->shadow_count > 1) {
216			/* shared object */
217			total.t_vmshr += object->size;
218			total.t_rmshr += object->resident_page_count;
219			if (object->flags & OBJ_ACTIVE) {
220				total.t_avmshr += object->size;
221				total.t_armshr += object->resident_page_count;
222			}
223		}
224	}
225	mtx_unlock(&vm_object_list_mtx);
226	total.t_free = vm_cnt.v_free_count + vm_cnt.v_cache_count;
227	return (sysctl_handle_opaque(oidp, &total, sizeof(total), req));
228}
229
230/*
231 * vcnt() -	accumulate statistics from all cpus and the global cnt
232 *		structure.
233 *
234 *	The vmmeter structure is now per-cpu as well as global.  Those
235 *	statistics which can be kept on a per-cpu basis (to avoid cache
236 *	stalls between cpus) can be moved to the per-cpu vmmeter.  Remaining
237 *	statistics, such as v_free_reserved, are left in the global
238 *	structure.
239 *
240 * (sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
241 */
242static int
243vcnt(SYSCTL_HANDLER_ARGS)
244{
245	int count = *(int *)arg1;
246	int offset = (char *)arg1 - (char *)&vm_cnt;
247	int i;
248
249	CPU_FOREACH(i) {
250		struct pcpu *pcpu = pcpu_find(i);
251		count += *(int *)((char *)&pcpu->pc_cnt + offset);
252	}
253	return (SYSCTL_OUT(req, &count, sizeof(int)));
254}
255
256SYSCTL_PROC(_vm, VM_TOTAL, vmtotal, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
257    0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
258    "System virtual memory statistics");
259SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
260static SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0,
261	"VM meter sys stats");
262static SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0,
263	"VM meter vm stats");
264SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
265
266#define	VM_STATS(parent, var, descr) \
267	SYSCTL_PROC(parent, OID_AUTO, var, \
268	    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, &vm_cnt.var, 0, vcnt, \
269	    "IU", descr)
270#define	VM_STATS_VM(var, descr)		VM_STATS(_vm_stats_vm, var, descr)
271#define	VM_STATS_SYS(var, descr)	VM_STATS(_vm_stats_sys, var, descr)
272
273VM_STATS_SYS(v_swtch, "Context switches");
274VM_STATS_SYS(v_trap, "Traps");
275VM_STATS_SYS(v_syscall, "System calls");
276VM_STATS_SYS(v_intr, "Device interrupts");
277VM_STATS_SYS(v_soft, "Software interrupts");
278VM_STATS_VM(v_vm_faults, "Address memory faults");
279VM_STATS_VM(v_io_faults, "Page faults requiring I/O");
280VM_STATS_VM(v_cow_faults, "Copy-on-write faults");
281VM_STATS_VM(v_cow_optim, "Optimized COW faults");
282VM_STATS_VM(v_zfod, "Pages zero-filled on demand");
283VM_STATS_VM(v_ozfod, "Optimized zero fill pages");
284VM_STATS_VM(v_swapin, "Swap pager pageins");
285VM_STATS_VM(v_swapout, "Swap pager pageouts");
286VM_STATS_VM(v_swappgsin, "Swap pages swapped in");
287VM_STATS_VM(v_swappgsout, "Swap pages swapped out");
288VM_STATS_VM(v_vnodein, "Vnode pager pageins");
289VM_STATS_VM(v_vnodeout, "Vnode pager pageouts");
290VM_STATS_VM(v_vnodepgsin, "Vnode pages paged in");
291VM_STATS_VM(v_vnodepgsout, "Vnode pages paged out");
292VM_STATS_VM(v_intrans, "In transit page faults");
293VM_STATS_VM(v_reactivated, "Pages reactivated from free list");
294VM_STATS_VM(v_pdwakeups, "Pagedaemon wakeups");
295VM_STATS_VM(v_pdpages, "Pages analyzed by pagedaemon");
296VM_STATS_VM(v_tcached, "Total pages cached");
297VM_STATS_VM(v_dfree, "Pages freed by pagedaemon");
298VM_STATS_VM(v_pfree, "Pages freed by exiting processes");
299VM_STATS_VM(v_tfree, "Total pages freed");
300VM_STATS_VM(v_page_size, "Page size in bytes");
301VM_STATS_VM(v_page_count, "Total number of pages in system");
302VM_STATS_VM(v_free_reserved, "Pages reserved for deadlock");
303VM_STATS_VM(v_free_target, "Pages desired free");
304VM_STATS_VM(v_free_min, "Minimum low-free-pages threshold");
305VM_STATS_VM(v_free_count, "Free pages");
306VM_STATS_VM(v_wire_count, "Wired pages");
307VM_STATS_VM(v_active_count, "Active pages");
308VM_STATS_VM(v_inactive_target, "Desired inactive pages");
309VM_STATS_VM(v_inactive_count, "Inactive pages");
310VM_STATS_VM(v_cache_count, "Pages on cache queue");
311VM_STATS_VM(v_cache_min, "Min pages on cache queue");
312VM_STATS_VM(v_cache_max, "Max pages on cached queue");
313VM_STATS_VM(v_pageout_free_min, "Min pages reserved for kernel");
314VM_STATS_VM(v_interrupt_free_min, "Reserved pages for interrupt code");
315VM_STATS_VM(v_forks, "Number of fork() calls");
316VM_STATS_VM(v_vforks, "Number of vfork() calls");
317VM_STATS_VM(v_rforks, "Number of rfork() calls");
318VM_STATS_VM(v_kthreads, "Number of fork() calls by kernel");
319VM_STATS_VM(v_forkpages, "VM pages affected by fork()");
320VM_STATS_VM(v_vforkpages, "VM pages affected by vfork()");
321VM_STATS_VM(v_rforkpages, "VM pages affected by rfork()");
322VM_STATS_VM(v_kthreadpages, "VM pages affected by fork() by kernel");
323
324SYSCTL_INT(_vm_stats_misc, OID_AUTO, zero_page_count, CTLFLAG_RD,
325	&vm_page_zero_count, 0, "Number of zero-ed free pages");
326