1139825Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes *	@(#)vm_meter.c	8.4 (Berkeley) 1/4/94
301541Srgrimes */
311541Srgrimes
32116226Sobrien#include <sys/cdefs.h>
33116226Sobrien__FBSDID("$FreeBSD: releng/11.0/sys/vm/vm_meter.c 302063 2016-06-21 17:49:33Z kib $");
34116226Sobrien
351541Srgrimes#include <sys/param.h>
361541Srgrimes#include <sys/systm.h>
371541Srgrimes#include <sys/kernel.h>
3876166Smarkm#include <sys/lock.h>
3976166Smarkm#include <sys/mutex.h>
4076166Smarkm#include <sys/proc.h>
4134924Sbde#include <sys/resource.h>
42248084Sattilio#include <sys/rwlock.h>
4374927Sjhb#include <sys/sx.h>
4412662Sdg#include <sys/vmmeter.h>
4593823Sdillon#include <sys/smp.h>
4612662Sdg
471541Srgrimes#include <vm/vm.h>
4840794Speter#include <vm/vm_page.h>
4912726Sbde#include <vm/vm_extern.h>
5012662Sdg#include <vm/vm_param.h>
5112662Sdg#include <vm/pmap.h>
5212662Sdg#include <vm/vm_map.h>
5312662Sdg#include <vm/vm_object.h>
541541Srgrimes#include <sys/sysctl.h>
551541Srgrimes
56263620Sbdrewerystruct vmmeter vm_cnt;
571541Srgrimes
5862622SjhbSYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
59263620Sbdrewery	CTLFLAG_RW, &vm_cnt.v_free_min, 0, "Minimum low-free-pages threshold");
6062622SjhbSYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
61263620Sbdrewery	CTLFLAG_RW, &vm_cnt.v_free_target, 0, "Desired free pages");
6262622SjhbSYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
63263620Sbdrewery	CTLFLAG_RW, &vm_cnt.v_free_reserved, 0, "Pages reserved for deadlock");
6462622SjhbSYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
65263620Sbdrewery	CTLFLAG_RW, &vm_cnt.v_inactive_target, 0, "Pages desired inactive");
6662622SjhbSYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
67263620Sbdrewery	CTLFLAG_RW, &vm_cnt.v_pageout_free_min, 0, "Min pages reserved for kernel");
6862622SjhbSYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
69263620Sbdrewery	CTLFLAG_RW, &vm_cnt.v_free_severe, 0, "Severe page depletion point");
7012286Sphk
71136404Speterstatic int
72136404Spetersysctl_vm_loadavg(SYSCTL_HANDLER_ARGS)
73136404Speter{
74181887Sjulian
75136404Speter#ifdef SCTL_MASK32
76136404Speter	u_int32_t la[4];
771541Srgrimes
78136404Speter	if (req->flags & SCTL_MASK32) {
79136404Speter		la[0] = averunnable.ldavg[0];
80136404Speter		la[1] = averunnable.ldavg[1];
81136404Speter		la[2] = averunnable.ldavg[2];
82136404Speter		la[3] = averunnable.fscale;
83136404Speter		return SYSCTL_OUT(req, la, sizeof(la));
84136404Speter	} else
85136404Speter#endif
86136404Speter		return SYSCTL_OUT(req, &averunnable, sizeof(averunnable));
87136404Speter}
88187658SjhbSYSCTL_PROC(_vm, VM_LOADAVG, loadavg, CTLTYPE_STRUCT | CTLFLAG_RD |
89187658Sjhb    CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_loadavg, "S,loadavg",
90187658Sjhb    "Machine loadaverage history");
91136404Speter
92302063Skib/*
93302063Skib * This function aims to determine if the object is mapped,
94302063Skib * specifically, if it is referenced by a vm_map_entry.  Because
95302063Skib * objects occasionally acquire transient references that do not
96302063Skib * represent a mapping, the method used here is inexact.  However, it
97302063Skib * has very low overhead and is good enough for the advisory
98302063Skib * vm.vmtotal sysctl.
99302063Skib */
100302063Skibstatic bool
101302063Skibis_object_active(vm_object_t obj)
102302063Skib{
103302063Skib
104302063Skib	return (obj->ref_count > obj->shadow_count);
105302063Skib}
106302063Skib
10712286Sphkstatic int
10862573Sphkvmtotal(SYSCTL_HANDLER_ARGS)
1091541Srgrimes{
110164437Sru	struct vmtotal total;
11112286Sphk	vm_object_t object;
112302063Skib	struct proc *p;
11399072Sjulian	struct thread *td;
1141541Srgrimes
115164437Sru	bzero(&total, sizeof(total));
116302063Skib
1171541Srgrimes	/*
1181541Srgrimes	 * Calculate process statistics.
1191541Srgrimes	 */
12074927Sjhb	sx_slock(&allproc_lock);
12183366Sjulian	FOREACH_PROC_IN_SYSTEM(p) {
1221541Srgrimes		if (p->p_flag & P_SYSTEM)
1231541Srgrimes			continue;
124177368Sjeff		PROC_LOCK(p);
12599072Sjulian		switch (p->p_state) {
12699072Sjulian		case PRS_NEW:
127177368Sjeff			PROC_UNLOCK(p);
1281541Srgrimes			continue;
1291541Srgrimes			break;
13099072Sjulian		default:
13199072Sjulian			FOREACH_THREAD_IN_PROC(p, td) {
132170307Sjeff				thread_lock(td);
13399072Sjulian				switch (td->td_state) {
134103216Sjulian				case TDS_INHIBITED:
135177085Sjeff					if (TD_IS_SWAPPED(td))
136164437Sru						total.t_sw++;
137302063Skib					else if (TD_IS_SLEEPING(td)) {
138302063Skib						if (td->td_priority <= PZERO)
139302063Skib							total.t_dw++;
140302063Skib						else
141302063Skib							total.t_sl++;
142302063Skib						if (td->td_wchan ==
143302063Skib						    &vm_cnt.v_free_count)
144302063Skib							total.t_pw++;
145302063Skib					}
14699072Sjulian					break;
1471541Srgrimes
148103216Sjulian				case TDS_CAN_RUN:
149164437Sru					total.t_sw++;
150103216Sjulian					break;
15199072Sjulian				case TDS_RUNQ:
15299072Sjulian				case TDS_RUNNING:
153164437Sru					total.t_rq++;
154170307Sjeff					thread_unlock(td);
15599072Sjulian					continue;
15699072Sjulian				default:
15799072Sjulian					break;
15899072Sjulian				}
159170307Sjeff				thread_unlock(td);
16071572Sjhb			}
1611541Srgrimes		}
162177368Sjeff		PROC_UNLOCK(p);
1631541Srgrimes	}
16474927Sjhb	sx_sunlock(&allproc_lock);
1651541Srgrimes	/*
1661541Srgrimes	 * Calculate object memory usage statistics.
1671541Srgrimes	 */
16895112Salc	mtx_lock(&vm_object_list_mtx);
16975523Salfred	TAILQ_FOREACH(object, &vm_object_list, object_list) {
17042957Sdillon		/*
171282660Sjhb		 * Perform unsynchronized reads on the object.  In
172282660Sjhb		 * this case, the lack of synchronization should not
173282660Sjhb		 * impair the accuracy of the reported statistics.
17442957Sdillon		 */
175244043Salc		if ((object->flags & OBJ_FICTITIOUS) != 0) {
176124083Salc			/*
177124083Salc			 * Devices, like /dev/mem, will badly skew our totals.
178124083Salc			 */
17942957Sdillon			continue;
180108585Salc		}
181164429Sru		if (object->ref_count == 0) {
182164429Sru			/*
183164429Sru			 * Also skip unreferenced objects, including
184164429Sru			 * vnodes representing mounted file systems.
185164429Sru			 */
186164429Sru			continue;
187164429Sru		}
188302063Skib		if (object->ref_count == 1 &&
189302063Skib		    (object->flags & OBJ_NOSPLIT) != 0) {
190302063Skib			/*
191302063Skib			 * Also skip otherwise unreferenced swap
192302063Skib			 * objects backing tmpfs vnodes, and POSIX or
193302063Skib			 * SysV shared memory.
194302063Skib			 */
195302063Skib			continue;
196302063Skib		}
197164437Sru		total.t_vm += object->size;
198164437Sru		total.t_rm += object->resident_page_count;
199302063Skib		if (is_object_active(object)) {
200164437Sru			total.t_avm += object->size;
201164437Sru			total.t_arm += object->resident_page_count;
2021541Srgrimes		}
20318169Sdyson		if (object->shadow_count > 1) {
2041541Srgrimes			/* shared object */
205164437Sru			total.t_vmshr += object->size;
206164437Sru			total.t_rmshr += object->resident_page_count;
207302063Skib			if (is_object_active(object)) {
208164437Sru				total.t_avmshr += object->size;
209164437Sru				total.t_armshr += object->resident_page_count;
2101541Srgrimes			}
2111541Srgrimes		}
2121541Srgrimes	}
21395112Salc	mtx_unlock(&vm_object_list_mtx);
214263620Sbdrewery	total.t_free = vm_cnt.v_free_count + vm_cnt.v_cache_count;
215164437Sru	return (sysctl_handle_opaque(oidp, &total, sizeof(total), req));
2161541Srgrimes}
21712286Sphk
21893823Sdillon/*
21993823Sdillon * vcnt() -	accumulate statistics from all cpus and the global cnt
22093823Sdillon *		structure.
22193823Sdillon *
22293823Sdillon *	The vmmeter structure is now per-cpu as well as global.  Those
22393823Sdillon *	statistics which can be kept on a per-cpu basis (to avoid cache
22493823Sdillon *	stalls between cpus) can be moved to the per-cpu vmmeter.  Remaining
22593823Sdillon *	statistics, such as v_free_reserved, are left in the global
22693823Sdillon *	structure.
22793823Sdillon *
22893823Sdillon * (sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
22993823Sdillon */
23093823Sdillonstatic int
23193823Sdillonvcnt(SYSCTL_HANDLER_ARGS)
23293823Sdillon{
23393823Sdillon	int count = *(int *)arg1;
234263620Sbdrewery	int offset = (char *)arg1 - (char *)&vm_cnt;
23593823Sdillon	int i;
23693823Sdillon
237218592Sjmallett	CPU_FOREACH(i) {
23893823Sdillon		struct pcpu *pcpu = pcpu_find(i);
23993823Sdillon		count += *(int *)((char *)&pcpu->pc_cnt + offset);
24093823Sdillon	}
241144970Sjhb	return (SYSCTL_OUT(req, &count, sizeof(int)));
24293823Sdillon}
24393823Sdillon
244187658SjhbSYSCTL_PROC(_vm, VM_TOTAL, vmtotal, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
24546381Sbillf    0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
24646381Sbillf    "System virtual memory statistics");
247141696SphkSYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
248141630Sphkstatic SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0,
249141630Sphk	"VM meter sys stats");
250141630Sphkstatic SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0,
251141630Sphk	"VM meter vm stats");
25240794SpeterSYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
25393823Sdillon
254228498Seadler#define	VM_STATS(parent, var, descr) \
255228498Seadler	SYSCTL_PROC(parent, OID_AUTO, var, \
256263620Sbdrewery	    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, &vm_cnt.var, 0, vcnt, \
257228498Seadler	    "IU", descr)
258228498Seadler#define	VM_STATS_VM(var, descr)		VM_STATS(_vm_stats_vm, var, descr)
259228498Seadler#define	VM_STATS_SYS(var, descr)	VM_STATS(_vm_stats_sys, var, descr)
26093823Sdillon
261228449SeadlerVM_STATS_SYS(v_swtch, "Context switches");
262228449SeadlerVM_STATS_SYS(v_trap, "Traps");
263228449SeadlerVM_STATS_SYS(v_syscall, "System calls");
264228449SeadlerVM_STATS_SYS(v_intr, "Device interrupts");
265228449SeadlerVM_STATS_SYS(v_soft, "Software interrupts");
266228498SeadlerVM_STATS_VM(v_vm_faults, "Address memory faults");
267246032SzontVM_STATS_VM(v_io_faults, "Page faults requiring I/O");
268228498SeadlerVM_STATS_VM(v_cow_faults, "Copy-on-write faults");
269228498SeadlerVM_STATS_VM(v_cow_optim, "Optimized COW faults");
270228498SeadlerVM_STATS_VM(v_zfod, "Pages zero-filled on demand");
271228498SeadlerVM_STATS_VM(v_ozfod, "Optimized zero fill pages");
272228498SeadlerVM_STATS_VM(v_swapin, "Swap pager pageins");
273228498SeadlerVM_STATS_VM(v_swapout, "Swap pager pageouts");
274228498SeadlerVM_STATS_VM(v_swappgsin, "Swap pages swapped in");
275228498SeadlerVM_STATS_VM(v_swappgsout, "Swap pages swapped out");
276228498SeadlerVM_STATS_VM(v_vnodein, "Vnode pager pageins");
277228498SeadlerVM_STATS_VM(v_vnodeout, "Vnode pager pageouts");
278228498SeadlerVM_STATS_VM(v_vnodepgsin, "Vnode pages paged in");
279228498SeadlerVM_STATS_VM(v_vnodepgsout, "Vnode pages paged out");
280228498SeadlerVM_STATS_VM(v_intrans, "In transit page faults");
281228498SeadlerVM_STATS_VM(v_reactivated, "Pages reactivated from free list");
282228498SeadlerVM_STATS_VM(v_pdwakeups, "Pagedaemon wakeups");
283228498SeadlerVM_STATS_VM(v_pdpages, "Pages analyzed by pagedaemon");
284228498SeadlerVM_STATS_VM(v_tcached, "Total pages cached");
285228498SeadlerVM_STATS_VM(v_dfree, "Pages freed by pagedaemon");
286228498SeadlerVM_STATS_VM(v_pfree, "Pages freed by exiting processes");
287228498SeadlerVM_STATS_VM(v_tfree, "Total pages freed");
288228498SeadlerVM_STATS_VM(v_page_size, "Page size in bytes");
289228498SeadlerVM_STATS_VM(v_page_count, "Total number of pages in system");
290228498SeadlerVM_STATS_VM(v_free_reserved, "Pages reserved for deadlock");
291228498SeadlerVM_STATS_VM(v_free_target, "Pages desired free");
292228498SeadlerVM_STATS_VM(v_free_min, "Minimum low-free-pages threshold");
293228498SeadlerVM_STATS_VM(v_free_count, "Free pages");
294228498SeadlerVM_STATS_VM(v_wire_count, "Wired pages");
295228498SeadlerVM_STATS_VM(v_active_count, "Active pages");
296228498SeadlerVM_STATS_VM(v_inactive_target, "Desired inactive pages");
297228498SeadlerVM_STATS_VM(v_inactive_count, "Inactive pages");
298228498SeadlerVM_STATS_VM(v_cache_count, "Pages on cache queue");
299228498SeadlerVM_STATS_VM(v_pageout_free_min, "Min pages reserved for kernel");
300228498SeadlerVM_STATS_VM(v_interrupt_free_min, "Reserved pages for interrupt code");
301228498SeadlerVM_STATS_VM(v_forks, "Number of fork() calls");
302228498SeadlerVM_STATS_VM(v_vforks, "Number of vfork() calls");
303228498SeadlerVM_STATS_VM(v_rforks, "Number of rfork() calls");
304228498SeadlerVM_STATS_VM(v_kthreads, "Number of fork() calls by kernel");
305228498SeadlerVM_STATS_VM(v_forkpages, "VM pages affected by fork()");
306228498SeadlerVM_STATS_VM(v_vforkpages, "VM pages affected by vfork()");
307228498SeadlerVM_STATS_VM(v_rforkpages, "VM pages affected by rfork()");
308228498SeadlerVM_STATS_VM(v_kthreadpages, "VM pages affected by fork() by kernel");
309228449Seadler
310228449SeadlerSYSCTL_INT(_vm_stats_misc, OID_AUTO, zero_page_count, CTLFLAG_RD,
311228449Seadler	&vm_page_zero_count, 0, "Number of zero-ed free pages");
312