xref: /openbsd-src/sys/uvm/uvm_meter.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: uvm_meter.c,v 1.29 2010/07/22 17:31:39 thib Exp $	*/
2 /*	$NetBSD: uvm_meter.c,v 1.21 2001/07/14 06:36:03 matt Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * Copyright (c) 1982, 1986, 1989, 1993
7  *      The Regents of the University of California.
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by Charles D. Cranor,
22  *      Washington University, and the University of California, Berkeley
23  *      and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *      @(#)vm_meter.c  8.4 (Berkeley) 1/4/94
41  * from: Id: uvm_meter.c,v 1.1.2.1 1997/08/14 19:10:35 chuck Exp
42  */
43 
44 #include <sys/param.h>
45 #include <sys/proc.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <uvm/uvm_extern.h>
49 #include <sys/sysctl.h>
50 #include <sys/exec.h>
51 
52 #ifdef UVM_SWAP_ENCRYPT
53 #include <uvm/uvm_swap.h>
54 #include <uvm/uvm_swap_encrypt.h>
55 #endif
56 
57 /*
58  * maxslp: ???? XXXCDC
59  */
60 
61 int maxslp = MAXSLP;	/* patchable ... */
62 struct loadavg averunnable;
63 
64 /*
65  * constants for averages over 1, 5, and 15 minutes when sampling at
66  * 5 second intervals.
67  */
68 
69 static fixpt_t cexp[3] = {
70 	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
71 	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
72 	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
73 };
74 
75 /*
76  * prototypes
77  */
78 
79 static void uvm_loadav(struct loadavg *);
80 
81 /*
82  * uvm_meter: calculate load average and wake up the swapper (if needed)
83  */
84 void
85 uvm_meter(void)
86 {
87 	if ((time_second % 5) == 0)
88 		uvm_loadav(&averunnable);
89 	if (proc0.p_slptime > (maxslp / 2))
90 		wakeup(&proc0);
91 }
92 
93 /*
94  * uvm_loadav: compute a tenex style load average of a quantity on
95  * 1, 5, and 15 minute intervals.
96  */
97 static void
98 uvm_loadav(struct loadavg *avg)
99 {
100 	CPU_INFO_ITERATOR cii;
101 	struct cpu_info *ci;
102 	int i, nrun;
103 	struct proc *p;
104 	int nrun_cpu[MAXCPUS];
105 
106 	nrun = 0;
107 	memset(nrun_cpu, 0, sizeof(nrun_cpu));
108 
109 	LIST_FOREACH(p, &allproc, p_list) {
110 		switch (p->p_stat) {
111 		case SSLEEP:
112 			if (p->p_priority > PZERO || p->p_slptime > 1)
113 				continue;
114 		/* FALLTHROUGH */
115 		case SRUN:
116 		case SONPROC:
117 			if (p == p->p_cpu->ci_schedstate.spc_idleproc)
118 				continue;
119 		case SIDL:
120 			nrun++;
121 			if (p->p_cpu)
122 				nrun_cpu[CPU_INFO_UNIT(p->p_cpu)]++;
123 		}
124 	}
125 
126 	for (i = 0; i < 3; i++) {
127 		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
128 		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
129 	}
130 
131 	CPU_INFO_FOREACH(cii, ci) {
132 		struct schedstate_percpu *spc = &ci->ci_schedstate;
133 
134 		if (nrun_cpu[CPU_INFO_UNIT(ci)] == 0)
135 			continue;
136 		spc->spc_ldavg = (cexp[0] * spc->spc_ldavg +
137 		    nrun_cpu[CPU_INFO_UNIT(ci)] * FSCALE *
138 		    (FSCALE - cexp[0])) >> FSHIFT;
139 	}
140 }
141 
142 /*
143  * uvm_sysctl: sysctl hook into UVM system.
144  */
145 int
146 uvm_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
147     size_t newlen, struct proc *p)
148 {
149 	struct vmtotal vmtotals;
150 	int rv, t;
151 	struct _ps_strings _ps = { PS_STRINGS };
152 
153 	switch (name[0]) {
154 	case VM_SWAPENCRYPT:
155 #ifdef UVM_SWAP_ENCRYPT
156 		return (swap_encrypt_ctl(name + 1, namelen - 1, oldp, oldlenp,
157 					 newp, newlen, p));
158 #else
159 		return (EOPNOTSUPP);
160 #endif
161 	default:
162 		/* all sysctl names at this level are terminal */
163 		if (namelen != 1)
164 			return (ENOTDIR);		/* overloaded */
165 		break;
166 	}
167 
168 	switch (name[0]) {
169 	case VM_LOADAVG:
170 		return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
171 		    sizeof(averunnable)));
172 
173 	case VM_METER:
174 		uvm_total(&vmtotals);
175 		return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
176 		    sizeof(vmtotals)));
177 
178 	case VM_UVMEXP:
179 		return (sysctl_rdstruct(oldp, oldlenp, newp, &uvmexp,
180 		    sizeof(uvmexp)));
181 
182 	case VM_NKMEMPAGES:
183 		return (sysctl_rdint(oldp, oldlenp, newp, nkmempages));
184 
185 	case VM_PSSTRINGS:
186 		return (sysctl_rdstruct(oldp, oldlenp, newp, &_ps,
187 		    sizeof(_ps)));
188 	case VM_ANONMIN:
189 		t = uvmexp.anonminpct;
190 		rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
191 		if (rv) {
192 			return rv;
193 		}
194 		if (t + uvmexp.vtextminpct + uvmexp.vnodeminpct > 95 || t < 0) {
195 			return EINVAL;
196 		}
197 		uvmexp.anonminpct = t;
198 		uvmexp.anonmin = t * 256 / 100;
199 		return rv;
200 
201 	case VM_VTEXTMIN:
202 		t = uvmexp.vtextminpct;
203 		rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
204 		if (rv) {
205 			return rv;
206 		}
207 		if (uvmexp.anonminpct + t + uvmexp.vnodeminpct > 95 || t < 0) {
208 			return EINVAL;
209 		}
210 		uvmexp.vtextminpct = t;
211 		uvmexp.vtextmin = t * 256 / 100;
212 		return rv;
213 
214 	case VM_VNODEMIN:
215 		t = uvmexp.vnodeminpct;
216 		rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
217 		if (rv) {
218 			return rv;
219 		}
220 		if (uvmexp.anonminpct + uvmexp.vtextminpct + t > 95 || t < 0) {
221 			return EINVAL;
222 		}
223 		uvmexp.vnodeminpct = t;
224 		uvmexp.vnodemin = t * 256 / 100;
225 		return rv;
226 
227 	case VM_MAXSLP:
228 		return (sysctl_rdint(oldp, oldlenp, newp, maxslp));
229 
230 	case VM_USPACE:
231 		return (sysctl_rdint(oldp, oldlenp, newp, USPACE));
232 
233 	default:
234 		return (EOPNOTSUPP);
235 	}
236 	/* NOTREACHED */
237 }
238 
239 /*
240  * uvm_total: calculate the current state of the system.
241  */
242 void
243 uvm_total(struct vmtotal *totalp)
244 {
245 	struct proc *p;
246 #if 0
247 	struct vm_map_entry *	entry;
248 	struct vm_map *map;
249 	int paging;
250 #endif
251 
252 	memset(totalp, 0, sizeof *totalp);
253 
254 	/*
255 	 * calculate process statistics
256 	 */
257 
258 	LIST_FOREACH(p, &allproc, p_list) {
259 		if (p->p_flag & P_SYSTEM)
260 			continue;
261 		switch (p->p_stat) {
262 		case 0:
263 			continue;
264 
265 		case SSLEEP:
266 		case SSTOP:
267 			if (p->p_priority <= PZERO)
268 				totalp->t_dw++;
269 			else if (p->p_slptime < maxslp)
270 				totalp->t_sl++;
271 			if (p->p_slptime >= maxslp)
272 				continue;
273 			break;
274 
275 		case SRUN:
276 		case SIDL:
277 		case SONPROC:
278 			totalp->t_rq++;
279 			if (p->p_stat == SIDL)
280 				continue;
281 			break;
282 		}
283 		/*
284 		 * note active objects
285 		 */
286 #if 0
287 		/*
288 		 * XXXCDC: BOGUS!  rethink this.   in the mean time
289 		 * don't do it.
290 		 */
291 		paging = 0;
292 		vm_map_lock(map);
293 		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
294 		    entry != &map->header; entry = entry->next) {
295 			if (entry->is_a_map || entry->is_sub_map ||
296 			    entry->object.uvm_obj == NULL)
297 				continue;
298 			/* XXX how to do this with uvm */
299 		}
300 		vm_map_unlock(map);
301 		if (paging)
302 			totalp->t_pw++;
303 #endif
304 	}
305 	/*
306 	 * Calculate object memory usage statistics.
307 	 */
308 	totalp->t_free = uvmexp.free;
309 	totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
310 	totalp->t_avm = uvmexp.active + uvmexp.swpginuse;	/* XXX */
311 	totalp->t_rm = uvmexp.npages - uvmexp.free;
312 	totalp->t_arm = uvmexp.active;
313 	totalp->t_vmshr = 0;		/* XXX */
314 	totalp->t_avmshr = 0;		/* XXX */
315 	totalp->t_rmshr = 0;		/* XXX */
316 	totalp->t_armshr = 0;		/* XXX */
317 }
318