xref: /freebsd/sys/vm/vm_meter.c (revision 48991a368427cadb9cdac39581d1676c29619c52)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)vm_meter.c	8.4 (Berkeley) 1/4/94
34  * $Id: vm_meter.c,v 1.8 1995/07/29 11:44:25 bde Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/proc.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <vm/vm.h>
42 #include <sys/sysctl.h>
43 
44 struct loadavg averunnable;	/* load average, of runnable procs */
45 struct vmmeter cnt;
46 
47 int maxslp = MAXSLP;
48 
49 /*
50  * Constants for averages over 1, 5, and 15 minutes
51  * when sampling at 5 second intervals.
52  */
53 fixpt_t cexp[3] = {
54 	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
55 	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
56 	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
57 };
58 
59 /*
60  * Compute a tenex style load average of a quantity on
61  * 1, 5 and 15 minute intervals.
62  */
63 static void
64 loadav(struct loadavg *avg)
65 {
66 	register int i, nrun;
67 	register struct proc *p;
68 
69 	for (nrun = 0, p = (struct proc *) allproc; p != NULL; p = p->p_next) {
70 		switch (p->p_stat) {
71 		case SSLEEP:
72 			if (p->p_priority > PZERO || p->p_slptime != 0)
73 				continue;
74 			/* fall through */
75 		case SRUN:
76 		case SIDL:
77 			nrun++;
78 		}
79 	}
80 	for (i = 0; i < 3; i++)
81 		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
82 		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
83 }
84 
85 void
86 vmmeter()
87 {
88 
89 	if (time.tv_sec % 5 == 0)
90 		loadav(&averunnable);
91 	if (proc0.p_slptime > maxslp / 2)
92 		wakeup(&proc0);
93 }
94 
95 SYSCTL_INT(_vm, VM_V_FREE_MIN, v_free_min,
96 	CTLFLAG_RW, &cnt.v_free_min, 0, "");
97 SYSCTL_INT(_vm, VM_V_FREE_TARGET, v_free_target,
98 	CTLFLAG_RW, &cnt.v_free_target, 0, "");
99 SYSCTL_INT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
100 	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
101 SYSCTL_INT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
102 	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
103 SYSCTL_INT(_vm, VM_V_CACHE_MIN, v_cache_min,
104 	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
105 SYSCTL_INT(_vm, VM_V_CACHE_MAX, v_cache_max,
106 	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
107 SYSCTL_INT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
108 	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
109 
110 static int
111 vm_loadavg SYSCTL_HANDLER_ARGS
112 {
113 	averunnable.fscale = FSCALE;
114 	return (sysctl_handle_opaque(oidp,
115 		oidp->oid_arg1, oidp->oid_arg2, req));
116 }
117 
118 SYSCTL_PROC(_vm, VM_LOADAVG, loadavg, CTLTYPE_OPAQUE|CTLFLAG_RD,
119 	&averunnable, sizeof(averunnable), vm_loadavg, "");
120 
121 static int
122 vmtotal SYSCTL_HANDLER_ARGS
123 {
124 	struct proc *p;
125 	struct vmtotal total, *totalp;
126 	vm_map_entry_t entry;
127 	vm_object_t object;
128 	vm_map_t map;
129 	int paging;
130 
131 	totalp = &total;
132 	bzero(totalp, sizeof *totalp);
133 	/*
134 	 * Mark all objects as inactive.
135 	 */
136 	for (object = vm_object_list.tqh_first;
137 	    object != NULL;
138 	    object = object->object_list.tqe_next)
139 		object->flags &= ~OBJ_ACTIVE;
140 	/*
141 	 * Calculate process statistics.
142 	 */
143 	for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
144 		if (p->p_flag & P_SYSTEM)
145 			continue;
146 		switch (p->p_stat) {
147 		case 0:
148 			continue;
149 
150 		case SSLEEP:
151 		case SSTOP:
152 			if (p->p_flag & P_INMEM) {
153 				if (p->p_priority <= PZERO)
154 					totalp->t_dw++;
155 				else if (p->p_slptime < maxslp)
156 					totalp->t_sl++;
157 			} else if (p->p_slptime < maxslp)
158 				totalp->t_sw++;
159 			if (p->p_slptime >= maxslp)
160 				continue;
161 			break;
162 
163 		case SRUN:
164 		case SIDL:
165 			if (p->p_flag & P_INMEM)
166 				totalp->t_rq++;
167 			else
168 				totalp->t_sw++;
169 			if (p->p_stat == SIDL)
170 				continue;
171 			break;
172 		}
173 		/*
174 		 * Note active objects.
175 		 */
176 		paging = 0;
177 		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
178 		    entry != &map->header; entry = entry->next) {
179 			if (entry->is_a_map || entry->is_sub_map ||
180 			    entry->object.vm_object == NULL)
181 				continue;
182 			entry->object.vm_object->flags |= OBJ_ACTIVE;
183 			paging |= entry->object.vm_object->paging_in_progress;
184 		}
185 		if (paging)
186 			totalp->t_pw++;
187 	}
188 	/*
189 	 * Calculate object memory usage statistics.
190 	 */
191 	for (object = vm_object_list.tqh_first;
192 	    object != NULL;
193 	    object = object->object_list.tqe_next) {
194 		totalp->t_vm += num_pages(object->size);
195 		totalp->t_rm += object->resident_page_count;
196 		if (object->flags & OBJ_ACTIVE) {
197 			totalp->t_avm += num_pages(object->size);
198 			totalp->t_arm += object->resident_page_count;
199 		}
200 		if (object->ref_count > 1) {
201 			/* shared object */
202 			totalp->t_vmshr += num_pages(object->size);
203 			totalp->t_rmshr += object->resident_page_count;
204 			if (object->flags & OBJ_ACTIVE) {
205 				totalp->t_avmshr += num_pages(object->size);
206 				totalp->t_armshr += object->resident_page_count;
207 			}
208 		}
209 	}
210 	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
211 	return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
212 }
213 
214 SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
215 	0, sizeof(struct vmtotal), vmtotal, "");
216