| 1 | /*- |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * |
| 4 | * Copyright (c) 1982, 1986, 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 | |
| 32 | #ifndef _SYS_VMMETER_H_ |
| 33 | #define _SYS_VMMETER_H_ |
| 34 | |
| 35 | /* |
| 36 | * This value is used by ps(1) to change sleep state flag from 'S' to |
| 37 | * 'I' and by the sched process to set the alarm clock. |
| 38 | */ |
| 39 | #define	MAXSLP			20 |
| 40 | |
| 41 | struct vmtotal { |
| 42 | 	uint64_t	t_vm;		/* total virtual memory */ |
| 43 | 	uint64_t	t_avm;		/* active virtual memory */ |
| 44 | 	uint64_t	t_rm;		/* total real memory in use */ |
| 45 | 	uint64_t	t_arm;		/* active real memory */ |
| 46 | 	uint64_t	t_vmshr;	/* shared virtual memory */ |
| 47 | 	uint64_t	t_avmshr;	/* active shared virtual memory */ |
| 48 | 	uint64_t	t_rmshr;	/* shared real memory */ |
| 49 | 	uint64_t	t_armshr;	/* active shared real memory */ |
| 50 | 	uint64_t	t_free;		/* free memory pages */ |
| 51 | 	int16_t		t_rq;		/* length of the run queue */ |
| 52 | 	int16_t		t_dw;		/* threads in ``disk wait'' (neg |
| 53 | 					 priority) */ |
| 54 | 	int16_t		t_pw;		/* threads in page wait */ |
| 55 | 	int16_t		t_sl;		/* threads sleeping in core */ |
| 56 | 	int16_t		t_sw;		/* swapped out runnable/short |
| 57 | 					 block threads */ |
| 58 | 	uint16_t	t_pad[3]; |
| 59 | }; |
| 60 | |
| 61 | #if defined(_KERNEL) || defined(_WANT_VMMETER) |
| 62 | #include <sys/counter.h> |
| 63 | |
| 64 | #ifdef _KERNEL |
| 65 | #define VMMETER_ALIGNED	__aligned(CACHE_LINE_SIZE) |
| 66 | #else |
| 67 | #define VMMETER_ALIGNED |
| 68 | #endif |
| 69 | |
| 70 | /* |
| 71 | * System wide statistics counters. |
| 72 | * Locking: |
| 73 | * c - constant after initialization |
| 74 | * p - uses counter(9) |
| 75 | */ |
| 76 | struct vmmeter { |
| 77 | 	/* |
| 78 | 	 * General system activity. |
| 79 | 	 */ |
| 80 | 	counter_u64_t v_swtch;		/* (p) context switches */ |
| 81 | 	counter_u64_t v_trap;		/* (p) calls to trap */ |
| 82 | 	counter_u64_t v_syscall;	/* (p) calls to syscall() */ |
| 83 | 	counter_u64_t v_intr;		/* (p) device interrupts */ |
| 84 | 	counter_u64_t v_soft;		/* (p) software interrupts */ |
| 85 | 	/* |
| 86 | 	 * Virtual memory activity. |
| 87 | 	 */ |
| 88 | 	counter_u64_t v_vm_faults;	/* (p) address memory faults */ |
| 89 | 	counter_u64_t v_io_faults;	/* (p) page faults requiring I/O */ |
| 90 | 	counter_u64_t v_cow_faults;	/* (p) copy-on-writes faults */ |
| 91 | 	counter_u64_t v_cow_optim;	/* (p) optimized COW faults */ |
| 92 | 	counter_u64_t v_zfod;		/* (p) pages zero filled on demand */ |
| 93 | 	counter_u64_t v_ozfod;		/* (p) optimized zero fill pages */ |
| 94 | 	counter_u64_t v_swapin;		/* (p) swap pager pageins */ |
| 95 | 	counter_u64_t v_swapout;	/* (p) swap pager pageouts */ |
| 96 | 	counter_u64_t v_swappgsin;	/* (p) swap pager pages paged in */ |
| 97 | 	counter_u64_t v_swappgsout;	/* (p) swap pager pages paged out */ |
| 98 | 	counter_u64_t v_vnodein;	/* (p) vnode pager pageins */ |
| 99 | 	counter_u64_t v_vnodeout;	/* (p) vnode pager pageouts */ |
| 100 | 	counter_u64_t v_vnodepgsin;	/* (p) vnode_pager pages paged in */ |
| 101 | 	counter_u64_t v_vnodepgsout;	/* (p) vnode pager pages paged out */ |
| 102 | 	counter_u64_t v_intrans;	/* (p) intransit blocking page faults */ |
| 103 | 	counter_u64_t v_reactivated;	/* (p) reactivated by the pagedaemon */ |
| 104 | 	counter_u64_t v_pdwakeups;	/* (p) times daemon has awaken */ |
| 105 | 	counter_u64_t v_pdpages;	/* (p) pages analyzed by daemon */ |
| 106 | 	counter_u64_t v_pdshortfalls;	/* (p) page reclamation shortfalls */ |
| 107 | |
| 108 | 	counter_u64_t v_dfree;		/* (p) pages freed by daemon */ |
| 109 | 	counter_u64_t v_pfree;		/* (p) pages freed by processes */ |
| 110 | 	counter_u64_t v_tfree;		/* (p) total pages freed */ |
| 111 | 	/* |
| 112 | 	 * Fork/vfork/rfork activity. |
| 113 | 	 */ |
| 114 | 	counter_u64_t v_forks;		/* (p) fork() calls */ |
| 115 | 	counter_u64_t v_vforks;		/* (p) vfork() calls */ |
| 116 | 	counter_u64_t v_rforks;		/* (p) rfork() calls */ |
| 117 | 	counter_u64_t v_kthreads;	/* (p) fork() calls by kernel */ |
| 118 | 	counter_u64_t v_forkpages;	/* (p) pages affected by fork() */ |
| 119 | 	counter_u64_t v_vforkpages;	/* (p) pages affected by vfork() */ |
| 120 | 	counter_u64_t v_rforkpages;	/* (p) pages affected by rfork() */ |
| 121 | 	counter_u64_t v_kthreadpages;	/* (p) ... and by kernel fork() */ |
| 122 | 	counter_u64_t v_wire_count;	/* (p) pages wired down */ |
| 123 | 	counter_u64_t v_nofree_count;	/* (p) permanently allocated pages */ |
| 124 | #define	VM_METER_NCOUNTERS	\ |
| 125 | 	(offsetof(struct vmmeter, v_page_size) / sizeof(counter_u64_t)) |
| 126 | 	/* |
| 127 | 	 * Distribution of page usages. |
| 128 | 	 */ |
| 129 | 	u_int v_page_size;	/* (c) page size in bytes */ |
| 130 | 	u_int v_page_count;	/* (c) total number of pages in system */ |
| 131 | 	u_int v_free_reserved;	/* (c) pages reserved for deadlock */ |
| 132 | 	u_int v_free_target;	/* (c) pages desired free */ |
| 133 | 	u_int v_free_min;	/* (c) pages desired free */ |
| 134 | 	u_int v_inactive_target; /* (c) pages desired inactive */ |
| 135 | 	u_int v_pageout_free_min; /* (c) min pages reserved for kernel */ |
| 136 | 	u_int v_interrupt_free_min; /* (c) reserved pages for int code */ |
| 137 | 	u_int v_free_severe;	/* (c) severe page depletion point */ |
| 138 | }; |
| 139 | #endif /* _KERNEL || _WANT_VMMETER */ |
| 140 | |
| 141 | #ifdef _KERNEL |
| 142 | |
| 143 | #include <sys/domainset.h> |
| 144 | |
| 145 | extern struct vmmeter vm_cnt; |
| 146 | extern domainset_t all_domains; |
| 147 | extern domainset_t vm_min_domains; |
| 148 | extern domainset_t vm_severe_domains; |
| 149 | |
| 150 | #define	VM_CNT_ADD(var, x)	counter_u64_add(vm_cnt.var, x) |
| 151 | #define	VM_CNT_INC(var)		VM_CNT_ADD(var, 1) |
| 152 | #define	VM_CNT_FETCH(var)	counter_u64_fetch(vm_cnt.var) |
| 153 | |
| 154 | extern u_long vm_user_wire_count; |
| 155 | |
| 156 | static inline void |
| 157 | vm_wire_add(int cnt) |
| 158 | { |
| 159 | |
| 160 | 	VM_CNT_ADD(v_wire_count, cnt); |
| 161 | } |
| 162 | |
| 163 | static inline void |
| 164 | vm_wire_sub(int cnt) |
| 165 | { |
| 166 | |
| 167 | 	VM_CNT_ADD(v_wire_count, -cnt); |
| 168 | } |
| 169 | |
| 170 | u_int vm_free_count(void); |
| 171 | static inline u_int |
| 172 | vm_wire_count(void) |
| 173 | { |
| 174 | |
| 175 | 	return (VM_CNT_FETCH(v_wire_count)); |
| 176 | } |
| 177 | |
| 178 | static inline u_int |
| 179 | vm_nofree_count(void) |
| 180 | { |
| 181 | |
| 182 | 	return (VM_CNT_FETCH(v_nofree_count)); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Return TRUE if we are under our severe low-free-pages threshold |
| 187 | * |
| 188 | * These routines are typically used at the user<->system interface to determine |
| 189 | * whether we need to block in order to avoid a low memory deadlock. |
| 190 | */ |
| 191 | static inline int |
| 192 | vm_page_count_severe(void) |
| 193 | { |
| 194 | |
| 195 | 	return (!DOMAINSET_EMPTY(&vm_severe_domains)); |
| 196 | } |
| 197 | |
| 198 | static inline int |
| 199 | vm_page_count_severe_domain(int domain) |
| 200 | { |
| 201 | |
| 202 | 	return (DOMAINSET_ISSET(domain, &vm_severe_domains)); |
| 203 | } |
| 204 | |
| 205 | static inline int |
| 206 | vm_page_count_severe_set(const domainset_t *mask) |
| 207 | { |
| 208 | |
| 209 | 	return (DOMAINSET_SUBSET(&vm_severe_domains, mask)); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Return TRUE if we are under our minimum low-free-pages threshold. |
| 214 | * |
| 215 | * These routines are typically used within the system to determine whether |
| 216 | * we can execute potentially very expensive code in terms of memory. It |
| 217 | * is also used by the pageout daemon to calculate when to sleep, when |
| 218 | * to wake waiters up, and when (after making a pass) to become more |
| 219 | * desperate. |
| 220 | */ |
| 221 | static inline int |
| 222 | vm_page_count_min(void) |
| 223 | { |
| 224 | |
| 225 | 	return (!DOMAINSET_EMPTY(&vm_min_domains)); |
| 226 | } |
| 227 | |
| 228 | static inline int |
| 229 | vm_page_count_min_domain(int domain) |
| 230 | { |
| 231 | |
| 232 | 	return (DOMAINSET_ISSET(domain, &vm_min_domains)); |
| 233 | } |
| 234 | |
| 235 | static inline int |
| 236 | vm_page_count_min_set(const domainset_t *mask) |
| 237 | { |
| 238 | |
| 239 | 	return (DOMAINSET_SUBSET(&vm_min_domains, mask)); |
| 240 | } |
| 241 | |
| 242 | #endif	/* _KERNEL */ |
| 243 | #endif	/* _SYS_VMMETER_H_ */ |