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