xref: /freebsd/sys/vm/vm_meter.c (revision a9ad941c7af2ef2d8b68cc9871f10fa02f877e01)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)vm_meter.c	8.4 (Berkeley) 1/4/94
34a9ad941cSPoul-Henning Kamp  * $Id: vm_meter.c,v 1.8 1995/07/29 11:44:25 bde Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/proc.h>
39df8bae1dSRodney W. Grimes #include <sys/systm.h>
40df8bae1dSRodney W. Grimes #include <sys/kernel.h>
41df8bae1dSRodney W. Grimes #include <vm/vm.h>
42df8bae1dSRodney W. Grimes #include <sys/sysctl.h>
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes struct loadavg averunnable;	/* load average, of runnable procs */
4528f8db14SBruce Evans struct vmmeter cnt;
46df8bae1dSRodney W. Grimes 
47df8bae1dSRodney W. Grimes int maxslp = MAXSLP;
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes /*
50df8bae1dSRodney W. Grimes  * Constants for averages over 1, 5, and 15 minutes
51df8bae1dSRodney W. Grimes  * when sampling at 5 second intervals.
52df8bae1dSRodney W. Grimes  */
53df8bae1dSRodney W. Grimes fixpt_t cexp[3] = {
54df8bae1dSRodney W. Grimes 	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
55df8bae1dSRodney W. Grimes 	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
56df8bae1dSRodney W. Grimes 	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
57df8bae1dSRodney W. Grimes };
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes /*
60df8bae1dSRodney W. Grimes  * Compute a tenex style load average of a quantity on
61df8bae1dSRodney W. Grimes  * 1, 5 and 15 minute intervals.
62df8bae1dSRodney W. Grimes  */
6324a1cce3SDavid Greenman static void
64a9ad941cSPoul-Henning Kamp loadav(struct loadavg *avg)
65df8bae1dSRodney W. Grimes {
66df8bae1dSRodney W. Grimes 	register int i, nrun;
67df8bae1dSRodney W. Grimes 	register struct proc *p;
68df8bae1dSRodney W. Grimes 
69df8bae1dSRodney W. Grimes 	for (nrun = 0, p = (struct proc *) allproc; p != NULL; p = p->p_next) {
70df8bae1dSRodney W. Grimes 		switch (p->p_stat) {
71df8bae1dSRodney W. Grimes 		case SSLEEP:
72df8bae1dSRodney W. Grimes 			if (p->p_priority > PZERO || p->p_slptime != 0)
73df8bae1dSRodney W. Grimes 				continue;
74df8bae1dSRodney W. Grimes 			/* fall through */
75df8bae1dSRodney W. Grimes 		case SRUN:
76df8bae1dSRodney W. Grimes 		case SIDL:
77df8bae1dSRodney W. Grimes 			nrun++;
78df8bae1dSRodney W. Grimes 		}
79df8bae1dSRodney W. Grimes 	}
80df8bae1dSRodney W. Grimes 	for (i = 0; i < 3; i++)
81df8bae1dSRodney W. Grimes 		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
82df8bae1dSRodney W. Grimes 		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
83df8bae1dSRodney W. Grimes }
84df8bae1dSRodney W. Grimes 
8524a1cce3SDavid Greenman void
8624a1cce3SDavid Greenman vmmeter()
8724a1cce3SDavid Greenman {
8824a1cce3SDavid Greenman 
8924a1cce3SDavid Greenman 	if (time.tv_sec % 5 == 0)
9024a1cce3SDavid Greenman 		loadav(&averunnable);
9124a1cce3SDavid Greenman 	if (proc0.p_slptime > maxslp / 2)
9224a1cce3SDavid Greenman 		wakeup(&proc0);
9324a1cce3SDavid Greenman }
9424a1cce3SDavid Greenman 
95a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_FREE_MIN, v_free_min,
96a9ad941cSPoul-Henning Kamp 	CTLFLAG_RW, &cnt.v_free_min, 0, "");
97a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_FREE_TARGET, v_free_target,
98a9ad941cSPoul-Henning Kamp 	CTLFLAG_RW, &cnt.v_free_target, 0, "");
99a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
100a9ad941cSPoul-Henning Kamp 	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
101a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
102a9ad941cSPoul-Henning Kamp 	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
103a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_CACHE_MIN, v_cache_min,
104a9ad941cSPoul-Henning Kamp 	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
105a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_CACHE_MAX, v_cache_max,
106a9ad941cSPoul-Henning Kamp 	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
107a9ad941cSPoul-Henning Kamp SYSCTL_INT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
108a9ad941cSPoul-Henning Kamp 	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
109a9ad941cSPoul-Henning Kamp 
110a9ad941cSPoul-Henning Kamp static int
111a9ad941cSPoul-Henning Kamp vm_loadavg SYSCTL_HANDLER_ARGS
112df8bae1dSRodney W. Grimes {
113df8bae1dSRodney W. Grimes 	averunnable.fscale = FSCALE;
114a9ad941cSPoul-Henning Kamp 	return (sysctl_handle_opaque(oidp,
115a9ad941cSPoul-Henning Kamp 		oidp->oid_arg1, oidp->oid_arg2, req));
116df8bae1dSRodney W. Grimes }
117df8bae1dSRodney W. Grimes 
118a9ad941cSPoul-Henning Kamp SYSCTL_PROC(_vm, VM_LOADAVG, loadavg, CTLTYPE_OPAQUE|CTLFLAG_RD,
119a9ad941cSPoul-Henning Kamp 	&averunnable, sizeof(averunnable), vm_loadavg, "");
120a9ad941cSPoul-Henning Kamp 
121a9ad941cSPoul-Henning Kamp static int
122a9ad941cSPoul-Henning Kamp vmtotal SYSCTL_HANDLER_ARGS
123df8bae1dSRodney W. Grimes {
124a9ad941cSPoul-Henning Kamp 	struct proc *p;
125a9ad941cSPoul-Henning Kamp 	struct vmtotal total, *totalp;
126a9ad941cSPoul-Henning Kamp 	vm_map_entry_t entry;
127a9ad941cSPoul-Henning Kamp 	vm_object_t object;
128a9ad941cSPoul-Henning Kamp 	vm_map_t map;
129df8bae1dSRodney W. Grimes 	int paging;
130df8bae1dSRodney W. Grimes 
131a9ad941cSPoul-Henning Kamp 	totalp = &total;
132df8bae1dSRodney W. Grimes 	bzero(totalp, sizeof *totalp);
133df8bae1dSRodney W. Grimes 	/*
134df8bae1dSRodney W. Grimes 	 * Mark all objects as inactive.
135df8bae1dSRodney W. Grimes 	 */
136df8bae1dSRodney W. Grimes 	for (object = vm_object_list.tqh_first;
137df8bae1dSRodney W. Grimes 	    object != NULL;
138df8bae1dSRodney W. Grimes 	    object = object->object_list.tqe_next)
139df8bae1dSRodney W. Grimes 		object->flags &= ~OBJ_ACTIVE;
140df8bae1dSRodney W. Grimes 	/*
141df8bae1dSRodney W. Grimes 	 * Calculate process statistics.
142df8bae1dSRodney W. Grimes 	 */
143df8bae1dSRodney W. Grimes 	for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
144df8bae1dSRodney W. Grimes 		if (p->p_flag & P_SYSTEM)
145df8bae1dSRodney W. Grimes 			continue;
146df8bae1dSRodney W. Grimes 		switch (p->p_stat) {
147df8bae1dSRodney W. Grimes 		case 0:
148df8bae1dSRodney W. Grimes 			continue;
149df8bae1dSRodney W. Grimes 
150df8bae1dSRodney W. Grimes 		case SSLEEP:
151df8bae1dSRodney W. Grimes 		case SSTOP:
152df8bae1dSRodney W. Grimes 			if (p->p_flag & P_INMEM) {
153df8bae1dSRodney W. Grimes 				if (p->p_priority <= PZERO)
154df8bae1dSRodney W. Grimes 					totalp->t_dw++;
155df8bae1dSRodney W. Grimes 				else if (p->p_slptime < maxslp)
156df8bae1dSRodney W. Grimes 					totalp->t_sl++;
157df8bae1dSRodney W. Grimes 			} else if (p->p_slptime < maxslp)
158df8bae1dSRodney W. Grimes 				totalp->t_sw++;
159df8bae1dSRodney W. Grimes 			if (p->p_slptime >= maxslp)
160df8bae1dSRodney W. Grimes 				continue;
161df8bae1dSRodney W. Grimes 			break;
162df8bae1dSRodney W. Grimes 
163df8bae1dSRodney W. Grimes 		case SRUN:
164df8bae1dSRodney W. Grimes 		case SIDL:
165df8bae1dSRodney W. Grimes 			if (p->p_flag & P_INMEM)
166df8bae1dSRodney W. Grimes 				totalp->t_rq++;
167df8bae1dSRodney W. Grimes 			else
168df8bae1dSRodney W. Grimes 				totalp->t_sw++;
169df8bae1dSRodney W. Grimes 			if (p->p_stat == SIDL)
170df8bae1dSRodney W. Grimes 				continue;
171df8bae1dSRodney W. Grimes 			break;
172df8bae1dSRodney W. Grimes 		}
173df8bae1dSRodney W. Grimes 		/*
174df8bae1dSRodney W. Grimes 		 * Note active objects.
175df8bae1dSRodney W. Grimes 		 */
176df8bae1dSRodney W. Grimes 		paging = 0;
177df8bae1dSRodney W. Grimes 		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
178df8bae1dSRodney W. Grimes 		    entry != &map->header; entry = entry->next) {
179df8bae1dSRodney W. Grimes 			if (entry->is_a_map || entry->is_sub_map ||
180df8bae1dSRodney W. Grimes 			    entry->object.vm_object == NULL)
181df8bae1dSRodney W. Grimes 				continue;
182df8bae1dSRodney W. Grimes 			entry->object.vm_object->flags |= OBJ_ACTIVE;
183df8bae1dSRodney W. Grimes 			paging |= entry->object.vm_object->paging_in_progress;
184df8bae1dSRodney W. Grimes 		}
185df8bae1dSRodney W. Grimes 		if (paging)
186df8bae1dSRodney W. Grimes 			totalp->t_pw++;
187df8bae1dSRodney W. Grimes 	}
188df8bae1dSRodney W. Grimes 	/*
189df8bae1dSRodney W. Grimes 	 * Calculate object memory usage statistics.
190df8bae1dSRodney W. Grimes 	 */
191df8bae1dSRodney W. Grimes 	for (object = vm_object_list.tqh_first;
192df8bae1dSRodney W. Grimes 	    object != NULL;
193df8bae1dSRodney W. Grimes 	    object = object->object_list.tqe_next) {
194df8bae1dSRodney W. Grimes 		totalp->t_vm += num_pages(object->size);
195df8bae1dSRodney W. Grimes 		totalp->t_rm += object->resident_page_count;
196df8bae1dSRodney W. Grimes 		if (object->flags & OBJ_ACTIVE) {
197df8bae1dSRodney W. Grimes 			totalp->t_avm += num_pages(object->size);
198df8bae1dSRodney W. Grimes 			totalp->t_arm += object->resident_page_count;
199df8bae1dSRodney W. Grimes 		}
200df8bae1dSRodney W. Grimes 		if (object->ref_count > 1) {
201df8bae1dSRodney W. Grimes 			/* shared object */
202df8bae1dSRodney W. Grimes 			totalp->t_vmshr += num_pages(object->size);
203df8bae1dSRodney W. Grimes 			totalp->t_rmshr += object->resident_page_count;
204df8bae1dSRodney W. Grimes 			if (object->flags & OBJ_ACTIVE) {
205df8bae1dSRodney W. Grimes 				totalp->t_avmshr += num_pages(object->size);
206df8bae1dSRodney W. Grimes 				totalp->t_armshr += object->resident_page_count;
207df8bae1dSRodney W. Grimes 			}
208df8bae1dSRodney W. Grimes 		}
209df8bae1dSRodney W. Grimes 	}
2100d94caffSDavid Greenman 	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
211a9ad941cSPoul-Henning Kamp 	return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
212df8bae1dSRodney W. Grimes }
213a9ad941cSPoul-Henning Kamp 
214a9ad941cSPoul-Henning Kamp SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
215a9ad941cSPoul-Henning Kamp 	0, sizeof(struct vmtotal), vmtotal, "");
216