xref: /freebsd/sys/vm/vm_meter.c (revision 41059135ce931c0f1014a999ffabc6bc470ce856)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)vm_meter.c	8.4 (Berkeley) 1/4/94
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_compat.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/proc.h>
44 #include <sys/resource.h>
45 #include <sys/rwlock.h>
46 #include <sys/sx.h>
47 #include <sys/vmmeter.h>
48 #include <sys/smp.h>
49 
50 #include <vm/vm.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_param.h>
54 #include <vm/pmap.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_object.h>
57 #include <sys/sysctl.h>
58 
59 struct vmmeter vm_cnt = {
60 	.v_swtch = EARLY_COUNTER,
61 	.v_trap = EARLY_COUNTER,
62 	.v_syscall = EARLY_COUNTER,
63 	.v_intr = EARLY_COUNTER,
64 	.v_soft = EARLY_COUNTER,
65 	.v_vm_faults = EARLY_COUNTER,
66 	.v_io_faults = EARLY_COUNTER,
67 	.v_cow_faults = EARLY_COUNTER,
68 	.v_cow_optim = EARLY_COUNTER,
69 	.v_zfod = EARLY_COUNTER,
70 	.v_ozfod = EARLY_COUNTER,
71 	.v_swapin = EARLY_COUNTER,
72 	.v_swapout = EARLY_COUNTER,
73 	.v_swappgsin = EARLY_COUNTER,
74 	.v_swappgsout = EARLY_COUNTER,
75 	.v_vnodein = EARLY_COUNTER,
76 	.v_vnodeout = EARLY_COUNTER,
77 	.v_vnodepgsin = EARLY_COUNTER,
78 	.v_vnodepgsout = EARLY_COUNTER,
79 	.v_intrans = EARLY_COUNTER,
80 	.v_reactivated = EARLY_COUNTER,
81 	.v_pdwakeups = EARLY_COUNTER,
82 	.v_pdpages = EARLY_COUNTER,
83 	.v_pdshortfalls = EARLY_COUNTER,
84 	.v_dfree = EARLY_COUNTER,
85 	.v_pfree = EARLY_COUNTER,
86 	.v_tfree = EARLY_COUNTER,
87 	.v_forks = EARLY_COUNTER,
88 	.v_vforks = EARLY_COUNTER,
89 	.v_rforks = EARLY_COUNTER,
90 	.v_kthreads = EARLY_COUNTER,
91 	.v_forkpages = EARLY_COUNTER,
92 	.v_vforkpages = EARLY_COUNTER,
93 	.v_rforkpages = EARLY_COUNTER,
94 	.v_kthreadpages = EARLY_COUNTER,
95 };
96 
97 static void
98 vmcounter_startup(void)
99 {
100 	counter_u64_t *cnt = (counter_u64_t *)&vm_cnt;
101 
102 	COUNTER_ARRAY_ALLOC(cnt, VM_METER_NCOUNTERS, M_WAITOK);
103 }
104 SYSINIT(counter, SI_SUB_CPU, SI_ORDER_FOURTH + 1, vmcounter_startup, NULL);
105 
106 SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
107 	CTLFLAG_RW, &vm_cnt.v_free_min, 0, "Minimum low-free-pages threshold");
108 SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
109 	CTLFLAG_RW, &vm_cnt.v_free_target, 0, "Desired free pages");
110 SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
111 	CTLFLAG_RW, &vm_cnt.v_free_reserved, 0, "Pages reserved for deadlock");
112 SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
113 	CTLFLAG_RW, &vm_cnt.v_inactive_target, 0, "Pages desired inactive");
114 SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
115 	CTLFLAG_RW, &vm_cnt.v_pageout_free_min, 0, "Min pages reserved for kernel");
116 SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
117 	CTLFLAG_RW, &vm_cnt.v_free_severe, 0, "Severe page depletion point");
118 
119 static int
120 sysctl_vm_loadavg(SYSCTL_HANDLER_ARGS)
121 {
122 
123 #ifdef SCTL_MASK32
124 	u_int32_t la[4];
125 
126 	if (req->flags & SCTL_MASK32) {
127 		la[0] = averunnable.ldavg[0];
128 		la[1] = averunnable.ldavg[1];
129 		la[2] = averunnable.ldavg[2];
130 		la[3] = averunnable.fscale;
131 		return SYSCTL_OUT(req, la, sizeof(la));
132 	} else
133 #endif
134 		return SYSCTL_OUT(req, &averunnable, sizeof(averunnable));
135 }
136 SYSCTL_PROC(_vm, VM_LOADAVG, loadavg, CTLTYPE_STRUCT | CTLFLAG_RD |
137     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_loadavg, "S,loadavg",
138     "Machine loadaverage history");
139 
140 /*
141  * This function aims to determine if the object is mapped,
142  * specifically, if it is referenced by a vm_map_entry.  Because
143  * objects occasionally acquire transient references that do not
144  * represent a mapping, the method used here is inexact.  However, it
145  * has very low overhead and is good enough for the advisory
146  * vm.vmtotal sysctl.
147  */
148 static bool
149 is_object_active(vm_object_t obj)
150 {
151 
152 	return (obj->ref_count > obj->shadow_count);
153 }
154 
155 static int
156 vmtotal(SYSCTL_HANDLER_ARGS)
157 {
158 	struct vmtotal total;
159 	vm_object_t object;
160 	struct proc *p;
161 	struct thread *td;
162 
163 	bzero(&total, sizeof(total));
164 
165 	/*
166 	 * Calculate process statistics.
167 	 */
168 	sx_slock(&allproc_lock);
169 	FOREACH_PROC_IN_SYSTEM(p) {
170 		if ((p->p_flag & P_SYSTEM) != 0)
171 			continue;
172 		PROC_LOCK(p);
173 		if (p->p_state != PRS_NEW) {
174 			FOREACH_THREAD_IN_PROC(p, td) {
175 				thread_lock(td);
176 				switch (td->td_state) {
177 				case TDS_INHIBITED:
178 					if (TD_IS_SWAPPED(td))
179 						total.t_sw++;
180 					else if (TD_IS_SLEEPING(td)) {
181 						if (td->td_priority <= PZERO)
182 							total.t_dw++;
183 						else
184 							total.t_sl++;
185 						if (td->td_wchan ==
186 						    &vm_cnt.v_free_count)
187 							total.t_pw++;
188 					}
189 					break;
190 				case TDS_CAN_RUN:
191 					total.t_sw++;
192 					break;
193 				case TDS_RUNQ:
194 				case TDS_RUNNING:
195 					total.t_rq++;
196 					break;
197 				default:
198 					break;
199 				}
200 				thread_unlock(td);
201 			}
202 		}
203 		PROC_UNLOCK(p);
204 	}
205 	sx_sunlock(&allproc_lock);
206 	/*
207 	 * Calculate object memory usage statistics.
208 	 */
209 	mtx_lock(&vm_object_list_mtx);
210 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
211 		/*
212 		 * Perform unsynchronized reads on the object.  In
213 		 * this case, the lack of synchronization should not
214 		 * impair the accuracy of the reported statistics.
215 		 */
216 		if ((object->flags & OBJ_FICTITIOUS) != 0) {
217 			/*
218 			 * Devices, like /dev/mem, will badly skew our totals.
219 			 */
220 			continue;
221 		}
222 		if (object->ref_count == 0) {
223 			/*
224 			 * Also skip unreferenced objects, including
225 			 * vnodes representing mounted file systems.
226 			 */
227 			continue;
228 		}
229 		if (object->ref_count == 1 &&
230 		    (object->flags & OBJ_NOSPLIT) != 0) {
231 			/*
232 			 * Also skip otherwise unreferenced swap
233 			 * objects backing tmpfs vnodes, and POSIX or
234 			 * SysV shared memory.
235 			 */
236 			continue;
237 		}
238 		total.t_vm += object->size;
239 		total.t_rm += object->resident_page_count;
240 		if (is_object_active(object)) {
241 			total.t_avm += object->size;
242 			total.t_arm += object->resident_page_count;
243 		}
244 		if (object->shadow_count > 1) {
245 			/* shared object */
246 			total.t_vmshr += object->size;
247 			total.t_rmshr += object->resident_page_count;
248 			if (is_object_active(object)) {
249 				total.t_avmshr += object->size;
250 				total.t_armshr += object->resident_page_count;
251 			}
252 		}
253 	}
254 	mtx_unlock(&vm_object_list_mtx);
255 	total.t_free = vm_cnt.v_free_count;
256 	return (sysctl_handle_opaque(oidp, &total, sizeof(total), req));
257 }
258 
259 SYSCTL_PROC(_vm, VM_TOTAL, vmtotal, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
260     0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
261     "System virtual memory statistics");
262 SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
263 static SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0,
264 	"VM meter sys stats");
265 static SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0,
266 	"VM meter vm stats");
267 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
268 
269 static int
270 sysctl_handle_vmstat(SYSCTL_HANDLER_ARGS)
271 {
272 	uint64_t val;
273 #ifdef COMPAT_FREEBSD11
274 	uint32_t val32;
275 #endif
276 
277 	val = counter_u64_fetch(*(counter_u64_t *)arg1);
278 #ifdef COMPAT_FREEBSD11
279 	if (req->oldlen == sizeof(val32)) {
280 		val32 = val;		/* truncate */
281 		return (SYSCTL_OUT(req, &val32, sizeof(val32)));
282 	}
283 #endif
284 	return (SYSCTL_OUT(req, &val, sizeof(val)));
285 }
286 
287 #define	VM_STATS(parent, var, descr) \
288     SYSCTL_OID(parent, OID_AUTO, var, CTLTYPE_U64 | CTLFLAG_MPSAFE | \
289     CTLFLAG_RD, &vm_cnt.var, 0, sysctl_handle_vmstat, "QU", descr);
290 #define	VM_STATS_VM(var, descr)		VM_STATS(_vm_stats_vm, var, descr)
291 #define	VM_STATS_SYS(var, descr)	VM_STATS(_vm_stats_sys, var, descr)
292 
293 VM_STATS_SYS(v_swtch, "Context switches");
294 VM_STATS_SYS(v_trap, "Traps");
295 VM_STATS_SYS(v_syscall, "System calls");
296 VM_STATS_SYS(v_intr, "Device interrupts");
297 VM_STATS_SYS(v_soft, "Software interrupts");
298 VM_STATS_VM(v_vm_faults, "Address memory faults");
299 VM_STATS_VM(v_io_faults, "Page faults requiring I/O");
300 VM_STATS_VM(v_cow_faults, "Copy-on-write faults");
301 VM_STATS_VM(v_cow_optim, "Optimized COW faults");
302 VM_STATS_VM(v_zfod, "Pages zero-filled on demand");
303 VM_STATS_VM(v_ozfod, "Optimized zero fill pages");
304 VM_STATS_VM(v_swapin, "Swap pager pageins");
305 VM_STATS_VM(v_swapout, "Swap pager pageouts");
306 VM_STATS_VM(v_swappgsin, "Swap pages swapped in");
307 VM_STATS_VM(v_swappgsout, "Swap pages swapped out");
308 VM_STATS_VM(v_vnodein, "Vnode pager pageins");
309 VM_STATS_VM(v_vnodeout, "Vnode pager pageouts");
310 VM_STATS_VM(v_vnodepgsin, "Vnode pages paged in");
311 VM_STATS_VM(v_vnodepgsout, "Vnode pages paged out");
312 VM_STATS_VM(v_intrans, "In transit page faults");
313 VM_STATS_VM(v_reactivated, "Pages reactivated by pagedaemon");
314 VM_STATS_VM(v_pdwakeups, "Pagedaemon wakeups");
315 VM_STATS_VM(v_pdpages, "Pages analyzed by pagedaemon");
316 VM_STATS_VM(v_pdshortfalls, "Page reclamation shortfalls");
317 VM_STATS_VM(v_dfree, "Pages freed by pagedaemon");
318 VM_STATS_VM(v_pfree, "Pages freed by exiting processes");
319 VM_STATS_VM(v_tfree, "Total pages freed");
320 VM_STATS_VM(v_forks, "Number of fork() calls");
321 VM_STATS_VM(v_vforks, "Number of vfork() calls");
322 VM_STATS_VM(v_rforks, "Number of rfork() calls");
323 VM_STATS_VM(v_kthreads, "Number of fork() calls by kernel");
324 VM_STATS_VM(v_forkpages, "VM pages affected by fork()");
325 VM_STATS_VM(v_vforkpages, "VM pages affected by vfork()");
326 VM_STATS_VM(v_rforkpages, "VM pages affected by rfork()");
327 VM_STATS_VM(v_kthreadpages, "VM pages affected by fork() by kernel");
328 
329 #define	VM_STATS_UINT(var, descr)	\
330     SYSCTL_UINT(_vm_stats_vm, OID_AUTO, var, CTLFLAG_RD, &vm_cnt.var, 0, descr)
331 VM_STATS_UINT(v_page_size, "Page size in bytes");
332 VM_STATS_UINT(v_page_count, "Total number of pages in system");
333 VM_STATS_UINT(v_free_reserved, "Pages reserved for deadlock");
334 VM_STATS_UINT(v_free_target, "Pages desired free");
335 VM_STATS_UINT(v_free_min, "Minimum low-free-pages threshold");
336 VM_STATS_UINT(v_free_count, "Free pages");
337 VM_STATS_UINT(v_wire_count, "Wired pages");
338 VM_STATS_UINT(v_active_count, "Active pages");
339 VM_STATS_UINT(v_inactive_target, "Desired inactive pages");
340 VM_STATS_UINT(v_inactive_count, "Inactive pages");
341 VM_STATS_UINT(v_laundry_count, "Pages eligible for laundering");
342 VM_STATS_UINT(v_pageout_free_min, "Min pages reserved for kernel");
343 VM_STATS_UINT(v_interrupt_free_min, "Reserved pages for interrupt code");
344 VM_STATS_UINT(v_free_severe, "Severe page depletion point");
345 
346 #ifdef COMPAT_FREEBSD11
347 /*
348  * Provide compatibility sysctls for the benefit of old utilities which exit
349  * with an error if they cannot be found.
350  */
351 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, v_cache_count, CTLFLAG_RD,
352     SYSCTL_NULL_UINT_PTR, 0, "Dummy for compatibility");
353 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, v_tcached, CTLFLAG_RD,
354     SYSCTL_NULL_UINT_PTR, 0, "Dummy for compatibility");
355 #endif
356