xref: /freebsd/sys/sys/vmmeter.h (revision d93a896e)
1 /*-
2  * Copyright (c) 1982, 1986, 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  *	@(#)vmmeter.h	8.2 (Berkeley) 7/10/94
30  * $FreeBSD$
31  */
32 
33 #ifndef _SYS_VMMETER_H_
34 #define _SYS_VMMETER_H_
35 
36 /*
37  * This value is used by ps(1) to change sleep state flag from 'S' to
38  * 'I' and by the sched process to set the alarm clock.
39  */
40 #define	MAXSLP			20
41 
42 /* Systemwide totals computed every five seconds. */
43 struct vmtotal {
44 	int16_t	t_rq;		/* length of the run queue */
45 	int16_t	t_dw;		/* jobs in ``disk wait'' (neg priority) */
46 	int16_t	t_pw;		/* jobs in page wait */
47 	int16_t	t_sl;		/* jobs sleeping in core */
48 	int16_t	t_sw;		/* swapped out runnable/short block jobs */
49 	int32_t	t_vm;		/* total virtual memory */
50 	int32_t	t_avm;		/* active virtual memory */
51 	int32_t	t_rm;		/* total real memory in use */
52 	int32_t	t_arm;		/* active real memory */
53 	int32_t	t_vmshr;	/* shared virtual memory */
54 	int32_t	t_avmshr;	/* active shared virtual memory */
55 	int32_t	t_rmshr;	/* shared real memory */
56 	int32_t	t_armshr;	/* active shared real memory */
57 	int32_t	t_free;		/* free memory pages */
58 };
59 
60 #if defined(_KERNEL) || defined(_WANT_VMMETER)
61 #include <sys/counter.h>
62 
63 /*
64  * System wide statistics counters.
65  * Locking:
66  *      a - locked by atomic operations
67  *      c - constant after initialization
68  *      f - locked by vm_page_queue_free_mtx
69  *      p - uses counter(9)
70  *      q - changes are synchronized by the corresponding vm_pagequeue lock
71  */
72 struct vmmeter {
73 	/*
74 	 * General system activity.
75 	 */
76 	counter_u64_t v_swtch;		/* (p) context switches */
77 	counter_u64_t v_trap;		/* (p) calls to trap */
78 	counter_u64_t v_syscall;	/* (p) calls to syscall() */
79 	counter_u64_t v_intr;		/* (p) device interrupts */
80 	counter_u64_t v_soft;		/* (p) software interrupts */
81 	/*
82 	 * Virtual memory activity.
83 	 */
84 	counter_u64_t v_vm_faults;	/* (p) address memory faults */
85 	counter_u64_t v_io_faults;	/* (p) page faults requiring I/O */
86 	counter_u64_t v_cow_faults;	/* (p) copy-on-writes faults */
87 	counter_u64_t v_cow_optim;	/* (p) optimized COW faults */
88 	counter_u64_t v_zfod;		/* (p) pages zero filled on demand */
89 	counter_u64_t v_ozfod;		/* (p) optimized zero fill pages */
90 	counter_u64_t v_swapin;		/* (p) swap pager pageins */
91 	counter_u64_t v_swapout;	/* (p) swap pager pageouts */
92 	counter_u64_t v_swappgsin;	/* (p) swap pager pages paged in */
93 	counter_u64_t v_swappgsout;	/* (p) swap pager pages paged out */
94 	counter_u64_t v_vnodein;	/* (p) vnode pager pageins */
95 	counter_u64_t v_vnodeout;	/* (p) vnode pager pageouts */
96 	counter_u64_t v_vnodepgsin;	/* (p) vnode_pager pages paged in */
97 	counter_u64_t v_vnodepgsout;	/* (p) vnode pager pages paged out */
98 	counter_u64_t v_intrans;	/* (p) intransit blocking page faults */
99 	counter_u64_t v_reactivated;	/* (p) reactivated by the pagedaemon */
100 	counter_u64_t v_pdwakeups;	/* (p) times daemon has awaken */
101 	counter_u64_t v_pdpages;	/* (p) pages analyzed by daemon */
102 	counter_u64_t v_pdshortfalls;	/* (p) page reclamation shortfalls */
103 
104 	counter_u64_t v_dfree;		/* (p) pages freed by daemon */
105 	counter_u64_t v_pfree;		/* (p) pages freed by processes */
106 	counter_u64_t v_tfree;		/* (p) total pages freed */
107 	/*
108 	 * Fork/vfork/rfork activity.
109 	 */
110 	counter_u64_t v_forks;		/* (p) fork() calls */
111 	counter_u64_t v_vforks;		/* (p) vfork() calls */
112 	counter_u64_t v_rforks;		/* (p) rfork() calls */
113 	counter_u64_t v_kthreads;	/* (p) fork() calls by kernel */
114 	counter_u64_t v_forkpages;	/* (p) pages affected by fork() */
115 	counter_u64_t v_vforkpages;	/* (p) pages affected by vfork() */
116 	counter_u64_t v_rforkpages;	/* (p) pages affected by rfork() */
117 	counter_u64_t v_kthreadpages;	/* (p) ... and by kernel fork() */
118 #define	VM_METER_NCOUNTERS	\
119 	(offsetof(struct vmmeter, v_page_size) / sizeof(counter_u64_t))
120 	/*
121 	 * Distribution of page usages.
122 	 */
123 	u_int v_page_size;	/* (c) page size in bytes */
124 	u_int v_page_count;	/* (c) total number of pages in system */
125 	u_int v_free_reserved;	/* (c) pages reserved for deadlock */
126 	u_int v_free_target;	/* (c) pages desired free */
127 	u_int v_free_min;	/* (c) pages desired free */
128 	u_int v_free_count;	/* (f) pages free */
129 	u_int v_wire_count;	/* (a) pages wired down */
130 	u_int v_active_count;	/* (q) pages active */
131 	u_int v_inactive_target; /* (c) pages desired inactive */
132 	u_int v_inactive_count;	/* (q) pages inactive */
133 	u_int v_laundry_count;	/* (q) pages eligible for laundering */
134 	u_int v_pageout_free_min;   /* (c) min pages reserved for kernel */
135 	u_int v_interrupt_free_min; /* (c) reserved pages for int code */
136 	u_int v_free_severe;	/* (c) severe page depletion point */
137 };
138 #endif /* _KERNEL || _WANT_VMMETER */
139 
140 #ifdef _KERNEL
141 
142 extern struct vmmeter vm_cnt;
143 extern u_int vm_pageout_wakeup_thresh;
144 
145 #define	VM_CNT_ADD(var, x)	counter_u64_add(vm_cnt.var, x)
146 #define	VM_CNT_INC(var)		VM_CNT_ADD(var, 1)
147 #define	VM_CNT_FETCH(var)	counter_u64_fetch(vm_cnt.var)
148 
149 /*
150  * Return TRUE if we are under our severe low-free-pages threshold
151  *
152  * This routine is typically used at the user<->system interface to determine
153  * whether we need to block in order to avoid a low memory deadlock.
154  */
155 static inline int
156 vm_page_count_severe(void)
157 {
158 
159 	return (vm_cnt.v_free_severe > vm_cnt.v_free_count);
160 }
161 
162 /*
163  * Return TRUE if we are under our minimum low-free-pages threshold.
164  *
165  * This routine is typically used within the system to determine whether
166  * we can execute potentially very expensive code in terms of memory.  It
167  * is also used by the pageout daemon to calculate when to sleep, when
168  * to wake waiters up, and when (after making a pass) to become more
169  * desperate.
170  */
171 static inline int
172 vm_page_count_min(void)
173 {
174 
175 	return (vm_cnt.v_free_min > vm_cnt.v_free_count);
176 }
177 
178 /*
179  * Return TRUE if we have not reached our free page target during
180  * free page recovery operations.
181  */
182 static inline int
183 vm_page_count_target(void)
184 {
185 
186 	return (vm_cnt.v_free_target > vm_cnt.v_free_count);
187 }
188 
189 /*
190  * Return the number of pages we need to free-up or cache
191  * A positive number indicates that we do not have enough free pages.
192  */
193 static inline int
194 vm_paging_target(void)
195 {
196 
197 	return (vm_cnt.v_free_target - vm_cnt.v_free_count);
198 }
199 
200 /*
201  * Returns TRUE if the pagedaemon needs to be woken up.
202  */
203 static inline int
204 vm_paging_needed(void)
205 {
206 
207 	return (vm_cnt.v_free_count < vm_pageout_wakeup_thresh);
208 }
209 
210 /*
211  * Return the number of pages we need to launder.
212  * A positive number indicates that we have a shortfall of clean pages.
213  */
214 static inline int
215 vm_laundry_target(void)
216 {
217 
218 	return (vm_paging_target());
219 }
220 #endif	/* _KERNEL */
221 #endif	/* _SYS_VMMETER_H_ */
222