uvm_meter.c revision 1.15 1 /* $NetBSD: uvm_meter.c,v 1.15 2000/11/29 09:52:19 simonb 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 <uvm/uvm_extern.h>
48 #include <sys/sysctl.h>
49
50 /*
51 * maxslp: ???? XXXCDC
52 */
53
54 int maxslp = MAXSLP; /* patchable ... */
55 struct loadavg averunnable;
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 static int sysctl_uvmexp __P((void *, size_t *));
75
76 /*
77 * uvm_meter: calculate load average and wake up the swapper (if needed)
78 */
79 void
80 uvm_meter()
81 {
82 if ((time.tv_sec % 5) == 0)
83 uvm_loadav(&averunnable);
84 if (proc0.p_slptime > (maxslp / 2))
85 wakeup(&proc0);
86 }
87
88 /*
89 * uvm_loadav: compute a tenex style load average of a quantity on
90 * 1, 5, and 15 minute internvals.
91 */
92 static void
93 uvm_loadav(avg)
94 struct loadavg *avg;
95 {
96 int i, nrun;
97 struct proc *p;
98
99 proclist_lock_read();
100 nrun = 0;
101 LIST_FOREACH(p, &allproc, p_list) {
102 switch (p->p_stat) {
103 case SSLEEP:
104 if (p->p_priority > PZERO || p->p_slptime > 1)
105 continue;
106 /* fall through */
107 case SRUN:
108 case SONPROC:
109 case SIDL:
110 nrun++;
111 }
112 }
113 proclist_unlock_read();
114 for (i = 0; i < 3; i++)
115 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
116 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
117 }
118
119 /*
120 * uvm_sysctl: sysctl hook into UVM system.
121 */
122 int
123 uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
124 int *name;
125 u_int namelen;
126 void *oldp;
127 size_t *oldlenp;
128 void *newp;
129 size_t newlen;
130 struct proc *p;
131 {
132 struct vmtotal vmtotals;
133
134 /* all sysctl names at this level are terminal */
135 if (namelen != 1)
136 return (ENOTDIR); /* overloaded */
137
138 switch (name[0]) {
139 case VM_LOADAVG:
140 return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
141 sizeof(averunnable)));
142
143 case VM_METER:
144 uvm_total(&vmtotals);
145 return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
146 sizeof(vmtotals)));
147
148 case VM_UVMEXP:
149 return (sysctl_rdminstruct(oldp, oldlenp, newp, &uvmexp,
150 sizeof(uvmexp)));
151 case VM_UVMEXP2:
152 if (newp)
153 return (EPERM);
154 return (sysctl_uvmexp(oldp, oldlenp));
155
156 case VM_NKMEMPAGES:
157 return (sysctl_rdint(oldp, oldlenp, newp, nkmempages));
158
159 default:
160 return (EOPNOTSUPP);
161 }
162 /* NOTREACHED */
163 }
164
165 static int
166 sysctl_uvmexp(oldp, oldlenp)
167 void *oldp;
168 size_t *oldlenp;
169 {
170 struct uvmexp_sysctl u;
171
172 u.pagesize = uvmexp.pagesize;
173 u.pagemask = uvmexp.pagemask;
174 u.pageshift = uvmexp.pageshift;
175 u.npages = uvmexp.npages;
176 u.free = uvmexp.free;
177 u.active = uvmexp.active;
178 u.inactive = uvmexp.inactive;
179 u.paging = uvmexp.paging;
180 u.wired = uvmexp.wired;
181 u.zeropages = uvmexp.zeropages;
182 u.reserve_pagedaemon = uvmexp.reserve_pagedaemon;
183 u.reserve_kernel = uvmexp.reserve_kernel;
184 u.freemin = uvmexp.freemin;
185 u.freetarg = uvmexp.freetarg;
186 u.inactarg = uvmexp.inactarg;
187 u.wiredmax = uvmexp.wiredmax;
188 u.nswapdev = uvmexp.nswapdev;
189 u.swpages = uvmexp.swpages;
190 u.swpguniq = uvmexp.swpguniq;
191 u.swpginuse = uvmexp.swpginuse;
192 u.swpgonly = uvmexp.swpgonly;
193 u.nswget = uvmexp.nswget;
194 u.nanon = uvmexp.nanon;
195 u.nanonneeded = uvmexp.nanonneeded;
196 u.nfreeanon = uvmexp.nfreeanon;
197 u.faults = uvmexp.faults;
198 u.traps = uvmexp.traps;
199 u.intrs = uvmexp.intrs;
200 u.swtch = uvmexp.swtch;
201 u.softs = uvmexp.softs;
202 u.syscalls = uvmexp.syscalls;
203 u.pageins = uvmexp.pageins;
204 u.swapins = uvmexp.swapins;
205 u.swapouts = uvmexp.swapouts;
206 u.pgswapin = uvmexp.pgswapin;
207 u.pgswapout = uvmexp.pgswapout;
208 u.forks = uvmexp.forks;
209 u.forks_ppwait = uvmexp.forks_ppwait;
210 u.forks_sharevm = uvmexp.forks_sharevm;
211 u.pga_zerohit = uvmexp.pga_zerohit;
212 u.pga_zeromiss = uvmexp.pga_zeromiss;
213 u.zeroaborts = uvmexp.zeroaborts;
214 u.fltnoram = uvmexp.fltnoram;
215 u.fltnoanon = uvmexp.fltnoanon;
216 u.fltpgwait = uvmexp.fltpgwait;
217 u.fltpgrele = uvmexp.fltpgrele;
218 u.fltrelck = uvmexp.fltrelck;
219 u.fltrelckok = uvmexp.fltrelckok;
220 u.fltanget = uvmexp.fltanget;
221 u.fltanretry = uvmexp.fltanretry;
222 u.fltamcopy = uvmexp.fltamcopy;
223 u.fltnamap = uvmexp.fltnamap;
224 u.fltnomap = uvmexp.fltnomap;
225 u.fltlget = uvmexp.fltlget;
226 u.fltget = uvmexp.fltget;
227 u.flt_anon = uvmexp.flt_anon;
228 u.flt_acow = uvmexp.flt_acow;
229 u.flt_obj = uvmexp.flt_obj;
230 u.flt_prcopy = uvmexp.flt_prcopy;
231 u.flt_przero = uvmexp.flt_przero;
232 u.pdwoke = uvmexp.pdwoke;
233 u.pdrevs = uvmexp.pdrevs;
234 u.pdswout = uvmexp.pdswout;
235 u.pdfreed = uvmexp.pdfreed;
236 u.pdscans = uvmexp.pdscans;
237 u.pdanscan = uvmexp.pdanscan;
238 u.pdobscan = uvmexp.pdobscan;
239 u.pdreact = uvmexp.pdreact;
240 u.pdbusy = uvmexp.pdbusy;
241 u.pdpageouts = uvmexp.pdpageouts;
242 u.pdpending = uvmexp.pdpending;
243 u.pddeact = uvmexp.pddeact;
244
245 return (sysctl_rdminstruct(oldp, oldlenp, NULL, &u, sizeof(u)));
246 }
247
248 /*
249 * uvm_total: calculate the current state of the system.
250 */
251 static void
252 uvm_total(totalp)
253 struct vmtotal *totalp;
254 {
255 struct proc *p;
256 #if 0
257 vm_map_entry_t entry;
258 vm_map_t map;
259 int paging;
260 #endif
261
262 memset(totalp, 0, sizeof *totalp);
263
264 /*
265 * calculate process statistics
266 */
267
268 proclist_lock_read();
269 LIST_FOREACH(p, &allproc, p_list) {
270 if (p->p_flag & P_SYSTEM)
271 continue;
272 switch (p->p_stat) {
273 case 0:
274 continue;
275
276 case SSLEEP:
277 case SSTOP:
278 if (p->p_flag & P_INMEM) {
279 if (p->p_priority <= PZERO)
280 totalp->t_dw++;
281 else if (p->p_slptime < maxslp)
282 totalp->t_sl++;
283 } else if (p->p_slptime < maxslp)
284 totalp->t_sw++;
285 if (p->p_slptime >= maxslp)
286 continue;
287 break;
288
289 case SRUN:
290 case SONPROC:
291 case SIDL:
292 if (p->p_flag & P_INMEM)
293 totalp->t_rq++;
294 else
295 totalp->t_sw++;
296 if (p->p_stat == SIDL)
297 continue;
298 break;
299 }
300 /*
301 * note active objects
302 */
303 #if 0
304 /*
305 * XXXCDC: BOGUS! rethink this. in the mean time
306 * don't do it.
307 */
308 paging = 0;
309 vm_map_lock(map);
310 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
311 entry != &map->header; entry = entry->next) {
312 if (entry->is_a_map || entry->is_sub_map ||
313 entry->object.uvm_obj == NULL)
314 continue;
315 /* XXX how to do this with uvm */
316 }
317 vm_map_unlock(map);
318 if (paging)
319 totalp->t_pw++;
320 #endif
321 }
322 proclist_unlock_read();
323 /*
324 * Calculate object memory usage statistics.
325 */
326 totalp->t_free = uvmexp.free;
327 totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
328 totalp->t_avm = uvmexp.active + uvmexp.swpginuse; /* XXX */
329 totalp->t_rm = uvmexp.npages - uvmexp.free;
330 totalp->t_arm = uvmexp.active;
331 totalp->t_vmshr = 0; /* XXX */
332 totalp->t_avmshr = 0; /* XXX */
333 totalp->t_rmshr = 0; /* XXX */
334 totalp->t_armshr = 0; /* XXX */
335 }
336