vm_meter.c revision 18169
190075Sobrien/*
2169689Skan * Copyright (c) 1982, 1986, 1989, 1993
3169689Skan *	The Regents of the University of California.  All rights reserved.
490075Sobrien *
590075Sobrien * Redistribution and use in source and binary forms, with or without
6132718Skan * modification, are permitted provided that the following conditions
790075Sobrien * are met:
8132718Skan * 1. Redistributions of source code must retain the above copyright
9132718Skan *    notice, this list of conditions and the following disclaimer.
10132718Skan * 2. Redistributions in binary form must reproduce the above copyright
11132718Skan *    notice, this list of conditions and the following disclaimer in the
1290075Sobrien *    documentation and/or other materials provided with the distribution.
13132718Skan * 3. All advertising materials mentioning features or use of this software
14132718Skan *    must display the following acknowledgement:
15132718Skan *	This product includes software developed by the University of
16132718Skan *	California, Berkeley and its contributors.
1790075Sobrien * 4. Neither the name of the University nor the names of its contributors
18132718Skan *    may be used to endorse or promote products derived from this software
19132718Skan *    without specific prior written permission.
20169689Skan *
21169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2290075Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2390075Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2490075Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2590075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2690075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2790075Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2890075Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2990075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3090075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3190075Sobrien * SUCH DAMAGE.
3290075Sobrien *
3390075Sobrien *	@(#)vm_meter.c	8.4 (Berkeley) 1/4/94
3490075Sobrien * $Id: vm_meter.c,v 1.15 1996/05/18 03:37:47 dyson Exp $
3590075Sobrien */
3690075Sobrien
3790075Sobrien#include <sys/param.h>
3890075Sobrien#include <sys/proc.h>
3990075Sobrien#include <sys/systm.h>
4090075Sobrien#include <sys/kernel.h>
41169689Skan#include <sys/vmmeter.h>
42169689Skan#include <sys/queue.h>
43169689Skan
44169689Skan#include <vm/vm.h>
4590075Sobrien#include <vm/vm_extern.h>
46169689Skan#include <vm/vm_param.h>
4790075Sobrien#include <vm/vm_prot.h>
4890075Sobrien#include <vm/lock.h>
4990075Sobrien#include <vm/pmap.h>
5090075Sobrien#include <vm/vm_map.h>
5190075Sobrien#include <vm/vm_object.h>
5290075Sobrien#include <sys/sysctl.h>
5390075Sobrien
5490075Sobrienstruct loadavg averunnable =
5590075Sobrien	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
5690075Sobrien
5790075Sobrienstruct vmmeter cnt;
5890075Sobrien
5990075Sobrienstatic int maxslp = MAXSLP;
6090075Sobrien
6190075Sobrien/*
6290075Sobrien * Constants for averages over 1, 5, and 15 minutes
6390075Sobrien * when sampling at 5 second intervals.
6490075Sobrien */
6590075Sobrienstatic fixpt_t cexp[3] = {
6690075Sobrien	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
67132718Skan	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
6890075Sobrien	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
69117395Skan};
70117395Skan
7190075Sobrien/*
72169689Skan * Compute a tenex style load average of a quantity on
73169689Skan * 1, 5 and 15 minute intervals.
7490075Sobrien */
7590075Sobrienstatic void
7690075Sobrienloadav(struct loadavg *avg)
77{
78	register int i, nrun;
79	register struct proc *p;
80
81	for (nrun = 0, p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
82		switch (p->p_stat) {
83		case SSLEEP:
84			if (p->p_priority > PZERO || p->p_slptime != 0)
85				continue;
86			/* fall through */
87		case SRUN:
88		case SIDL:
89			nrun++;
90		}
91	}
92	for (i = 0; i < 3; i++)
93		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
94		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
95}
96
97void
98vmmeter()
99{
100
101	if (time.tv_sec % 5 == 0)
102		loadav(&averunnable);
103	if (proc0.p_slptime > maxslp / 2)
104		wakeup(&proc0);
105}
106
107SYSCTL_INT(_vm, VM_V_FREE_MIN, v_free_min,
108	CTLFLAG_RW, &cnt.v_free_min, 0, "");
109SYSCTL_INT(_vm, VM_V_FREE_TARGET, v_free_target,
110	CTLFLAG_RW, &cnt.v_free_target, 0, "");
111SYSCTL_INT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
112	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
113SYSCTL_INT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
114	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
115SYSCTL_INT(_vm, VM_V_CACHE_MIN, v_cache_min,
116	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
117SYSCTL_INT(_vm, VM_V_CACHE_MAX, v_cache_max,
118	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
119SYSCTL_INT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
120	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
121
122SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD, &averunnable, loadavg, "");
123
124static int
125vmtotal SYSCTL_HANDLER_ARGS
126{
127	struct proc *p;
128	struct vmtotal total, *totalp;
129	vm_map_entry_t entry;
130	vm_object_t object;
131	vm_map_t map;
132	int paging;
133
134	totalp = &total;
135	bzero(totalp, sizeof *totalp);
136	/*
137	 * Mark all objects as inactive.
138	 */
139	for (object = TAILQ_FIRST(&vm_object_list);
140	    object != NULL;
141	    object = TAILQ_NEXT(object,object_list))
142		object->flags &= ~OBJ_ACTIVE;
143	/*
144	 * Calculate process statistics.
145	 */
146	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
147		if (p->p_flag & P_SYSTEM)
148			continue;
149		switch (p->p_stat) {
150		case 0:
151			continue;
152
153		case SSLEEP:
154		case SSTOP:
155			if (p->p_flag & P_INMEM) {
156				if (p->p_priority <= PZERO)
157					totalp->t_dw++;
158				else if (p->p_slptime < maxslp)
159					totalp->t_sl++;
160			} else if (p->p_slptime < maxslp)
161				totalp->t_sw++;
162			if (p->p_slptime >= maxslp)
163				continue;
164			break;
165
166		case SRUN:
167		case SIDL:
168			if (p->p_flag & P_INMEM)
169				totalp->t_rq++;
170			else
171				totalp->t_sw++;
172			if (p->p_stat == SIDL)
173				continue;
174			break;
175		}
176		/*
177		 * Note active objects.
178		 */
179		paging = 0;
180		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
181		    entry != &map->header; entry = entry->next) {
182			if (entry->is_a_map || entry->is_sub_map ||
183			    entry->object.vm_object == NULL)
184				continue;
185			entry->object.vm_object->flags |= OBJ_ACTIVE;
186			paging |= entry->object.vm_object->paging_in_progress;
187		}
188		if (paging)
189			totalp->t_pw++;
190	}
191	/*
192	 * Calculate object memory usage statistics.
193	 */
194	for (object = TAILQ_FIRST(&vm_object_list);
195	    object != NULL;
196	    object = TAILQ_NEXT(object, object_list)) {
197		totalp->t_vm += object->size;
198		totalp->t_rm += object->resident_page_count;
199		if (object->flags & OBJ_ACTIVE) {
200			totalp->t_avm += object->size;
201			totalp->t_arm += object->resident_page_count;
202		}
203		if (object->shadow_count > 1) {
204			/* shared object */
205			totalp->t_vmshr += object->size;
206			totalp->t_rmshr += object->resident_page_count;
207			if (object->flags & OBJ_ACTIVE) {
208				totalp->t_avmshr += object->size;
209				totalp->t_armshr += object->resident_page_count;
210			}
211		}
212	}
213	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
214	return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
215}
216
217SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
218	0, sizeof(struct vmtotal), vmtotal, "S,vmtotal", "");
219