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