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