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