xref: /freebsd/sys/vm/vm_meter.c (revision 7f9dff23d3092aa33ad45b2b63e52469b3c13a6e)
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  * 4. 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/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/resource.h>
44 #include <sys/rwlock.h>
45 #include <sys/sx.h>
46 #include <sys/vmmeter.h>
47 #include <sys/smp.h>
48 
49 #include <vm/vm.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_param.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_object.h>
56 #include <sys/sysctl.h>
57 
58 struct vmmeter vm_cnt;
59 
60 SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
61 	CTLFLAG_RW, &vm_cnt.v_free_min, 0, "Minimum low-free-pages threshold");
62 SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
63 	CTLFLAG_RW, &vm_cnt.v_free_target, 0, "Desired free pages");
64 SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
65 	CTLFLAG_RW, &vm_cnt.v_free_reserved, 0, "Pages reserved for deadlock");
66 SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
67 	CTLFLAG_RW, &vm_cnt.v_inactive_target, 0, "Pages desired inactive");
68 SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
69 	CTLFLAG_RW, &vm_cnt.v_pageout_free_min, 0, "Min pages reserved for kernel");
70 SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
71 	CTLFLAG_RW, &vm_cnt.v_free_severe, 0, "Severe page depletion point");
72 
73 static int
74 sysctl_vm_loadavg(SYSCTL_HANDLER_ARGS)
75 {
76 
77 #ifdef SCTL_MASK32
78 	u_int32_t la[4];
79 
80 	if (req->flags & SCTL_MASK32) {
81 		la[0] = averunnable.ldavg[0];
82 		la[1] = averunnable.ldavg[1];
83 		la[2] = averunnable.ldavg[2];
84 		la[3] = averunnable.fscale;
85 		return SYSCTL_OUT(req, la, sizeof(la));
86 	} else
87 #endif
88 		return SYSCTL_OUT(req, &averunnable, sizeof(averunnable));
89 }
90 SYSCTL_PROC(_vm, VM_LOADAVG, loadavg, CTLTYPE_STRUCT | CTLFLAG_RD |
91     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_loadavg, "S,loadavg",
92     "Machine loadaverage history");
93 
94 /*
95  * This function aims to determine if the object is mapped,
96  * specifically, if it is referenced by a vm_map_entry.  Because
97  * objects occasionally acquire transient references that do not
98  * represent a mapping, the method used here is inexact.  However, it
99  * has very low overhead and is good enough for the advisory
100  * vm.vmtotal sysctl.
101  */
102 static bool
103 is_object_active(vm_object_t obj)
104 {
105 
106 	return (obj->ref_count > obj->shadow_count);
107 }
108 
109 static int
110 vmtotal(SYSCTL_HANDLER_ARGS)
111 {
112 	struct vmtotal total;
113 	vm_object_t object;
114 	struct proc *p;
115 	struct thread *td;
116 
117 	bzero(&total, sizeof(total));
118 
119 	/*
120 	 * Calculate process statistics.
121 	 */
122 	sx_slock(&allproc_lock);
123 	FOREACH_PROC_IN_SYSTEM(p) {
124 		if (p->p_flag & P_SYSTEM)
125 			continue;
126 		PROC_LOCK(p);
127 		switch (p->p_state) {
128 		case PRS_NEW:
129 			PROC_UNLOCK(p);
130 			continue;
131 			break;
132 		default:
133 			FOREACH_THREAD_IN_PROC(p, td) {
134 				thread_lock(td);
135 				switch (td->td_state) {
136 				case TDS_INHIBITED:
137 					if (TD_IS_SWAPPED(td))
138 						total.t_sw++;
139 					else if (TD_IS_SLEEPING(td)) {
140 						if (td->td_priority <= PZERO)
141 							total.t_dw++;
142 						else
143 							total.t_sl++;
144 						if (td->td_wchan ==
145 						    &vm_cnt.v_free_count)
146 							total.t_pw++;
147 					}
148 					break;
149 
150 				case TDS_CAN_RUN:
151 					total.t_sw++;
152 					break;
153 				case TDS_RUNQ:
154 				case TDS_RUNNING:
155 					total.t_rq++;
156 					thread_unlock(td);
157 					continue;
158 				default:
159 					break;
160 				}
161 				thread_unlock(td);
162 			}
163 		}
164 		PROC_UNLOCK(p);
165 	}
166 	sx_sunlock(&allproc_lock);
167 	/*
168 	 * Calculate object memory usage statistics.
169 	 */
170 	mtx_lock(&vm_object_list_mtx);
171 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
172 		/*
173 		 * Perform unsynchronized reads on the object.  In
174 		 * this case, the lack of synchronization should not
175 		 * impair the accuracy of the reported statistics.
176 		 */
177 		if ((object->flags & OBJ_FICTITIOUS) != 0) {
178 			/*
179 			 * Devices, like /dev/mem, will badly skew our totals.
180 			 */
181 			continue;
182 		}
183 		if (object->ref_count == 0) {
184 			/*
185 			 * Also skip unreferenced objects, including
186 			 * vnodes representing mounted file systems.
187 			 */
188 			continue;
189 		}
190 		if (object->ref_count == 1 &&
191 		    (object->flags & OBJ_NOSPLIT) != 0) {
192 			/*
193 			 * Also skip otherwise unreferenced swap
194 			 * objects backing tmpfs vnodes, and POSIX or
195 			 * SysV shared memory.
196 			 */
197 			continue;
198 		}
199 		total.t_vm += object->size;
200 		total.t_rm += object->resident_page_count;
201 		if (is_object_active(object)) {
202 			total.t_avm += object->size;
203 			total.t_arm += object->resident_page_count;
204 		}
205 		if (object->shadow_count > 1) {
206 			/* shared object */
207 			total.t_vmshr += object->size;
208 			total.t_rmshr += object->resident_page_count;
209 			if (is_object_active(object)) {
210 				total.t_avmshr += object->size;
211 				total.t_armshr += object->resident_page_count;
212 			}
213 		}
214 	}
215 	mtx_unlock(&vm_object_list_mtx);
216 	total.t_free = vm_cnt.v_free_count;
217 	return (sysctl_handle_opaque(oidp, &total, sizeof(total), req));
218 }
219 
220 /*
221  * vm_meter_cnt() -	accumulate statistics from all cpus and the global cnt
222  *			structure.
223  *
224  *	The vmmeter structure is now per-cpu as well as global.  Those
225  *	statistics which can be kept on a per-cpu basis (to avoid cache
226  *	stalls between cpus) can be moved to the per-cpu vmmeter.  Remaining
227  *	statistics, such as v_free_reserved, are left in the global
228  *	structure.
229  */
230 u_int
231 vm_meter_cnt(size_t offset)
232 {
233 	struct pcpu *pcpu;
234 	u_int count;
235 	int i;
236 
237 	count = *(u_int *)((char *)&vm_cnt + offset);
238 	CPU_FOREACH(i) {
239 		pcpu = pcpu_find(i);
240 		count += *(u_int *)((char *)&pcpu->pc_cnt + offset);
241 	}
242 	return (count);
243 }
244 
245 static int
246 cnt_sysctl(SYSCTL_HANDLER_ARGS)
247 {
248 	u_int count;
249 
250 	count = vm_meter_cnt((char *)arg1 - (char *)&vm_cnt);
251 	return (SYSCTL_OUT(req, &count, sizeof(count)));
252 }
253 
254 SYSCTL_PROC(_vm, VM_TOTAL, vmtotal, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
255     0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
256     "System virtual memory statistics");
257 SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
258 static SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0,
259 	"VM meter sys stats");
260 static SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0,
261 	"VM meter vm stats");
262 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
263 
264 #define	VM_STATS(parent, var, descr) \
265 	SYSCTL_PROC(parent, OID_AUTO, var, \
266 	    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, &vm_cnt.var, 0,	\
267 	    cnt_sysctl, "IU", descr)
268 #define	VM_STATS_VM(var, descr)		VM_STATS(_vm_stats_vm, var, descr)
269 #define	VM_STATS_SYS(var, descr)	VM_STATS(_vm_stats_sys, var, descr)
270 
271 VM_STATS_SYS(v_swtch, "Context switches");
272 VM_STATS_SYS(v_trap, "Traps");
273 VM_STATS_SYS(v_syscall, "System calls");
274 VM_STATS_SYS(v_intr, "Device interrupts");
275 VM_STATS_SYS(v_soft, "Software interrupts");
276 VM_STATS_VM(v_vm_faults, "Address memory faults");
277 VM_STATS_VM(v_io_faults, "Page faults requiring I/O");
278 VM_STATS_VM(v_cow_faults, "Copy-on-write faults");
279 VM_STATS_VM(v_cow_optim, "Optimized COW faults");
280 VM_STATS_VM(v_zfod, "Pages zero-filled on demand");
281 VM_STATS_VM(v_ozfod, "Optimized zero fill pages");
282 VM_STATS_VM(v_swapin, "Swap pager pageins");
283 VM_STATS_VM(v_swapout, "Swap pager pageouts");
284 VM_STATS_VM(v_swappgsin, "Swap pages swapped in");
285 VM_STATS_VM(v_swappgsout, "Swap pages swapped out");
286 VM_STATS_VM(v_vnodein, "Vnode pager pageins");
287 VM_STATS_VM(v_vnodeout, "Vnode pager pageouts");
288 VM_STATS_VM(v_vnodepgsin, "Vnode pages paged in");
289 VM_STATS_VM(v_vnodepgsout, "Vnode pages paged out");
290 VM_STATS_VM(v_intrans, "In transit page faults");
291 VM_STATS_VM(v_reactivated, "Pages reactivated by pagedaemon");
292 VM_STATS_VM(v_pdwakeups, "Pagedaemon wakeups");
293 VM_STATS_VM(v_pdpages, "Pages analyzed by pagedaemon");
294 VM_STATS_VM(v_pdshortfalls, "Page reclamation shortfalls");
295 VM_STATS_VM(v_dfree, "Pages freed by pagedaemon");
296 VM_STATS_VM(v_pfree, "Pages freed by exiting processes");
297 VM_STATS_VM(v_tfree, "Total pages freed");
298 VM_STATS_VM(v_page_size, "Page size in bytes");
299 VM_STATS_VM(v_page_count, "Total number of pages in system");
300 VM_STATS_VM(v_free_reserved, "Pages reserved for deadlock");
301 VM_STATS_VM(v_free_target, "Pages desired free");
302 VM_STATS_VM(v_free_min, "Minimum low-free-pages threshold");
303 VM_STATS_VM(v_free_count, "Free pages");
304 VM_STATS_VM(v_wire_count, "Wired pages");
305 VM_STATS_VM(v_active_count, "Active pages");
306 VM_STATS_VM(v_inactive_target, "Desired inactive pages");
307 VM_STATS_VM(v_inactive_count, "Inactive pages");
308 VM_STATS_VM(v_laundry_count, "Pages eligible for laundering");
309 VM_STATS_VM(v_pageout_free_min, "Min pages reserved for kernel");
310 VM_STATS_VM(v_interrupt_free_min, "Reserved pages for interrupt code");
311 VM_STATS_VM(v_forks, "Number of fork() calls");
312 VM_STATS_VM(v_vforks, "Number of vfork() calls");
313 VM_STATS_VM(v_rforks, "Number of rfork() calls");
314 VM_STATS_VM(v_kthreads, "Number of fork() calls by kernel");
315 VM_STATS_VM(v_forkpages, "VM pages affected by fork()");
316 VM_STATS_VM(v_vforkpages, "VM pages affected by vfork()");
317 VM_STATS_VM(v_rforkpages, "VM pages affected by rfork()");
318 VM_STATS_VM(v_kthreadpages, "VM pages affected by fork() by kernel");
319 
320 #ifdef COMPAT_FREEBSD11
321 /*
322  * Provide compatibility sysctls for the benefit of old utilities which exit
323  * with an error if they cannot be found.
324  */
325 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, v_cache_count, CTLFLAG_RD,
326     SYSCTL_NULL_UINT_PTR, 0, "Dummy for compatibility");
327 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, v_tcached, CTLFLAG_RD,
328     SYSCTL_NULL_UINT_PTR, 0, "Dummy for compatibility");
329 #endif
330