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