uvm_meter.c revision 1.1 1 /* $Id: uvm_meter.c,v 1.1 1998/02/05 06:25:09 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 */
45
46 #include <sys/param.h>
47 #include <sys/proc.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <vm/vm.h>
51 #include <sys/sysctl.h>
52
53 /*
54 * maxslp: ???? XXXCDC
55 */
56
57 int maxslp = MAXSLP; /* patchable ... */
58 struct loadavg averunnable; /* decl. */
59
60 /*
61 * constants for averages over 1, 5, and 15 minutes when sampling at
62 * 5 second intervals.
63 */
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 * prototypes
73 */
74
75 static void uvm_loadav __P((struct loadavg *));
76 static void uvm_total __P((struct vmtotal *));
77
78 /*
79 * uvm_meter: calculate load average and wake up the swapper (if needed)
80 */
81
82 void uvm_meter()
83
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 uvm_loadav(avg)
96
97 struct loadavg *avg;
98
99 {
100 int i, nrun;
101 struct proc *p;
102
103 for (nrun = 0, p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
104 switch (p->p_stat) {
105 case SSLEEP:
106 if (p->p_priority > PZERO || p->p_slptime > 1)
107 continue;
108 /* fall through */
109 case SRUN:
110 case SIDL:
111 nrun++;
112 }
113 }
114 for (i = 0; i < 3; i++)
115 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
116 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
117 }
118
119 /*
120 * uvm_sysctl: sysctl hook into UVM system.
121 */
122 int uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
123
124 int *name;
125 u_int namelen;
126 void *oldp;
127 size_t *oldlenp;
128 void *newp;
129 size_t newlen;
130 struct proc *p;
131
132 {
133 struct vmtotal vmtotals;
134
135 /* all sysctl names at this level are terminal */
136 if (namelen != 1)
137 return (ENOTDIR); /* overloaded */
138
139 switch (name[0]) {
140 case VM_LOADAVG:
141 return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
142 sizeof(averunnable)));
143 case VM_METER:
144 uvm_total(&vmtotals);
145 return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
146 sizeof(vmtotals)));
147 case VM_UVMEXP:
148 return (sysctl_rdstruct(oldp, oldlenp, newp, &uvmexp, sizeof(uvmexp)));
149
150 default:
151 return (EOPNOTSUPP);
152 }
153 /* NOTREACHED */
154 }
155
156 /*
157 * uvm_total: calculate the current state of the system.
158 */
159
160 static void uvm_total(totalp)
161
162 struct vmtotal *totalp;
163
164 {
165 struct proc *p;
166 #if 0
167 vm_map_entry_t entry;
168 vm_map_t map;
169 int paging;
170 #endif
171
172 bzero(totalp, sizeof *totalp);
173
174 /*
175 * calculate process statistics
176 */
177
178 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
179 if (p->p_flag & P_SYSTEM)
180 continue;
181 switch (p->p_stat) {
182 case 0:
183 continue;
184
185 case SSLEEP:
186 case SSTOP:
187 if (p->p_flag & P_INMEM) {
188 if (p->p_priority <= PZERO)
189 totalp->t_dw++;
190 else if (p->p_slptime < maxslp)
191 totalp->t_sl++;
192 } else if (p->p_slptime < maxslp)
193 totalp->t_sw++;
194 if (p->p_slptime >= maxslp)
195 continue;
196 break;
197
198 case SRUN:
199 case SIDL:
200 if (p->p_flag & P_INMEM)
201 totalp->t_rq++;
202 else
203 totalp->t_sw++;
204 if (p->p_stat == SIDL)
205 continue;
206 break;
207 }
208 /*
209 * note active objects
210 */
211 #if 0
212 /*
213 * XXXCDC: BOGUS! you can't walk a map entry chain without
214 * first locking the map. rethink this. in the mean time
215 * just don't do it.
216 */
217 paging = 0;
218 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
219 entry != &map->header; entry = entry->next) {
220 if (entry->is_a_map || entry->is_sub_map ||
221 entry->object.vm_object == NULL)
222 continue;
223 entry->object.vm_object->flags |= OBJ_ACTIVE;
224 paging |= vm_object_paging(entry->object.vm_object);
225 }
226 if (paging)
227 totalp->t_pw++;
228 #endif
229 }
230 /*
231 * Calculate object memory usage statistics.
232 */
233 #if 0 /* XXXCDC: rethink! rethink! */
234 simple_lock(&vm_object_list_lock);
235 for (object = vm_object_list.tqh_first;
236 object != NULL;
237 object = object->object_list.tqe_next) {
238 totalp->t_vm += num_pages(object->size);
239 totalp->t_rm += object->resident_page_count;
240 if (object->flags & OBJ_ACTIVE) {
241 totalp->t_avm += num_pages(object->size);
242 totalp->t_arm += object->resident_page_count;
243 }
244 if (object->ref_count > 1) {
245 /* shared object */
246 totalp->t_vmshr += num_pages(object->size);
247 totalp->t_rmshr += object->resident_page_count;
248 if (object->flags & OBJ_ACTIVE) {
249 totalp->t_avmshr += num_pages(object->size);
250 totalp->t_armshr += object->resident_page_count;
251 }
252 }
253 }
254 totalp->t_free = cnt.v_free_count;
255 #endif
256 }
257