xref: /openbsd-src/sys/uvm/uvm_meter.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: uvm_meter.c,v 1.17 2002/03/14 01:27:18 millert 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()
86 {
87 	if ((time.tv_sec % 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 internvals.
96  */
97 static void
98 uvm_loadav(avg)
99 	struct loadavg *avg;
100 {
101 	int i, nrun;
102 	struct proc *p;
103 
104 	nrun = 0;
105 	LIST_FOREACH(p, &allproc, p_list) {
106 		switch (p->p_stat) {
107 		case SSLEEP:
108 			if (p->p_priority > PZERO || p->p_slptime > 1)
109 				continue;
110 		/* fall through */
111 		case SRUN:
112 		case SIDL:
113 			nrun++;
114 		}
115 	}
116 	for (i = 0; i < 3; i++)
117 		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
118 		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
119 }
120 
121 /*
122  * uvm_sysctl: sysctl hook into UVM system.
123  */
124 int
125 uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
126 	int *name;
127 	u_int namelen;
128 	void *oldp;
129 	size_t *oldlenp;
130 	void *newp;
131 	size_t newlen;
132 	struct proc *p;
133 {
134 	struct vmtotal vmtotals;
135 	int rv, t;
136 	struct _ps_strings _ps = { PS_STRINGS };
137 
138 	switch (name[0]) {
139 	case VM_SWAPENCRYPT:
140 #ifdef UVM_SWAP_ENCRYPT
141 		return (swap_encrypt_ctl(name + 1, namelen - 1, oldp, oldlenp,
142 					 newp, newlen, p));
143 #else
144 		return (EOPNOTSUPP);
145 #endif
146 	default:
147 		/* all sysctl names at this level are terminal */
148 		if (namelen != 1)
149 			return (ENOTDIR);		/* overloaded */
150 		break;
151 	}
152 
153 	switch (name[0]) {
154 	case VM_LOADAVG:
155 		return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
156 		    sizeof(averunnable)));
157 
158 	case VM_METER:
159 		uvm_total(&vmtotals);
160 		return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
161 		    sizeof(vmtotals)));
162 
163 	case VM_UVMEXP:
164 		return (sysctl_rdstruct(oldp, oldlenp, newp, &uvmexp,
165 		    sizeof(uvmexp)));
166 
167 	case VM_NKMEMPAGES:
168 		return (sysctl_rdint(oldp, oldlenp, newp, nkmempages));
169 
170 	case VM_PSSTRINGS:
171 		return (sysctl_rdstruct(oldp, oldlenp, newp, &_ps,
172 		    sizeof(_ps)));
173 	case VM_ANONMIN:
174 		t = uvmexp.anonminpct;
175 		rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
176 		if (rv) {
177 			return rv;
178 		}
179 		if (t + uvmexp.vtextminpct + uvmexp.vnodeminpct > 95 || t < 0) {
180 			return EINVAL;
181 		}
182 		uvmexp.anonminpct = t;
183 		uvmexp.anonmin = t * 256 / 100;
184 		return rv;
185 
186 	case VM_VTEXTMIN:
187 		t = uvmexp.vtextminpct;
188 		rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
189 		if (rv) {
190 			return rv;
191 		}
192 		if (uvmexp.anonminpct + t + uvmexp.vnodeminpct > 95 || t < 0) {
193 			return EINVAL;
194 		}
195 		uvmexp.vtextminpct = t;
196 		uvmexp.vtextmin = t * 256 / 100;
197 		return rv;
198 
199 	case VM_VNODEMIN:
200 		t = uvmexp.vnodeminpct;
201 		rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
202 		if (rv) {
203 			return rv;
204 		}
205 		if (uvmexp.anonminpct + uvmexp.vtextminpct + t > 95 || t < 0) {
206 			return EINVAL;
207 		}
208 		uvmexp.vnodeminpct = t;
209 		uvmexp.vnodemin = t * 256 / 100;
210 		return rv;
211 
212 	case VM_MAXSLP:
213 		return (sysctl_rdint(oldp, oldlenp, newp, maxslp));
214 
215 	case VM_USPACE:
216 		return (sysctl_rdint(oldp, oldlenp, newp, USPACE));
217 
218 	default:
219 		return (EOPNOTSUPP);
220 	}
221 	/* NOTREACHED */
222 }
223 
224 /*
225  * uvm_total: calculate the current state of the system.
226  */
227 void
228 uvm_total(totalp)
229 	struct vmtotal *totalp;
230 {
231 	struct proc *p;
232 #if 0
233 	struct vm_map_entry *	entry;
234 	struct vm_map *map;
235 	int paging;
236 #endif
237 
238 	memset(totalp, 0, sizeof *totalp);
239 
240 	/*
241 	 * calculate process statistics
242 	 */
243 
244 	LIST_FOREACH(p, &allproc, p_list) {
245 		if (p->p_flag & P_SYSTEM)
246 			continue;
247 		switch (p->p_stat) {
248 		case 0:
249 			continue;
250 
251 		case SSLEEP:
252 		case SSTOP:
253 			if (p->p_flag & P_INMEM) {
254 				if (p->p_priority <= PZERO)
255 					totalp->t_dw++;
256 				else if (p->p_slptime < maxslp)
257 					totalp->t_sl++;
258 			} else if (p->p_slptime < maxslp)
259 				totalp->t_sw++;
260 			if (p->p_slptime >= maxslp)
261 				continue;
262 			break;
263 
264 		case SRUN:
265 		case SIDL:
266 			if (p->p_flag & P_INMEM)
267 				totalp->t_rq++;
268 			else
269 				totalp->t_sw++;
270 			if (p->p_stat == SIDL)
271 				continue;
272 			break;
273 		}
274 		/*
275 		 * note active objects
276 		 */
277 #if 0
278 		/*
279 		 * XXXCDC: BOGUS!  rethink this.   in the mean time
280 		 * don't do it.
281 		 */
282 		paging = 0;
283 		vm_map_lock(map);
284 		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
285 		    entry != &map->header; entry = entry->next) {
286 			if (entry->is_a_map || entry->is_sub_map ||
287 			    entry->object.uvm_obj == NULL)
288 				continue;
289 			/* XXX how to do this with uvm */
290 		}
291 		vm_map_unlock(map);
292 		if (paging)
293 			totalp->t_pw++;
294 #endif
295 	}
296 	/*
297 	 * Calculate object memory usage statistics.
298 	 */
299 	totalp->t_free = uvmexp.free;
300 	totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
301 	totalp->t_avm = uvmexp.active + uvmexp.swpginuse;	/* XXX */
302 	totalp->t_rm = uvmexp.npages - uvmexp.free;
303 	totalp->t_arm = uvmexp.active;
304 	totalp->t_vmshr = 0;		/* XXX */
305 	totalp->t_avmshr = 0;		/* XXX */
306 	totalp->t_rmshr = 0;		/* XXX */
307 	totalp->t_armshr = 0;		/* XXX */
308 }
309