uvm_meter.c revision 1.20 1 1.20 chs /* $NetBSD: uvm_meter.c,v 1.20 2001/06/02 18:09:27 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.1 mrg
43 1.1 mrg #include <sys/param.h>
44 1.1 mrg #include <sys/proc.h>
45 1.1 mrg #include <sys/systm.h>
46 1.1 mrg #include <sys/kernel.h>
47 1.13 mrg #include <uvm/uvm_extern.h>
48 1.1 mrg #include <sys/sysctl.h>
49 1.1 mrg
50 1.1 mrg /*
51 1.1 mrg * maxslp: ???? XXXCDC
52 1.1 mrg */
53 1.1 mrg
54 1.1 mrg int maxslp = MAXSLP; /* patchable ... */
55 1.14 chs struct loadavg averunnable;
56 1.1 mrg
57 1.1 mrg /*
58 1.19 chs * constants for averages over 1, 5, and 15 minutes when sampling at
59 1.1 mrg * 5 second intervals.
60 1.1 mrg */
61 1.1 mrg
62 1.1 mrg static fixpt_t cexp[3] = {
63 1.1 mrg 0.9200444146293232 * FSCALE, /* exp(-1/12) */
64 1.1 mrg 0.9834714538216174 * FSCALE, /* exp(-1/60) */
65 1.1 mrg 0.9944598480048967 * FSCALE, /* exp(-1/180) */
66 1.1 mrg };
67 1.1 mrg
68 1.1 mrg /*
69 1.1 mrg * prototypes
70 1.1 mrg */
71 1.1 mrg
72 1.1 mrg static void uvm_loadav __P((struct loadavg *));
73 1.1 mrg static void uvm_total __P((struct vmtotal *));
74 1.15 simonb static int sysctl_uvmexp __P((void *, size_t *));
75 1.1 mrg
76 1.1 mrg /*
77 1.1 mrg * uvm_meter: calculate load average and wake up the swapper (if needed)
78 1.1 mrg */
79 1.4 mrg void
80 1.4 mrg uvm_meter()
81 1.1 mrg {
82 1.4 mrg if ((time.tv_sec % 5) == 0)
83 1.4 mrg uvm_loadav(&averunnable);
84 1.4 mrg if (proc0.p_slptime > (maxslp / 2))
85 1.14 chs wakeup(&proc0);
86 1.1 mrg }
87 1.1 mrg
88 1.1 mrg /*
89 1.19 chs * uvm_loadav: compute a tenex style load average of a quantity on
90 1.1 mrg * 1, 5, and 15 minute internvals.
91 1.1 mrg */
92 1.4 mrg static void
93 1.4 mrg uvm_loadav(avg)
94 1.4 mrg struct loadavg *avg;
95 1.1 mrg {
96 1.4 mrg int i, nrun;
97 1.4 mrg struct proc *p;
98 1.1 mrg
99 1.10 thorpej proclist_lock_read();
100 1.14 chs nrun = 0;
101 1.14 chs LIST_FOREACH(p, &allproc, p_list) {
102 1.4 mrg switch (p->p_stat) {
103 1.4 mrg case SSLEEP:
104 1.4 mrg if (p->p_priority > PZERO || p->p_slptime > 1)
105 1.4 mrg continue;
106 1.4 mrg /* fall through */
107 1.4 mrg case SRUN:
108 1.12 thorpej case SONPROC:
109 1.4 mrg case SIDL:
110 1.4 mrg nrun++;
111 1.4 mrg }
112 1.4 mrg }
113 1.9 thorpej proclist_unlock_read();
114 1.4 mrg for (i = 0; i < 3; i++)
115 1.4 mrg avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
116 1.4 mrg nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
117 1.1 mrg }
118 1.1 mrg
119 1.1 mrg /*
120 1.1 mrg * uvm_sysctl: sysctl hook into UVM system.
121 1.1 mrg */
122 1.4 mrg int
123 1.4 mrg uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
124 1.4 mrg int *name;
125 1.4 mrg u_int namelen;
126 1.4 mrg void *oldp;
127 1.4 mrg size_t *oldlenp;
128 1.4 mrg void *newp;
129 1.4 mrg size_t newlen;
130 1.4 mrg struct proc *p;
131 1.1 mrg {
132 1.4 mrg struct vmtotal vmtotals;
133 1.17 chs int rv, t;
134 1.1 mrg
135 1.4 mrg /* all sysctl names at this level are terminal */
136 1.4 mrg if (namelen != 1)
137 1.4 mrg return (ENOTDIR); /* overloaded */
138 1.4 mrg
139 1.4 mrg switch (name[0]) {
140 1.4 mrg case VM_LOADAVG:
141 1.4 mrg return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
142 1.4 mrg sizeof(averunnable)));
143 1.4 mrg
144 1.4 mrg case VM_METER:
145 1.4 mrg uvm_total(&vmtotals);
146 1.4 mrg return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
147 1.4 mrg sizeof(vmtotals)));
148 1.4 mrg
149 1.4 mrg case VM_UVMEXP:
150 1.15 simonb return (sysctl_rdminstruct(oldp, oldlenp, newp, &uvmexp,
151 1.4 mrg sizeof(uvmexp)));
152 1.15 simonb case VM_UVMEXP2:
153 1.15 simonb if (newp)
154 1.15 simonb return (EPERM);
155 1.15 simonb return (sysctl_uvmexp(oldp, oldlenp));
156 1.11 thorpej
157 1.11 thorpej case VM_NKMEMPAGES:
158 1.11 thorpej return (sysctl_rdint(oldp, oldlenp, newp, nkmempages));
159 1.17 chs
160 1.17 chs case VM_ANONMIN:
161 1.17 chs t = uvmexp.anonminpct;
162 1.17 chs rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
163 1.17 chs if (rv) {
164 1.17 chs return rv;
165 1.17 chs }
166 1.17 chs if (t + uvmexp.vtextminpct + uvmexp.vnodeminpct > 95 || t < 0) {
167 1.17 chs return EINVAL;
168 1.17 chs }
169 1.17 chs uvmexp.anonminpct = t;
170 1.17 chs uvmexp.anonmin = t * 256 / 100;
171 1.17 chs return rv;
172 1.17 chs
173 1.17 chs case VM_VTEXTMIN:
174 1.17 chs t = uvmexp.vtextminpct;
175 1.17 chs rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
176 1.17 chs if (rv) {
177 1.17 chs return rv;
178 1.17 chs }
179 1.17 chs if (uvmexp.anonminpct + t + uvmexp.vnodeminpct > 95 || t < 0) {
180 1.17 chs return EINVAL;
181 1.17 chs }
182 1.17 chs uvmexp.vtextminpct = t;
183 1.17 chs uvmexp.vtextmin = t * 256 / 100;
184 1.17 chs return rv;
185 1.17 chs
186 1.17 chs case VM_VNODEMIN:
187 1.17 chs t = uvmexp.vnodeminpct;
188 1.17 chs rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
189 1.17 chs if (rv) {
190 1.17 chs return rv;
191 1.17 chs }
192 1.17 chs if (uvmexp.anonminpct + uvmexp.vtextminpct + t > 95 || t < 0) {
193 1.17 chs return EINVAL;
194 1.17 chs }
195 1.17 chs uvmexp.vnodeminpct = t;
196 1.17 chs uvmexp.vnodemin = t * 256 / 100;
197 1.17 chs return rv;
198 1.4 mrg
199 1.4 mrg default:
200 1.4 mrg return (EOPNOTSUPP);
201 1.4 mrg }
202 1.4 mrg /* NOTREACHED */
203 1.15 simonb }
204 1.15 simonb
205 1.15 simonb static int
206 1.15 simonb sysctl_uvmexp(oldp, oldlenp)
207 1.15 simonb void *oldp;
208 1.15 simonb size_t *oldlenp;
209 1.15 simonb {
210 1.15 simonb struct uvmexp_sysctl u;
211 1.15 simonb
212 1.16 simonb memset(&u, 0, sizeof(u));
213 1.16 simonb
214 1.16 simonb /* Entries here are in order of uvmexp_sysctl, not uvmexp */
215 1.15 simonb u.pagesize = uvmexp.pagesize;
216 1.15 simonb u.pagemask = uvmexp.pagemask;
217 1.15 simonb u.pageshift = uvmexp.pageshift;
218 1.15 simonb u.npages = uvmexp.npages;
219 1.15 simonb u.free = uvmexp.free;
220 1.15 simonb u.active = uvmexp.active;
221 1.15 simonb u.inactive = uvmexp.inactive;
222 1.15 simonb u.paging = uvmexp.paging;
223 1.15 simonb u.wired = uvmexp.wired;
224 1.15 simonb u.zeropages = uvmexp.zeropages;
225 1.15 simonb u.reserve_pagedaemon = uvmexp.reserve_pagedaemon;
226 1.15 simonb u.reserve_kernel = uvmexp.reserve_kernel;
227 1.15 simonb u.freemin = uvmexp.freemin;
228 1.15 simonb u.freetarg = uvmexp.freetarg;
229 1.15 simonb u.inactarg = uvmexp.inactarg;
230 1.15 simonb u.wiredmax = uvmexp.wiredmax;
231 1.15 simonb u.nswapdev = uvmexp.nswapdev;
232 1.15 simonb u.swpages = uvmexp.swpages;
233 1.15 simonb u.swpginuse = uvmexp.swpginuse;
234 1.15 simonb u.swpgonly = uvmexp.swpgonly;
235 1.15 simonb u.nswget = uvmexp.nswget;
236 1.15 simonb u.nanon = uvmexp.nanon;
237 1.15 simonb u.nanonneeded = uvmexp.nanonneeded;
238 1.15 simonb u.nfreeanon = uvmexp.nfreeanon;
239 1.15 simonb u.faults = uvmexp.faults;
240 1.15 simonb u.traps = uvmexp.traps;
241 1.15 simonb u.intrs = uvmexp.intrs;
242 1.15 simonb u.swtch = uvmexp.swtch;
243 1.15 simonb u.softs = uvmexp.softs;
244 1.15 simonb u.syscalls = uvmexp.syscalls;
245 1.15 simonb u.pageins = uvmexp.pageins;
246 1.15 simonb u.swapins = uvmexp.swapins;
247 1.15 simonb u.swapouts = uvmexp.swapouts;
248 1.15 simonb u.pgswapin = uvmexp.pgswapin;
249 1.15 simonb u.pgswapout = uvmexp.pgswapout;
250 1.15 simonb u.forks = uvmexp.forks;
251 1.15 simonb u.forks_ppwait = uvmexp.forks_ppwait;
252 1.15 simonb u.forks_sharevm = uvmexp.forks_sharevm;
253 1.15 simonb u.pga_zerohit = uvmexp.pga_zerohit;
254 1.15 simonb u.pga_zeromiss = uvmexp.pga_zeromiss;
255 1.15 simonb u.zeroaborts = uvmexp.zeroaborts;
256 1.15 simonb u.fltnoram = uvmexp.fltnoram;
257 1.15 simonb u.fltnoanon = uvmexp.fltnoanon;
258 1.15 simonb u.fltpgwait = uvmexp.fltpgwait;
259 1.15 simonb u.fltpgrele = uvmexp.fltpgrele;
260 1.15 simonb u.fltrelck = uvmexp.fltrelck;
261 1.15 simonb u.fltrelckok = uvmexp.fltrelckok;
262 1.15 simonb u.fltanget = uvmexp.fltanget;
263 1.15 simonb u.fltanretry = uvmexp.fltanretry;
264 1.15 simonb u.fltamcopy = uvmexp.fltamcopy;
265 1.15 simonb u.fltnamap = uvmexp.fltnamap;
266 1.15 simonb u.fltnomap = uvmexp.fltnomap;
267 1.15 simonb u.fltlget = uvmexp.fltlget;
268 1.15 simonb u.fltget = uvmexp.fltget;
269 1.15 simonb u.flt_anon = uvmexp.flt_anon;
270 1.15 simonb u.flt_acow = uvmexp.flt_acow;
271 1.15 simonb u.flt_obj = uvmexp.flt_obj;
272 1.15 simonb u.flt_prcopy = uvmexp.flt_prcopy;
273 1.15 simonb u.flt_przero = uvmexp.flt_przero;
274 1.15 simonb u.pdwoke = uvmexp.pdwoke;
275 1.15 simonb u.pdrevs = uvmexp.pdrevs;
276 1.15 simonb u.pdswout = uvmexp.pdswout;
277 1.15 simonb u.pdfreed = uvmexp.pdfreed;
278 1.15 simonb u.pdscans = uvmexp.pdscans;
279 1.15 simonb u.pdanscan = uvmexp.pdanscan;
280 1.15 simonb u.pdobscan = uvmexp.pdobscan;
281 1.15 simonb u.pdreact = uvmexp.pdreact;
282 1.15 simonb u.pdbusy = uvmexp.pdbusy;
283 1.15 simonb u.pdpageouts = uvmexp.pdpageouts;
284 1.15 simonb u.pdpending = uvmexp.pdpending;
285 1.15 simonb u.pddeact = uvmexp.pddeact;
286 1.16 simonb u.anonpages = uvmexp.anonpages;
287 1.16 simonb u.vnodepages = uvmexp.vnodepages;
288 1.16 simonb u.vtextpages = uvmexp.vtextpages;
289 1.18 thorpej u.colorhit = uvmexp.colorhit;
290 1.18 thorpej u.colormiss = uvmexp.colormiss;
291 1.15 simonb
292 1.15 simonb return (sysctl_rdminstruct(oldp, oldlenp, NULL, &u, sizeof(u)));
293 1.1 mrg }
294 1.1 mrg
295 1.1 mrg /*
296 1.1 mrg * uvm_total: calculate the current state of the system.
297 1.1 mrg */
298 1.4 mrg static void
299 1.4 mrg uvm_total(totalp)
300 1.4 mrg struct vmtotal *totalp;
301 1.1 mrg {
302 1.4 mrg struct proc *p;
303 1.1 mrg #if 0
304 1.20 chs struct vm_map_entry * entry;
305 1.20 chs struct vm_map *map;
306 1.4 mrg int paging;
307 1.1 mrg #endif
308 1.1 mrg
309 1.7 perry memset(totalp, 0, sizeof *totalp);
310 1.1 mrg
311 1.4 mrg /*
312 1.4 mrg * calculate process statistics
313 1.4 mrg */
314 1.4 mrg
315 1.10 thorpej proclist_lock_read();
316 1.14 chs LIST_FOREACH(p, &allproc, p_list) {
317 1.4 mrg if (p->p_flag & P_SYSTEM)
318 1.4 mrg continue;
319 1.4 mrg switch (p->p_stat) {
320 1.4 mrg case 0:
321 1.4 mrg continue;
322 1.6 mrg
323 1.4 mrg case SSLEEP:
324 1.4 mrg case SSTOP:
325 1.4 mrg if (p->p_flag & P_INMEM) {
326 1.4 mrg if (p->p_priority <= PZERO)
327 1.4 mrg totalp->t_dw++;
328 1.4 mrg else if (p->p_slptime < maxslp)
329 1.4 mrg totalp->t_sl++;
330 1.4 mrg } else if (p->p_slptime < maxslp)
331 1.4 mrg totalp->t_sw++;
332 1.4 mrg if (p->p_slptime >= maxslp)
333 1.4 mrg continue;
334 1.4 mrg break;
335 1.6 mrg
336 1.4 mrg case SRUN:
337 1.12 thorpej case SONPROC:
338 1.4 mrg case SIDL:
339 1.4 mrg if (p->p_flag & P_INMEM)
340 1.4 mrg totalp->t_rq++;
341 1.4 mrg else
342 1.4 mrg totalp->t_sw++;
343 1.4 mrg if (p->p_stat == SIDL)
344 1.4 mrg continue;
345 1.4 mrg break;
346 1.4 mrg }
347 1.4 mrg /*
348 1.4 mrg * note active objects
349 1.4 mrg */
350 1.1 mrg #if 0
351 1.4 mrg /*
352 1.5 mrg * XXXCDC: BOGUS! rethink this. in the mean time
353 1.5 mrg * don't do it.
354 1.4 mrg */
355 1.4 mrg paging = 0;
356 1.5 mrg vm_map_lock(map);
357 1.4 mrg for (map = &p->p_vmspace->vm_map, entry = map->header.next;
358 1.4 mrg entry != &map->header; entry = entry->next) {
359 1.4 mrg if (entry->is_a_map || entry->is_sub_map ||
360 1.5 mrg entry->object.uvm_obj == NULL)
361 1.4 mrg continue;
362 1.5 mrg /* XXX how to do this with uvm */
363 1.4 mrg }
364 1.5 mrg vm_map_unlock(map);
365 1.4 mrg if (paging)
366 1.4 mrg totalp->t_pw++;
367 1.1 mrg #endif
368 1.4 mrg }
369 1.9 thorpej proclist_unlock_read();
370 1.4 mrg /*
371 1.4 mrg * Calculate object memory usage statistics.
372 1.4 mrg */
373 1.5 mrg totalp->t_free = uvmexp.free;
374 1.5 mrg totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
375 1.5 mrg totalp->t_avm = uvmexp.active + uvmexp.swpginuse; /* XXX */
376 1.5 mrg totalp->t_rm = uvmexp.npages - uvmexp.free;
377 1.5 mrg totalp->t_arm = uvmexp.active;
378 1.5 mrg totalp->t_vmshr = 0; /* XXX */
379 1.5 mrg totalp->t_avmshr = 0; /* XXX */
380 1.5 mrg totalp->t_rmshr = 0; /* XXX */
381 1.5 mrg totalp->t_armshr = 0; /* XXX */
382 1.1 mrg }
383