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