uvm_meter.c revision 1.13 1 /* $NetBSD: uvm_meter.c,v 1.13 2000/06/27 17:29:27 mrg 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 <uvm/uvm_extern.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();
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 SONPROC:
107 case SIDL:
108 nrun++;
109 }
110 }
111 proclist_unlock_read();
112 for (i = 0; i < 3; i++)
113 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
114 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
115 }
116
117 /*
118 * uvm_sysctl: sysctl hook into UVM system.
119 */
120 int
121 uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
122 int *name;
123 u_int namelen;
124 void *oldp;
125 size_t *oldlenp;
126 void *newp;
127 size_t newlen;
128 struct proc *p;
129 {
130 struct vmtotal vmtotals;
131
132 /* all sysctl names at this level are terminal */
133 if (namelen != 1)
134 return (ENOTDIR); /* overloaded */
135
136 switch (name[0]) {
137 case VM_LOADAVG:
138 return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
139 sizeof(averunnable)));
140
141 case VM_METER:
142 uvm_total(&vmtotals);
143 return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
144 sizeof(vmtotals)));
145
146 case VM_UVMEXP:
147 return (sysctl_rdstruct(oldp, oldlenp, newp, &uvmexp,
148 sizeof(uvmexp)));
149
150 case VM_NKMEMPAGES:
151 return (sysctl_rdint(oldp, oldlenp, newp, nkmempages));
152
153 default:
154 return (EOPNOTSUPP);
155 }
156 /* NOTREACHED */
157 }
158
159 /*
160 * uvm_total: calculate the current state of the system.
161 */
162 static void
163 uvm_total(totalp)
164 struct vmtotal *totalp;
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 memset(totalp, 0, sizeof *totalp);
174
175 /*
176 * calculate process statistics
177 */
178
179 proclist_lock_read();
180 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
181 if (p->p_flag & P_SYSTEM)
182 continue;
183 switch (p->p_stat) {
184 case 0:
185 continue;
186
187 case SSLEEP:
188 case SSTOP:
189 if (p->p_flag & P_INMEM) {
190 if (p->p_priority <= PZERO)
191 totalp->t_dw++;
192 else if (p->p_slptime < maxslp)
193 totalp->t_sl++;
194 } else if (p->p_slptime < maxslp)
195 totalp->t_sw++;
196 if (p->p_slptime >= maxslp)
197 continue;
198 break;
199
200 case SRUN:
201 case SONPROC:
202 case SIDL:
203 if (p->p_flag & P_INMEM)
204 totalp->t_rq++;
205 else
206 totalp->t_sw++;
207 if (p->p_stat == SIDL)
208 continue;
209 break;
210 }
211 /*
212 * note active objects
213 */
214 #if 0
215 /*
216 * XXXCDC: BOGUS! rethink this. in the mean time
217 * don't do it.
218 */
219 paging = 0;
220 vm_map_lock(map);
221 for (map = &p->p_vmspace->vm_map, entry = map->header.next;
222 entry != &map->header; entry = entry->next) {
223 if (entry->is_a_map || entry->is_sub_map ||
224 entry->object.uvm_obj == NULL)
225 continue;
226 /* XXX how to do this with uvm */
227 }
228 vm_map_unlock(map);
229 if (paging)
230 totalp->t_pw++;
231 #endif
232 }
233 proclist_unlock_read();
234 /*
235 * Calculate object memory usage statistics.
236 */
237 totalp->t_free = uvmexp.free;
238 totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
239 totalp->t_avm = uvmexp.active + uvmexp.swpginuse; /* XXX */
240 totalp->t_rm = uvmexp.npages - uvmexp.free;
241 totalp->t_arm = uvmexp.active;
242 totalp->t_vmshr = 0; /* XXX */
243 totalp->t_avmshr = 0; /* XXX */
244 totalp->t_rmshr = 0; /* XXX */
245 totalp->t_armshr = 0; /* XXX */
246 }
247