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