uvm_meter.c revision 1.11 1 /* $NetBSD: uvm_meter.c,v 1.11 2000/02/11 19:22:54 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1982, 1986, 1989, 1993
6 * The Regents of the University of California.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Charles D. Cranor,
21 * Washington University, and the University of California, Berkeley
22 * and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94
40 * from: Id: uvm_meter.c,v 1.1.2.1 1997/08/14 19:10:35 chuck Exp
41 */
42
43 #include <sys/param.h>
44 #include <sys/proc.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <vm/vm.h>
48 #include <sys/sysctl.h>
49
50 /*
51 * maxslp: ???? XXXCDC
52 */
53
54 int maxslp = MAXSLP; /* patchable ... */
55 struct loadavg averunnable; /* decl. */
56
57 /*
58 * constants for averages over 1, 5, and 15 minutes when sampling at
59 * 5 second intervals.
60 */
61
62 static fixpt_t cexp[3] = {
63 0.9200444146293232 * FSCALE, /* exp(-1/12) */
64 0.9834714538216174 * FSCALE, /* exp(-1/60) */
65 0.9944598480048967 * FSCALE, /* exp(-1/180) */
66 };
67
68 /*
69 * prototypes
70 */
71
72 static void uvm_loadav __P((struct loadavg *));
73 static void uvm_total __P((struct vmtotal *));
74
75 /*
76 * uvm_meter: calculate load average and wake up the swapper (if needed)
77 */
78 void
79 uvm_meter()
80 {
81 if ((time.tv_sec % 5) == 0)
82 uvm_loadav(&averunnable);
83 if (proc0.p_slptime > (maxslp / 2))
84 wakeup((caddr_t)&proc0);
85 }
86
87 /*
88 * uvm_loadav: compute a tenex style load average of a quantity on
89 * 1, 5, and 15 minute internvals.
90 */
91 static void
92 uvm_loadav(avg)
93 struct loadavg *avg;
94 {
95 int i, nrun;
96 struct proc *p;
97
98 proclist_lock_read();
99 for (nrun = 0, p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
100 switch (p->p_stat) {
101 case SSLEEP:
102 if (p->p_priority > PZERO || p->p_slptime > 1)
103 continue;
104 /* fall through */
105 case SRUN:
106 case SIDL:
107 nrun++;
108 }
109 }
110 proclist_unlock_read();
111 for (i = 0; i < 3; i++)
112 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
113 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
114 }
115
116 /*
117 * uvm_sysctl: sysctl hook into UVM system.
118 */
119 int
120 uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
121 int *name;
122 u_int namelen;
123 void *oldp;
124 size_t *oldlenp;
125 void *newp;
126 size_t newlen;
127 struct proc *p;
128 {
129 struct vmtotal vmtotals;
130
131 /* all sysctl names at this level are terminal */
132 if (namelen != 1)
133 return (ENOTDIR); /* overloaded */
134
135 switch (name[0]) {
136 case VM_LOADAVG:
137 return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
138 sizeof(averunnable)));
139
140 case VM_METER:
141 uvm_total(&vmtotals);
142 return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
143 sizeof(vmtotals)));
144
145 case VM_UVMEXP:
146 return (sysctl_rdstruct(oldp, oldlenp, newp, &uvmexp,
147 sizeof(uvmexp)));
148
149 case VM_NKMEMPAGES:
150 return (sysctl_rdint(oldp, oldlenp, newp, nkmempages));
151
152 default:
153 return (EOPNOTSUPP);
154 }
155 /* NOTREACHED */
156 }
157
158 /*
159 * uvm_total: calculate the current state of the system.
160 */
161 static void
162 uvm_total(totalp)
163 struct vmtotal *totalp;
164 {
165 struct proc *p;
166 #if 0
167 vm_map_entry_t entry;
168 vm_map_t map;
169 int paging;
170 #endif
171
172 memset(totalp, 0, sizeof *totalp);
173
174 /*
175 * calculate process statistics
176 */
177
178 proclist_lock_read();
179 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
180 if (p->p_flag & P_SYSTEM)
181 continue;
182 switch (p->p_stat) {
183 case 0:
184 continue;
185
186 case SSLEEP:
187 case SSTOP:
188 if (p->p_flag & P_INMEM) {
189 if (p->p_priority <= PZERO)
190 totalp->t_dw++;
191 else if (p->p_slptime < maxslp)
192 totalp->t_sl++;
193 } else if (p->p_slptime < maxslp)
194 totalp->t_sw++;
195 if (p->p_slptime >= maxslp)
196 continue;
197 break;
198
199 case SRUN:
200 case SIDL:
201 if (p->p_flag & P_INMEM)
202 totalp->t_rq++;
203 else
204 totalp->t_sw++;
205 if (p->p_stat == SIDL)
206 continue;
207 break;
208 }
209 /*
210 * note active objects
211 */
212 #if 0
213 /*
214 * XXXCDC: BOGUS! rethink this. in the mean time
215 * don't do it.
216 */
217 paging = 0;
218 vm_map_lock(map);
219 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
220 entry != &map->header; entry = entry->next) {
221 if (entry->is_a_map || entry->is_sub_map ||
222 entry->object.uvm_obj == NULL)
223 continue;
224 /* XXX how to do this with uvm */
225 }
226 vm_map_unlock(map);
227 if (paging)
228 totalp->t_pw++;
229 #endif
230 }
231 proclist_unlock_read();
232 /*
233 * Calculate object memory usage statistics.
234 */
235 totalp->t_free = uvmexp.free;
236 totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
237 totalp->t_avm = uvmexp.active + uvmexp.swpginuse; /* XXX */
238 totalp->t_rm = uvmexp.npages - uvmexp.free;
239 totalp->t_arm = uvmexp.active;
240 totalp->t_vmshr = 0; /* XXX */
241 totalp->t_avmshr = 0; /* XXX */
242 totalp->t_rmshr = 0; /* XXX */
243 totalp->t_armshr = 0; /* XXX */
244 }
245