uvm_meter.c revision 1.4 1 /* $NetBSD: uvm_meter.c,v 1.4 1998/02/07 12:45:53 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 void
83 uvm_meter()
84 {
85 if ((time.tv_sec % 5) == 0)
86 uvm_loadav(&averunnable);
87 if (proc0.p_slptime > (maxslp / 2))
88 wakeup((caddr_t)&proc0);
89 }
90
91 /*
92 * uvm_loadav: compute a tenex style load average of a quantity on
93 * 1, 5, and 15 minute internvals.
94 */
95 static void
96 uvm_loadav(avg)
97 struct loadavg *avg;
98 {
99 int i, nrun;
100 struct proc *p;
101
102 for (nrun = 0, p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
103 switch (p->p_stat) {
104 case SSLEEP:
105 if (p->p_priority > PZERO || p->p_slptime > 1)
106 continue;
107 /* fall through */
108 case SRUN:
109 case SIDL:
110 nrun++;
111 }
112 }
113 for (i = 0; i < 3; i++)
114 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
115 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
116 }
117
118 /*
119 * uvm_sysctl: sysctl hook into UVM system.
120 */
121 int
122 uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
123 int *name;
124 u_int namelen;
125 void *oldp;
126 size_t *oldlenp;
127 void *newp;
128 size_t newlen;
129 struct proc *p;
130 {
131 struct vmtotal vmtotals;
132
133 /* all sysctl names at this level are terminal */
134 if (namelen != 1)
135 return (ENOTDIR); /* overloaded */
136
137 switch (name[0]) {
138 case VM_LOADAVG:
139 return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
140 sizeof(averunnable)));
141
142 case VM_METER:
143 uvm_total(&vmtotals);
144 return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
145 sizeof(vmtotals)));
146
147 case VM_UVMEXP:
148 return (sysctl_rdstruct(oldp, oldlenp, newp, &uvmexp,
149 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 static void
161 uvm_total(totalp)
162 struct vmtotal *totalp;
163 {
164 struct proc *p;
165 #if 0
166 vm_map_entry_t entry;
167 vm_map_t map;
168 int paging;
169 #endif
170
171 bzero(totalp, sizeof *totalp);
172
173 /*
174 * calculate process statistics
175 */
176
177 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
178 if (p->p_flag & P_SYSTEM)
179 continue;
180 switch (p->p_stat) {
181 case 0:
182 continue;
183
184 case SSLEEP:
185 case SSTOP:
186 if (p->p_flag & P_INMEM) {
187 if (p->p_priority <= PZERO)
188 totalp->t_dw++;
189 else if (p->p_slptime < maxslp)
190 totalp->t_sl++;
191 } else if (p->p_slptime < maxslp)
192 totalp->t_sw++;
193 if (p->p_slptime >= maxslp)
194 continue;
195 break;
196
197 case SRUN:
198 case SIDL:
199 if (p->p_flag & P_INMEM)
200 totalp->t_rq++;
201 else
202 totalp->t_sw++;
203 if (p->p_stat == SIDL)
204 continue;
205 break;
206 }
207 /*
208 * note active objects
209 */
210 #if 0
211 /*
212 * XXXCDC: BOGUS! you can't walk a map entry chain without
213 * first locking the map. rethink this. in the mean time
214 * just don't do it.
215 */
216 paging = 0;
217 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
218 entry != &map->header; entry = entry->next) {
219 if (entry->is_a_map || entry->is_sub_map ||
220 entry->object.vm_object == NULL)
221 continue;
222 entry->object.vm_object->flags |= OBJ_ACTIVE;
223 paging |= vm_object_paging(entry->object.vm_object);
224 }
225 if (paging)
226 totalp->t_pw++;
227 #endif
228 }
229 /*
230 * Calculate object memory usage statistics.
231 */
232 #if 0 /* XXXCDC: rethink! rethink! */
233 simple_lock(&vm_object_list_lock);
234 for (object = vm_object_list.tqh_first;
235 object != NULL;
236 object = object->object_list.tqe_next) {
237 totalp->t_vm += num_pages(object->size);
238 totalp->t_rm += object->resident_page_count;
239 if (object->flags & OBJ_ACTIVE) {
240 totalp->t_avm += num_pages(object->size);
241 totalp->t_arm += object->resident_page_count;
242 }
243 if (object->ref_count > 1) {
244 /* shared object */
245 totalp->t_vmshr += num_pages(object->size);
246 totalp->t_rmshr += object->resident_page_count;
247 if (object->flags & OBJ_ACTIVE) {
248 totalp->t_avmshr += num_pages(object->size);
249 totalp->t_armshr += object->resident_page_count;
250 }
251 }
252 }
253 totalp->t_free = cnt.v_free_count;
254 #endif
255 }
256