uvm_meter.c revision 1.3 1 /* $NetBSD: uvm_meter.c,v 1.3 1998/02/07 11:09:08 mrg Exp $ */
2
3 /*
4 * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
5 * >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
6 */
7 /*
8 * Copyright (c) 1997 Charles D. Cranor and Washington University.
9 * Copyright (c) 1982, 1986, 1989, 1993
10 * The Regents of the University of California.
11 *
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by Charles D. Cranor,
25 * Washington University, and the University of California, Berkeley
26 * and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 *
43 * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94
44 * from: Id: uvm_meter.c,v 1.1.2.1 1997/08/14 19:10:35 chuck Exp
45 */
46
47 #include <sys/param.h>
48 #include <sys/proc.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <vm/vm.h>
52 #include <sys/sysctl.h>
53
54 /*
55 * maxslp: ???? XXXCDC
56 */
57
58 int maxslp = MAXSLP; /* patchable ... */
59 struct loadavg averunnable; /* decl. */
60
61 /*
62 * constants for averages over 1, 5, and 15 minutes when sampling at
63 * 5 second intervals.
64 */
65
66 static fixpt_t cexp[3] = {
67 0.9200444146293232 * FSCALE, /* exp(-1/12) */
68 0.9834714538216174 * FSCALE, /* exp(-1/60) */
69 0.9944598480048967 * FSCALE, /* exp(-1/180) */
70 };
71
72 /*
73 * prototypes
74 */
75
76 static void uvm_loadav __P((struct loadavg *));
77 static void uvm_total __P((struct vmtotal *));
78
79 /*
80 * uvm_meter: calculate load average and wake up the swapper (if needed)
81 */
82
83 void uvm_meter()
84
85 {
86 if ((time.tv_sec % 5) == 0)
87 uvm_loadav(&averunnable);
88 if (proc0.p_slptime > (maxslp / 2))
89 wakeup((caddr_t)&proc0);
90 }
91
92 /*
93 * uvm_loadav: compute a tenex style load average of a quantity on
94 * 1, 5, and 15 minute internvals.
95 */
96 static void uvm_loadav(avg)
97
98 struct loadavg *avg;
99
100 {
101 int i, nrun;
102 struct proc *p;
103
104 for (nrun = 0, p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
105 switch (p->p_stat) {
106 case SSLEEP:
107 if (p->p_priority > PZERO || p->p_slptime > 1)
108 continue;
109 /* fall through */
110 case SRUN:
111 case SIDL:
112 nrun++;
113 }
114 }
115 for (i = 0; i < 3; i++)
116 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
117 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
118 }
119
120 /*
121 * uvm_sysctl: sysctl hook into UVM system.
122 */
123 int uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
124
125 int *name;
126 u_int namelen;
127 void *oldp;
128 size_t *oldlenp;
129 void *newp;
130 size_t newlen;
131 struct proc *p;
132
133 {
134 struct vmtotal vmtotals;
135
136 /* all sysctl names at this level are terminal */
137 if (namelen != 1)
138 return (ENOTDIR); /* overloaded */
139
140 switch (name[0]) {
141 case VM_LOADAVG:
142 return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
143 sizeof(averunnable)));
144 case VM_METER:
145 uvm_total(&vmtotals);
146 return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
147 sizeof(vmtotals)));
148 case VM_UVMEXP:
149 return (sysctl_rdstruct(oldp, oldlenp, newp, &uvmexp, sizeof(uvmexp)));
150
151 default:
152 return (EOPNOTSUPP);
153 }
154 /* NOTREACHED */
155 }
156
157 /*
158 * uvm_total: calculate the current state of the system.
159 */
160
161 static void uvm_total(totalp)
162
163 struct vmtotal *totalp;
164
165 {
166 struct proc *p;
167 #if 0
168 vm_map_entry_t entry;
169 vm_map_t map;
170 int paging;
171 #endif
172
173 bzero(totalp, sizeof *totalp);
174
175 /*
176 * calculate process statistics
177 */
178
179 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
180 if (p->p_flag & P_SYSTEM)
181 continue;
182 switch (p->p_stat) {
183 case 0:
184 continue;
185
186 case SSLEEP:
187 case SSTOP:
188 if (p->p_flag & P_INMEM) {
189 if (p->p_priority <= PZERO)
190 totalp->t_dw++;
191 else if (p->p_slptime < maxslp)
192 totalp->t_sl++;
193 } else if (p->p_slptime < maxslp)
194 totalp->t_sw++;
195 if (p->p_slptime >= maxslp)
196 continue;
197 break;
198
199 case SRUN:
200 case SIDL:
201 if (p->p_flag & P_INMEM)
202 totalp->t_rq++;
203 else
204 totalp->t_sw++;
205 if (p->p_stat == SIDL)
206 continue;
207 break;
208 }
209 /*
210 * note active objects
211 */
212 #if 0
213 /*
214 * XXXCDC: BOGUS! you can't walk a map entry chain without
215 * first locking the map. rethink this. in the mean time
216 * just don't do it.
217 */
218 paging = 0;
219 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
220 entry != &map->header; entry = entry->next) {
221 if (entry->is_a_map || entry->is_sub_map ||
222 entry->object.vm_object == NULL)
223 continue;
224 entry->object.vm_object->flags |= OBJ_ACTIVE;
225 paging |= vm_object_paging(entry->object.vm_object);
226 }
227 if (paging)
228 totalp->t_pw++;
229 #endif
230 }
231 /*
232 * Calculate object memory usage statistics.
233 */
234 #if 0 /* XXXCDC: rethink! rethink! */
235 simple_lock(&vm_object_list_lock);
236 for (object = vm_object_list.tqh_first;
237 object != NULL;
238 object = object->object_list.tqe_next) {
239 totalp->t_vm += num_pages(object->size);
240 totalp->t_rm += object->resident_page_count;
241 if (object->flags & OBJ_ACTIVE) {
242 totalp->t_avm += num_pages(object->size);
243 totalp->t_arm += object->resident_page_count;
244 }
245 if (object->ref_count > 1) {
246 /* shared object */
247 totalp->t_vmshr += num_pages(object->size);
248 totalp->t_rmshr += object->resident_page_count;
249 if (object->flags & OBJ_ACTIVE) {
250 totalp->t_avmshr += num_pages(object->size);
251 totalp->t_armshr += object->resident_page_count;
252 }
253 }
254 }
255 totalp->t_free = cnt.v_free_count;
256 #endif
257 }
258