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