vm_meter.c revision 69947
11541Srgrimes/*
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 * 3. All advertising materials mentioning features or use of this software
1458705Scharnier *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)vm_meter.c	8.4 (Berkeley) 1/4/94
3450477Speter * $FreeBSD: head/sys/vm/vm_meter.c 69947 2000-12-13 00:17:05Z jake $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
381541Srgrimes#include <sys/proc.h>
391541Srgrimes#include <sys/systm.h>
401541Srgrimes#include <sys/kernel.h>
4134924Sbde#include <sys/resource.h>
4212662Sdg#include <sys/vmmeter.h>
4312662Sdg
441541Srgrimes#include <vm/vm.h>
4540794Speter#include <vm/vm_page.h>
4612726Sbde#include <vm/vm_extern.h>
4712662Sdg#include <vm/vm_param.h>
4822521Sdyson#include <sys/lock.h>
4912662Sdg#include <vm/pmap.h>
5012662Sdg#include <vm/vm_map.h>
5112662Sdg#include <vm/vm_object.h>
521541Srgrimes#include <sys/sysctl.h>
531541Srgrimes
5412623Sphkstruct loadavg averunnable =
5512623Sphk	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
5612623Sphk
579759Sbdestruct vmmeter cnt;
581541Srgrimes
5912820Sphkstatic int maxslp = MAXSLP;
601541Srgrimes
611541Srgrimes/*
621541Srgrimes * Constants for averages over 1, 5, and 15 minutes
631541Srgrimes * when sampling at 5 second intervals.
641541Srgrimes */
6512820Sphkstatic fixpt_t cexp[3] = {
661541Srgrimes	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
671541Srgrimes	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
681541Srgrimes	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
691541Srgrimes};
701541Srgrimes
711541Srgrimes/*
721541Srgrimes * Compute a tenex style load average of a quantity on
731541Srgrimes * 1, 5 and 15 minute intervals.
741541Srgrimes */
759507Sdgstatic void
7612286Sphkloadav(struct loadavg *avg)
771541Srgrimes{
781541Srgrimes	register int i, nrun;
791541Srgrimes	register struct proc *p;
801541Srgrimes
8169947Sjake	ALLPROC_LOCK(AP_SHARED);
8214531Shsu	for (nrun = 0, p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
831541Srgrimes		switch (p->p_stat) {
841541Srgrimes		case SSLEEP:
851541Srgrimes			if (p->p_priority > PZERO || p->p_slptime != 0)
861541Srgrimes				continue;
8758634Scharnier			/* FALLTHROUGH */
881541Srgrimes		case SRUN:
8965904Sjhb			if ((p->p_flag & P_NOLOAD) != 0)
9065904Sjhb				continue;
9165904Sjhb			/* FALLTHROUGH */
921541Srgrimes		case SIDL:
931541Srgrimes			nrun++;
941541Srgrimes		}
951541Srgrimes	}
9669947Sjake	ALLPROC_LOCK(AP_RELEASE);
971541Srgrimes	for (i = 0; i < 3; i++)
981541Srgrimes		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
995455Sdg		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
1001541Srgrimes}
1011541Srgrimes
1029507Sdgvoid
1039507Sdgvmmeter()
1049507Sdg{
1059507Sdg
10634961Sphk	if (time_second % 5 == 0)
1079507Sdg		loadav(&averunnable);
1089507Sdg	if (proc0.p_slptime > maxslp / 2)
1099507Sdg		wakeup(&proc0);
1109507Sdg}
1119507Sdg
11262622SjhbSYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
11312286Sphk	CTLFLAG_RW, &cnt.v_free_min, 0, "");
11462622SjhbSYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
11512286Sphk	CTLFLAG_RW, &cnt.v_free_target, 0, "");
11662622SjhbSYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
11712286Sphk	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
11862622SjhbSYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
11912286Sphk	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
12062622SjhbSYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
12112286Sphk	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
12262622SjhbSYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
12312286Sphk	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
12462622SjhbSYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
12512286Sphk	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
12662622SjhbSYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
12751337Sdillon	CTLFLAG_RW, &cnt.v_free_severe, 0, "");
12812286Sphk
12946381SbillfSYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD,
13046381Sbillf    &averunnable, loadavg, "Machine loadaverage history");
1311541Srgrimes
13212286Sphkstatic int
13362573Sphkvmtotal(SYSCTL_HANDLER_ARGS)
1341541Srgrimes{
13512286Sphk	struct proc *p;
13612286Sphk	struct vmtotal total, *totalp;
13712286Sphk	vm_map_entry_t entry;
13812286Sphk	vm_object_t object;
13912286Sphk	vm_map_t map;
1401541Srgrimes	int paging;
1411541Srgrimes
14212286Sphk	totalp = &total;
1431541Srgrimes	bzero(totalp, sizeof *totalp);
1441541Srgrimes	/*
1451541Srgrimes	 * Mark all objects as inactive.
1461541Srgrimes	 */
14715809Sdyson	for (object = TAILQ_FIRST(&vm_object_list);
1485455Sdg	    object != NULL;
14915809Sdyson	    object = TAILQ_NEXT(object,object_list))
15038517Sdfr		vm_object_clear_flag(object, OBJ_ACTIVE);
1511541Srgrimes	/*
1521541Srgrimes	 * Calculate process statistics.
1531541Srgrimes	 */
15469947Sjake	ALLPROC_LOCK(AP_SHARED);
15514531Shsu	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1561541Srgrimes		if (p->p_flag & P_SYSTEM)
1571541Srgrimes			continue;
1581541Srgrimes		switch (p->p_stat) {
1591541Srgrimes		case 0:
1601541Srgrimes			continue;
1611541Srgrimes
16265557Sjasone		case SMTX:
1631541Srgrimes		case SSLEEP:
1641541Srgrimes		case SSTOP:
1651541Srgrimes			if (p->p_flag & P_INMEM) {
1661541Srgrimes				if (p->p_priority <= PZERO)
1671541Srgrimes					totalp->t_dw++;
1681541Srgrimes				else if (p->p_slptime < maxslp)
1691541Srgrimes					totalp->t_sl++;
1701541Srgrimes			} else if (p->p_slptime < maxslp)
1711541Srgrimes				totalp->t_sw++;
1721541Srgrimes			if (p->p_slptime >= maxslp)
1731541Srgrimes				continue;
1741541Srgrimes			break;
1751541Srgrimes
17665557Sjasone		case SWAIT:
17765557Sjasone			totalp->t_sl++;
17865557Sjasone			continue;
17965557Sjasone
1801541Srgrimes		case SRUN:
1811541Srgrimes		case SIDL:
1821541Srgrimes			if (p->p_flag & P_INMEM)
1831541Srgrimes				totalp->t_rq++;
1841541Srgrimes			else
1851541Srgrimes				totalp->t_sw++;
1861541Srgrimes			if (p->p_stat == SIDL)
1871541Srgrimes				continue;
1881541Srgrimes			break;
1891541Srgrimes		}
1901541Srgrimes		/*
1911541Srgrimes		 * Note active objects.
1921541Srgrimes		 */
1931541Srgrimes		paging = 0;
1941541Srgrimes		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
1955455Sdg		    entry != &map->header; entry = entry->next) {
19643748Sdillon			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
1971541Srgrimes			    entry->object.vm_object == NULL)
1981541Srgrimes				continue;
19938517Sdfr			vm_object_set_flag(entry->object.vm_object, OBJ_ACTIVE);
2001541Srgrimes			paging |= entry->object.vm_object->paging_in_progress;
2011541Srgrimes		}
2021541Srgrimes		if (paging)
2031541Srgrimes			totalp->t_pw++;
2041541Srgrimes	}
20569947Sjake	ALLPROC_LOCK(AP_RELEASE);
2061541Srgrimes	/*
2071541Srgrimes	 * Calculate object memory usage statistics.
2081541Srgrimes	 */
20915809Sdyson	for (object = TAILQ_FIRST(&vm_object_list);
2105455Sdg	    object != NULL;
21115809Sdyson	    object = TAILQ_NEXT(object, object_list)) {
21242957Sdillon		/*
21342957Sdillon		 * devices, like /dev/mem, will badly skew our totals
21442957Sdillon		 */
21542957Sdillon		if (object->type == OBJT_DEVICE)
21642957Sdillon			continue;
21718169Sdyson		totalp->t_vm += object->size;
2181541Srgrimes		totalp->t_rm += object->resident_page_count;
2191541Srgrimes		if (object->flags & OBJ_ACTIVE) {
22018169Sdyson			totalp->t_avm += object->size;
2211541Srgrimes			totalp->t_arm += object->resident_page_count;
2221541Srgrimes		}
22318169Sdyson		if (object->shadow_count > 1) {
2241541Srgrimes			/* shared object */
22518169Sdyson			totalp->t_vmshr += object->size;
2261541Srgrimes			totalp->t_rmshr += object->resident_page_count;
2271541Srgrimes			if (object->flags & OBJ_ACTIVE) {
22818169Sdyson				totalp->t_avmshr += object->size;
2291541Srgrimes				totalp->t_armshr += object->resident_page_count;
2301541Srgrimes			}
2311541Srgrimes		}
2321541Srgrimes	}
2335455Sdg	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
23412286Sphk	return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
2351541Srgrimes}
23612286Sphk
23712286SphkSYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
23846381Sbillf    0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
23946381Sbillf    "System virtual memory statistics");
24040794SpeterSYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
24140794SpeterSYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, "VM meter sys stats");
24240794SpeterSYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, "VM meter vm stats");
24340794SpeterSYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
24462622SjhbSYSCTL_UINT(_vm_stats_sys, OID_AUTO,
24540794Speter	v_swtch, CTLFLAG_RD, &cnt.v_swtch, 0, "Context switches");
24662622SjhbSYSCTL_UINT(_vm_stats_sys, OID_AUTO,
24740794Speter	v_trap, CTLFLAG_RD, &cnt.v_trap, 0, "Traps");
24862622SjhbSYSCTL_UINT(_vm_stats_sys, OID_AUTO,
24940794Speter	v_syscall, CTLFLAG_RD, &cnt.v_syscall, 0, "Syscalls");
25062622SjhbSYSCTL_UINT(_vm_stats_sys, OID_AUTO, v_intr, CTLFLAG_RD,
25146381Sbillf    &cnt.v_intr, 0, "Hardware interrupts");
25262622SjhbSYSCTL_UINT(_vm_stats_sys, OID_AUTO, v_soft, CTLFLAG_RD,
25346381Sbillf    &cnt.v_soft, 0, "Software interrupts");
25462622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
25540794Speter	v_vm_faults, CTLFLAG_RD, &cnt.v_vm_faults, 0, "VM faults");
25662622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
25740794Speter	v_cow_faults, CTLFLAG_RD, &cnt.v_cow_faults, 0, "COW faults");
25862622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
25940794Speter	v_cow_optim, CTLFLAG_RD, &cnt.v_cow_optim, 0, "Optimized COW faults");
26062622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
26140794Speter	v_zfod, CTLFLAG_RD, &cnt.v_zfod, 0, "Zero fill");
26262622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
26340794Speter	v_ozfod, CTLFLAG_RD, &cnt.v_ozfod, 0, "Optimized zero fill");
26462622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
26540794Speter	v_swapin, CTLFLAG_RD, &cnt.v_swapin, 0, "Swapin operations");
26662622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
26740794Speter	v_swapout, CTLFLAG_RD, &cnt.v_swapout, 0, "Swapout operations");
26862622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
26940794Speter	v_swappgsin, CTLFLAG_RD, &cnt.v_swappgsin, 0, "Swapin pages");
27062622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
27140794Speter	v_swappgsout, CTLFLAG_RD, &cnt.v_swappgsout, 0, "Swapout pages");
27262622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
27340794Speter	v_vnodein, CTLFLAG_RD, &cnt.v_vnodein, 0, "Vnodein operations");
27462622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
27540794Speter	v_vnodeout, CTLFLAG_RD, &cnt.v_vnodeout, 0, "Vnodeout operations");
27662622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
27740794Speter	v_vnodepgsin, CTLFLAG_RD, &cnt.v_vnodepgsin, 0, "Vnodein pages");
27862622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
27940794Speter	v_vnodepgsout, CTLFLAG_RD, &cnt.v_vnodepgsout, 0, "Vnodeout pages");
28062622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
28140794Speter	v_intrans, CTLFLAG_RD, &cnt.v_intrans, 0, "In transit page blocking");
28262622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
28340794Speter	v_reactivated, CTLFLAG_RD, &cnt.v_reactivated, 0, "Reactivated pages");
28462622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
28540794Speter	v_pdwakeups, CTLFLAG_RD, &cnt.v_pdwakeups, 0, "Pagedaemon wakeups");
28662622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
28740794Speter	v_pdpages, CTLFLAG_RD, &cnt.v_pdpages, 0, "Pagedaemon page scans");
28862622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
28940794Speter	v_dfree, CTLFLAG_RD, &cnt.v_dfree, 0, "");
29062622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
29140794Speter	v_pfree, CTLFLAG_RD, &cnt.v_pfree, 0, "");
29262622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
29340794Speter	v_tfree, CTLFLAG_RD, &cnt.v_tfree, 0, "");
29462622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
29540794Speter	v_page_size, CTLFLAG_RD, &cnt.v_page_size, 0, "");
29662622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
29740794Speter	v_page_count, CTLFLAG_RD, &cnt.v_page_count, 0, "");
29862622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
29940794Speter	v_free_reserved, CTLFLAG_RD, &cnt.v_free_reserved, 0, "");
30062622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
30140794Speter	v_free_target, CTLFLAG_RD, &cnt.v_free_target, 0, "");
30262622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
30340794Speter	v_free_min, CTLFLAG_RD, &cnt.v_free_min, 0, "");
30462622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
30540794Speter	v_free_count, CTLFLAG_RD, &cnt.v_free_count, 0, "");
30662622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
30740794Speter	v_wire_count, CTLFLAG_RD, &cnt.v_wire_count, 0, "");
30862622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
30940794Speter	v_active_count, CTLFLAG_RD, &cnt.v_active_count, 0, "");
31062622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
31140794Speter	v_inactive_target, CTLFLAG_RD, &cnt.v_inactive_target, 0, "");
31262622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
31340794Speter	v_inactive_count, CTLFLAG_RD, &cnt.v_inactive_count, 0, "");
31462622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
31540794Speter	v_cache_count, CTLFLAG_RD, &cnt.v_cache_count, 0, "");
31662622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
31740794Speter	v_cache_min, CTLFLAG_RD, &cnt.v_cache_min, 0, "");
31862622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
31940794Speter	v_cache_max, CTLFLAG_RD, &cnt.v_cache_max, 0, "");
32062622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
32140794Speter	v_pageout_free_min, CTLFLAG_RD, &cnt.v_pageout_free_min, 0, "");
32262622SjhbSYSCTL_UINT(_vm_stats_vm, OID_AUTO,
32340794Speter	v_interrupt_free_min, CTLFLAG_RD, &cnt.v_interrupt_free_min, 0, "");
32440794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO,
32540794Speter	zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0, "");
32640794Speter#if 0
32740794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO,
32840794Speter	page_mask, CTLFLAG_RD, &page_mask, 0, "");
32940794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO,
33040794Speter	page_shift, CTLFLAG_RD, &page_shift, 0, "");
33140794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO,
33240794Speter	first_page, CTLFLAG_RD, &first_page, 0, "");
33340794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO,
33440794Speter	last_page, CTLFLAG_RD, &last_page, 0, "");
33540794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO,
33640794Speter	vm_page_bucket_count, CTLFLAG_RD, &vm_page_bucket_count, 0, "");
33740794SpeterSYSCTL_INT(_vm_stats_misc, OID_AUTO,
33840794Speter	vm_page_hash_mask, CTLFLAG_RD, &vm_page_hash_mask, 0, "");
33940794Speter#endif
340