vm_meter.c revision 12820
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
141541Srgrimes *    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
3412820Sphk * $Id: vm_meter.c,v 1.12 1995/12/10 14:52:10 bde Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
381541Srgrimes#include <sys/proc.h>
391541Srgrimes#include <sys/systm.h>
401541Srgrimes#include <sys/kernel.h>
4112662Sdg#include <sys/vmmeter.h>
4212662Sdg#include <sys/queue.h>
4312662Sdg
441541Srgrimes#include <vm/vm.h>
4512726Sbde#include <vm/vm_extern.h>
4612662Sdg#include <vm/vm_param.h>
4712662Sdg#include <vm/vm_prot.h>
4812662Sdg#include <vm/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
815455Sdg	for (nrun = 0, p = (struct proc *) allproc; p != NULL; p = p->p_next) {
821541Srgrimes		switch (p->p_stat) {
831541Srgrimes		case SSLEEP:
841541Srgrimes			if (p->p_priority > PZERO || p->p_slptime != 0)
851541Srgrimes				continue;
861541Srgrimes			/* fall through */
871541Srgrimes		case SRUN:
881541Srgrimes		case SIDL:
891541Srgrimes			nrun++;
901541Srgrimes		}
911541Srgrimes	}
921541Srgrimes	for (i = 0; i < 3; i++)
931541Srgrimes		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
945455Sdg		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
951541Srgrimes}
961541Srgrimes
979507Sdgvoid
989507Sdgvmmeter()
999507Sdg{
1009507Sdg
1019507Sdg	if (time.tv_sec % 5 == 0)
1029507Sdg		loadav(&averunnable);
1039507Sdg	if (proc0.p_slptime > maxslp / 2)
1049507Sdg		wakeup(&proc0);
1059507Sdg}
1069507Sdg
10712286SphkSYSCTL_INT(_vm, VM_V_FREE_MIN, v_free_min,
10812286Sphk	CTLFLAG_RW, &cnt.v_free_min, 0, "");
10912286SphkSYSCTL_INT(_vm, VM_V_FREE_TARGET, v_free_target,
11012286Sphk	CTLFLAG_RW, &cnt.v_free_target, 0, "");
11112286SphkSYSCTL_INT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
11212286Sphk	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
11312286SphkSYSCTL_INT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
11412286Sphk	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
11512286SphkSYSCTL_INT(_vm, VM_V_CACHE_MIN, v_cache_min,
11612286Sphk	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
11712286SphkSYSCTL_INT(_vm, VM_V_CACHE_MAX, v_cache_max,
11812286Sphk	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
11912286SphkSYSCTL_INT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
12012286Sphk	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
12112286Sphk
12212623SphkSYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD, &averunnable, loadavg, "");
1231541Srgrimes
12412286Sphkstatic int
12512286Sphkvmtotal SYSCTL_HANDLER_ARGS
1261541Srgrimes{
12712286Sphk	struct proc *p;
12812286Sphk	struct vmtotal total, *totalp;
12912286Sphk	vm_map_entry_t entry;
13012286Sphk	vm_object_t object;
13112286Sphk	vm_map_t map;
1321541Srgrimes	int paging;
1331541Srgrimes
13412286Sphk	totalp = &total;
1351541Srgrimes	bzero(totalp, sizeof *totalp);
1361541Srgrimes	/*
1371541Srgrimes	 * Mark all objects as inactive.
1381541Srgrimes	 */
1391541Srgrimes	for (object = vm_object_list.tqh_first;
1405455Sdg	    object != NULL;
1415455Sdg	    object = object->object_list.tqe_next)
1421541Srgrimes		object->flags &= ~OBJ_ACTIVE;
1431541Srgrimes	/*
1441541Srgrimes	 * Calculate process statistics.
1451541Srgrimes	 */
1465455Sdg	for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
1471541Srgrimes		if (p->p_flag & P_SYSTEM)
1481541Srgrimes			continue;
1491541Srgrimes		switch (p->p_stat) {
1501541Srgrimes		case 0:
1511541Srgrimes			continue;
1521541Srgrimes
1531541Srgrimes		case SSLEEP:
1541541Srgrimes		case SSTOP:
1551541Srgrimes			if (p->p_flag & P_INMEM) {
1561541Srgrimes				if (p->p_priority <= PZERO)
1571541Srgrimes					totalp->t_dw++;
1581541Srgrimes				else if (p->p_slptime < maxslp)
1591541Srgrimes					totalp->t_sl++;
1601541Srgrimes			} else if (p->p_slptime < maxslp)
1611541Srgrimes				totalp->t_sw++;
1621541Srgrimes			if (p->p_slptime >= maxslp)
1631541Srgrimes				continue;
1641541Srgrimes			break;
1651541Srgrimes
1661541Srgrimes		case SRUN:
1671541Srgrimes		case SIDL:
1681541Srgrimes			if (p->p_flag & P_INMEM)
1691541Srgrimes				totalp->t_rq++;
1701541Srgrimes			else
1711541Srgrimes				totalp->t_sw++;
1721541Srgrimes			if (p->p_stat == SIDL)
1731541Srgrimes				continue;
1741541Srgrimes			break;
1751541Srgrimes		}
1761541Srgrimes		/*
1771541Srgrimes		 * Note active objects.
1781541Srgrimes		 */
1791541Srgrimes		paging = 0;
1801541Srgrimes		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
1815455Sdg		    entry != &map->header; entry = entry->next) {
1821541Srgrimes			if (entry->is_a_map || entry->is_sub_map ||
1831541Srgrimes			    entry->object.vm_object == NULL)
1841541Srgrimes				continue;
1851541Srgrimes			entry->object.vm_object->flags |= OBJ_ACTIVE;
1861541Srgrimes			paging |= entry->object.vm_object->paging_in_progress;
1871541Srgrimes		}
1881541Srgrimes		if (paging)
1891541Srgrimes			totalp->t_pw++;
1901541Srgrimes	}
1911541Srgrimes	/*
1921541Srgrimes	 * Calculate object memory usage statistics.
1931541Srgrimes	 */
1941541Srgrimes	for (object = vm_object_list.tqh_first;
1955455Sdg	    object != NULL;
1965455Sdg	    object = object->object_list.tqe_next) {
1971541Srgrimes		totalp->t_vm += num_pages(object->size);
1981541Srgrimes		totalp->t_rm += object->resident_page_count;
1991541Srgrimes		if (object->flags & OBJ_ACTIVE) {
2001541Srgrimes			totalp->t_avm += num_pages(object->size);
2011541Srgrimes			totalp->t_arm += object->resident_page_count;
2021541Srgrimes		}
2031541Srgrimes		if (object->ref_count > 1) {
2041541Srgrimes			/* shared object */
2051541Srgrimes			totalp->t_vmshr += num_pages(object->size);
2061541Srgrimes			totalp->t_rmshr += object->resident_page_count;
2071541Srgrimes			if (object->flags & OBJ_ACTIVE) {
2081541Srgrimes				totalp->t_avmshr += num_pages(object->size);
2091541Srgrimes				totalp->t_armshr += object->resident_page_count;
2101541Srgrimes			}
2111541Srgrimes		}
2121541Srgrimes	}
2135455Sdg	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
21412286Sphk	return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
2151541Srgrimes}
21612286Sphk
21712286SphkSYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
21812623Sphk	0, sizeof(struct vmtotal), vmtotal, "S,vmtotal", "");
219