vm_meter.c revision 12623
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
3412623Sphk * $Id: vm_meter.c,v 1.9 1995/11/14 09:29:34 phk Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
381541Srgrimes#include <sys/proc.h>
391541Srgrimes#include <sys/systm.h>
401541Srgrimes#include <sys/kernel.h>
411541Srgrimes#include <vm/vm.h>
421541Srgrimes#include <sys/sysctl.h>
431541Srgrimes
4412623Sphkstruct loadavg averunnable =
4512623Sphk	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
4612623Sphk
479759Sbdestruct vmmeter cnt;
481541Srgrimes
495455Sdgint maxslp = MAXSLP;
501541Srgrimes
511541Srgrimes/*
521541Srgrimes * Constants for averages over 1, 5, and 15 minutes
531541Srgrimes * when sampling at 5 second intervals.
541541Srgrimes */
555455Sdgfixpt_t cexp[3] = {
561541Srgrimes	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
571541Srgrimes	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
581541Srgrimes	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
591541Srgrimes};
601541Srgrimes
611541Srgrimes/*
621541Srgrimes * Compute a tenex style load average of a quantity on
631541Srgrimes * 1, 5 and 15 minute intervals.
641541Srgrimes */
659507Sdgstatic void
6612286Sphkloadav(struct loadavg *avg)
671541Srgrimes{
681541Srgrimes	register int i, nrun;
691541Srgrimes	register struct proc *p;
701541Srgrimes
715455Sdg	for (nrun = 0, p = (struct proc *) allproc; p != NULL; p = p->p_next) {
721541Srgrimes		switch (p->p_stat) {
731541Srgrimes		case SSLEEP:
741541Srgrimes			if (p->p_priority > PZERO || p->p_slptime != 0)
751541Srgrimes				continue;
761541Srgrimes			/* fall through */
771541Srgrimes		case SRUN:
781541Srgrimes		case SIDL:
791541Srgrimes			nrun++;
801541Srgrimes		}
811541Srgrimes	}
821541Srgrimes	for (i = 0; i < 3; i++)
831541Srgrimes		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
845455Sdg		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
851541Srgrimes}
861541Srgrimes
879507Sdgvoid
889507Sdgvmmeter()
899507Sdg{
909507Sdg
919507Sdg	if (time.tv_sec % 5 == 0)
929507Sdg		loadav(&averunnable);
939507Sdg	if (proc0.p_slptime > maxslp / 2)
949507Sdg		wakeup(&proc0);
959507Sdg}
969507Sdg
9712286SphkSYSCTL_INT(_vm, VM_V_FREE_MIN, v_free_min,
9812286Sphk	CTLFLAG_RW, &cnt.v_free_min, 0, "");
9912286SphkSYSCTL_INT(_vm, VM_V_FREE_TARGET, v_free_target,
10012286Sphk	CTLFLAG_RW, &cnt.v_free_target, 0, "");
10112286SphkSYSCTL_INT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
10212286Sphk	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
10312286SphkSYSCTL_INT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
10412286Sphk	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
10512286SphkSYSCTL_INT(_vm, VM_V_CACHE_MIN, v_cache_min,
10612286Sphk	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
10712286SphkSYSCTL_INT(_vm, VM_V_CACHE_MAX, v_cache_max,
10812286Sphk	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
10912286SphkSYSCTL_INT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
11012286Sphk	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
11112286Sphk
11212623SphkSYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD, &averunnable, loadavg, "");
1131541Srgrimes
11412286Sphkstatic int
11512286Sphkvmtotal SYSCTL_HANDLER_ARGS
1161541Srgrimes{
11712286Sphk	struct proc *p;
11812286Sphk	struct vmtotal total, *totalp;
11912286Sphk	vm_map_entry_t entry;
12012286Sphk	vm_object_t object;
12112286Sphk	vm_map_t map;
1221541Srgrimes	int paging;
1231541Srgrimes
12412286Sphk	totalp = &total;
1251541Srgrimes	bzero(totalp, sizeof *totalp);
1261541Srgrimes	/*
1271541Srgrimes	 * Mark all objects as inactive.
1281541Srgrimes	 */
1291541Srgrimes	for (object = vm_object_list.tqh_first;
1305455Sdg	    object != NULL;
1315455Sdg	    object = object->object_list.tqe_next)
1321541Srgrimes		object->flags &= ~OBJ_ACTIVE;
1331541Srgrimes	/*
1341541Srgrimes	 * Calculate process statistics.
1351541Srgrimes	 */
1365455Sdg	for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
1371541Srgrimes		if (p->p_flag & P_SYSTEM)
1381541Srgrimes			continue;
1391541Srgrimes		switch (p->p_stat) {
1401541Srgrimes		case 0:
1411541Srgrimes			continue;
1421541Srgrimes
1431541Srgrimes		case SSLEEP:
1441541Srgrimes		case SSTOP:
1451541Srgrimes			if (p->p_flag & P_INMEM) {
1461541Srgrimes				if (p->p_priority <= PZERO)
1471541Srgrimes					totalp->t_dw++;
1481541Srgrimes				else if (p->p_slptime < maxslp)
1491541Srgrimes					totalp->t_sl++;
1501541Srgrimes			} else if (p->p_slptime < maxslp)
1511541Srgrimes				totalp->t_sw++;
1521541Srgrimes			if (p->p_slptime >= maxslp)
1531541Srgrimes				continue;
1541541Srgrimes			break;
1551541Srgrimes
1561541Srgrimes		case SRUN:
1571541Srgrimes		case SIDL:
1581541Srgrimes			if (p->p_flag & P_INMEM)
1591541Srgrimes				totalp->t_rq++;
1601541Srgrimes			else
1611541Srgrimes				totalp->t_sw++;
1621541Srgrimes			if (p->p_stat == SIDL)
1631541Srgrimes				continue;
1641541Srgrimes			break;
1651541Srgrimes		}
1661541Srgrimes		/*
1671541Srgrimes		 * Note active objects.
1681541Srgrimes		 */
1691541Srgrimes		paging = 0;
1701541Srgrimes		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
1715455Sdg		    entry != &map->header; entry = entry->next) {
1721541Srgrimes			if (entry->is_a_map || entry->is_sub_map ||
1731541Srgrimes			    entry->object.vm_object == NULL)
1741541Srgrimes				continue;
1751541Srgrimes			entry->object.vm_object->flags |= OBJ_ACTIVE;
1761541Srgrimes			paging |= entry->object.vm_object->paging_in_progress;
1771541Srgrimes		}
1781541Srgrimes		if (paging)
1791541Srgrimes			totalp->t_pw++;
1801541Srgrimes	}
1811541Srgrimes	/*
1821541Srgrimes	 * Calculate object memory usage statistics.
1831541Srgrimes	 */
1841541Srgrimes	for (object = vm_object_list.tqh_first;
1855455Sdg	    object != NULL;
1865455Sdg	    object = object->object_list.tqe_next) {
1871541Srgrimes		totalp->t_vm += num_pages(object->size);
1881541Srgrimes		totalp->t_rm += object->resident_page_count;
1891541Srgrimes		if (object->flags & OBJ_ACTIVE) {
1901541Srgrimes			totalp->t_avm += num_pages(object->size);
1911541Srgrimes			totalp->t_arm += object->resident_page_count;
1921541Srgrimes		}
1931541Srgrimes		if (object->ref_count > 1) {
1941541Srgrimes			/* shared object */
1951541Srgrimes			totalp->t_vmshr += num_pages(object->size);
1961541Srgrimes			totalp->t_rmshr += object->resident_page_count;
1971541Srgrimes			if (object->flags & OBJ_ACTIVE) {
1981541Srgrimes				totalp->t_avmshr += num_pages(object->size);
1991541Srgrimes				totalp->t_armshr += object->resident_page_count;
2001541Srgrimes			}
2011541Srgrimes		}
2021541Srgrimes	}
2035455Sdg	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
20412286Sphk	return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
2051541Srgrimes}
20612286Sphk
20712286SphkSYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
20812623Sphk	0, sizeof(struct vmtotal), vmtotal, "S,vmtotal", "");
209