FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_meter.c
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94
34 * $FreeBSD: releng/5.0/sys/vm/vm_meter.c 104387 2002-10-02 20:31:47Z jhb $
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/resource.h>
44 #include <sys/sx.h>
45 #include <sys/vmmeter.h>
46 #include <sys/smp.h>
47
48 #include <vm/vm.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_extern.h>
51 #include <vm/vm_param.h>
52 #include <vm/pmap.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_object.h>
55 #include <sys/sysctl.h>
56
57 struct vmmeter cnt;
58
59 int maxslp = MAXSLP;
60
61 SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
62 CTLFLAG_RW, &cnt.v_free_min, 0, "");
63 SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
64 CTLFLAG_RW, &cnt.v_free_target, 0, "");
65 SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
66 CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
67 SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
68 CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
69 SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
70 CTLFLAG_RW, &cnt.v_cache_min, 0, "");
71 SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
72 CTLFLAG_RW, &cnt.v_cache_max, 0, "");
73 SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
74 CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
75 SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
76 CTLFLAG_RW, &cnt.v_free_severe, 0, "");
77
78 SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD,
79 &averunnable, loadavg, "Machine loadaverage history");
80
81 static int
82 vmtotal(SYSCTL_HANDLER_ARGS)
83 {
84 /* XXXKSE almost completely broken */
85 struct proc *p;
86 struct vmtotal total, *totalp;
87 vm_map_entry_t entry;
88 vm_object_t object;
89 vm_map_t map;
90 int paging;
91 struct thread *td;
92
93 totalp = &total;
94 bzero(totalp, sizeof *totalp);
95 /*
96 * Mark all objects as inactive.
97 */
98 GIANT_REQUIRED;
99 mtx_lock(&vm_object_list_mtx);
100 TAILQ_FOREACH(object, &vm_object_list, object_list)
101 vm_object_clear_flag(object, OBJ_ACTIVE);
102 mtx_unlock(&vm_object_list_mtx);
103 /*
104 * Calculate process statistics.
105 */
106 sx_slock(&allproc_lock);
107 FOREACH_PROC_IN_SYSTEM(p) {
108 if (p->p_flag & P_SYSTEM)
109 continue;
110 mtx_lock_spin(&sched_lock);
111 switch (p->p_state) {
112 case PRS_NEW:
113 mtx_unlock_spin(&sched_lock);
114 continue;
115 break;
116 default:
117 FOREACH_THREAD_IN_PROC(p, td) {
118 /* Need new statistics XXX */
119 switch (td->td_state) {
120 case TDS_INHIBITED:
121 if (TD_ON_LOCK(td) ||
122 (td->td_inhibitors ==
123 TDI_SWAPPED)) {
124 totalp->t_sw++;
125 } else if (TD_IS_SLEEPING(td) ||
126 TD_AWAITING_INTR(td) ||
127 TD_IS_SUSPENDED(td)) {
128 if (td->td_priority <= PZERO)
129 totalp->t_dw++;
130 else
131 totalp->t_sl++;
132 }
133 break;
134
135 case TDS_CAN_RUN:
136 totalp->t_sw++;
137 break;
138 case TDS_RUNQ:
139 case TDS_RUNNING:
140 totalp->t_rq++;
141 continue;
142 default:
143 break;
144 }
145 }
146 }
147 mtx_unlock_spin(&sched_lock);
148 /*
149 * Note active objects.
150 */
151 paging = 0;
152 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
153 entry != &map->header; entry = entry->next) {
154 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
155 entry->object.vm_object == NULL)
156 continue;
157 vm_object_set_flag(entry->object.vm_object, OBJ_ACTIVE);
158 paging |= entry->object.vm_object->paging_in_progress;
159 }
160 if (paging)
161 totalp->t_pw++;
162 }
163 sx_sunlock(&allproc_lock);
164 /*
165 * Calculate object memory usage statistics.
166 */
167 mtx_lock(&vm_object_list_mtx);
168 TAILQ_FOREACH(object, &vm_object_list, object_list) {
169 /*
170 * devices, like /dev/mem, will badly skew our totals
171 */
172 if (object->type == OBJT_DEVICE)
173 continue;
174 totalp->t_vm += object->size;
175 totalp->t_rm += object->resident_page_count;
176 if (object->flags & OBJ_ACTIVE) {
177 totalp->t_avm += object->size;
178 totalp->t_arm += object->resident_page_count;
179 }
180 if (object->shadow_count > 1) {
181 /* shared object */
182 totalp->t_vmshr += object->size;
183 totalp->t_rmshr += object->resident_page_count;
184 if (object->flags & OBJ_ACTIVE) {
185 totalp->t_avmshr += object->size;
186 totalp->t_armshr += object->resident_page_count;
187 }
188 }
189 }
190 mtx_unlock(&vm_object_list_mtx);
191 totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
192 return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
193 }
194
195 /*
196 * vcnt() - accumulate statistics from all cpus and the global cnt
197 * structure.
198 *
199 * The vmmeter structure is now per-cpu as well as global. Those
200 * statistics which can be kept on a per-cpu basis (to avoid cache
201 * stalls between cpus) can be moved to the per-cpu vmmeter. Remaining
202 * statistics, such as v_free_reserved, are left in the global
203 * structure.
204 *
205 * (sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req)
206 */
207 static int
208 vcnt(SYSCTL_HANDLER_ARGS)
209 {
210 int error = 0;
211 int count = *(int *)arg1;
212 #ifdef SMP
213 int i;
214 int offset = (char *)arg1 - (char *)&cnt;
215
216 for (i = 0; i < mp_ncpus; ++i) {
217 struct pcpu *pcpu = pcpu_find(i);
218 count += *(int *)((char *)&pcpu->pc_cnt + offset);
219 }
220 #endif
221 error = SYSCTL_OUT(req, &count, sizeof(int));
222 return(error);
223 }
224
225 SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
226 0, sizeof(struct vmtotal), vmtotal, "S,vmtotal",
227 "System virtual memory statistics");
228 SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
229 SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, "VM meter sys stats");
230 SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, "VM meter vm stats");
231 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
232
233 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_swtch, CTLTYPE_UINT|CTLFLAG_RD,
234 &cnt.v_swtch, 0, vcnt, "IU", "Context switches");
235 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_trap, CTLTYPE_UINT|CTLFLAG_RD,
236 &cnt.v_trap, 0, vcnt, "IU", "Traps");
237 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_syscall, CTLTYPE_UINT|CTLFLAG_RD,
238 &cnt.v_syscall, 0, vcnt, "IU", "Syscalls");
239 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intr, CTLTYPE_UINT|CTLFLAG_RD,
240 &cnt.v_intr, 0, vcnt, "IU", "Hardware interrupts");
241 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_soft, CTLTYPE_UINT|CTLFLAG_RD,
242 &cnt.v_soft, 0, vcnt, "IU", "Software interrupts");
243 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vm_faults, CTLTYPE_UINT|CTLFLAG_RD,
244 &cnt.v_vm_faults, 0, vcnt, "IU", "VM faults");
245 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_faults, CTLTYPE_UINT|CTLFLAG_RD,
246 &cnt.v_cow_faults, 0, vcnt, "IU", "COW faults");
247 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_optim, CTLTYPE_UINT|CTLFLAG_RD,
248 &cnt.v_cow_optim, 0, vcnt, "IU", "Optimized COW faults");
249 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_zfod, CTLTYPE_UINT|CTLFLAG_RD,
250 &cnt.v_zfod, 0, vcnt, "IU", "Zero fill");
251 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_ozfod, CTLTYPE_UINT|CTLFLAG_RD,
252 &cnt.v_ozfod, 0, vcnt, "IU", "Optimized zero fill");
253 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapin, CTLTYPE_UINT|CTLFLAG_RD,
254 &cnt.v_swapin, 0, vcnt, "IU", "Swapin operations");
255 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapout, CTLTYPE_UINT|CTLFLAG_RD,
256 &cnt.v_swapout, 0, vcnt, "IU", "Swapout operations");
257 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsin, CTLTYPE_UINT|CTLFLAG_RD,
258 &cnt.v_swappgsin, 0, vcnt, "IU", "Swapin pages");
259 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsout, CTLTYPE_UINT|CTLFLAG_RD,
260 &cnt.v_swappgsout, 0, vcnt, "IU", "Swapout pages");
261 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodein, CTLTYPE_UINT|CTLFLAG_RD,
262 &cnt.v_vnodein, 0, vcnt, "IU", "Vnodein operations");
263 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodeout, CTLTYPE_UINT|CTLFLAG_RD,
264 &cnt.v_vnodeout, 0, vcnt, "IU", "Vnodeout operations");
265 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsin, CTLTYPE_UINT|CTLFLAG_RD,
266 &cnt.v_vnodepgsin, 0, vcnt, "IU", "Vnodein pages");
267 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsout, CTLTYPE_UINT|CTLFLAG_RD,
268 &cnt.v_vnodepgsout, 0, vcnt, "IU", "Vnodeout pages");
269 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_intrans, CTLTYPE_UINT|CTLFLAG_RD,
270 &cnt.v_intrans, 0, vcnt, "IU", "In transit page blocking");
271 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_reactivated, CTLTYPE_UINT|CTLFLAG_RD,
272 &cnt.v_reactivated, 0, vcnt, "IU", "Reactivated pages");
273 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdwakeups, CTLTYPE_UINT|CTLFLAG_RD,
274 &cnt.v_pdwakeups, 0, vcnt, "IU", "Pagedaemon wakeups");
275 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdpages, CTLTYPE_UINT|CTLFLAG_RD,
276 &cnt.v_pdpages, 0, vcnt, "IU", "Pagedaemon page scans");
277 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_dfree, CTLTYPE_UINT|CTLFLAG_RD,
278 &cnt.v_dfree, 0, vcnt, "IU", "");
279 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pfree, CTLTYPE_UINT|CTLFLAG_RD,
280 &cnt.v_pfree, 0, vcnt, "IU", "");
281 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_tfree, CTLTYPE_UINT|CTLFLAG_RD,
282 &cnt.v_tfree, 0, vcnt, "IU", "");
283 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_page_size, CTLTYPE_UINT|CTLFLAG_RD,
284 &cnt.v_page_size, 0, vcnt, "IU", "");
285 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_page_count, CTLTYPE_UINT|CTLFLAG_RD,
286 &cnt.v_page_count, 0, vcnt, "IU", "");
287 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_reserved, CTLTYPE_UINT|CTLFLAG_RD,
288 &cnt.v_free_reserved, 0, vcnt, "IU", "");
289 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_target, CTLTYPE_UINT|CTLFLAG_RD,
290 &cnt.v_free_target, 0, vcnt, "IU", "");
291 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_min, CTLTYPE_UINT|CTLFLAG_RD,
292 &cnt.v_free_min, 0, vcnt, "IU", "");
293 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_count, CTLTYPE_UINT|CTLFLAG_RD,
294 &cnt.v_free_count, 0, vcnt, "IU", "");
295 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_wire_count, CTLTYPE_UINT|CTLFLAG_RD,
296 &cnt.v_wire_count, 0, vcnt, "IU", "");
297 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_active_count, CTLTYPE_UINT|CTLFLAG_RD,
298 &cnt.v_active_count, 0, vcnt, "IU", "");
299 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_inactive_target, CTLTYPE_UINT|CTLFLAG_RD,
300 &cnt.v_inactive_target, 0, vcnt, "IU", "");
301 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_inactive_count, CTLTYPE_UINT|CTLFLAG_RD,
302 &cnt.v_inactive_count, 0, vcnt, "IU", "");
303 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cache_count, CTLTYPE_UINT|CTLFLAG_RD,
304 &cnt.v_cache_count, 0, vcnt, "IU", "");
305 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cache_min, CTLTYPE_UINT|CTLFLAG_RD,
306 &cnt.v_cache_min, 0, vcnt, "IU", "");
307 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cache_max, CTLTYPE_UINT|CTLFLAG_RD,
308 &cnt.v_cache_max, 0, vcnt, "IU", "");
309 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pageout_free_min, CTLTYPE_UINT|CTLFLAG_RD,
310 &cnt.v_pageout_free_min, 0, vcnt, "IU", "");
311 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_interrupt_free_min, CTLTYPE_UINT|CTLFLAG_RD,
312 &cnt.v_interrupt_free_min, 0, vcnt, "IU", "");
313 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forks, CTLTYPE_UINT|CTLFLAG_RD,
314 &cnt.v_forks, 0, vcnt, "IU", "Number of fork() calls");
315 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforks, CTLTYPE_UINT|CTLFLAG_RD,
316 &cnt.v_vforks, 0, vcnt, "IU", "Number of vfork() calls");
317 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforks, CTLTYPE_UINT|CTLFLAG_RD,
318 &cnt.v_rforks, 0, vcnt, "IU", "Number of rfork() calls");
319 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreads, CTLTYPE_UINT|CTLFLAG_RD,
320 &cnt.v_kthreads, 0, vcnt, "IU", "Number of fork() calls by kernel");
321 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forkpages, CTLTYPE_UINT|CTLFLAG_RD,
322 &cnt.v_forkpages, 0, vcnt, "IU", "VM pages affected by fork()");
323 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforkpages, CTLTYPE_UINT|CTLFLAG_RD,
324 &cnt.v_vforkpages, 0, vcnt, "IU", "VM pages affected by vfork()");
325 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforkpages, CTLTYPE_UINT|CTLFLAG_RD,
326 &cnt.v_rforkpages, 0, vcnt, "IU", "VM pages affected by rfork()");
327 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreadpages, CTLTYPE_UINT|CTLFLAG_RD,
328 &cnt.v_kthreadpages, 0, vcnt, "IU", "VM pages affected by fork() by kernel");
329
330 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
331 zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0, "");
332 #if 0
333 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
334 page_mask, CTLFLAG_RD, &page_mask, 0, "");
335 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
336 page_shift, CTLFLAG_RD, &page_shift, 0, "");
337 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
338 first_page, CTLFLAG_RD, &first_page, 0, "");
339 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
340 last_page, CTLFLAG_RD, &last_page, 0, "");
341 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
342 vm_page_bucket_count, CTLFLAG_RD, &vm_page_bucket_count, 0, "");
343 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
344 vm_page_hash_mask, CTLFLAG_RD, &vm_page_hash_mask, 0, "");
345 #endif
Cache object: 71a64fe45940b5cf5bc5935581533bf8
|