uvm_meter.c revision 1.22 1 1.22 lukem /* $NetBSD: uvm_meter.c,v 1.22 2001/11/10 07:37:00 lukem 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.22 lukem __KERNEL_RCSID(0, "$NetBSD: uvm_meter.c,v 1.22 2001/11/10 07:37:00 lukem 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.17 chs case VM_ANONMIN:
164 1.17 chs t = uvmexp.anonminpct;
165 1.17 chs rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
166 1.17 chs if (rv) {
167 1.17 chs return rv;
168 1.17 chs }
169 1.17 chs if (t + uvmexp.vtextminpct + uvmexp.vnodeminpct > 95 || t < 0) {
170 1.17 chs return EINVAL;
171 1.17 chs }
172 1.17 chs uvmexp.anonminpct = t;
173 1.17 chs uvmexp.anonmin = t * 256 / 100;
174 1.17 chs return rv;
175 1.17 chs
176 1.17 chs case VM_VTEXTMIN:
177 1.17 chs t = uvmexp.vtextminpct;
178 1.17 chs rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
179 1.17 chs if (rv) {
180 1.17 chs return rv;
181 1.17 chs }
182 1.17 chs if (uvmexp.anonminpct + t + uvmexp.vnodeminpct > 95 || t < 0) {
183 1.17 chs return EINVAL;
184 1.17 chs }
185 1.17 chs uvmexp.vtextminpct = t;
186 1.17 chs uvmexp.vtextmin = t * 256 / 100;
187 1.17 chs return rv;
188 1.17 chs
189 1.17 chs case VM_VNODEMIN:
190 1.17 chs t = uvmexp.vnodeminpct;
191 1.17 chs rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
192 1.17 chs if (rv) {
193 1.17 chs return rv;
194 1.17 chs }
195 1.17 chs if (uvmexp.anonminpct + uvmexp.vtextminpct + t > 95 || t < 0) {
196 1.17 chs return EINVAL;
197 1.17 chs }
198 1.17 chs uvmexp.vnodeminpct = t;
199 1.17 chs uvmexp.vnodemin = t * 256 / 100;
200 1.17 chs return rv;
201 1.21 matt
202 1.21 matt case VM_MAXSLP:
203 1.21 matt return (sysctl_rdint(oldp, oldlenp, newp, maxslp));
204 1.21 matt
205 1.21 matt case VM_USPACE:
206 1.21 matt return (sysctl_rdint(oldp, oldlenp, newp, USPACE));
207 1.4 mrg
208 1.4 mrg default:
209 1.4 mrg return (EOPNOTSUPP);
210 1.4 mrg }
211 1.4 mrg /* NOTREACHED */
212 1.15 simonb }
213 1.15 simonb
214 1.15 simonb static int
215 1.15 simonb sysctl_uvmexp(oldp, oldlenp)
216 1.15 simonb void *oldp;
217 1.15 simonb size_t *oldlenp;
218 1.15 simonb {
219 1.15 simonb struct uvmexp_sysctl u;
220 1.15 simonb
221 1.16 simonb memset(&u, 0, sizeof(u));
222 1.16 simonb
223 1.16 simonb /* Entries here are in order of uvmexp_sysctl, not uvmexp */
224 1.15 simonb u.pagesize = uvmexp.pagesize;
225 1.15 simonb u.pagemask = uvmexp.pagemask;
226 1.15 simonb u.pageshift = uvmexp.pageshift;
227 1.15 simonb u.npages = uvmexp.npages;
228 1.15 simonb u.free = uvmexp.free;
229 1.15 simonb u.active = uvmexp.active;
230 1.15 simonb u.inactive = uvmexp.inactive;
231 1.15 simonb u.paging = uvmexp.paging;
232 1.15 simonb u.wired = uvmexp.wired;
233 1.15 simonb u.zeropages = uvmexp.zeropages;
234 1.15 simonb u.reserve_pagedaemon = uvmexp.reserve_pagedaemon;
235 1.15 simonb u.reserve_kernel = uvmexp.reserve_kernel;
236 1.15 simonb u.freemin = uvmexp.freemin;
237 1.15 simonb u.freetarg = uvmexp.freetarg;
238 1.15 simonb u.inactarg = uvmexp.inactarg;
239 1.15 simonb u.wiredmax = uvmexp.wiredmax;
240 1.15 simonb u.nswapdev = uvmexp.nswapdev;
241 1.15 simonb u.swpages = uvmexp.swpages;
242 1.15 simonb u.swpginuse = uvmexp.swpginuse;
243 1.15 simonb u.swpgonly = uvmexp.swpgonly;
244 1.15 simonb u.nswget = uvmexp.nswget;
245 1.15 simonb u.nanon = uvmexp.nanon;
246 1.15 simonb u.nanonneeded = uvmexp.nanonneeded;
247 1.15 simonb u.nfreeanon = uvmexp.nfreeanon;
248 1.15 simonb u.faults = uvmexp.faults;
249 1.15 simonb u.traps = uvmexp.traps;
250 1.15 simonb u.intrs = uvmexp.intrs;
251 1.15 simonb u.swtch = uvmexp.swtch;
252 1.15 simonb u.softs = uvmexp.softs;
253 1.15 simonb u.syscalls = uvmexp.syscalls;
254 1.15 simonb u.pageins = uvmexp.pageins;
255 1.15 simonb u.swapins = uvmexp.swapins;
256 1.15 simonb u.swapouts = uvmexp.swapouts;
257 1.15 simonb u.pgswapin = uvmexp.pgswapin;
258 1.15 simonb u.pgswapout = uvmexp.pgswapout;
259 1.15 simonb u.forks = uvmexp.forks;
260 1.15 simonb u.forks_ppwait = uvmexp.forks_ppwait;
261 1.15 simonb u.forks_sharevm = uvmexp.forks_sharevm;
262 1.15 simonb u.pga_zerohit = uvmexp.pga_zerohit;
263 1.15 simonb u.pga_zeromiss = uvmexp.pga_zeromiss;
264 1.15 simonb u.zeroaborts = uvmexp.zeroaborts;
265 1.15 simonb u.fltnoram = uvmexp.fltnoram;
266 1.15 simonb u.fltnoanon = uvmexp.fltnoanon;
267 1.15 simonb u.fltpgwait = uvmexp.fltpgwait;
268 1.15 simonb u.fltpgrele = uvmexp.fltpgrele;
269 1.15 simonb u.fltrelck = uvmexp.fltrelck;
270 1.15 simonb u.fltrelckok = uvmexp.fltrelckok;
271 1.15 simonb u.fltanget = uvmexp.fltanget;
272 1.15 simonb u.fltanretry = uvmexp.fltanretry;
273 1.15 simonb u.fltamcopy = uvmexp.fltamcopy;
274 1.15 simonb u.fltnamap = uvmexp.fltnamap;
275 1.15 simonb u.fltnomap = uvmexp.fltnomap;
276 1.15 simonb u.fltlget = uvmexp.fltlget;
277 1.15 simonb u.fltget = uvmexp.fltget;
278 1.15 simonb u.flt_anon = uvmexp.flt_anon;
279 1.15 simonb u.flt_acow = uvmexp.flt_acow;
280 1.15 simonb u.flt_obj = uvmexp.flt_obj;
281 1.15 simonb u.flt_prcopy = uvmexp.flt_prcopy;
282 1.15 simonb u.flt_przero = uvmexp.flt_przero;
283 1.15 simonb u.pdwoke = uvmexp.pdwoke;
284 1.15 simonb u.pdrevs = uvmexp.pdrevs;
285 1.15 simonb u.pdswout = uvmexp.pdswout;
286 1.15 simonb u.pdfreed = uvmexp.pdfreed;
287 1.15 simonb u.pdscans = uvmexp.pdscans;
288 1.15 simonb u.pdanscan = uvmexp.pdanscan;
289 1.15 simonb u.pdobscan = uvmexp.pdobscan;
290 1.15 simonb u.pdreact = uvmexp.pdreact;
291 1.15 simonb u.pdbusy = uvmexp.pdbusy;
292 1.15 simonb u.pdpageouts = uvmexp.pdpageouts;
293 1.15 simonb u.pdpending = uvmexp.pdpending;
294 1.15 simonb u.pddeact = uvmexp.pddeact;
295 1.16 simonb u.anonpages = uvmexp.anonpages;
296 1.16 simonb u.vnodepages = uvmexp.vnodepages;
297 1.16 simonb u.vtextpages = uvmexp.vtextpages;
298 1.18 thorpej u.colorhit = uvmexp.colorhit;
299 1.18 thorpej u.colormiss = uvmexp.colormiss;
300 1.15 simonb
301 1.15 simonb return (sysctl_rdminstruct(oldp, oldlenp, NULL, &u, sizeof(u)));
302 1.1 mrg }
303 1.1 mrg
304 1.1 mrg /*
305 1.1 mrg * uvm_total: calculate the current state of the system.
306 1.1 mrg */
307 1.4 mrg static void
308 1.4 mrg uvm_total(totalp)
309 1.4 mrg struct vmtotal *totalp;
310 1.1 mrg {
311 1.4 mrg struct proc *p;
312 1.1 mrg #if 0
313 1.20 chs struct vm_map_entry * entry;
314 1.20 chs struct vm_map *map;
315 1.4 mrg int paging;
316 1.1 mrg #endif
317 1.1 mrg
318 1.7 perry memset(totalp, 0, sizeof *totalp);
319 1.1 mrg
320 1.4 mrg /*
321 1.4 mrg * calculate process statistics
322 1.4 mrg */
323 1.4 mrg
324 1.10 thorpej proclist_lock_read();
325 1.14 chs LIST_FOREACH(p, &allproc, p_list) {
326 1.4 mrg if (p->p_flag & P_SYSTEM)
327 1.4 mrg continue;
328 1.4 mrg switch (p->p_stat) {
329 1.4 mrg case 0:
330 1.4 mrg continue;
331 1.6 mrg
332 1.4 mrg case SSLEEP:
333 1.4 mrg case SSTOP:
334 1.4 mrg if (p->p_flag & P_INMEM) {
335 1.4 mrg if (p->p_priority <= PZERO)
336 1.4 mrg totalp->t_dw++;
337 1.4 mrg else if (p->p_slptime < maxslp)
338 1.4 mrg totalp->t_sl++;
339 1.4 mrg } else if (p->p_slptime < maxslp)
340 1.4 mrg totalp->t_sw++;
341 1.4 mrg if (p->p_slptime >= maxslp)
342 1.4 mrg continue;
343 1.4 mrg break;
344 1.6 mrg
345 1.4 mrg case SRUN:
346 1.12 thorpej case SONPROC:
347 1.4 mrg case SIDL:
348 1.4 mrg if (p->p_flag & P_INMEM)
349 1.4 mrg totalp->t_rq++;
350 1.4 mrg else
351 1.4 mrg totalp->t_sw++;
352 1.4 mrg if (p->p_stat == SIDL)
353 1.4 mrg continue;
354 1.4 mrg break;
355 1.4 mrg }
356 1.4 mrg /*
357 1.4 mrg * note active objects
358 1.4 mrg */
359 1.1 mrg #if 0
360 1.4 mrg /*
361 1.5 mrg * XXXCDC: BOGUS! rethink this. in the mean time
362 1.5 mrg * don't do it.
363 1.4 mrg */
364 1.4 mrg paging = 0;
365 1.5 mrg vm_map_lock(map);
366 1.4 mrg for (map = &p->p_vmspace->vm_map, entry = map->header.next;
367 1.4 mrg entry != &map->header; entry = entry->next) {
368 1.4 mrg if (entry->is_a_map || entry->is_sub_map ||
369 1.5 mrg entry->object.uvm_obj == NULL)
370 1.4 mrg continue;
371 1.5 mrg /* XXX how to do this with uvm */
372 1.4 mrg }
373 1.5 mrg vm_map_unlock(map);
374 1.4 mrg if (paging)
375 1.4 mrg totalp->t_pw++;
376 1.1 mrg #endif
377 1.4 mrg }
378 1.9 thorpej proclist_unlock_read();
379 1.4 mrg /*
380 1.4 mrg * Calculate object memory usage statistics.
381 1.4 mrg */
382 1.5 mrg totalp->t_free = uvmexp.free;
383 1.5 mrg totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
384 1.5 mrg totalp->t_avm = uvmexp.active + uvmexp.swpginuse; /* XXX */
385 1.5 mrg totalp->t_rm = uvmexp.npages - uvmexp.free;
386 1.5 mrg totalp->t_arm = uvmexp.active;
387 1.5 mrg totalp->t_vmshr = 0; /* XXX */
388 1.5 mrg totalp->t_avmshr = 0; /* XXX */
389 1.5 mrg totalp->t_rmshr = 0; /* XXX */
390 1.5 mrg totalp->t_armshr = 0; /* XXX */
391 1.1 mrg }
392