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 __exclusive_cache_line 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 #if defined(COMPAT_FREEBSD11) 156 struct vmtotal11 { 157 int16_t t_rq; 158 int16_t t_dw; 159 int16_t t_pw; 160 int16_t t_sl; 161 int16_t t_sw; 162 int32_t t_vm; 163 int32_t t_avm; 164 int32_t t_rm; 165 int32_t t_arm; 166 int32_t t_vmshr; 167 int32_t t_avmshr; 168 int32_t t_rmshr; 169 int32_t t_armshr; 170 int32_t t_free; 171 }; 172 #endif 173 174 static int 175 vmtotal(SYSCTL_HANDLER_ARGS) 176 { 177 struct vmtotal total; 178 #if defined(COMPAT_FREEBSD11) 179 struct vmtotal11 total11; 180 #endif 181 vm_object_t object; 182 struct proc *p; 183 struct thread *td; 184 185 if (req->oldptr == NULL) { 186 #if defined(COMPAT_FREEBSD11) 187 if (curproc->p_osrel < P_OSREL_VMTOTAL64) 188 return (SYSCTL_OUT(req, NULL, sizeof(total11))); 189 #endif 190 return (SYSCTL_OUT(req, NULL, sizeof(total))); 191 } 192 bzero(&total, sizeof(total)); 193 194 /* 195 * Calculate process statistics. 196 */ 197 sx_slock(&allproc_lock); 198 FOREACH_PROC_IN_SYSTEM(p) { 199 if ((p->p_flag & P_SYSTEM) != 0) 200 continue; 201 PROC_LOCK(p); 202 if (p->p_state != PRS_NEW) { 203 FOREACH_THREAD_IN_PROC(p, td) { 204 thread_lock(td); 205 switch (td->td_state) { 206 case TDS_INHIBITED: 207 if (TD_IS_SWAPPED(td)) 208 total.t_sw++; 209 else if (TD_IS_SLEEPING(td)) { 210 if (td->td_priority <= PZERO) 211 total.t_dw++; 212 else 213 total.t_sl++; 214 if (td->td_wchan == 215 &vm_cnt.v_free_count) 216 total.t_pw++; 217 } 218 break; 219 case TDS_CAN_RUN: 220 total.t_sw++; 221 break; 222 case TDS_RUNQ: 223 case TDS_RUNNING: 224 total.t_rq++; 225 break; 226 default: 227 break; 228 } 229 thread_unlock(td); 230 } 231 } 232 PROC_UNLOCK(p); 233 } 234 sx_sunlock(&allproc_lock); 235 /* 236 * Calculate object memory usage statistics. 237 */ 238 mtx_lock(&vm_object_list_mtx); 239 TAILQ_FOREACH(object, &vm_object_list, object_list) { 240 /* 241 * Perform unsynchronized reads on the object. In 242 * this case, the lack of synchronization should not 243 * impair the accuracy of the reported statistics. 244 */ 245 if ((object->flags & OBJ_FICTITIOUS) != 0) { 246 /* 247 * Devices, like /dev/mem, will badly skew our totals. 248 */ 249 continue; 250 } 251 if (object->ref_count == 0) { 252 /* 253 * Also skip unreferenced objects, including 254 * vnodes representing mounted file systems. 255 */ 256 continue; 257 } 258 if (object->ref_count == 1 && 259 (object->flags & OBJ_NOSPLIT) != 0) { 260 /* 261 * Also skip otherwise unreferenced swap 262 * objects backing tmpfs vnodes, and POSIX or 263 * SysV shared memory. 264 */ 265 continue; 266 } 267 total.t_vm += object->size; 268 total.t_rm += object->resident_page_count; 269 if (is_object_active(object)) { 270 total.t_avm += object->size; 271 total.t_arm += object->resident_page_count; 272 } 273 if (object->shadow_count > 1) { 274 /* shared object */ 275 total.t_vmshr += object->size; 276 total.t_rmshr += object->resident_page_count; 277 if (is_object_active(object)) { 278 total.t_avmshr += object->size; 279 total.t_armshr += object->resident_page_count; 280 } 281 } 282 } 283 mtx_unlock(&vm_object_list_mtx); 284 total.t_free = vm_cnt.v_free_count; 285 #if defined(COMPAT_FREEBSD11) 286 /* sysctl(8) allocates twice as much memory as reported by sysctl(3) */ 287 if (curproc->p_osrel < P_OSREL_VMTOTAL64 && (req->oldlen == 288 sizeof(total11) || req->oldlen == 2 * sizeof(total11))) { 289 bzero(&total11, sizeof(total11)); 290 total11.t_rq = total.t_rq; 291 total11.t_dw = total.t_dw; 292 total11.t_pw = total.t_pw; 293 total11.t_sl = total.t_sl; 294 total11.t_sw = total.t_sw; 295 total11.t_vm = total.t_vm; /* truncate */ 296 total11.t_avm = total.t_avm; /* truncate */ 297 total11.t_rm = total.t_rm; /* truncate */ 298 total11.t_arm = total.t_arm; /* truncate */ 299 total11.t_vmshr = total.t_vmshr; /* truncate */ 300 total11.t_avmshr = total.t_avmshr; /* truncate */ 301 total11.t_rmshr = total.t_rmshr; /* truncate */ 302 total11.t_armshr = total.t_armshr; /* truncate */ 303 total11.t_free = total.t_free; /* truncate */ 304 return (SYSCTL_OUT(req, &total11, sizeof(total11))); 305 } 306 #endif 307 return (SYSCTL_OUT(req, &total, sizeof(total))); 308 } 309 310 SYSCTL_PROC(_vm, VM_TOTAL, vmtotal, CTLTYPE_OPAQUE | CTLFLAG_RD | 311 CTLFLAG_MPSAFE, NULL, 0, vmtotal, "S,vmtotal", 312 "System virtual memory statistics"); 313 SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats"); 314 static SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, 315 "VM meter sys stats"); 316 static SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, 317 "VM meter vm stats"); 318 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats"); 319 320 static int 321 sysctl_handle_vmstat(SYSCTL_HANDLER_ARGS) 322 { 323 uint64_t val; 324 #ifdef COMPAT_FREEBSD11 325 uint32_t val32; 326 #endif 327 328 val = counter_u64_fetch(*(counter_u64_t *)arg1); 329 #ifdef COMPAT_FREEBSD11 330 if (req->oldlen == sizeof(val32)) { 331 val32 = val; /* truncate */ 332 return (SYSCTL_OUT(req, &val32, sizeof(val32))); 333 } 334 #endif 335 return (SYSCTL_OUT(req, &val, sizeof(val))); 336 } 337 338 #define VM_STATS(parent, var, descr) \ 339 SYSCTL_OID(parent, OID_AUTO, var, CTLTYPE_U64 | CTLFLAG_MPSAFE | \ 340 CTLFLAG_RD, &vm_cnt.var, 0, sysctl_handle_vmstat, "QU", descr); 341 #define VM_STATS_VM(var, descr) VM_STATS(_vm_stats_vm, var, descr) 342 #define VM_STATS_SYS(var, descr) VM_STATS(_vm_stats_sys, var, descr) 343 344 VM_STATS_SYS(v_swtch, "Context switches"); 345 VM_STATS_SYS(v_trap, "Traps"); 346 VM_STATS_SYS(v_syscall, "System calls"); 347 VM_STATS_SYS(v_intr, "Device interrupts"); 348 VM_STATS_SYS(v_soft, "Software interrupts"); 349 VM_STATS_VM(v_vm_faults, "Address memory faults"); 350 VM_STATS_VM(v_io_faults, "Page faults requiring I/O"); 351 VM_STATS_VM(v_cow_faults, "Copy-on-write faults"); 352 VM_STATS_VM(v_cow_optim, "Optimized COW faults"); 353 VM_STATS_VM(v_zfod, "Pages zero-filled on demand"); 354 VM_STATS_VM(v_ozfod, "Optimized zero fill pages"); 355 VM_STATS_VM(v_swapin, "Swap pager pageins"); 356 VM_STATS_VM(v_swapout, "Swap pager pageouts"); 357 VM_STATS_VM(v_swappgsin, "Swap pages swapped in"); 358 VM_STATS_VM(v_swappgsout, "Swap pages swapped out"); 359 VM_STATS_VM(v_vnodein, "Vnode pager pageins"); 360 VM_STATS_VM(v_vnodeout, "Vnode pager pageouts"); 361 VM_STATS_VM(v_vnodepgsin, "Vnode pages paged in"); 362 VM_STATS_VM(v_vnodepgsout, "Vnode pages paged out"); 363 VM_STATS_VM(v_intrans, "In transit page faults"); 364 VM_STATS_VM(v_reactivated, "Pages reactivated by pagedaemon"); 365 VM_STATS_VM(v_pdwakeups, "Pagedaemon wakeups"); 366 VM_STATS_VM(v_pdpages, "Pages analyzed by pagedaemon"); 367 VM_STATS_VM(v_pdshortfalls, "Page reclamation shortfalls"); 368 VM_STATS_VM(v_dfree, "Pages freed by pagedaemon"); 369 VM_STATS_VM(v_pfree, "Pages freed by exiting processes"); 370 VM_STATS_VM(v_tfree, "Total pages freed"); 371 VM_STATS_VM(v_forks, "Number of fork() calls"); 372 VM_STATS_VM(v_vforks, "Number of vfork() calls"); 373 VM_STATS_VM(v_rforks, "Number of rfork() calls"); 374 VM_STATS_VM(v_kthreads, "Number of fork() calls by kernel"); 375 VM_STATS_VM(v_forkpages, "VM pages affected by fork()"); 376 VM_STATS_VM(v_vforkpages, "VM pages affected by vfork()"); 377 VM_STATS_VM(v_rforkpages, "VM pages affected by rfork()"); 378 VM_STATS_VM(v_kthreadpages, "VM pages affected by fork() by kernel"); 379 380 #define VM_STATS_UINT(var, descr) \ 381 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, var, CTLFLAG_RD, &vm_cnt.var, 0, descr) 382 VM_STATS_UINT(v_page_size, "Page size in bytes"); 383 VM_STATS_UINT(v_page_count, "Total number of pages in system"); 384 VM_STATS_UINT(v_free_reserved, "Pages reserved for deadlock"); 385 VM_STATS_UINT(v_free_target, "Pages desired free"); 386 VM_STATS_UINT(v_free_min, "Minimum low-free-pages threshold"); 387 VM_STATS_UINT(v_free_count, "Free pages"); 388 VM_STATS_UINT(v_wire_count, "Wired pages"); 389 VM_STATS_UINT(v_active_count, "Active pages"); 390 VM_STATS_UINT(v_inactive_target, "Desired inactive pages"); 391 VM_STATS_UINT(v_inactive_count, "Inactive pages"); 392 VM_STATS_UINT(v_laundry_count, "Pages eligible for laundering"); 393 VM_STATS_UINT(v_pageout_free_min, "Min pages reserved for kernel"); 394 VM_STATS_UINT(v_interrupt_free_min, "Reserved pages for interrupt code"); 395 VM_STATS_UINT(v_free_severe, "Severe page depletion point"); 396 397 #ifdef COMPAT_FREEBSD11 398 /* 399 * Provide compatibility sysctls for the benefit of old utilities which exit 400 * with an error if they cannot be found. 401 */ 402 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, v_cache_count, CTLFLAG_RD, 403 SYSCTL_NULL_UINT_PTR, 0, "Dummy for compatibility"); 404 SYSCTL_UINT(_vm_stats_vm, OID_AUTO, v_tcached, CTLFLAG_RD, 405 SYSCTL_NULL_UINT_PTR, 0, "Dummy for compatibility"); 406 #endif 407