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