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