vm_meter.c revision 9507
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.6 1995/01/10 07:32:47 davidg 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;	/* load average, of runnable procs */
45
46int maxslp = MAXSLP;
47
48/*
49 * Constants for averages over 1, 5, and 15 minutes
50 * when sampling at 5 second intervals.
51 */
52fixpt_t cexp[3] = {
53	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
54	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
55	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
56};
57
58/*
59 * Compute a tenex style load average of a quantity on
60 * 1, 5 and 15 minute intervals.
61 */
62static void
63loadav(avg)
64	register struct loadavg *avg;
65{
66	register int i, nrun;
67	register struct proc *p;
68
69	for (nrun = 0, p = (struct proc *) allproc; p != NULL; p = p->p_next) {
70		switch (p->p_stat) {
71		case SSLEEP:
72			if (p->p_priority > PZERO || p->p_slptime != 0)
73				continue;
74			/* fall through */
75		case SRUN:
76		case SIDL:
77			nrun++;
78		}
79	}
80	for (i = 0; i < 3; i++)
81		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
82		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
83}
84
85void
86vmmeter()
87{
88
89	if (time.tv_sec % 5 == 0)
90		loadav(&averunnable);
91	if (proc0.p_slptime > maxslp / 2)
92		wakeup(&proc0);
93}
94
95/*
96 * Attributes associated with virtual memory.
97 */
98int
99vm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
100	int *name;
101	u_int namelen;
102	void *oldp;
103	size_t *oldlenp;
104	void *newp;
105	size_t newlen;
106	struct proc *p;
107{
108	struct vmtotal vmtotals;
109
110	/* all sysctl names at this level are terminal */
111	if (namelen != 1)
112		return (ENOTDIR);	/* overloaded */
113
114	switch (name[0]) {
115	case VM_LOADAVG:
116		averunnable.fscale = FSCALE;
117		return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
118			sizeof(averunnable)));
119	case VM_METER:
120		vmtotal(&vmtotals);
121		return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
122			sizeof(vmtotals)));
123	case VM_V_FREE_MIN:
124		return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_free_min));
125	case VM_V_FREE_TARGET:
126		return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_free_target));
127	case VM_V_FREE_RESERVED:
128		return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_free_reserved));
129	case VM_V_INACTIVE_TARGET:
130		return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_inactive_target));
131	case VM_V_CACHE_MIN:
132		return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_cache_min));
133	case VM_V_CACHE_MAX:
134		return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_cache_max));
135	case VM_V_PAGEOUT_FREE_MIN:
136		return (sysctl_int(oldp, oldlenp, newp, newlen, &cnt.v_pageout_free_min));
137
138	default:
139		return (EOPNOTSUPP);
140	}
141	/* NOTREACHED */
142}
143
144/*
145 * Calculate the current state of the system.
146 * Done on demand from getkerninfo().
147 */
148void
149vmtotal(totalp)
150	register struct vmtotal *totalp;
151{
152	register struct proc *p;
153	register vm_map_entry_t entry;
154	register vm_object_t object;
155	register vm_map_t map;
156	int paging;
157
158	bzero(totalp, sizeof *totalp);
159	/*
160	 * Mark all objects as inactive.
161	 */
162	for (object = vm_object_list.tqh_first;
163	    object != NULL;
164	    object = object->object_list.tqe_next)
165		object->flags &= ~OBJ_ACTIVE;
166	/*
167	 * Calculate process statistics.
168	 */
169	for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
170		if (p->p_flag & P_SYSTEM)
171			continue;
172		switch (p->p_stat) {
173		case 0:
174			continue;
175
176		case SSLEEP:
177		case SSTOP:
178			if (p->p_flag & P_INMEM) {
179				if (p->p_priority <= PZERO)
180					totalp->t_dw++;
181				else if (p->p_slptime < maxslp)
182					totalp->t_sl++;
183			} else if (p->p_slptime < maxslp)
184				totalp->t_sw++;
185			if (p->p_slptime >= maxslp)
186				continue;
187			break;
188
189		case SRUN:
190		case SIDL:
191			if (p->p_flag & P_INMEM)
192				totalp->t_rq++;
193			else
194				totalp->t_sw++;
195			if (p->p_stat == SIDL)
196				continue;
197			break;
198		}
199		/*
200		 * Note active objects.
201		 */
202		paging = 0;
203		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
204		    entry != &map->header; entry = entry->next) {
205			if (entry->is_a_map || entry->is_sub_map ||
206			    entry->object.vm_object == NULL)
207				continue;
208			entry->object.vm_object->flags |= OBJ_ACTIVE;
209			paging |= entry->object.vm_object->paging_in_progress;
210		}
211		if (paging)
212			totalp->t_pw++;
213	}
214	/*
215	 * Calculate object memory usage statistics.
216	 */
217	for (object = vm_object_list.tqh_first;
218	    object != NULL;
219	    object = object->object_list.tqe_next) {
220		totalp->t_vm += num_pages(object->size);
221		totalp->t_rm += object->resident_page_count;
222		if (object->flags & OBJ_ACTIVE) {
223			totalp->t_avm += num_pages(object->size);
224			totalp->t_arm += object->resident_page_count;
225		}
226		if (object->ref_count > 1) {
227			/* shared object */
228			totalp->t_vmshr += num_pages(object->size);
229			totalp->t_rmshr += object->resident_page_count;
230			if (object->flags & OBJ_ACTIVE) {
231				totalp->t_avmshr += num_pages(object->size);
232				totalp->t_armshr += object->resident_page_count;
233			}
234		}
235	}
236	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
237}
238