vm_meter.c revision 12623
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.9 1995/11/14 09:29:34 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 <vm/vm.h>
42#include <sys/sysctl.h>
43
44struct loadavg averunnable =
45	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
46
47struct vmmeter cnt;
48
49int maxslp = MAXSLP;
50
51/*
52 * Constants for averages over 1, 5, and 15 minutes
53 * when sampling at 5 second intervals.
54 */
55fixpt_t cexp[3] = {
56	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
57	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
58	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
59};
60
61/*
62 * Compute a tenex style load average of a quantity on
63 * 1, 5 and 15 minute intervals.
64 */
65static void
66loadav(struct loadavg *avg)
67{
68	register int i, nrun;
69	register struct proc *p;
70
71	for (nrun = 0, p = (struct proc *) allproc; p != NULL; p = p->p_next) {
72		switch (p->p_stat) {
73		case SSLEEP:
74			if (p->p_priority > PZERO || p->p_slptime != 0)
75				continue;
76			/* fall through */
77		case SRUN:
78		case SIDL:
79			nrun++;
80		}
81	}
82	for (i = 0; i < 3; i++)
83		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
84		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
85}
86
87void
88vmmeter()
89{
90
91	if (time.tv_sec % 5 == 0)
92		loadav(&averunnable);
93	if (proc0.p_slptime > maxslp / 2)
94		wakeup(&proc0);
95}
96
97SYSCTL_INT(_vm, VM_V_FREE_MIN, v_free_min,
98	CTLFLAG_RW, &cnt.v_free_min, 0, "");
99SYSCTL_INT(_vm, VM_V_FREE_TARGET, v_free_target,
100	CTLFLAG_RW, &cnt.v_free_target, 0, "");
101SYSCTL_INT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
102	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
103SYSCTL_INT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
104	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
105SYSCTL_INT(_vm, VM_V_CACHE_MIN, v_cache_min,
106	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
107SYSCTL_INT(_vm, VM_V_CACHE_MAX, v_cache_max,
108	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
109SYSCTL_INT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
110	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
111
112SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD, &averunnable, loadavg, "");
113
114static int
115vmtotal SYSCTL_HANDLER_ARGS
116{
117	struct proc *p;
118	struct vmtotal total, *totalp;
119	vm_map_entry_t entry;
120	vm_object_t object;
121	vm_map_t map;
122	int paging;
123
124	totalp = &total;
125	bzero(totalp, sizeof *totalp);
126	/*
127	 * Mark all objects as inactive.
128	 */
129	for (object = vm_object_list.tqh_first;
130	    object != NULL;
131	    object = object->object_list.tqe_next)
132		object->flags &= ~OBJ_ACTIVE;
133	/*
134	 * Calculate process statistics.
135	 */
136	for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
137		if (p->p_flag & P_SYSTEM)
138			continue;
139		switch (p->p_stat) {
140		case 0:
141			continue;
142
143		case SSLEEP:
144		case SSTOP:
145			if (p->p_flag & P_INMEM) {
146				if (p->p_priority <= PZERO)
147					totalp->t_dw++;
148				else if (p->p_slptime < maxslp)
149					totalp->t_sl++;
150			} else if (p->p_slptime < maxslp)
151				totalp->t_sw++;
152			if (p->p_slptime >= maxslp)
153				continue;
154			break;
155
156		case SRUN:
157		case SIDL:
158			if (p->p_flag & P_INMEM)
159				totalp->t_rq++;
160			else
161				totalp->t_sw++;
162			if (p->p_stat == SIDL)
163				continue;
164			break;
165		}
166		/*
167		 * Note active objects.
168		 */
169		paging = 0;
170		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
171		    entry != &map->header; entry = entry->next) {
172			if (entry->is_a_map || entry->is_sub_map ||
173			    entry->object.vm_object == NULL)
174				continue;
175			entry->object.vm_object->flags |= OBJ_ACTIVE;
176			paging |= entry->object.vm_object->paging_in_progress;
177		}
178		if (paging)
179			totalp->t_pw++;
180	}
181	/*
182	 * Calculate object memory usage statistics.
183	 */
184	for (object = vm_object_list.tqh_first;
185	    object != NULL;
186	    object = object->object_list.tqe_next) {
187		totalp->t_vm += num_pages(object->size);
188		totalp->t_rm += object->resident_page_count;
189		if (object->flags & OBJ_ACTIVE) {
190			totalp->t_avm += num_pages(object->size);
191			totalp->t_arm += object->resident_page_count;
192		}
193		if (object->ref_count > 1) {
194			/* shared object */
195			totalp->t_vmshr += num_pages(object->size);
196			totalp->t_rmshr += object->resident_page_count;
197			if (object->flags & OBJ_ACTIVE) {
198				totalp->t_avmshr += num_pages(object->size);
199				totalp->t_armshr += object->resident_page_count;
200			}
201		}
202	}
203	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
204	return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
205}
206
207SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
208	0, sizeof(struct vmtotal), vmtotal, "S,vmtotal", "");
209