Initial import from FreeBSD RELENG_4:
[dragonfly.git] / 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: src/sys/vm/vm_meter.c,v 1.34.2.7 2002/10/10 19:28:22 dillon Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/proc.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/resource.h>
42 #include <sys/vmmeter.h>
43
44 #include <vm/vm.h>
45 #include <vm/vm_page.h>
46 #include <vm/vm_extern.h>
47 #include <vm/vm_param.h>
48 #include <sys/lock.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_object.h>
52 #include <sys/sysctl.h>
53
54 struct vmmeter cnt;
55
56 static int maxslp = MAXSLP;
57
58 SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
59         CTLFLAG_RW, &cnt.v_free_min, 0, "");
60 SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
61         CTLFLAG_RW, &cnt.v_free_target, 0, "");
62 SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
63         CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
64 SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
65         CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
66 SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
67         CTLFLAG_RW, &cnt.v_cache_min, 0, "");
68 SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
69         CTLFLAG_RW, &cnt.v_cache_max, 0, "");
70 SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
71         CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
72 SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
73         CTLFLAG_RW, &cnt.v_free_severe, 0, "");
74 SYSCTL_UINT(_vm, OID_AUTO, v_intrans_coll,
75         CTLFLAG_RW, &cnt.v_intrans_coll, 0, "");
76 SYSCTL_UINT(_vm, OID_AUTO, v_intrans_wait,
77         CTLFLAG_RW, &cnt.v_intrans_wait, 0, "");
78
79 SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD, 
80     &averunnable, loadavg, "Machine loadaverage history");
81
82 static int
83 vmtotal(SYSCTL_HANDLER_ARGS)
84 {
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
92         totalp = &total;
93         bzero(totalp, sizeof *totalp);
94         /*
95          * Mark all objects as inactive.
96          */
97         for (object = TAILQ_FIRST(&vm_object_list);
98             object != NULL;
99             object = TAILQ_NEXT(object,object_list))
100                 vm_object_clear_flag(object, OBJ_ACTIVE);
101         /*
102          * Calculate process statistics.
103          */
104         for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
105                 if (p->p_flag & P_SYSTEM)
106                         continue;
107                 switch (p->p_stat) {
108                 case 0:
109                         continue;
110
111                 case SSLEEP:
112                 case SSTOP:
113                         if (p->p_flag & P_INMEM) {
114                                 if (p->p_priority <= PZERO)
115                                         totalp->t_dw++;
116                                 else if (p->p_slptime < maxslp)
117                                         totalp->t_sl++;
118                         } else if (p->p_slptime < maxslp)
119                                 totalp->t_sw++;
120                         if (p->p_slptime >= maxslp)
121                                 continue;
122                         break;
123
124                 case SRUN:
125                 case SIDL:
126                         if (p->p_flag & P_INMEM)
127                                 totalp->t_rq++;
128                         else
129                                 totalp->t_sw++;
130                         if (p->p_stat == SIDL)
131                                 continue;
132                         break;
133                 }
134                 /*
135                  * Note active objects.
136                  */
137                 paging = 0;
138                 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
139                     entry != &map->header; entry = entry->next) {
140                         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
141                             entry->object.vm_object == NULL)
142                                 continue;
143                         vm_object_set_flag(entry->object.vm_object, OBJ_ACTIVE);
144                         paging |= entry->object.vm_object->paging_in_progress;
145                 }
146                 if (paging)
147                         totalp->t_pw++;
148         }
149         /*
150          * Calculate object memory usage statistics.
151          */
152         for (object = TAILQ_FIRST(&vm_object_list);
153             object != NULL;
154             object = TAILQ_NEXT(object, object_list)) {
155                 /*
156                  * devices, like /dev/mem, will badly skew our totals
157                  */
158                 if (object->type == OBJT_DEVICE)
159                         continue;
160                 totalp->t_vm += object->size;
161                 totalp->t_rm += object->resident_page_count;
162                 if (object->flags & OBJ_ACTIVE) {
163                         totalp->t_avm += object->size;
164                         totalp->t_arm += object->resident_page_count;
165                 }
166                 if (object->shadow_count > 1) {
167                         /* shared object */
168                         totalp->t_vmshr += object->size;
169                         totalp->t_rmshr += object->resident_page_count;
170                         if (object->flags & OBJ_ACTIVE) {
171                                 totalp->t_avmshr += object->size;
172                                 totalp->t_armshr += object->resident_page_count;
173                         }
174                 }
175         }
176         totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
177         return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
178 }
179
180 SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
181     0, sizeof(struct vmtotal), vmtotal, "S,vmtotal", 
182     "System virtual memory statistics");
183 SYSCTL_NODE(_vm, OID_AUTO, stats, CTLFLAG_RW, 0, "VM meter stats");
184 SYSCTL_NODE(_vm_stats, OID_AUTO, sys, CTLFLAG_RW, 0, "VM meter sys stats");
185 SYSCTL_NODE(_vm_stats, OID_AUTO, vm, CTLFLAG_RW, 0, "VM meter vm stats");
186 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
187 SYSCTL_UINT(_vm_stats_sys, OID_AUTO,
188         v_swtch, CTLFLAG_RD, &cnt.v_swtch, 0, "Context switches");
189 SYSCTL_UINT(_vm_stats_sys, OID_AUTO,
190         v_trap, CTLFLAG_RD, &cnt.v_trap, 0, "Traps");
191 SYSCTL_UINT(_vm_stats_sys, OID_AUTO,
192         v_syscall, CTLFLAG_RD, &cnt.v_syscall, 0, "Syscalls");
193 SYSCTL_UINT(_vm_stats_sys, OID_AUTO, v_intr, CTLFLAG_RD,
194     &cnt.v_intr, 0, "Hardware interrupts");
195 SYSCTL_UINT(_vm_stats_sys, OID_AUTO, v_soft, CTLFLAG_RD, 
196     &cnt.v_soft, 0, "Software interrupts");
197 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
198         v_vm_faults, CTLFLAG_RD, &cnt.v_vm_faults, 0, "VM faults");
199 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
200         v_cow_faults, CTLFLAG_RD, &cnt.v_cow_faults, 0, "COW faults");
201 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
202         v_cow_optim, CTLFLAG_RD, &cnt.v_cow_optim, 0, "Optimized COW faults");
203 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
204         v_zfod, CTLFLAG_RD, &cnt.v_zfod, 0, "Zero fill");
205 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
206         v_ozfod, CTLFLAG_RD, &cnt.v_ozfod, 0, "Optimized zero fill");
207 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
208         v_swapin, CTLFLAG_RD, &cnt.v_swapin, 0, "Swapin operations");
209 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
210         v_swapout, CTLFLAG_RD, &cnt.v_swapout, 0, "Swapout operations");
211 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
212         v_swappgsin, CTLFLAG_RD, &cnt.v_swappgsin, 0, "Swapin pages");
213 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
214         v_swappgsout, CTLFLAG_RD, &cnt.v_swappgsout, 0, "Swapout pages");
215 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
216         v_vnodein, CTLFLAG_RD, &cnt.v_vnodein, 0, "Vnodein operations");
217 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
218         v_vnodeout, CTLFLAG_RD, &cnt.v_vnodeout, 0, "Vnodeout operations");
219 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
220         v_vnodepgsin, CTLFLAG_RD, &cnt.v_vnodepgsin, 0, "Vnodein pages");
221 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
222         v_vnodepgsout, CTLFLAG_RD, &cnt.v_vnodepgsout, 0, "Vnodeout pages");
223 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
224         v_intrans, CTLFLAG_RD, &cnt.v_intrans, 0, "In transit page blocking");
225 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
226         v_reactivated, CTLFLAG_RD, &cnt.v_reactivated, 0, "Reactivated pages");
227 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
228         v_pdwakeups, CTLFLAG_RD, &cnt.v_pdwakeups, 0, "Pagedaemon wakeups");
229 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
230         v_pdpages, CTLFLAG_RD, &cnt.v_pdpages, 0, "Pagedaemon page scans");
231 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
232         v_dfree, CTLFLAG_RD, &cnt.v_dfree, 0, "");
233 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
234         v_pfree, CTLFLAG_RD, &cnt.v_pfree, 0, "");
235 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
236         v_tfree, CTLFLAG_RD, &cnt.v_tfree, 0, "");
237 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
238         v_page_size, CTLFLAG_RD, &cnt.v_page_size, 0, "");
239 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
240         v_page_count, CTLFLAG_RD, &cnt.v_page_count, 0, "");
241 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
242         v_free_reserved, CTLFLAG_RD, &cnt.v_free_reserved, 0, "");
243 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
244         v_free_target, CTLFLAG_RD, &cnt.v_free_target, 0, "");
245 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
246         v_free_min, CTLFLAG_RD, &cnt.v_free_min, 0, "");
247 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
248         v_free_count, CTLFLAG_RD, &cnt.v_free_count, 0, "");
249 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
250         v_wire_count, CTLFLAG_RD, &cnt.v_wire_count, 0, "");
251 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
252         v_active_count, CTLFLAG_RD, &cnt.v_active_count, 0, "");
253 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
254         v_inactive_target, CTLFLAG_RD, &cnt.v_inactive_target, 0, "");
255 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
256         v_inactive_count, CTLFLAG_RD, &cnt.v_inactive_count, 0, "");
257 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
258         v_cache_count, CTLFLAG_RD, &cnt.v_cache_count, 0, "");
259 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
260         v_cache_min, CTLFLAG_RD, &cnt.v_cache_min, 0, "");
261 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
262         v_cache_max, CTLFLAG_RD, &cnt.v_cache_max, 0, "");
263 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
264         v_pageout_free_min, CTLFLAG_RD, &cnt.v_pageout_free_min, 0, "");
265 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
266         v_interrupt_free_min, CTLFLAG_RD, &cnt.v_interrupt_free_min, 0, "");
267 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
268         zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0, "");
269 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
270         v_forks, CTLFLAG_RD, &cnt.v_forks, 0, "Number of fork() calls");
271 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
272         v_vforks, CTLFLAG_RD, &cnt.v_vforks, 0, "Number of vfork() calls");
273 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
274         v_rforks, CTLFLAG_RD, &cnt.v_rforks, 0, "Number of rfork() calls");
275 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
276         v_kthreads, CTLFLAG_RD, &cnt.v_kthreads, 0, "Number of fork() calls by kernel");
277 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
278         v_forkpages, CTLFLAG_RD, &cnt.v_forkpages, 0, "VM pages affected by fork()");
279 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
280         v_vforkpages, CTLFLAG_RD, &cnt.v_vforkpages, 0, "VM pages affected by vfork()");
281 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
282         v_rforkpages, CTLFLAG_RD, &cnt.v_rforkpages, 0, "VM pages affected by rfork()");
283 SYSCTL_UINT(_vm_stats_vm, OID_AUTO,
284         v_kthreadpages, CTLFLAG_RD, &cnt.v_kthreadpages, 0, "VM pages affected by fork() by kernel");
285 #if 0
286 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
287         page_mask, CTLFLAG_RD, &page_mask, 0, "");
288 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
289         page_shift, CTLFLAG_RD, &page_shift, 0, "");
290 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
291         first_page, CTLFLAG_RD, &first_page, 0, "");
292 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
293         last_page, CTLFLAG_RD, &last_page, 0, "");
294 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
295         vm_page_bucket_count, CTLFLAG_RD, &vm_page_bucket_count, 0, "");
296 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
297         vm_page_hash_mask, CTLFLAG_RD, &vm_page_hash_mask, 0, "");
298 #endif