kern_synch.c revision 1.1.1.3 1 1.1.1.2 fvdl /*-
2 1.1.1.2 fvdl * Copyright (c) 1982, 1986, 1990, 1991, 1993
3 1.1.1.2 fvdl * The Regents of the University of California. All rights reserved.
4 1.1.1.2 fvdl * (c) UNIX System Laboratories, Inc.
5 1.1.1.2 fvdl * All or some portions of this file are derived from material licensed
6 1.1.1.2 fvdl * to the University of California by American Telephone and Telegraph
7 1.1.1.2 fvdl * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 1.1.1.2 fvdl * the permission of UNIX System Laboratories, Inc.
9 1.1.1.2 fvdl *
10 1.1.1.2 fvdl * Redistribution and use in source and binary forms, with or without
11 1.1.1.2 fvdl * modification, are permitted provided that the following conditions
12 1.1.1.2 fvdl * are met:
13 1.1.1.2 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1.1.2 fvdl * notice, this list of conditions and the following disclaimer.
15 1.1.1.2 fvdl * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.1.2 fvdl * notice, this list of conditions and the following disclaimer in the
17 1.1.1.2 fvdl * documentation and/or other materials provided with the distribution.
18 1.1.1.2 fvdl * 3. All advertising materials mentioning features or use of this software
19 1.1.1.2 fvdl * must display the following acknowledgement:
20 1.1.1.2 fvdl * This product includes software developed by the University of
21 1.1.1.2 fvdl * California, Berkeley and its contributors.
22 1.1.1.2 fvdl * 4. Neither the name of the University nor the names of its contributors
23 1.1.1.2 fvdl * may be used to endorse or promote products derived from this software
24 1.1.1.2 fvdl * without specific prior written permission.
25 1.1.1.2 fvdl *
26 1.1.1.2 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1.1.2 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1.1.2 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1.1.2 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1.1.2 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1.1.2 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1.1.2 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1.1.2 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1.1.2 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1.1.2 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1.1.2 fvdl * SUCH DAMAGE.
37 1.1.1.2 fvdl *
38 1.1.1.3 fvdl * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
39 1.1.1.2 fvdl */
40 1.1.1.2 fvdl
41 1.1.1.2 fvdl #include <sys/param.h>
42 1.1.1.2 fvdl #include <sys/systm.h>
43 1.1.1.2 fvdl #include <sys/proc.h>
44 1.1.1.2 fvdl #include <sys/kernel.h>
45 1.1.1.2 fvdl #include <sys/buf.h>
46 1.1.1.2 fvdl #include <sys/signalvar.h>
47 1.1.1.2 fvdl #include <sys/resourcevar.h>
48 1.1.1.2 fvdl #include <sys/vmmeter.h>
49 1.1.1.2 fvdl #ifdef KTRACE
50 1.1.1.2 fvdl #include <sys/ktrace.h>
51 1.1.1.2 fvdl #endif
52 1.1.1.2 fvdl
53 1.1.1.2 fvdl #include <machine/cpu.h>
54 1.1.1.2 fvdl
55 1.1.1.2 fvdl u_char curpriority; /* usrpri of curproc */
56 1.1.1.2 fvdl int lbolt; /* once a second sleep address */
57 1.1.1.2 fvdl
58 1.1.1.2 fvdl /*
59 1.1.1.2 fvdl * Force switch among equal priority processes every 100ms.
60 1.1.1.2 fvdl */
61 1.1.1.2 fvdl /* ARGSUSED */
62 1.1.1.2 fvdl void
63 1.1.1.2 fvdl roundrobin(arg)
64 1.1.1.2 fvdl void *arg;
65 1.1.1.2 fvdl {
66 1.1.1.2 fvdl
67 1.1.1.2 fvdl need_resched();
68 1.1.1.2 fvdl timeout(roundrobin, NULL, hz / 10);
69 1.1.1.2 fvdl }
70 1.1.1.2 fvdl
71 1.1.1.2 fvdl /*
72 1.1.1.2 fvdl * Constants for digital decay and forget:
73 1.1.1.2 fvdl * 90% of (p_estcpu) usage in 5 * loadav time
74 1.1.1.2 fvdl * 95% of (p_pctcpu) usage in 60 seconds (load insensitive)
75 1.1.1.2 fvdl * Note that, as ps(1) mentions, this can let percentages
76 1.1.1.2 fvdl * total over 100% (I've seen 137.9% for 3 processes).
77 1.1.1.2 fvdl *
78 1.1.1.2 fvdl * Note that hardclock updates p_estcpu and p_cpticks independently.
79 1.1.1.2 fvdl *
80 1.1.1.2 fvdl * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
81 1.1.1.2 fvdl * That is, the system wants to compute a value of decay such
82 1.1.1.2 fvdl * that the following for loop:
83 1.1.1.2 fvdl * for (i = 0; i < (5 * loadavg); i++)
84 1.1.1.2 fvdl * p_estcpu *= decay;
85 1.1.1.2 fvdl * will compute
86 1.1.1.2 fvdl * p_estcpu *= 0.1;
87 1.1.1.2 fvdl * for all values of loadavg:
88 1.1.1.2 fvdl *
89 1.1.1.2 fvdl * Mathematically this loop can be expressed by saying:
90 1.1.1.2 fvdl * decay ** (5 * loadavg) ~= .1
91 1.1.1.2 fvdl *
92 1.1.1.2 fvdl * The system computes decay as:
93 1.1.1.2 fvdl * decay = (2 * loadavg) / (2 * loadavg + 1)
94 1.1.1.2 fvdl *
95 1.1.1.2 fvdl * We wish to prove that the system's computation of decay
96 1.1.1.2 fvdl * will always fulfill the equation:
97 1.1.1.2 fvdl * decay ** (5 * loadavg) ~= .1
98 1.1.1.2 fvdl *
99 1.1.1.2 fvdl * If we compute b as:
100 1.1.1.2 fvdl * b = 2 * loadavg
101 1.1.1.2 fvdl * then
102 1.1.1.2 fvdl * decay = b / (b + 1)
103 1.1.1.2 fvdl *
104 1.1.1.2 fvdl * We now need to prove two things:
105 1.1.1.2 fvdl * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
106 1.1.1.2 fvdl * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
107 1.1.1.2 fvdl *
108 1.1.1.2 fvdl * Facts:
109 1.1.1.2 fvdl * For x close to zero, exp(x) =~ 1 + x, since
110 1.1.1.2 fvdl * exp(x) = 0! + x**1/1! + x**2/2! + ... .
111 1.1.1.2 fvdl * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
112 1.1.1.2 fvdl * For x close to zero, ln(1+x) =~ x, since
113 1.1.1.2 fvdl * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1
114 1.1.1.2 fvdl * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
115 1.1.1.2 fvdl * ln(.1) =~ -2.30
116 1.1.1.2 fvdl *
117 1.1.1.2 fvdl * Proof of (1):
118 1.1.1.2 fvdl * Solve (factor)**(power) =~ .1 given power (5*loadav):
119 1.1.1.2 fvdl * solving for factor,
120 1.1.1.2 fvdl * ln(factor) =~ (-2.30/5*loadav), or
121 1.1.1.2 fvdl * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
122 1.1.1.2 fvdl * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED
123 1.1.1.2 fvdl *
124 1.1.1.2 fvdl * Proof of (2):
125 1.1.1.2 fvdl * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
126 1.1.1.2 fvdl * solving for power,
127 1.1.1.2 fvdl * power*ln(b/(b+1)) =~ -2.30, or
128 1.1.1.2 fvdl * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
129 1.1.1.2 fvdl *
130 1.1.1.2 fvdl * Actual power values for the implemented algorithm are as follows:
131 1.1.1.2 fvdl * loadav: 1 2 3 4
132 1.1.1.2 fvdl * power: 5.68 10.32 14.94 19.55
133 1.1.1.2 fvdl */
134 1.1.1.2 fvdl
135 1.1.1.2 fvdl /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
136 1.1.1.2 fvdl #define loadfactor(loadav) (2 * (loadav))
137 1.1.1.2 fvdl #define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE))
138 1.1.1.2 fvdl
139 1.1.1.2 fvdl /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
140 1.1.1.2 fvdl fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */
141 1.1.1.2 fvdl
142 1.1.1.2 fvdl /*
143 1.1.1.2 fvdl * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
144 1.1.1.2 fvdl * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
145 1.1.1.2 fvdl * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
146 1.1.1.2 fvdl *
147 1.1.1.2 fvdl * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
148 1.1.1.2 fvdl * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
149 1.1.1.2 fvdl *
150 1.1.1.2 fvdl * If you dont want to bother with the faster/more-accurate formula, you
151 1.1.1.2 fvdl * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
152 1.1.1.2 fvdl * (more general) method of calculating the %age of CPU used by a process.
153 1.1.1.2 fvdl */
154 1.1.1.2 fvdl #define CCPU_SHIFT 11
155 1.1.1.2 fvdl
156 1.1.1.2 fvdl /*
157 1.1.1.2 fvdl * Recompute process priorities, every hz ticks.
158 1.1.1.2 fvdl */
159 1.1.1.2 fvdl /* ARGSUSED */
160 1.1.1.2 fvdl void
161 1.1.1.2 fvdl schedcpu(arg)
162 1.1.1.2 fvdl void *arg;
163 1.1.1.2 fvdl {
164 1.1.1.2 fvdl register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
165 1.1.1.2 fvdl register struct proc *p;
166 1.1.1.2 fvdl register int s;
167 1.1.1.2 fvdl register unsigned int newcpu;
168 1.1.1.2 fvdl
169 1.1.1.2 fvdl wakeup((caddr_t)&lbolt);
170 1.1.1.3 fvdl for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
171 1.1.1.2 fvdl /*
172 1.1.1.2 fvdl * Increment time in/out of memory and sleep time
173 1.1.1.2 fvdl * (if sleeping). We ignore overflow; with 16-bit int's
174 1.1.1.2 fvdl * (remember them?) overflow takes 45 days.
175 1.1.1.2 fvdl */
176 1.1.1.2 fvdl p->p_swtime++;
177 1.1.1.2 fvdl if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
178 1.1.1.2 fvdl p->p_slptime++;
179 1.1.1.2 fvdl p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
180 1.1.1.2 fvdl /*
181 1.1.1.2 fvdl * If the process has slept the entire second,
182 1.1.1.2 fvdl * stop recalculating its priority until it wakes up.
183 1.1.1.2 fvdl */
184 1.1.1.2 fvdl if (p->p_slptime > 1)
185 1.1.1.2 fvdl continue;
186 1.1.1.2 fvdl s = splstatclock(); /* prevent state changes */
187 1.1.1.2 fvdl /*
188 1.1.1.2 fvdl * p_pctcpu is only for ps.
189 1.1.1.2 fvdl */
190 1.1.1.2 fvdl #if (FSHIFT >= CCPU_SHIFT)
191 1.1.1.2 fvdl p->p_pctcpu += (hz == 100)?
192 1.1.1.2 fvdl ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
193 1.1.1.2 fvdl 100 * (((fixpt_t) p->p_cpticks)
194 1.1.1.2 fvdl << (FSHIFT - CCPU_SHIFT)) / hz;
195 1.1.1.2 fvdl #else
196 1.1.1.2 fvdl p->p_pctcpu += ((FSCALE - ccpu) *
197 1.1.1.2 fvdl (p->p_cpticks * FSCALE / hz)) >> FSHIFT;
198 1.1.1.2 fvdl #endif
199 1.1.1.2 fvdl p->p_cpticks = 0;
200 1.1.1.2 fvdl newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu) + p->p_nice;
201 1.1.1.2 fvdl p->p_estcpu = min(newcpu, UCHAR_MAX);
202 1.1.1.2 fvdl resetpriority(p);
203 1.1.1.2 fvdl if (p->p_priority >= PUSER) {
204 1.1.1.2 fvdl #define PPQ (128 / NQS) /* priorities per queue */
205 1.1.1.2 fvdl if ((p != curproc) &&
206 1.1.1.2 fvdl p->p_stat == SRUN &&
207 1.1.1.2 fvdl (p->p_flag & P_INMEM) &&
208 1.1.1.2 fvdl (p->p_priority / PPQ) != (p->p_usrpri / PPQ)) {
209 1.1.1.2 fvdl remrq(p);
210 1.1.1.2 fvdl p->p_priority = p->p_usrpri;
211 1.1.1.2 fvdl setrunqueue(p);
212 1.1.1.2 fvdl } else
213 1.1.1.2 fvdl p->p_priority = p->p_usrpri;
214 1.1.1.2 fvdl }
215 1.1.1.2 fvdl splx(s);
216 1.1.1.2 fvdl }
217 1.1.1.2 fvdl vmmeter();
218 1.1.1.2 fvdl if (bclnlist != NULL)
219 1.1.1.2 fvdl wakeup((caddr_t)pageproc);
220 1.1.1.2 fvdl timeout(schedcpu, (void *)0, hz);
221 1.1.1.2 fvdl }
222 1.1.1.2 fvdl
223 1.1.1.2 fvdl /*
224 1.1.1.2 fvdl * Recalculate the priority of a process after it has slept for a while.
225 1.1.1.2 fvdl * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
226 1.1.1.2 fvdl * least six times the loadfactor will decay p_estcpu to zero.
227 1.1.1.2 fvdl */
228 1.1.1.2 fvdl void
229 1.1.1.2 fvdl updatepri(p)
230 1.1.1.2 fvdl register struct proc *p;
231 1.1.1.2 fvdl {
232 1.1.1.2 fvdl register unsigned int newcpu = p->p_estcpu;
233 1.1.1.2 fvdl register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
234 1.1.1.2 fvdl
235 1.1.1.2 fvdl if (p->p_slptime > 5 * loadfac)
236 1.1.1.2 fvdl p->p_estcpu = 0;
237 1.1.1.2 fvdl else {
238 1.1.1.2 fvdl p->p_slptime--; /* the first time was done in schedcpu */
239 1.1.1.2 fvdl while (newcpu && --p->p_slptime)
240 1.1.1.2 fvdl newcpu = (int) decay_cpu(loadfac, newcpu);
241 1.1.1.2 fvdl p->p_estcpu = min(newcpu, UCHAR_MAX);
242 1.1.1.2 fvdl }
243 1.1.1.2 fvdl resetpriority(p);
244 1.1.1.2 fvdl }
245 1.1.1.2 fvdl
246 1.1.1.2 fvdl /*
247 1.1.1.2 fvdl * We're only looking at 7 bits of the address; everything is
248 1.1.1.2 fvdl * aligned to 4, lots of things are aligned to greater powers
249 1.1.1.2 fvdl * of 2. Shift right by 8, i.e. drop the bottom 256 worth.
250 1.1.1.2 fvdl */
251 1.1.1.2 fvdl #define TABLESIZE 128
252 1.1.1.3 fvdl #define LOOKUP(x) (((long)(x) >> 8) & (TABLESIZE - 1))
253 1.1.1.2 fvdl struct slpque {
254 1.1.1.2 fvdl struct proc *sq_head;
255 1.1.1.2 fvdl struct proc **sq_tailp;
256 1.1.1.2 fvdl } slpque[TABLESIZE];
257 1.1.1.2 fvdl
258 1.1.1.2 fvdl /*
259 1.1.1.2 fvdl * During autoconfiguration or after a panic, a sleep will simply
260 1.1.1.2 fvdl * lower the priority briefly to allow interrupts, then return.
261 1.1.1.2 fvdl * The priority to be used (safepri) is machine-dependent, thus this
262 1.1.1.2 fvdl * value is initialized and maintained in the machine-dependent layers.
263 1.1.1.2 fvdl * This priority will typically be 0, or the lowest priority
264 1.1.1.2 fvdl * that is safe for use on the interrupt stack; it can be made
265 1.1.1.2 fvdl * higher to block network software interrupts after panics.
266 1.1.1.2 fvdl */
267 1.1.1.2 fvdl int safepri;
268 1.1.1.2 fvdl
269 1.1.1.2 fvdl /*
270 1.1.1.2 fvdl * General sleep call. Suspends the current process until a wakeup is
271 1.1.1.2 fvdl * performed on the specified identifier. The process will then be made
272 1.1.1.2 fvdl * runnable with the specified priority. Sleeps at most timo/hz seconds
273 1.1.1.2 fvdl * (0 means no timeout). If pri includes PCATCH flag, signals are checked
274 1.1.1.2 fvdl * before and after sleeping, else signals are not checked. Returns 0 if
275 1.1.1.2 fvdl * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
276 1.1.1.2 fvdl * signal needs to be delivered, ERESTART is returned if the current system
277 1.1.1.2 fvdl * call should be restarted if possible, and EINTR is returned if the system
278 1.1.1.2 fvdl * call should be interrupted by the signal (return EINTR).
279 1.1.1.2 fvdl */
280 1.1.1.2 fvdl int
281 1.1.1.2 fvdl tsleep(ident, priority, wmesg, timo)
282 1.1.1.2 fvdl void *ident;
283 1.1.1.2 fvdl int priority, timo;
284 1.1.1.2 fvdl char *wmesg;
285 1.1.1.2 fvdl {
286 1.1.1.2 fvdl register struct proc *p = curproc;
287 1.1.1.2 fvdl register struct slpque *qp;
288 1.1.1.2 fvdl register s;
289 1.1.1.2 fvdl int sig, catch = priority & PCATCH;
290 1.1.1.2 fvdl extern int cold;
291 1.1.1.2 fvdl void endtsleep __P((void *));
292 1.1.1.2 fvdl
293 1.1.1.2 fvdl #ifdef KTRACE
294 1.1.1.2 fvdl if (KTRPOINT(p, KTR_CSW))
295 1.1.1.2 fvdl ktrcsw(p->p_tracep, 1, 0);
296 1.1.1.2 fvdl #endif
297 1.1.1.2 fvdl s = splhigh();
298 1.1.1.2 fvdl if (cold || panicstr) {
299 1.1.1.2 fvdl /*
300 1.1.1.2 fvdl * After a panic, or during autoconfiguration,
301 1.1.1.2 fvdl * just give interrupts a chance, then just return;
302 1.1.1.2 fvdl * don't run any other procs or panic below,
303 1.1.1.2 fvdl * in case this is the idle process and already asleep.
304 1.1.1.2 fvdl */
305 1.1.1.2 fvdl splx(safepri);
306 1.1.1.2 fvdl splx(s);
307 1.1.1.2 fvdl return (0);
308 1.1.1.2 fvdl }
309 1.1.1.2 fvdl #ifdef DIAGNOSTIC
310 1.1.1.2 fvdl if (ident == NULL || p->p_stat != SRUN || p->p_back)
311 1.1.1.2 fvdl panic("tsleep");
312 1.1.1.2 fvdl #endif
313 1.1.1.2 fvdl p->p_wchan = ident;
314 1.1.1.2 fvdl p->p_wmesg = wmesg;
315 1.1.1.2 fvdl p->p_slptime = 0;
316 1.1.1.2 fvdl p->p_priority = priority & PRIMASK;
317 1.1.1.2 fvdl qp = &slpque[LOOKUP(ident)];
318 1.1.1.2 fvdl if (qp->sq_head == 0)
319 1.1.1.2 fvdl qp->sq_head = p;
320 1.1.1.2 fvdl else
321 1.1.1.2 fvdl *qp->sq_tailp = p;
322 1.1.1.2 fvdl *(qp->sq_tailp = &p->p_forw) = 0;
323 1.1.1.2 fvdl if (timo)
324 1.1.1.2 fvdl timeout(endtsleep, (void *)p, timo);
325 1.1.1.2 fvdl /*
326 1.1.1.2 fvdl * We put ourselves on the sleep queue and start our timeout
327 1.1.1.2 fvdl * before calling CURSIG, as we could stop there, and a wakeup
328 1.1.1.2 fvdl * or a SIGCONT (or both) could occur while we were stopped.
329 1.1.1.2 fvdl * A SIGCONT would cause us to be marked as SSLEEP
330 1.1.1.2 fvdl * without resuming us, thus we must be ready for sleep
331 1.1.1.2 fvdl * when CURSIG is called. If the wakeup happens while we're
332 1.1.1.2 fvdl * stopped, p->p_wchan will be 0 upon return from CURSIG.
333 1.1.1.2 fvdl */
334 1.1.1.2 fvdl if (catch) {
335 1.1.1.2 fvdl p->p_flag |= P_SINTR;
336 1.1.1.2 fvdl if (sig = CURSIG(p)) {
337 1.1.1.2 fvdl if (p->p_wchan)
338 1.1.1.2 fvdl unsleep(p);
339 1.1.1.2 fvdl p->p_stat = SRUN;
340 1.1.1.2 fvdl goto resume;
341 1.1.1.2 fvdl }
342 1.1.1.2 fvdl if (p->p_wchan == 0) {
343 1.1.1.2 fvdl catch = 0;
344 1.1.1.2 fvdl goto resume;
345 1.1.1.2 fvdl }
346 1.1.1.2 fvdl } else
347 1.1.1.2 fvdl sig = 0;
348 1.1.1.2 fvdl p->p_stat = SSLEEP;
349 1.1.1.2 fvdl p->p_stats->p_ru.ru_nvcsw++;
350 1.1.1.2 fvdl mi_switch();
351 1.1.1.2 fvdl resume:
352 1.1.1.2 fvdl curpriority = p->p_usrpri;
353 1.1.1.2 fvdl splx(s);
354 1.1.1.2 fvdl p->p_flag &= ~P_SINTR;
355 1.1.1.2 fvdl if (p->p_flag & P_TIMEOUT) {
356 1.1.1.2 fvdl p->p_flag &= ~P_TIMEOUT;
357 1.1.1.2 fvdl if (sig == 0) {
358 1.1.1.2 fvdl #ifdef KTRACE
359 1.1.1.2 fvdl if (KTRPOINT(p, KTR_CSW))
360 1.1.1.2 fvdl ktrcsw(p->p_tracep, 0, 0);
361 1.1.1.2 fvdl #endif
362 1.1.1.2 fvdl return (EWOULDBLOCK);
363 1.1.1.2 fvdl }
364 1.1.1.2 fvdl } else if (timo)
365 1.1.1.2 fvdl untimeout(endtsleep, (void *)p);
366 1.1.1.2 fvdl if (catch && (sig != 0 || (sig = CURSIG(p)))) {
367 1.1.1.2 fvdl #ifdef KTRACE
368 1.1.1.2 fvdl if (KTRPOINT(p, KTR_CSW))
369 1.1.1.2 fvdl ktrcsw(p->p_tracep, 0, 0);
370 1.1.1.2 fvdl #endif
371 1.1.1.2 fvdl if (p->p_sigacts->ps_sigintr & sigmask(sig))
372 1.1.1.2 fvdl return (EINTR);
373 1.1.1.2 fvdl return (ERESTART);
374 1.1.1.2 fvdl }
375 1.1.1.2 fvdl #ifdef KTRACE
376 1.1.1.2 fvdl if (KTRPOINT(p, KTR_CSW))
377 1.1.1.2 fvdl ktrcsw(p->p_tracep, 0, 0);
378 1.1.1.2 fvdl #endif
379 1.1.1.2 fvdl return (0);
380 1.1.1.2 fvdl }
381 1.1.1.2 fvdl
382 1.1.1.2 fvdl /*
383 1.1.1.2 fvdl * Implement timeout for tsleep.
384 1.1.1.2 fvdl * If process hasn't been awakened (wchan non-zero),
385 1.1.1.2 fvdl * set timeout flag and undo the sleep. If proc
386 1.1.1.2 fvdl * is stopped, just unsleep so it will remain stopped.
387 1.1.1.2 fvdl */
388 1.1.1.2 fvdl void
389 1.1.1.2 fvdl endtsleep(arg)
390 1.1.1.2 fvdl void *arg;
391 1.1.1.2 fvdl {
392 1.1.1.2 fvdl register struct proc *p;
393 1.1.1.2 fvdl int s;
394 1.1.1.2 fvdl
395 1.1.1.2 fvdl p = (struct proc *)arg;
396 1.1.1.2 fvdl s = splhigh();
397 1.1.1.2 fvdl if (p->p_wchan) {
398 1.1.1.2 fvdl if (p->p_stat == SSLEEP)
399 1.1.1.2 fvdl setrunnable(p);
400 1.1.1.2 fvdl else
401 1.1.1.2 fvdl unsleep(p);
402 1.1.1.2 fvdl p->p_flag |= P_TIMEOUT;
403 1.1.1.2 fvdl }
404 1.1.1.2 fvdl splx(s);
405 1.1.1.2 fvdl }
406 1.1.1.2 fvdl
407 1.1.1.2 fvdl /*
408 1.1.1.2 fvdl * Short-term, non-interruptable sleep.
409 1.1.1.2 fvdl */
410 1.1.1.2 fvdl void
411 1.1.1.2 fvdl sleep(ident, priority)
412 1.1.1.2 fvdl void *ident;
413 1.1.1.2 fvdl int priority;
414 1.1.1.2 fvdl {
415 1.1.1.2 fvdl register struct proc *p = curproc;
416 1.1.1.2 fvdl register struct slpque *qp;
417 1.1.1.2 fvdl register s;
418 1.1.1.2 fvdl extern int cold;
419 1.1.1.2 fvdl
420 1.1.1.2 fvdl #ifdef DIAGNOSTIC
421 1.1.1.2 fvdl if (priority > PZERO) {
422 1.1.1.2 fvdl printf("sleep called with priority %d > PZERO, wchan: %x\n",
423 1.1.1.2 fvdl priority, ident);
424 1.1.1.2 fvdl panic("old sleep");
425 1.1.1.2 fvdl }
426 1.1.1.2 fvdl #endif
427 1.1.1.2 fvdl s = splhigh();
428 1.1.1.2 fvdl if (cold || panicstr) {
429 1.1.1.2 fvdl /*
430 1.1.1.2 fvdl * After a panic, or during autoconfiguration,
431 1.1.1.2 fvdl * just give interrupts a chance, then just return;
432 1.1.1.2 fvdl * don't run any other procs or panic below,
433 1.1.1.2 fvdl * in case this is the idle process and already asleep.
434 1.1.1.2 fvdl */
435 1.1.1.2 fvdl splx(safepri);
436 1.1.1.2 fvdl splx(s);
437 1.1.1.2 fvdl return;
438 1.1.1.2 fvdl }
439 1.1.1.2 fvdl #ifdef DIAGNOSTIC
440 1.1.1.2 fvdl if (ident == NULL || p->p_stat != SRUN || p->p_back)
441 1.1.1.2 fvdl panic("sleep");
442 1.1.1.2 fvdl #endif
443 1.1.1.2 fvdl p->p_wchan = ident;
444 1.1.1.2 fvdl p->p_wmesg = NULL;
445 1.1.1.2 fvdl p->p_slptime = 0;
446 1.1.1.2 fvdl p->p_priority = priority;
447 1.1.1.2 fvdl qp = &slpque[LOOKUP(ident)];
448 1.1.1.2 fvdl if (qp->sq_head == 0)
449 1.1.1.2 fvdl qp->sq_head = p;
450 1.1.1.2 fvdl else
451 1.1.1.2 fvdl *qp->sq_tailp = p;
452 1.1.1.2 fvdl *(qp->sq_tailp = &p->p_forw) = 0;
453 1.1.1.2 fvdl p->p_stat = SSLEEP;
454 1.1.1.2 fvdl p->p_stats->p_ru.ru_nvcsw++;
455 1.1.1.2 fvdl #ifdef KTRACE
456 1.1.1.2 fvdl if (KTRPOINT(p, KTR_CSW))
457 1.1.1.2 fvdl ktrcsw(p->p_tracep, 1, 0);
458 1.1.1.2 fvdl #endif
459 1.1.1.2 fvdl mi_switch();
460 1.1.1.2 fvdl #ifdef KTRACE
461 1.1.1.2 fvdl if (KTRPOINT(p, KTR_CSW))
462 1.1.1.2 fvdl ktrcsw(p->p_tracep, 0, 0);
463 1.1.1.2 fvdl #endif
464 1.1.1.2 fvdl curpriority = p->p_usrpri;
465 1.1.1.2 fvdl splx(s);
466 1.1.1.2 fvdl }
467 1.1.1.2 fvdl
468 1.1.1.2 fvdl /*
469 1.1.1.2 fvdl * Remove a process from its wait queue
470 1.1.1.2 fvdl */
471 1.1.1.2 fvdl void
472 1.1.1.2 fvdl unsleep(p)
473 1.1.1.2 fvdl register struct proc *p;
474 1.1.1.2 fvdl {
475 1.1.1.2 fvdl register struct slpque *qp;
476 1.1.1.2 fvdl register struct proc **hp;
477 1.1.1.2 fvdl int s;
478 1.1.1.2 fvdl
479 1.1.1.2 fvdl s = splhigh();
480 1.1.1.2 fvdl if (p->p_wchan) {
481 1.1.1.2 fvdl hp = &(qp = &slpque[LOOKUP(p->p_wchan)])->sq_head;
482 1.1.1.2 fvdl while (*hp != p)
483 1.1.1.2 fvdl hp = &(*hp)->p_forw;
484 1.1.1.2 fvdl *hp = p->p_forw;
485 1.1.1.2 fvdl if (qp->sq_tailp == &p->p_forw)
486 1.1.1.2 fvdl qp->sq_tailp = hp;
487 1.1.1.2 fvdl p->p_wchan = 0;
488 1.1.1.2 fvdl }
489 1.1.1.2 fvdl splx(s);
490 1.1.1.2 fvdl }
491 1.1.1.2 fvdl
492 1.1.1.2 fvdl /*
493 1.1.1.2 fvdl * Make all processes sleeping on the specified identifier runnable.
494 1.1.1.2 fvdl */
495 1.1.1.2 fvdl void
496 1.1.1.2 fvdl wakeup(ident)
497 1.1.1.2 fvdl register void *ident;
498 1.1.1.2 fvdl {
499 1.1.1.2 fvdl register struct slpque *qp;
500 1.1.1.2 fvdl register struct proc *p, **q;
501 1.1.1.2 fvdl int s;
502 1.1.1.2 fvdl
503 1.1.1.2 fvdl s = splhigh();
504 1.1.1.2 fvdl qp = &slpque[LOOKUP(ident)];
505 1.1.1.2 fvdl restart:
506 1.1.1.2 fvdl for (q = &qp->sq_head; p = *q; ) {
507 1.1.1.2 fvdl #ifdef DIAGNOSTIC
508 1.1.1.2 fvdl if (p->p_back || p->p_stat != SSLEEP && p->p_stat != SSTOP)
509 1.1.1.2 fvdl panic("wakeup");
510 1.1.1.2 fvdl #endif
511 1.1.1.2 fvdl if (p->p_wchan == ident) {
512 1.1.1.2 fvdl p->p_wchan = 0;
513 1.1.1.2 fvdl *q = p->p_forw;
514 1.1.1.2 fvdl if (qp->sq_tailp == &p->p_forw)
515 1.1.1.2 fvdl qp->sq_tailp = q;
516 1.1.1.2 fvdl if (p->p_stat == SSLEEP) {
517 1.1.1.2 fvdl /* OPTIMIZED EXPANSION OF setrunnable(p); */
518 1.1.1.2 fvdl if (p->p_slptime > 1)
519 1.1.1.2 fvdl updatepri(p);
520 1.1.1.2 fvdl p->p_slptime = 0;
521 1.1.1.2 fvdl p->p_stat = SRUN;
522 1.1.1.2 fvdl if (p->p_flag & P_INMEM)
523 1.1.1.2 fvdl setrunqueue(p);
524 1.1.1.2 fvdl /*
525 1.1.1.2 fvdl * Since curpriority is a user priority,
526 1.1.1.2 fvdl * p->p_priority is always better than
527 1.1.1.2 fvdl * curpriority.
528 1.1.1.2 fvdl */
529 1.1.1.2 fvdl if ((p->p_flag & P_INMEM) == 0)
530 1.1.1.2 fvdl wakeup((caddr_t)&proc0);
531 1.1.1.2 fvdl else
532 1.1.1.2 fvdl need_resched();
533 1.1.1.2 fvdl /* END INLINE EXPANSION */
534 1.1.1.2 fvdl goto restart;
535 1.1.1.2 fvdl }
536 1.1.1.2 fvdl } else
537 1.1.1.2 fvdl q = &p->p_forw;
538 1.1.1.2 fvdl }
539 1.1.1.2 fvdl splx(s);
540 1.1.1.2 fvdl }
541 1.1.1.2 fvdl
542 1.1.1.2 fvdl /*
543 1.1.1.2 fvdl * The machine independent parts of mi_switch().
544 1.1.1.2 fvdl * Must be called at splstatclock() or higher.
545 1.1.1.2 fvdl */
546 1.1.1.2 fvdl void
547 1.1.1.2 fvdl mi_switch()
548 1.1.1.2 fvdl {
549 1.1.1.2 fvdl register struct proc *p = curproc; /* XXX */
550 1.1.1.2 fvdl register struct rlimit *rlim;
551 1.1.1.2 fvdl register long s, u;
552 1.1.1.2 fvdl struct timeval tv;
553 1.1.1.2 fvdl
554 1.1.1.3 fvdl #ifdef DEBUG
555 1.1.1.3 fvdl if (p->p_simple_locks)
556 1.1.1.3 fvdl panic("sleep: holding simple lock");
557 1.1.1.3 fvdl #endif
558 1.1.1.2 fvdl /*
559 1.1.1.2 fvdl * Compute the amount of time during which the current
560 1.1.1.2 fvdl * process was running, and add that to its total so far.
561 1.1.1.2 fvdl */
562 1.1.1.2 fvdl microtime(&tv);
563 1.1.1.2 fvdl u = p->p_rtime.tv_usec + (tv.tv_usec - runtime.tv_usec);
564 1.1.1.2 fvdl s = p->p_rtime.tv_sec + (tv.tv_sec - runtime.tv_sec);
565 1.1.1.2 fvdl if (u < 0) {
566 1.1.1.2 fvdl u += 1000000;
567 1.1.1.2 fvdl s--;
568 1.1.1.2 fvdl } else if (u >= 1000000) {
569 1.1.1.2 fvdl u -= 1000000;
570 1.1.1.2 fvdl s++;
571 1.1.1.2 fvdl }
572 1.1.1.2 fvdl p->p_rtime.tv_usec = u;
573 1.1.1.2 fvdl p->p_rtime.tv_sec = s;
574 1.1.1.2 fvdl
575 1.1.1.2 fvdl /*
576 1.1.1.2 fvdl * Check if the process exceeds its cpu resource allocation.
577 1.1.1.2 fvdl * If over max, kill it. In any case, if it has run for more
578 1.1.1.2 fvdl * than 10 minutes, reduce priority to give others a chance.
579 1.1.1.2 fvdl */
580 1.1.1.2 fvdl rlim = &p->p_rlimit[RLIMIT_CPU];
581 1.1.1.2 fvdl if (s >= rlim->rlim_cur) {
582 1.1.1.2 fvdl if (s >= rlim->rlim_max)
583 1.1.1.2 fvdl psignal(p, SIGKILL);
584 1.1.1.2 fvdl else {
585 1.1.1.2 fvdl psignal(p, SIGXCPU);
586 1.1.1.2 fvdl if (rlim->rlim_cur < rlim->rlim_max)
587 1.1.1.2 fvdl rlim->rlim_cur += 5;
588 1.1.1.2 fvdl }
589 1.1.1.2 fvdl }
590 1.1.1.2 fvdl if (s > 10 * 60 && p->p_ucred->cr_uid && p->p_nice == NZERO) {
591 1.1.1.2 fvdl p->p_nice = NZERO + 4;
592 1.1.1.2 fvdl resetpriority(p);
593 1.1.1.2 fvdl }
594 1.1.1.2 fvdl
595 1.1.1.2 fvdl /*
596 1.1.1.2 fvdl * Pick a new current process and record its start time.
597 1.1.1.2 fvdl */
598 1.1.1.2 fvdl cnt.v_swtch++;
599 1.1.1.2 fvdl cpu_switch(p);
600 1.1.1.2 fvdl microtime(&runtime);
601 1.1.1.2 fvdl }
602 1.1.1.2 fvdl
603 1.1.1.2 fvdl /*
604 1.1.1.2 fvdl * Initialize the (doubly-linked) run queues
605 1.1.1.2 fvdl * to be empty.
606 1.1.1.2 fvdl */
607 1.1.1.3 fvdl void
608 1.1.1.2 fvdl rqinit()
609 1.1.1.2 fvdl {
610 1.1.1.2 fvdl register int i;
611 1.1.1.2 fvdl
612 1.1.1.2 fvdl for (i = 0; i < NQS; i++)
613 1.1.1.2 fvdl qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
614 1.1.1.2 fvdl }
615 1.1.1.2 fvdl
616 1.1.1.2 fvdl /*
617 1.1.1.2 fvdl * Change process state to be runnable,
618 1.1.1.2 fvdl * placing it on the run queue if it is in memory,
619 1.1.1.2 fvdl * and awakening the swapper if it isn't in memory.
620 1.1.1.2 fvdl */
621 1.1.1.2 fvdl void
622 1.1.1.2 fvdl setrunnable(p)
623 1.1.1.2 fvdl register struct proc *p;
624 1.1.1.2 fvdl {
625 1.1.1.2 fvdl register int s;
626 1.1.1.2 fvdl
627 1.1.1.2 fvdl s = splhigh();
628 1.1.1.2 fvdl switch (p->p_stat) {
629 1.1.1.2 fvdl case 0:
630 1.1.1.2 fvdl case SRUN:
631 1.1.1.2 fvdl case SZOMB:
632 1.1.1.2 fvdl default:
633 1.1.1.2 fvdl panic("setrunnable");
634 1.1.1.2 fvdl case SSTOP:
635 1.1.1.2 fvdl case SSLEEP:
636 1.1.1.2 fvdl unsleep(p); /* e.g. when sending signals */
637 1.1.1.2 fvdl break;
638 1.1.1.2 fvdl
639 1.1.1.2 fvdl case SIDL:
640 1.1.1.2 fvdl break;
641 1.1.1.2 fvdl }
642 1.1.1.2 fvdl p->p_stat = SRUN;
643 1.1.1.2 fvdl if (p->p_flag & P_INMEM)
644 1.1.1.2 fvdl setrunqueue(p);
645 1.1.1.2 fvdl splx(s);
646 1.1.1.2 fvdl if (p->p_slptime > 1)
647 1.1.1.2 fvdl updatepri(p);
648 1.1.1.2 fvdl p->p_slptime = 0;
649 1.1.1.2 fvdl if ((p->p_flag & P_INMEM) == 0)
650 1.1.1.2 fvdl wakeup((caddr_t)&proc0);
651 1.1.1.2 fvdl else if (p->p_priority < curpriority)
652 1.1.1.2 fvdl need_resched();
653 1.1.1.2 fvdl }
654 1.1.1.2 fvdl
655 1.1.1.2 fvdl /*
656 1.1.1.2 fvdl * Compute the priority of a process when running in user mode.
657 1.1.1.2 fvdl * Arrange to reschedule if the resulting priority is better
658 1.1.1.2 fvdl * than that of the current process.
659 1.1.1.2 fvdl */
660 1.1.1.2 fvdl void
661 1.1.1.2 fvdl resetpriority(p)
662 1.1.1.2 fvdl register struct proc *p;
663 1.1.1.2 fvdl {
664 1.1.1.2 fvdl register unsigned int newpriority;
665 1.1.1.2 fvdl
666 1.1.1.2 fvdl newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice;
667 1.1.1.2 fvdl newpriority = min(newpriority, MAXPRI);
668 1.1.1.2 fvdl p->p_usrpri = newpriority;
669 1.1.1.2 fvdl if (newpriority < curpriority)
670 1.1.1.2 fvdl need_resched();
671 1.1.1.2 fvdl }
672