uvm_meter.c revision 1.23 1 1.23 chs /* $NetBSD: uvm_meter.c,v 1.23 2001/12/09 03:07:19 chs Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.1 mrg * Copyright (c) 1982, 1986, 1989, 1993
6 1.19 chs * The Regents of the University of California.
7 1.1 mrg *
8 1.1 mrg * All rights reserved.
9 1.1 mrg *
10 1.1 mrg * Redistribution and use in source and binary forms, with or without
11 1.1 mrg * modification, are permitted provided that the following conditions
12 1.1 mrg * are met:
13 1.1 mrg * 1. Redistributions of source code must retain the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer.
15 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mrg * notice, this list of conditions and the following disclaimer in the
17 1.1 mrg * documentation and/or other materials provided with the distribution.
18 1.1 mrg * 3. All advertising materials mentioning features or use of this software
19 1.1 mrg * must display the following acknowledgement:
20 1.1 mrg * This product includes software developed by Charles D. Cranor,
21 1.19 chs * Washington University, and the University of California, Berkeley
22 1.1 mrg * and its contributors.
23 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
24 1.1 mrg * may be used to endorse or promote products derived from this software
25 1.1 mrg * without specific prior written permission.
26 1.1 mrg *
27 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 mrg * SUCH DAMAGE.
38 1.1 mrg *
39 1.1 mrg * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94
40 1.3 mrg * from: Id: uvm_meter.c,v 1.1.2.1 1997/08/14 19:10:35 chuck Exp
41 1.1 mrg */
42 1.22 lukem
43 1.22 lukem #include <sys/cdefs.h>
44 1.23 chs __KERNEL_RCSID(0, "$NetBSD: uvm_meter.c,v 1.23 2001/12/09 03:07:19 chs Exp $");
45 1.1 mrg
46 1.1 mrg #include <sys/param.h>
47 1.1 mrg #include <sys/proc.h>
48 1.1 mrg #include <sys/systm.h>
49 1.1 mrg #include <sys/kernel.h>
50 1.13 mrg #include <uvm/uvm_extern.h>
51 1.1 mrg #include <sys/sysctl.h>
52 1.1 mrg
53 1.1 mrg /*
54 1.1 mrg * maxslp: ???? XXXCDC
55 1.1 mrg */
56 1.1 mrg
57 1.1 mrg int maxslp = MAXSLP; /* patchable ... */
58 1.14 chs struct loadavg averunnable;
59 1.1 mrg
60 1.1 mrg /*
61 1.19 chs * constants for averages over 1, 5, and 15 minutes when sampling at
62 1.1 mrg * 5 second intervals.
63 1.1 mrg */
64 1.1 mrg
65 1.1 mrg static fixpt_t cexp[3] = {
66 1.1 mrg 0.9200444146293232 * FSCALE, /* exp(-1/12) */
67 1.1 mrg 0.9834714538216174 * FSCALE, /* exp(-1/60) */
68 1.1 mrg 0.9944598480048967 * FSCALE, /* exp(-1/180) */
69 1.1 mrg };
70 1.1 mrg
71 1.1 mrg /*
72 1.1 mrg * prototypes
73 1.1 mrg */
74 1.1 mrg
75 1.1 mrg static void uvm_loadav __P((struct loadavg *));
76 1.1 mrg static void uvm_total __P((struct vmtotal *));
77 1.15 simonb static int sysctl_uvmexp __P((void *, size_t *));
78 1.1 mrg
79 1.1 mrg /*
80 1.1 mrg * uvm_meter: calculate load average and wake up the swapper (if needed)
81 1.1 mrg */
82 1.4 mrg void
83 1.4 mrg uvm_meter()
84 1.1 mrg {
85 1.4 mrg if ((time.tv_sec % 5) == 0)
86 1.4 mrg uvm_loadav(&averunnable);
87 1.4 mrg if (proc0.p_slptime > (maxslp / 2))
88 1.14 chs wakeup(&proc0);
89 1.1 mrg }
90 1.1 mrg
91 1.1 mrg /*
92 1.19 chs * uvm_loadav: compute a tenex style load average of a quantity on
93 1.1 mrg * 1, 5, and 15 minute internvals.
94 1.1 mrg */
95 1.4 mrg static void
96 1.4 mrg uvm_loadav(avg)
97 1.4 mrg struct loadavg *avg;
98 1.1 mrg {
99 1.4 mrg int i, nrun;
100 1.4 mrg struct proc *p;
101 1.1 mrg
102 1.10 thorpej proclist_lock_read();
103 1.14 chs nrun = 0;
104 1.14 chs LIST_FOREACH(p, &allproc, p_list) {
105 1.4 mrg switch (p->p_stat) {
106 1.4 mrg case SSLEEP:
107 1.4 mrg if (p->p_priority > PZERO || p->p_slptime > 1)
108 1.4 mrg continue;
109 1.4 mrg /* fall through */
110 1.4 mrg case SRUN:
111 1.12 thorpej case SONPROC:
112 1.4 mrg case SIDL:
113 1.4 mrg nrun++;
114 1.4 mrg }
115 1.4 mrg }
116 1.9 thorpej proclist_unlock_read();
117 1.4 mrg for (i = 0; i < 3; i++)
118 1.4 mrg avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
119 1.4 mrg nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
120 1.1 mrg }
121 1.1 mrg
122 1.1 mrg /*
123 1.1 mrg * uvm_sysctl: sysctl hook into UVM system.
124 1.1 mrg */
125 1.4 mrg int
126 1.4 mrg uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
127 1.4 mrg int *name;
128 1.4 mrg u_int namelen;
129 1.4 mrg void *oldp;
130 1.4 mrg size_t *oldlenp;
131 1.4 mrg void *newp;
132 1.4 mrg size_t newlen;
133 1.4 mrg struct proc *p;
134 1.1 mrg {
135 1.4 mrg struct vmtotal vmtotals;
136 1.17 chs int rv, t;
137 1.1 mrg
138 1.4 mrg /* all sysctl names at this level are terminal */
139 1.4 mrg if (namelen != 1)
140 1.4 mrg return (ENOTDIR); /* overloaded */
141 1.4 mrg
142 1.4 mrg switch (name[0]) {
143 1.4 mrg case VM_LOADAVG:
144 1.4 mrg return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
145 1.4 mrg sizeof(averunnable)));
146 1.4 mrg
147 1.4 mrg case VM_METER:
148 1.4 mrg uvm_total(&vmtotals);
149 1.4 mrg return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
150 1.4 mrg sizeof(vmtotals)));
151 1.4 mrg
152 1.4 mrg case VM_UVMEXP:
153 1.15 simonb return (sysctl_rdminstruct(oldp, oldlenp, newp, &uvmexp,
154 1.4 mrg sizeof(uvmexp)));
155 1.15 simonb case VM_UVMEXP2:
156 1.15 simonb if (newp)
157 1.15 simonb return (EPERM);
158 1.15 simonb return (sysctl_uvmexp(oldp, oldlenp));
159 1.11 thorpej
160 1.11 thorpej case VM_NKMEMPAGES:
161 1.11 thorpej return (sysctl_rdint(oldp, oldlenp, newp, nkmempages));
162 1.17 chs
163 1.23 chs #define UPDATEMIN(a, ap, bp, cp) \
164 1.23 chs { \
165 1.23 chs t = uvmexp.ap; \
166 1.23 chs rv = sysctl_int(oldp, oldlenp, newp, newlen, &t); \
167 1.23 chs if (rv) { \
168 1.23 chs return rv; \
169 1.23 chs } \
170 1.23 chs if (t + uvmexp.bp + uvmexp.cp > 95 || t < 0) { \
171 1.23 chs return EINVAL; \
172 1.23 chs } \
173 1.23 chs uvmexp.ap = t; \
174 1.23 chs uvmexp.a = t * 256 / 100; \
175 1.23 chs return rv; \
176 1.23 chs }
177 1.23 chs
178 1.17 chs case VM_ANONMIN:
179 1.23 chs UPDATEMIN(anonmin, anonminpct, fileminpct, execminpct);
180 1.23 chs
181 1.23 chs case VM_EXECMIN:
182 1.23 chs UPDATEMIN(execmin, execminpct, fileminpct, anonminpct);
183 1.23 chs
184 1.23 chs case VM_FILEMIN:
185 1.23 chs UPDATEMIN(filemin, fileminpct, execminpct, anonminpct);
186 1.23 chs
187 1.23 chs #undef UPDATEMIN
188 1.23 chs #define UPDATEMAX(a, ap) \
189 1.23 chs { \
190 1.23 chs t = uvmexp.ap; \
191 1.23 chs rv = sysctl_int(oldp, oldlenp, newp, newlen, &t); \
192 1.23 chs if (rv) { \
193 1.23 chs return rv; \
194 1.23 chs } \
195 1.23 chs if (t > 100 || t < 0) { \
196 1.23 chs return EINVAL; \
197 1.23 chs } \
198 1.23 chs uvmexp.ap = t; \
199 1.23 chs uvmexp.a = t * 256 / 100; \
200 1.23 chs return rv; \
201 1.23 chs }
202 1.23 chs
203 1.23 chs case VM_ANONMAX:
204 1.23 chs UPDATEMAX(anonmax, anonmaxpct);
205 1.23 chs
206 1.23 chs case VM_EXECMAX:
207 1.23 chs UPDATEMAX(execmax, execmaxpct);
208 1.23 chs
209 1.23 chs case VM_FILEMAX:
210 1.23 chs UPDATEMAX(filemax, filemaxpct);
211 1.23 chs
212 1.23 chs #undef UPDATEMAX
213 1.21 matt
214 1.21 matt case VM_MAXSLP:
215 1.21 matt return (sysctl_rdint(oldp, oldlenp, newp, maxslp));
216 1.21 matt
217 1.21 matt case VM_USPACE:
218 1.21 matt return (sysctl_rdint(oldp, oldlenp, newp, USPACE));
219 1.4 mrg
220 1.4 mrg default:
221 1.4 mrg return (EOPNOTSUPP);
222 1.4 mrg }
223 1.4 mrg /* NOTREACHED */
224 1.15 simonb }
225 1.15 simonb
226 1.15 simonb static int
227 1.15 simonb sysctl_uvmexp(oldp, oldlenp)
228 1.15 simonb void *oldp;
229 1.15 simonb size_t *oldlenp;
230 1.15 simonb {
231 1.15 simonb struct uvmexp_sysctl u;
232 1.15 simonb
233 1.16 simonb memset(&u, 0, sizeof(u));
234 1.16 simonb
235 1.16 simonb /* Entries here are in order of uvmexp_sysctl, not uvmexp */
236 1.15 simonb u.pagesize = uvmexp.pagesize;
237 1.15 simonb u.pagemask = uvmexp.pagemask;
238 1.15 simonb u.pageshift = uvmexp.pageshift;
239 1.15 simonb u.npages = uvmexp.npages;
240 1.15 simonb u.free = uvmexp.free;
241 1.15 simonb u.active = uvmexp.active;
242 1.15 simonb u.inactive = uvmexp.inactive;
243 1.15 simonb u.paging = uvmexp.paging;
244 1.15 simonb u.wired = uvmexp.wired;
245 1.15 simonb u.zeropages = uvmexp.zeropages;
246 1.15 simonb u.reserve_pagedaemon = uvmexp.reserve_pagedaemon;
247 1.15 simonb u.reserve_kernel = uvmexp.reserve_kernel;
248 1.15 simonb u.freemin = uvmexp.freemin;
249 1.15 simonb u.freetarg = uvmexp.freetarg;
250 1.15 simonb u.inactarg = uvmexp.inactarg;
251 1.15 simonb u.wiredmax = uvmexp.wiredmax;
252 1.15 simonb u.nswapdev = uvmexp.nswapdev;
253 1.15 simonb u.swpages = uvmexp.swpages;
254 1.15 simonb u.swpginuse = uvmexp.swpginuse;
255 1.15 simonb u.swpgonly = uvmexp.swpgonly;
256 1.15 simonb u.nswget = uvmexp.nswget;
257 1.15 simonb u.nanon = uvmexp.nanon;
258 1.15 simonb u.nanonneeded = uvmexp.nanonneeded;
259 1.15 simonb u.nfreeanon = uvmexp.nfreeanon;
260 1.15 simonb u.faults = uvmexp.faults;
261 1.15 simonb u.traps = uvmexp.traps;
262 1.15 simonb u.intrs = uvmexp.intrs;
263 1.15 simonb u.swtch = uvmexp.swtch;
264 1.15 simonb u.softs = uvmexp.softs;
265 1.15 simonb u.syscalls = uvmexp.syscalls;
266 1.15 simonb u.pageins = uvmexp.pageins;
267 1.15 simonb u.swapins = uvmexp.swapins;
268 1.15 simonb u.swapouts = uvmexp.swapouts;
269 1.15 simonb u.pgswapin = uvmexp.pgswapin;
270 1.15 simonb u.pgswapout = uvmexp.pgswapout;
271 1.15 simonb u.forks = uvmexp.forks;
272 1.15 simonb u.forks_ppwait = uvmexp.forks_ppwait;
273 1.15 simonb u.forks_sharevm = uvmexp.forks_sharevm;
274 1.15 simonb u.pga_zerohit = uvmexp.pga_zerohit;
275 1.15 simonb u.pga_zeromiss = uvmexp.pga_zeromiss;
276 1.15 simonb u.zeroaborts = uvmexp.zeroaborts;
277 1.15 simonb u.fltnoram = uvmexp.fltnoram;
278 1.15 simonb u.fltnoanon = uvmexp.fltnoanon;
279 1.15 simonb u.fltpgwait = uvmexp.fltpgwait;
280 1.15 simonb u.fltpgrele = uvmexp.fltpgrele;
281 1.15 simonb u.fltrelck = uvmexp.fltrelck;
282 1.15 simonb u.fltrelckok = uvmexp.fltrelckok;
283 1.15 simonb u.fltanget = uvmexp.fltanget;
284 1.15 simonb u.fltanretry = uvmexp.fltanretry;
285 1.15 simonb u.fltamcopy = uvmexp.fltamcopy;
286 1.15 simonb u.fltnamap = uvmexp.fltnamap;
287 1.15 simonb u.fltnomap = uvmexp.fltnomap;
288 1.15 simonb u.fltlget = uvmexp.fltlget;
289 1.15 simonb u.fltget = uvmexp.fltget;
290 1.15 simonb u.flt_anon = uvmexp.flt_anon;
291 1.15 simonb u.flt_acow = uvmexp.flt_acow;
292 1.15 simonb u.flt_obj = uvmexp.flt_obj;
293 1.15 simonb u.flt_prcopy = uvmexp.flt_prcopy;
294 1.15 simonb u.flt_przero = uvmexp.flt_przero;
295 1.15 simonb u.pdwoke = uvmexp.pdwoke;
296 1.15 simonb u.pdrevs = uvmexp.pdrevs;
297 1.15 simonb u.pdswout = uvmexp.pdswout;
298 1.15 simonb u.pdfreed = uvmexp.pdfreed;
299 1.15 simonb u.pdscans = uvmexp.pdscans;
300 1.15 simonb u.pdanscan = uvmexp.pdanscan;
301 1.15 simonb u.pdobscan = uvmexp.pdobscan;
302 1.15 simonb u.pdreact = uvmexp.pdreact;
303 1.15 simonb u.pdbusy = uvmexp.pdbusy;
304 1.15 simonb u.pdpageouts = uvmexp.pdpageouts;
305 1.15 simonb u.pdpending = uvmexp.pdpending;
306 1.15 simonb u.pddeact = uvmexp.pddeact;
307 1.16 simonb u.anonpages = uvmexp.anonpages;
308 1.23 chs u.filepages = uvmexp.filepages;
309 1.23 chs u.execpages = uvmexp.execpages;
310 1.18 thorpej u.colorhit = uvmexp.colorhit;
311 1.18 thorpej u.colormiss = uvmexp.colormiss;
312 1.15 simonb
313 1.15 simonb return (sysctl_rdminstruct(oldp, oldlenp, NULL, &u, sizeof(u)));
314 1.1 mrg }
315 1.1 mrg
316 1.1 mrg /*
317 1.1 mrg * uvm_total: calculate the current state of the system.
318 1.1 mrg */
319 1.4 mrg static void
320 1.4 mrg uvm_total(totalp)
321 1.4 mrg struct vmtotal *totalp;
322 1.1 mrg {
323 1.4 mrg struct proc *p;
324 1.1 mrg #if 0
325 1.20 chs struct vm_map_entry * entry;
326 1.20 chs struct vm_map *map;
327 1.4 mrg int paging;
328 1.1 mrg #endif
329 1.1 mrg
330 1.7 perry memset(totalp, 0, sizeof *totalp);
331 1.1 mrg
332 1.4 mrg /*
333 1.4 mrg * calculate process statistics
334 1.4 mrg */
335 1.4 mrg
336 1.10 thorpej proclist_lock_read();
337 1.14 chs LIST_FOREACH(p, &allproc, p_list) {
338 1.4 mrg if (p->p_flag & P_SYSTEM)
339 1.4 mrg continue;
340 1.4 mrg switch (p->p_stat) {
341 1.4 mrg case 0:
342 1.4 mrg continue;
343 1.6 mrg
344 1.4 mrg case SSLEEP:
345 1.4 mrg case SSTOP:
346 1.4 mrg if (p->p_flag & P_INMEM) {
347 1.4 mrg if (p->p_priority <= PZERO)
348 1.4 mrg totalp->t_dw++;
349 1.4 mrg else if (p->p_slptime < maxslp)
350 1.4 mrg totalp->t_sl++;
351 1.4 mrg } else if (p->p_slptime < maxslp)
352 1.4 mrg totalp->t_sw++;
353 1.4 mrg if (p->p_slptime >= maxslp)
354 1.4 mrg continue;
355 1.4 mrg break;
356 1.6 mrg
357 1.4 mrg case SRUN:
358 1.12 thorpej case SONPROC:
359 1.4 mrg case SIDL:
360 1.4 mrg if (p->p_flag & P_INMEM)
361 1.4 mrg totalp->t_rq++;
362 1.4 mrg else
363 1.4 mrg totalp->t_sw++;
364 1.4 mrg if (p->p_stat == SIDL)
365 1.4 mrg continue;
366 1.4 mrg break;
367 1.4 mrg }
368 1.4 mrg /*
369 1.4 mrg * note active objects
370 1.4 mrg */
371 1.1 mrg #if 0
372 1.4 mrg /*
373 1.5 mrg * XXXCDC: BOGUS! rethink this. in the mean time
374 1.5 mrg * don't do it.
375 1.4 mrg */
376 1.4 mrg paging = 0;
377 1.5 mrg vm_map_lock(map);
378 1.4 mrg for (map = &p->p_vmspace->vm_map, entry = map->header.next;
379 1.4 mrg entry != &map->header; entry = entry->next) {
380 1.4 mrg if (entry->is_a_map || entry->is_sub_map ||
381 1.5 mrg entry->object.uvm_obj == NULL)
382 1.4 mrg continue;
383 1.5 mrg /* XXX how to do this with uvm */
384 1.4 mrg }
385 1.5 mrg vm_map_unlock(map);
386 1.4 mrg if (paging)
387 1.4 mrg totalp->t_pw++;
388 1.1 mrg #endif
389 1.4 mrg }
390 1.9 thorpej proclist_unlock_read();
391 1.4 mrg /*
392 1.4 mrg * Calculate object memory usage statistics.
393 1.4 mrg */
394 1.5 mrg totalp->t_free = uvmexp.free;
395 1.5 mrg totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
396 1.5 mrg totalp->t_avm = uvmexp.active + uvmexp.swpginuse; /* XXX */
397 1.5 mrg totalp->t_rm = uvmexp.npages - uvmexp.free;
398 1.5 mrg totalp->t_arm = uvmexp.active;
399 1.5 mrg totalp->t_vmshr = 0; /* XXX */
400 1.5 mrg totalp->t_avmshr = 0; /* XXX */
401 1.5 mrg totalp->t_rmshr = 0; /* XXX */
402 1.5 mrg totalp->t_armshr = 0; /* XXX */
403 1.1 mrg }
404