xref: /freebsd/sys/vm/vm_meter.c (revision 05c7a37afb48ddd5ee1bd921a5d46fe59cc70b15)
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.13 1995/12/14 09:55:02 phk 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 <sys/vmmeter.h>
42 #include <sys/queue.h>
43 
44 #include <vm/vm.h>
45 #include <vm/vm_extern.h>
46 #include <vm/vm_param.h>
47 #include <vm/vm_prot.h>
48 #include <vm/lock.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_object.h>
52 #include <sys/sysctl.h>
53 
54 struct loadavg averunnable =
55 	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
56 
57 struct vmmeter cnt;
58 
59 static int maxslp = MAXSLP;
60 
61 /*
62  * Constants for averages over 1, 5, and 15 minutes
63  * when sampling at 5 second intervals.
64  */
65 static fixpt_t cexp[3] = {
66 	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
67 	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
68 	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
69 };
70 
71 /*
72  * Compute a tenex style load average of a quantity on
73  * 1, 5 and 15 minute intervals.
74  */
75 static void
76 loadav(struct loadavg *avg)
77 {
78 	register int i, nrun;
79 	register struct proc *p;
80 
81 	for (nrun = 0, p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
82 		switch (p->p_stat) {
83 		case SSLEEP:
84 			if (p->p_priority > PZERO || p->p_slptime != 0)
85 				continue;
86 			/* fall through */
87 		case SRUN:
88 		case SIDL:
89 			nrun++;
90 		}
91 	}
92 	for (i = 0; i < 3; i++)
93 		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
94 		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
95 }
96 
97 void
98 vmmeter()
99 {
100 
101 	if (time.tv_sec % 5 == 0)
102 		loadav(&averunnable);
103 	if (proc0.p_slptime > maxslp / 2)
104 		wakeup(&proc0);
105 }
106 
107 SYSCTL_INT(_vm, VM_V_FREE_MIN, v_free_min,
108 	CTLFLAG_RW, &cnt.v_free_min, 0, "");
109 SYSCTL_INT(_vm, VM_V_FREE_TARGET, v_free_target,
110 	CTLFLAG_RW, &cnt.v_free_target, 0, "");
111 SYSCTL_INT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
112 	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
113 SYSCTL_INT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
114 	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
115 SYSCTL_INT(_vm, VM_V_CACHE_MIN, v_cache_min,
116 	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
117 SYSCTL_INT(_vm, VM_V_CACHE_MAX, v_cache_max,
118 	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
119 SYSCTL_INT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
120 	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
121 
122 SYSCTL_STRUCT(_vm, VM_LOADAVG, loadavg, CTLFLAG_RD, &averunnable, loadavg, "");
123 
124 static int
125 vmtotal SYSCTL_HANDLER_ARGS
126 {
127 	struct proc *p;
128 	struct vmtotal total, *totalp;
129 	vm_map_entry_t entry;
130 	vm_object_t object;
131 	vm_map_t map;
132 	int paging;
133 
134 	totalp = &total;
135 	bzero(totalp, sizeof *totalp);
136 	/*
137 	 * Mark all objects as inactive.
138 	 */
139 	for (object = vm_object_list.tqh_first;
140 	    object != NULL;
141 	    object = object->object_list.tqe_next)
142 		object->flags &= ~OBJ_ACTIVE;
143 	/*
144 	 * Calculate process statistics.
145 	 */
146 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
147 		if (p->p_flag & P_SYSTEM)
148 			continue;
149 		switch (p->p_stat) {
150 		case 0:
151 			continue;
152 
153 		case SSLEEP:
154 		case SSTOP:
155 			if (p->p_flag & P_INMEM) {
156 				if (p->p_priority <= PZERO)
157 					totalp->t_dw++;
158 				else if (p->p_slptime < maxslp)
159 					totalp->t_sl++;
160 			} else if (p->p_slptime < maxslp)
161 				totalp->t_sw++;
162 			if (p->p_slptime >= maxslp)
163 				continue;
164 			break;
165 
166 		case SRUN:
167 		case SIDL:
168 			if (p->p_flag & P_INMEM)
169 				totalp->t_rq++;
170 			else
171 				totalp->t_sw++;
172 			if (p->p_stat == SIDL)
173 				continue;
174 			break;
175 		}
176 		/*
177 		 * Note active objects.
178 		 */
179 		paging = 0;
180 		for (map = &p->p_vmspace->vm_map, entry = map->header.next;
181 		    entry != &map->header; entry = entry->next) {
182 			if (entry->is_a_map || entry->is_sub_map ||
183 			    entry->object.vm_object == NULL)
184 				continue;
185 			entry->object.vm_object->flags |= OBJ_ACTIVE;
186 			paging |= entry->object.vm_object->paging_in_progress;
187 		}
188 		if (paging)
189 			totalp->t_pw++;
190 	}
191 	/*
192 	 * Calculate object memory usage statistics.
193 	 */
194 	for (object = vm_object_list.tqh_first;
195 	    object != NULL;
196 	    object = object->object_list.tqe_next) {
197 		totalp->t_vm += num_pages(object->size);
198 		totalp->t_rm += object->resident_page_count;
199 		if (object->flags & OBJ_ACTIVE) {
200 			totalp->t_avm += num_pages(object->size);
201 			totalp->t_arm += object->resident_page_count;
202 		}
203 		if (object->ref_count > 1) {
204 			/* shared object */
205 			totalp->t_vmshr += num_pages(object->size);
206 			totalp->t_rmshr += object->resident_page_count;
207 			if (object->flags & OBJ_ACTIVE) {
208 				totalp->t_avmshr += num_pages(object->size);
209 				totalp->t_armshr += object->resident_page_count;
210 			}
211 		}
212 	}
213 	totalp->t_free = cnt.v_free_count + cnt.v_cache_count;
214 	return (sysctl_handle_opaque(oidp, totalp, sizeof total, req));
215 }
216 
217 SYSCTL_PROC(_vm, VM_METER, vmmeter, CTLTYPE_OPAQUE|CTLFLAG_RD,
218 	0, sizeof(struct vmtotal), vmtotal, "S,vmtotal", "");
219