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