vm_meter.c revision 65557
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 * $FreeBSD: head/sys/vm/vm_meter.c 65557 2000-09-07 01:33:02Z jasone $
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_page.h>
46#include <vm/vm_extern.h>
47#include <vm/vm_param.h>
48#include <sys/lock.h>
49#include <vm/pmap.h>
50#include <vm/vm_map.h>
51#include <vm/vm_object.h>
52#include <sys/sysctl.h>
53
54struct loadavg averunnable =
55	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
56
57struct vmmeter cnt;
58
59static int maxslp = MAXSLP;
60
61/*
62 * Constants for averages over 1, 5, and 15 minutes
63 * when sampling at 5 second intervals.
64 */
65static fixpt_t cexp[3] = {
66	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
67	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
68	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
69};
70
71/*
72 * Compute a tenex style load average of a quantity on
73 * 1, 5 and 15 minute intervals.
74 */
75static void
76loadav(struct loadavg *avg)
77{
78	register int i, nrun;
79	register struct proc *p;
80
81	for (nrun = 0, p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
82		switch (p->p_stat) {
83		case SSLEEP:
84			if (p->p_priority > PZERO || p->p_slptime != 0)
85				continue;
86			/* FALLTHROUGH */
87		case SRUN:
88		case SIDL:
89			nrun++;
90		}
91	}
92	for (i = 0; i < 3; i++)
93		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
94		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
95}
96
97void
98vmmeter()
99{
100
101	if (time_second % 5 == 0)
102		loadav(&averunnable);
103	if (proc0.p_slptime > maxslp / 2)
104		wakeup(&proc0);
105}
106
107SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
108	CTLFLAG_RW, &cnt.v_free_min, 0, "");
109SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
110	CTLFLAG_RW, &cnt.v_free_target, 0, "");
111SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
112	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
113SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
114	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
115SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
116	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
117SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
118	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
119SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
120	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
121SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
122	CTLFLAG_RW, &cnt.v_free_severe, 0, "");
123
124SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD,
125    &averunnable, loadavg, "Machine loadaverage history");
126
127static int
128vmtotal(SYSCTL_HANDLER_ARGS)
129{
130	struct proc *p;
131	struct vmtotal total, *totalp;
132	vm_map_entry_t entry;
133	vm_object_t object;
134	vm_map_t map;
135	int paging;
136
137	totalp = &total;
138	bzero(totalp, sizeof *totalp);
139	/*
140	 * Mark all objects as inactive.
141	 */
142	for (object = TAILQ_FIRST(&vm_object_list);
143	    object != NULL;
144	    object = TAILQ_NEXT(object,object_list))
145		vm_object_clear_flag(object, OBJ_ACTIVE);
146	/*
147	 * Calculate process statistics.
148	 */
149	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
150		if (p->p_flag & P_SYSTEM)
151			continue;
152		switch (p->p_stat) {
153		case 0:
154			continue;
155
156		case SMTX:
157		case SSLEEP:
158		case SSTOP:
159			if (p->p_flag & P_INMEM) {
160				if (p->p_priority <= PZERO)
161					totalp->t_dw++;
162				else if (p->p_slptime < maxslp)
163					totalp->t_sl++;
164			} else if (p->p_slptime < maxslp)
165				totalp->t_sw++;
166			if (p->p_slptime >= maxslp)
167				continue;
168			break;
169
170		case SWAIT:
171			totalp->t_sl++;
172			continue;
173
174		case SRUN:
175		case SIDL:
176			if (p->p_flag & P_INMEM)
177				totalp->t_rq++;
178			else
179				totalp->t_sw++;
180			if (p->p_stat == SIDL)
181				continue;
182			break;
183		}
184		/*
185		 * Note active objects.
186		 */
187		paging = 0;
188		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
189		    entry != &map->header; entry = entry->next) {
190			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
191			    entry->object.vm_object == NULL)
192				continue;
193			vm_object_set_flag(entry->object.vm_object, OBJ_ACTIVE);
194			paging |= entry->object.vm_object->paging_in_progress;
195		}
196		if (paging)
197			totalp->t_pw++;
198	}
199	/*
200	 * Calculate object memory usage statistics.
201	 */
202	for (object = TAILQ_FIRST(&vm_object_list);
203	    object != NULL;
204	    object = TAILQ_NEXT(object, object_list)) {
205		/*
206		 * devices, like /dev/mem, will badly skew our totals
207		 */
208		if (object->type == OBJT_DEVICE)
209			continue;
210		totalp->t_vm += object->size;
211		totalp->t_rm += object->resident_page_count;
212		if (object->flags & OBJ_ACTIVE) {
213			totalp->t_avm += object->size;
214			totalp->t_arm += object->resident_page_count;
215		}
216		if (object->shadow_count > 1) {
217			/* shared object */
218			totalp->t_vmshr += object->size;
219			totalp->t_rmshr += object->resident_page_count;
220			if (object->flags & OBJ_ACTIVE) {
221				totalp->t_avmshr += object->size;
222				totalp->t_armshr += object->resident_page_count;
223			}
224		}
225	}
226	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
227	return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
228}
229
230SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
231    0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
232    "System virtual memory statistics");
233SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
234SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, "VM meter sys stats");
235SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, "VM meter vm stats");
236SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
237SYSCTL_UINT(_vm_stats_sys, OID_AUTO,
238	v_swtch, CTLFLAG_RD, &cnt.v_swtch, 0, "Context switches");
239SYSCTL_UINT(_vm_stats_sys, OID_AUTO,
240	v_trap, CTLFLAG_RD, &cnt.v_trap, 0, "Traps");
241SYSCTL_UINT(_vm_stats_sys, OID_AUTO,
242	v_syscall, CTLFLAG_RD, &cnt.v_syscall, 0, "Syscalls");
243SYSCTL_UINT(_vm_stats_sys, OID_AUTO, v_intr, CTLFLAG_RD,
244    &cnt.v_intr, 0, "Hardware interrupts");
245SYSCTL_UINT(_vm_stats_sys, OID_AUTO, v_soft, CTLFLAG_RD,
246    &cnt.v_soft, 0, "Software interrupts");
247SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
248	v_vm_faults, CTLFLAG_RD, &cnt.v_vm_faults, 0, "VM faults");
249SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
250	v_cow_faults, CTLFLAG_RD, &cnt.v_cow_faults, 0, "COW faults");
251SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
252	v_cow_optim, CTLFLAG_RD, &cnt.v_cow_optim, 0, "Optimized COW faults");
253SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
254	v_zfod, CTLFLAG_RD, &cnt.v_zfod, 0, "Zero fill");
255SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
256	v_ozfod, CTLFLAG_RD, &cnt.v_ozfod, 0, "Optimized zero fill");
257SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
258	v_swapin, CTLFLAG_RD, &cnt.v_swapin, 0, "Swapin operations");
259SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
260	v_swapout, CTLFLAG_RD, &cnt.v_swapout, 0, "Swapout operations");
261SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
262	v_swappgsin, CTLFLAG_RD, &cnt.v_swappgsin, 0, "Swapin pages");
263SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
264	v_swappgsout, CTLFLAG_RD, &cnt.v_swappgsout, 0, "Swapout pages");
265SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
266	v_vnodein, CTLFLAG_RD, &cnt.v_vnodein, 0, "Vnodein operations");
267SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
268	v_vnodeout, CTLFLAG_RD, &cnt.v_vnodeout, 0, "Vnodeout operations");
269SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
270	v_vnodepgsin, CTLFLAG_RD, &cnt.v_vnodepgsin, 0, "Vnodein pages");
271SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
272	v_vnodepgsout, CTLFLAG_RD, &cnt.v_vnodepgsout, 0, "Vnodeout pages");
273SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
274	v_intrans, CTLFLAG_RD, &cnt.v_intrans, 0, "In transit page blocking");
275SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
276	v_reactivated, CTLFLAG_RD, &cnt.v_reactivated, 0, "Reactivated pages");
277SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
278	v_pdwakeups, CTLFLAG_RD, &cnt.v_pdwakeups, 0, "Pagedaemon wakeups");
279SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
280	v_pdpages, CTLFLAG_RD, &cnt.v_pdpages, 0, "Pagedaemon page scans");
281SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
282	v_dfree, CTLFLAG_RD, &cnt.v_dfree, 0, "");
283SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
284	v_pfree, CTLFLAG_RD, &cnt.v_pfree, 0, "");
285SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
286	v_tfree, CTLFLAG_RD, &cnt.v_tfree, 0, "");
287SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
288	v_page_size, CTLFLAG_RD, &cnt.v_page_size, 0, "");
289SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
290	v_page_count, CTLFLAG_RD, &cnt.v_page_count, 0, "");
291SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
292	v_free_reserved, CTLFLAG_RD, &cnt.v_free_reserved, 0, "");
293SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
294	v_free_target, CTLFLAG_RD, &cnt.v_free_target, 0, "");
295SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
296	v_free_min, CTLFLAG_RD, &cnt.v_free_min, 0, "");
297SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
298	v_free_count, CTLFLAG_RD, &cnt.v_free_count, 0, "");
299SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
300	v_wire_count, CTLFLAG_RD, &cnt.v_wire_count, 0, "");
301SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
302	v_active_count, CTLFLAG_RD, &cnt.v_active_count, 0, "");
303SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
304	v_inactive_target, CTLFLAG_RD, &cnt.v_inactive_target, 0, "");
305SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
306	v_inactive_count, CTLFLAG_RD, &cnt.v_inactive_count, 0, "");
307SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
308	v_cache_count, CTLFLAG_RD, &cnt.v_cache_count, 0, "");
309SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
310	v_cache_min, CTLFLAG_RD, &cnt.v_cache_min, 0, "");
311SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
312	v_cache_max, CTLFLAG_RD, &cnt.v_cache_max, 0, "");
313SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
314	v_pageout_free_min, CTLFLAG_RD, &cnt.v_pageout_free_min, 0, "");
315SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
316	v_interrupt_free_min, CTLFLAG_RD, &cnt.v_interrupt_free_min, 0, "");
317SYSCTL_INT(_vm_stats_misc, OID_AUTO,
318	zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0, "");
319#if 0
320SYSCTL_INT(_vm_stats_misc, OID_AUTO,
321	page_mask, CTLFLAG_RD, &page_mask, 0, "");
322SYSCTL_INT(_vm_stats_misc, OID_AUTO,
323	page_shift, CTLFLAG_RD, &page_shift, 0, "");
324SYSCTL_INT(_vm_stats_misc, OID_AUTO,
325	first_page, CTLFLAG_RD, &first_page, 0, "");
326SYSCTL_INT(_vm_stats_misc, OID_AUTO,
327	last_page, CTLFLAG_RD, &last_page, 0, "");
328SYSCTL_INT(_vm_stats_misc, OID_AUTO,
329	vm_page_bucket_count, CTLFLAG_RD, &vm_page_bucket_count, 0, "");
330SYSCTL_INT(_vm_stats_misc, OID_AUTO,
331	vm_page_hash_mask, CTLFLAG_RD, &vm_page_hash_mask, 0, "");
332#endif
333