xref: /netbsd-src/sys/uvm/uvm_meter.c (revision 7cc2f76925f078d01ddc9e640a98f4ccfc9f8c3b)
1 /*	$NetBSD: uvm_meter.c,v 1.16 2000/11/30 11:04:44 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 	memset(&u, 0, sizeof(u));
173 
174 	/* Entries here are in order of uvmexp_sysctl, not uvmexp */
175 	u.pagesize = uvmexp.pagesize;
176 	u.pagemask = uvmexp.pagemask;
177 	u.pageshift = uvmexp.pageshift;
178 	u.npages = uvmexp.npages;
179 	u.free = uvmexp.free;
180 	u.active = uvmexp.active;
181 	u.inactive = uvmexp.inactive;
182 	u.paging = uvmexp.paging;
183 	u.wired = uvmexp.wired;
184 	u.zeropages = uvmexp.zeropages;
185 	u.reserve_pagedaemon = uvmexp.reserve_pagedaemon;
186 	u.reserve_kernel = uvmexp.reserve_kernel;
187 	u.freemin = uvmexp.freemin;
188 	u.freetarg = uvmexp.freetarg;
189 	u.inactarg = uvmexp.inactarg;
190 	u.wiredmax = uvmexp.wiredmax;
191 	u.nswapdev = uvmexp.nswapdev;
192 	u.swpages = uvmexp.swpages;
193 	u.swpginuse = uvmexp.swpginuse;
194 	u.swpgonly = uvmexp.swpgonly;
195 	u.nswget = uvmexp.nswget;
196 	u.nanon = uvmexp.nanon;
197 	u.nanonneeded = uvmexp.nanonneeded;
198 	u.nfreeanon = uvmexp.nfreeanon;
199 	u.faults = uvmexp.faults;
200 	u.traps = uvmexp.traps;
201 	u.intrs = uvmexp.intrs;
202 	u.swtch = uvmexp.swtch;
203 	u.softs = uvmexp.softs;
204 	u.syscalls = uvmexp.syscalls;
205 	u.pageins = uvmexp.pageins;
206 	u.swapins = uvmexp.swapins;
207 	u.swapouts = uvmexp.swapouts;
208 	u.pgswapin = uvmexp.pgswapin;
209 	u.pgswapout = uvmexp.pgswapout;
210 	u.forks = uvmexp.forks;
211 	u.forks_ppwait = uvmexp.forks_ppwait;
212 	u.forks_sharevm = uvmexp.forks_sharevm;
213 	u.pga_zerohit = uvmexp.pga_zerohit;
214 	u.pga_zeromiss = uvmexp.pga_zeromiss;
215 	u.zeroaborts = uvmexp.zeroaborts;
216 	u.fltnoram = uvmexp.fltnoram;
217 	u.fltnoanon = uvmexp.fltnoanon;
218 	u.fltpgwait = uvmexp.fltpgwait;
219 	u.fltpgrele = uvmexp.fltpgrele;
220 	u.fltrelck = uvmexp.fltrelck;
221 	u.fltrelckok = uvmexp.fltrelckok;
222 	u.fltanget = uvmexp.fltanget;
223 	u.fltanretry = uvmexp.fltanretry;
224 	u.fltamcopy = uvmexp.fltamcopy;
225 	u.fltnamap = uvmexp.fltnamap;
226 	u.fltnomap = uvmexp.fltnomap;
227 	u.fltlget = uvmexp.fltlget;
228 	u.fltget = uvmexp.fltget;
229 	u.flt_anon = uvmexp.flt_anon;
230 	u.flt_acow = uvmexp.flt_acow;
231 	u.flt_obj = uvmexp.flt_obj;
232 	u.flt_prcopy = uvmexp.flt_prcopy;
233 	u.flt_przero = uvmexp.flt_przero;
234 	u.pdwoke = uvmexp.pdwoke;
235 	u.pdrevs = uvmexp.pdrevs;
236 	u.pdswout = uvmexp.pdswout;
237 	u.pdfreed = uvmexp.pdfreed;
238 	u.pdscans = uvmexp.pdscans;
239 	u.pdanscan = uvmexp.pdanscan;
240 	u.pdobscan = uvmexp.pdobscan;
241 	u.pdreact = uvmexp.pdreact;
242 	u.pdbusy = uvmexp.pdbusy;
243 	u.pdpageouts = uvmexp.pdpageouts;
244 	u.pdpending = uvmexp.pdpending;
245 	u.pddeact = uvmexp.pddeact;
246 	u.anonpages = uvmexp.anonpages;
247 	u.vnodepages = uvmexp.vnodepages;
248 	u.vtextpages = uvmexp.vtextpages;
249 
250 	return (sysctl_rdminstruct(oldp, oldlenp, NULL, &u, sizeof(u)));
251 }
252 
253 /*
254  * uvm_total: calculate the current state of the system.
255  */
256 static void
257 uvm_total(totalp)
258 	struct vmtotal *totalp;
259 {
260 	struct proc *p;
261 #if 0
262 	vm_map_entry_t	entry;
263 	vm_map_t map;
264 	int paging;
265 #endif
266 
267 	memset(totalp, 0, sizeof *totalp);
268 
269 	/*
270 	 * calculate process statistics
271 	 */
272 
273 	proclist_lock_read();
274 	LIST_FOREACH(p, &allproc, p_list) {
275 		if (p->p_flag & P_SYSTEM)
276 			continue;
277 		switch (p->p_stat) {
278 		case 0:
279 			continue;
280 
281 		case SSLEEP:
282 		case SSTOP:
283 			if (p->p_flag & P_INMEM) {
284 				if (p->p_priority <= PZERO)
285 					totalp->t_dw++;
286 				else if (p->p_slptime < maxslp)
287 					totalp->t_sl++;
288 			} else if (p->p_slptime < maxslp)
289 				totalp->t_sw++;
290 			if (p->p_slptime >= maxslp)
291 				continue;
292 			break;
293 
294 		case SRUN:
295 		case SONPROC:
296 		case SIDL:
297 			if (p->p_flag & P_INMEM)
298 				totalp->t_rq++;
299 			else
300 				totalp->t_sw++;
301 			if (p->p_stat == SIDL)
302 				continue;
303 			break;
304 		}
305 		/*
306 		 * note active objects
307 		 */
308 #if 0
309 		/*
310 		 * XXXCDC: BOGUS!  rethink this.   in the mean time
311 		 * don't do it.
312 		 */
313 		paging = 0;
314 		vm_map_lock(map);
315 		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
316 		    entry != &map->header; entry = entry->next) {
317 			if (entry->is_a_map || entry->is_sub_map ||
318 			    entry->object.uvm_obj == NULL)
319 				continue;
320 			/* XXX how to do this with uvm */
321 		}
322 		vm_map_unlock(map);
323 		if (paging)
324 			totalp->t_pw++;
325 #endif
326 	}
327 	proclist_unlock_read();
328 	/*
329 	 * Calculate object memory usage statistics.
330 	 */
331 	totalp->t_free = uvmexp.free;
332 	totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
333 	totalp->t_avm = uvmexp.active + uvmexp.swpginuse;	/* XXX */
334 	totalp->t_rm = uvmexp.npages - uvmexp.free;
335 	totalp->t_arm = uvmexp.active;
336 	totalp->t_vmshr = 0;		/* XXX */
337 	totalp->t_avmshr = 0;		/* XXX */
338 	totalp->t_rmshr = 0;		/* XXX */
339 	totalp->t_armshr = 0;		/* XXX */
340 }
341