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