vm_meter.c revision 74670
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 74670 2001-03-23 03:45:17Z tmm $
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	ALLPROC_LOCK(AP_SHARED);
82	for (nrun = 0, p = LIST_FIRST(&allproc); p != 0; p = LIST_NEXT(p, p_list)) {
83		switch (p->p_stat) {
84		case SSLEEP:
85			if (p->p_pri.pri_level > PZERO ||
86			    p->p_slptime != 0)
87				continue;
88			/* FALLTHROUGH */
89		case SRUN:
90			if ((p->p_flag & P_NOLOAD) != 0)
91				continue;
92			/* FALLTHROUGH */
93		case SIDL:
94			nrun++;
95		}
96	}
97	ALLPROC_LOCK(AP_RELEASE);
98	for (i = 0; i < 3; i++)
99		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
100		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
101}
102
103void
104vmmeter()
105{
106
107	if (time_second % 5 == 0)
108		loadav(&averunnable);
109	if (proc0.p_slptime > maxslp / 2)
110		wakeup(&proc0);
111}
112
113SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
114	CTLFLAG_RW, &cnt.v_free_min, 0, "");
115SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
116	CTLFLAG_RW, &cnt.v_free_target, 0, "");
117SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
118	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
119SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
120	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
121SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
122	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
123SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
124	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
125SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
126	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
127SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
128	CTLFLAG_RW, &cnt.v_free_severe, 0, "");
129
130SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD,
131    &averunnable, loadavg, "Machine loadaverage history");
132
133static int
134vmtotal(SYSCTL_HANDLER_ARGS)
135{
136	struct proc *p;
137	struct vmtotal total, *totalp;
138	vm_map_entry_t entry;
139	vm_object_t object;
140	vm_map_t map;
141	int paging;
142
143	totalp = &total;
144	bzero(totalp, sizeof *totalp);
145	/*
146	 * Mark all objects as inactive.
147	 */
148	TAILQ_FOREACH(object, &vm_object_list, object_list)
149		vm_object_clear_flag(object, OBJ_ACTIVE);
150	/*
151	 * Calculate process statistics.
152	 */
153	ALLPROC_LOCK(AP_SHARED);
154	LIST_FOREACH(p, &allproc, p_list) {
155		if (p->p_flag & P_SYSTEM)
156			continue;
157		mtx_lock_spin(&sched_lock);
158		switch (p->p_stat) {
159		case 0:
160			mtx_unlock_spin(&sched_lock);
161			continue;
162
163		case SMTX:
164		case SSLEEP:
165		case SSTOP:
166			if (p->p_sflag & PS_INMEM) {
167				if (p->p_pri.pri_level <= PZERO)
168					totalp->t_dw++;
169				else if (p->p_slptime < maxslp)
170					totalp->t_sl++;
171			} else if (p->p_slptime < maxslp)
172				totalp->t_sw++;
173			if (p->p_slptime >= maxslp) {
174				mtx_unlock_spin(&sched_lock);
175				continue;
176			}
177			break;
178
179		case SWAIT:
180			totalp->t_sl++;
181			continue;
182
183		case SRUN:
184		case SIDL:
185			if (p->p_sflag & PS_INMEM)
186				totalp->t_rq++;
187			else
188				totalp->t_sw++;
189			if (p->p_stat == SIDL) {
190				mtx_unlock_spin(&sched_lock);
191				continue;
192			}
193			break;
194		}
195		mtx_unlock_spin(&sched_lock);
196		/*
197		 * Note active objects.
198		 */
199		paging = 0;
200		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
201		    entry != &map->header; entry = entry->next) {
202			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
203			    entry->object.vm_object == NULL)
204				continue;
205			vm_object_set_flag(entry->object.vm_object, OBJ_ACTIVE);
206			paging |= entry->object.vm_object->paging_in_progress;
207		}
208		if (paging)
209			totalp->t_pw++;
210	}
211	ALLPROC_LOCK(AP_RELEASE);
212	/*
213	 * Calculate object memory usage statistics.
214	 */
215	for (object = TAILQ_FIRST(&vm_object_list);
216	    object != NULL;
217	    object = TAILQ_NEXT(object, object_list)) {
218		/*
219		 * devices, like /dev/mem, will badly skew our totals
220		 */
221		if (object->type == OBJT_DEVICE)
222			continue;
223		totalp->t_vm += object->size;
224		totalp->t_rm += object->resident_page_count;
225		if (object->flags & OBJ_ACTIVE) {
226			totalp->t_avm += object->size;
227			totalp->t_arm += object->resident_page_count;
228		}
229		if (object->shadow_count > 1) {
230			/* shared object */
231			totalp->t_vmshr += object->size;
232			totalp->t_rmshr += object->resident_page_count;
233			if (object->flags & OBJ_ACTIVE) {
234				totalp->t_avmshr += object->size;
235				totalp->t_armshr += object->resident_page_count;
236			}
237		}
238	}
239	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
240	return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
241}
242
243SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
244    0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
245    "System virtual memory statistics");
246SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
247SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, "VM meter sys stats");
248SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, "VM meter vm stats");
249SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
250SYSCTL_UINT(_vm_stats_sys, OID_AUTO,
251	v_swtch, CTLFLAG_RD, &cnt.v_swtch, 0, "Context switches");
252SYSCTL_UINT(_vm_stats_sys, OID_AUTO,
253	v_trap, CTLFLAG_RD, &cnt.v_trap, 0, "Traps");
254SYSCTL_UINT(_vm_stats_sys, OID_AUTO,
255	v_syscall, CTLFLAG_RD, &cnt.v_syscall, 0, "Syscalls");
256SYSCTL_UINT(_vm_stats_sys, OID_AUTO, v_intr, CTLFLAG_RD,
257    &cnt.v_intr, 0, "Hardware interrupts");
258SYSCTL_UINT(_vm_stats_sys, OID_AUTO, v_soft, CTLFLAG_RD,
259    &cnt.v_soft, 0, "Software interrupts");
260SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
261	v_vm_faults, CTLFLAG_RD, &cnt.v_vm_faults, 0, "VM faults");
262SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
263	v_cow_faults, CTLFLAG_RD, &cnt.v_cow_faults, 0, "COW faults");
264SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
265	v_cow_optim, CTLFLAG_RD, &cnt.v_cow_optim, 0, "Optimized COW faults");
266SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
267	v_zfod, CTLFLAG_RD, &cnt.v_zfod, 0, "Zero fill");
268SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
269	v_ozfod, CTLFLAG_RD, &cnt.v_ozfod, 0, "Optimized zero fill");
270SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
271	v_swapin, CTLFLAG_RD, &cnt.v_swapin, 0, "Swapin operations");
272SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
273	v_swapout, CTLFLAG_RD, &cnt.v_swapout, 0, "Swapout operations");
274SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
275	v_swappgsin, CTLFLAG_RD, &cnt.v_swappgsin, 0, "Swapin pages");
276SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
277	v_swappgsout, CTLFLAG_RD, &cnt.v_swappgsout, 0, "Swapout pages");
278SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
279	v_vnodein, CTLFLAG_RD, &cnt.v_vnodein, 0, "Vnodein operations");
280SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
281	v_vnodeout, CTLFLAG_RD, &cnt.v_vnodeout, 0, "Vnodeout operations");
282SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
283	v_vnodepgsin, CTLFLAG_RD, &cnt.v_vnodepgsin, 0, "Vnodein pages");
284SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
285	v_vnodepgsout, CTLFLAG_RD, &cnt.v_vnodepgsout, 0, "Vnodeout pages");
286SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
287	v_intrans, CTLFLAG_RD, &cnt.v_intrans, 0, "In transit page blocking");
288SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
289	v_reactivated, CTLFLAG_RD, &cnt.v_reactivated, 0, "Reactivated pages");
290SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
291	v_pdwakeups, CTLFLAG_RD, &cnt.v_pdwakeups, 0, "Pagedaemon wakeups");
292SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
293	v_pdpages, CTLFLAG_RD, &cnt.v_pdpages, 0, "Pagedaemon page scans");
294SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
295	v_dfree, CTLFLAG_RD, &cnt.v_dfree, 0, "");
296SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
297	v_pfree, CTLFLAG_RD, &cnt.v_pfree, 0, "");
298SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
299	v_tfree, CTLFLAG_RD, &cnt.v_tfree, 0, "");
300SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
301	v_page_size, CTLFLAG_RD, &cnt.v_page_size, 0, "");
302SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
303	v_page_count, CTLFLAG_RD, &cnt.v_page_count, 0, "");
304SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
305	v_free_reserved, CTLFLAG_RD, &cnt.v_free_reserved, 0, "");
306SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
307	v_free_target, CTLFLAG_RD, &cnt.v_free_target, 0, "");
308SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
309	v_free_min, CTLFLAG_RD, &cnt.v_free_min, 0, "");
310SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
311	v_free_count, CTLFLAG_RD, &cnt.v_free_count, 0, "");
312SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
313	v_wire_count, CTLFLAG_RD, &cnt.v_wire_count, 0, "");
314SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
315	v_active_count, CTLFLAG_RD, &cnt.v_active_count, 0, "");
316SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
317	v_inactive_target, CTLFLAG_RD, &cnt.v_inactive_target, 0, "");
318SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
319	v_inactive_count, CTLFLAG_RD, &cnt.v_inactive_count, 0, "");
320SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
321	v_cache_count, CTLFLAG_RD, &cnt.v_cache_count, 0, "");
322SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
323	v_cache_min, CTLFLAG_RD, &cnt.v_cache_min, 0, "");
324SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
325	v_cache_max, CTLFLAG_RD, &cnt.v_cache_max, 0, "");
326SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
327	v_pageout_free_min, CTLFLAG_RD, &cnt.v_pageout_free_min, 0, "");
328SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
329	v_interrupt_free_min, CTLFLAG_RD, &cnt.v_interrupt_free_min, 0, "");
330SYSCTL_INT(_vm_stats_misc, OID_AUTO,
331	zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0, "");
332SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
333	v_forks, CTLFLAG_RD, &cnt.v_forks, 0, "Number of fork() calls");
334SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
335	v_vforks, CTLFLAG_RD, &cnt.v_vforks, 0, "Number of vfork() calls");
336SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
337	v_rforks, CTLFLAG_RD, &cnt.v_rforks, 0, "Number of rfork() calls");
338SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
339	v_kthreads, CTLFLAG_RD, &cnt.v_kthreads, 0, "Number of fork() calls by kernel");
340SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
341	v_forkpages, CTLFLAG_RD, &cnt.v_forkpages, 0, "VM pages affected by fork()");
342SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
343	v_vforkpages, CTLFLAG_RD, &cnt.v_vforkpages, 0, "VM pages affected by vfork()");
344SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
345	v_rforkpages, CTLFLAG_RD, &cnt.v_rforkpages, 0, "VM pages affected by rfork()");
346SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
347	v_kthreadpages, CTLFLAG_RD, &cnt.v_kthreadpages, 0, "VM pages affected by fork() by kernel");
348#if 0
349SYSCTL_INT(_vm_stats_misc, OID_AUTO,
350	page_mask, CTLFLAG_RD, &page_mask, 0, "");
351SYSCTL_INT(_vm_stats_misc, OID_AUTO,
352	page_shift, CTLFLAG_RD, &page_shift, 0, "");
353SYSCTL_INT(_vm_stats_misc, OID_AUTO,
354	first_page, CTLFLAG_RD, &first_page, 0, "");
355SYSCTL_INT(_vm_stats_misc, OID_AUTO,
356	last_page, CTLFLAG_RD, &last_page, 0, "");
357SYSCTL_INT(_vm_stats_misc, OID_AUTO,
358	vm_page_bucket_count, CTLFLAG_RD, &vm_page_bucket_count, 0, "");
359SYSCTL_INT(_vm_stats_misc, OID_AUTO,
360	vm_page_hash_mask, CTLFLAG_RD, &vm_page_hash_mask, 0, "");
361#endif
362
363/*
364 * Further sysctls used by systat: hw.nintr, hw.intrnames, hw.intrcnt.
365 * This does probably not really fit in here, but it is somehow connected.
366 * The definitions for this are machdep, but are currently defined for
367 * any architecture.
368 */
369
370/* include the machdep stuff */
371#include <machine/intrcnt.h>
372
373int nintr = INTRCNT_COUNT;
374SYSCTL_INT(_hw, OID_AUTO, nintr, CTLFLAG_RD, &nintr, 0, "Number of Interrupts");
375
376SYSCTL_OPAQUE(_hw, OID_AUTO, intrcnt, CTLFLAG_RD, &intrcnt,
377   sizeof(long) * INTRCNT_COUNT, "", "Interrupt Counts");
378
379/*
380 * We do not know the length in advance (in an MI fashion), so calculate things
381 * at run-time.
382 */
383static int
384sysctl_intrnames(SYSCTL_HANDLER_ARGS)
385{
386	return sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
387	   req);
388}
389
390SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
391    sysctl_intrnames, "", "Interrupt Names");
392