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