xref: /netbsd-src/sys/kern/kern_synch.c (revision 81b108b45f75f89f1e3ffad9fb6f074e771c0935)
1 /*	$NetBSD: kern_synch.c,v 1.38 1996/07/17 21:54:06 explorer 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) + p->p_nice;
208 		p->p_estcpu = min(newcpu, UCHAR_MAX);
209 		resetpriority(p);
210 		if (p->p_priority >= PUSER) {
211 #define	PPQ	(128 / NQS)		/* priorities per queue */
212 			if ((p != curproc) &&
213 			    p->p_stat == SRUN &&
214 			    (p->p_flag & P_INMEM) &&
215 			    (p->p_priority / PPQ) != (p->p_usrpri / PPQ)) {
216 				remrq(p);
217 				p->p_priority = p->p_usrpri;
218 				setrunqueue(p);
219 			} else
220 				p->p_priority = p->p_usrpri;
221 		}
222 		splx(s);
223 	}
224 	vmmeter();
225 	if (bclnlist != NULL)
226 		wakeup((caddr_t)pageproc);
227 	timeout(schedcpu, (void *)0, hz);
228 }
229 
230 /*
231  * Recalculate the priority of a process after it has slept for a while.
232  * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
233  * least six times the loadfactor will decay p_estcpu to zero.
234  */
235 void
236 updatepri(p)
237 	register struct proc *p;
238 {
239 	register unsigned int newcpu = p->p_estcpu;
240 	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
241 
242 	if (p->p_slptime > 5 * loadfac)
243 		p->p_estcpu = 0;
244 	else {
245 		p->p_slptime--;	/* the first time was done in schedcpu */
246 		while (newcpu && --p->p_slptime)
247 			newcpu = (int) decay_cpu(loadfac, newcpu);
248 		p->p_estcpu = min(newcpu, UCHAR_MAX);
249 	}
250 	resetpriority(p);
251 }
252 
253 /*
254  * We're only looking at 7 bits of the address; everything is
255  * aligned to 4, lots of things are aligned to greater powers
256  * of 2.  Shift right by 8, i.e. drop the bottom 256 worth.
257  */
258 #define TABLESIZE	128
259 #define LOOKUP(x)	(((long)(x) >> 8) & (TABLESIZE - 1))
260 struct slpque {
261 	struct proc *sq_head;
262 	struct proc **sq_tailp;
263 } slpque[TABLESIZE];
264 
265 /*
266  * During autoconfiguration or after a panic, a sleep will simply
267  * lower the priority briefly to allow interrupts, then return.
268  * The priority to be used (safepri) is machine-dependent, thus this
269  * value is initialized and maintained in the machine-dependent layers.
270  * This priority will typically be 0, or the lowest priority
271  * that is safe for use on the interrupt stack; it can be made
272  * higher to block network software interrupts after panics.
273  */
274 int safepri;
275 
276 /*
277  * General sleep call.  Suspends the current process until a wakeup is
278  * performed on the specified identifier.  The process will then be made
279  * runnable with the specified priority.  Sleeps at most timo/hz seconds
280  * (0 means no timeout).  If pri includes PCATCH flag, signals are checked
281  * before and after sleeping, else signals are not checked.  Returns 0 if
282  * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
283  * signal needs to be delivered, ERESTART is returned if the current system
284  * call should be restarted if possible, and EINTR is returned if the system
285  * call should be interrupted by the signal (return EINTR).
286  */
287 int
288 tsleep(ident, priority, wmesg, timo)
289 	void *ident;
290 	int priority, timo;
291 	char *wmesg;
292 {
293 	register struct proc *p = curproc;
294 	register struct slpque *qp;
295 	register s;
296 	int sig, catch = priority & PCATCH;
297 	extern int cold;
298 	void endtsleep __P((void *));
299 
300 #ifdef KTRACE
301 	if (KTRPOINT(p, KTR_CSW))
302 		ktrcsw(p->p_tracep, 1, 0);
303 #endif
304 	s = splhigh();
305 	if (cold || panicstr) {
306 		/*
307 		 * After a panic, or during autoconfiguration,
308 		 * just give interrupts a chance, then just return;
309 		 * don't run any other procs or panic below,
310 		 * in case this is the idle process and already asleep.
311 		 */
312 		splx(safepri);
313 		splx(s);
314 		return (0);
315 	}
316 #ifdef DIAGNOSTIC
317 	if (ident == NULL || p->p_stat != SRUN || p->p_back)
318 		panic("tsleep");
319 #endif
320 	p->p_wchan = ident;
321 	p->p_wmesg = wmesg;
322 	p->p_slptime = 0;
323 	p->p_priority = priority & PRIMASK;
324 	qp = &slpque[LOOKUP(ident)];
325 	if (qp->sq_head == 0)
326 		qp->sq_head = p;
327 	else
328 		*qp->sq_tailp = p;
329 	*(qp->sq_tailp = &p->p_forw) = 0;
330 	if (timo)
331 		timeout(endtsleep, (void *)p, timo);
332 	/*
333 	 * We put ourselves on the sleep queue and start our timeout
334 	 * before calling CURSIG, as we could stop there, and a wakeup
335 	 * or a SIGCONT (or both) could occur while we were stopped.
336 	 * A SIGCONT would cause us to be marked as SSLEEP
337 	 * without resuming us, thus we must be ready for sleep
338 	 * when CURSIG is called.  If the wakeup happens while we're
339 	 * stopped, p->p_wchan will be 0 upon return from CURSIG.
340 	 */
341 	if (catch) {
342 		p->p_flag |= P_SINTR;
343 		if ((sig = CURSIG(p)) != 0) {
344 			if (p->p_wchan)
345 				unsleep(p);
346 			p->p_stat = SRUN;
347 			goto resume;
348 		}
349 		if (p->p_wchan == 0) {
350 			catch = 0;
351 			goto resume;
352 		}
353 	} else
354 		sig = 0;
355 	p->p_stat = SSLEEP;
356 	p->p_stats->p_ru.ru_nvcsw++;
357 	mi_switch();
358 #ifdef	DDB
359 	/* handy breakpoint location after process "wakes" */
360 	asm(".globl bpendtsleep ; bpendtsleep:");
361 #endif
362 resume:
363 	curpriority = p->p_usrpri;
364 	splx(s);
365 	p->p_flag &= ~P_SINTR;
366 	if (p->p_flag & P_TIMEOUT) {
367 		p->p_flag &= ~P_TIMEOUT;
368 		if (sig == 0) {
369 #ifdef KTRACE
370 			if (KTRPOINT(p, KTR_CSW))
371 				ktrcsw(p->p_tracep, 0, 0);
372 #endif
373 			return (EWOULDBLOCK);
374 		}
375 	} else if (timo)
376 		untimeout(endtsleep, (void *)p);
377 	if (catch && (sig != 0 || (sig = CURSIG(p)) != 0)) {
378 #ifdef KTRACE
379 		if (KTRPOINT(p, KTR_CSW))
380 			ktrcsw(p->p_tracep, 0, 0);
381 #endif
382 		if (p->p_sigacts->ps_sigintr & sigmask(sig))
383 			return (EINTR);
384 		return (ERESTART);
385 	}
386 #ifdef KTRACE
387 	if (KTRPOINT(p, KTR_CSW))
388 		ktrcsw(p->p_tracep, 0, 0);
389 #endif
390 	return (0);
391 }
392 
393 /*
394  * Implement timeout for tsleep.
395  * If process hasn't been awakened (wchan non-zero),
396  * set timeout flag and undo the sleep.  If proc
397  * is stopped, just unsleep so it will remain stopped.
398  */
399 void
400 endtsleep(arg)
401 	void *arg;
402 {
403 	register struct proc *p;
404 	int s;
405 
406 	p = (struct proc *)arg;
407 	s = splhigh();
408 	if (p->p_wchan) {
409 		if (p->p_stat == SSLEEP)
410 			setrunnable(p);
411 		else
412 			unsleep(p);
413 		p->p_flag |= P_TIMEOUT;
414 	}
415 	splx(s);
416 }
417 
418 /*
419  * Short-term, non-interruptable sleep.
420  */
421 void
422 sleep(ident, priority)
423 	void *ident;
424 	int priority;
425 {
426 	register struct proc *p = curproc;
427 	register struct slpque *qp;
428 	register s;
429 	extern int cold;
430 
431 #ifdef DIAGNOSTIC
432 	if (priority > PZERO) {
433 		printf("sleep called with priority %d > PZERO, wchan: %p\n",
434 		    priority, ident);
435 		panic("old sleep");
436 	}
437 #endif
438 	s = splhigh();
439 	if (cold || panicstr) {
440 		/*
441 		 * After a panic, or during autoconfiguration,
442 		 * just give interrupts a chance, then just return;
443 		 * don't run any other procs or panic below,
444 		 * in case this is the idle process and already asleep.
445 		 */
446 		splx(safepri);
447 		splx(s);
448 		return;
449 	}
450 #ifdef DIAGNOSTIC
451 	if (ident == NULL || p->p_stat != SRUN || p->p_back)
452 		panic("sleep");
453 #endif
454 	p->p_wchan = ident;
455 	p->p_wmesg = NULL;
456 	p->p_slptime = 0;
457 	p->p_priority = priority;
458 	qp = &slpque[LOOKUP(ident)];
459 	if (qp->sq_head == 0)
460 		qp->sq_head = p;
461 	else
462 		*qp->sq_tailp = p;
463 	*(qp->sq_tailp = &p->p_forw) = 0;
464 	p->p_stat = SSLEEP;
465 	p->p_stats->p_ru.ru_nvcsw++;
466 #ifdef KTRACE
467 	if (KTRPOINT(p, KTR_CSW))
468 		ktrcsw(p->p_tracep, 1, 0);
469 #endif
470 	mi_switch();
471 #ifdef	DDB
472 	/* handy breakpoint location after process "wakes" */
473 	asm(".globl bpendsleep ; bpendsleep:");
474 #endif
475 #ifdef KTRACE
476 	if (KTRPOINT(p, KTR_CSW))
477 		ktrcsw(p->p_tracep, 0, 0);
478 #endif
479 	curpriority = p->p_usrpri;
480 	splx(s);
481 }
482 
483 /*
484  * Remove a process from its wait queue
485  */
486 void
487 unsleep(p)
488 	register struct proc *p;
489 {
490 	register struct slpque *qp;
491 	register struct proc **hp;
492 	int s;
493 
494 	s = splhigh();
495 	if (p->p_wchan) {
496 		hp = &(qp = &slpque[LOOKUP(p->p_wchan)])->sq_head;
497 		while (*hp != p)
498 			hp = &(*hp)->p_forw;
499 		*hp = p->p_forw;
500 		if (qp->sq_tailp == &p->p_forw)
501 			qp->sq_tailp = hp;
502 		p->p_wchan = 0;
503 	}
504 	splx(s);
505 }
506 
507 /*
508  * Make all processes sleeping on the specified identifier runnable.
509  */
510 void
511 wakeup(ident)
512 	register void *ident;
513 {
514 	register struct slpque *qp;
515 	register struct proc *p, **q;
516 	int s;
517 
518 	s = splhigh();
519 	qp = &slpque[LOOKUP(ident)];
520 restart:
521 	for (q = &qp->sq_head; (p = *q) != NULL; ) {
522 #ifdef DIAGNOSTIC
523 		if (p->p_back || (p->p_stat != SSLEEP && p->p_stat != SSTOP))
524 			panic("wakeup");
525 #endif
526 		if (p->p_wchan == ident) {
527 			p->p_wchan = 0;
528 			*q = p->p_forw;
529 			if (qp->sq_tailp == &p->p_forw)
530 				qp->sq_tailp = q;
531 			if (p->p_stat == SSLEEP) {
532 				/* OPTIMIZED EXPANSION OF setrunnable(p); */
533 				if (p->p_slptime > 1)
534 					updatepri(p);
535 				p->p_slptime = 0;
536 				p->p_stat = SRUN;
537 				if (p->p_flag & P_INMEM)
538 					setrunqueue(p);
539 				/*
540 				 * Since curpriority is a user priority,
541 				 * p->p_priority is always better than
542 				 * curpriority.
543 				 */
544 				if ((p->p_flag & P_INMEM) == 0)
545 					wakeup((caddr_t)&proc0);
546 				else
547 					need_resched();
548 				/* END INLINE EXPANSION */
549 				goto restart;
550 			}
551 		} else
552 			q = &p->p_forw;
553 	}
554 	splx(s);
555 }
556 
557 /*
558  * The machine independent parts of mi_switch().
559  * Must be called at splstatclock() or higher.
560  */
561 void
562 mi_switch()
563 {
564 	register struct proc *p = curproc;	/* XXX */
565 	register struct rlimit *rlim;
566 	register long s, u;
567 	struct timeval tv;
568 
569 	/*
570 	 * Compute the amount of time during which the current
571 	 * process was running, and add that to its total so far.
572 	 */
573 	microtime(&tv);
574 	u = p->p_rtime.tv_usec + (tv.tv_usec - runtime.tv_usec);
575 	s = p->p_rtime.tv_sec + (tv.tv_sec - runtime.tv_sec);
576 	if (u < 0) {
577 		u += 1000000;
578 		s--;
579 	} else if (u >= 1000000) {
580 		u -= 1000000;
581 		s++;
582 	}
583 	p->p_rtime.tv_usec = u;
584 	p->p_rtime.tv_sec = s;
585 
586 	/*
587 	 * Check if the process exceeds its cpu resource allocation.
588 	 * If over max, kill it.  In any case, if it has run for more
589 	 * than 10 minutes, reduce priority to give others a chance.
590 	 */
591 	rlim = &p->p_rlimit[RLIMIT_CPU];
592 	if (s >= rlim->rlim_cur) {
593 		if (s >= rlim->rlim_max)
594 			psignal(p, SIGKILL);
595 		else {
596 			psignal(p, SIGXCPU);
597 			if (rlim->rlim_cur < rlim->rlim_max)
598 				rlim->rlim_cur += 5;
599 		}
600 	}
601 	if (autonicetime && s > autonicetime && p->p_ucred->cr_uid && p->p_nice == NZERO) {
602 		p->p_nice = autoniceval;
603 		resetpriority(p);
604 	}
605 
606 	/*
607 	 * Pick a new current process and record its start time.
608 	 */
609 	cnt.v_swtch++;
610 	cpu_switch(p);
611 	microtime(&runtime);
612 }
613 
614 /*
615  * Initialize the (doubly-linked) run queues
616  * to be empty.
617  */
618 void
619 rqinit()
620 {
621 	register int i;
622 
623 	for (i = 0; i < NQS; i++)
624 		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
625 }
626 
627 /*
628  * Change process state to be runnable,
629  * placing it on the run queue if it is in memory,
630  * and awakening the swapper if it isn't in memory.
631  */
632 void
633 setrunnable(p)
634 	register struct proc *p;
635 {
636 	register int s;
637 
638 	s = splhigh();
639 	switch (p->p_stat) {
640 	case 0:
641 	case SRUN:
642 	case SZOMB:
643 	default:
644 		panic("setrunnable");
645 	case SSTOP:
646 		/*
647 		 * If we're being traced (possibly because someone attached us
648 		 * while we were stopped), check for a signal from the debugger.
649 		 */
650 		if ((p->p_flag & P_TRACED) != 0 && p->p_xstat != 0)
651 			p->p_siglist |= sigmask(p->p_xstat);
652 	case SSLEEP:
653 		unsleep(p);		/* e.g. when sending signals */
654 		break;
655 
656 	case SIDL:
657 		break;
658 	}
659 	p->p_stat = SRUN;
660 	if (p->p_flag & P_INMEM)
661 		setrunqueue(p);
662 	splx(s);
663 	if (p->p_slptime > 1)
664 		updatepri(p);
665 	p->p_slptime = 0;
666 	if ((p->p_flag & P_INMEM) == 0)
667 		wakeup((caddr_t)&proc0);
668 	else if (p->p_priority < curpriority)
669 		need_resched();
670 }
671 
672 /*
673  * Compute the priority of a process when running in user mode.
674  * Arrange to reschedule if the resulting priority is better
675  * than that of the current process.
676  */
677 void
678 resetpriority(p)
679 	register struct proc *p;
680 {
681 	register unsigned int newpriority;
682 
683 	newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice;
684 	newpriority = min(newpriority, MAXPRI);
685 	p->p_usrpri = newpriority;
686 	if (newpriority < curpriority)
687 		need_resched();
688 }
689 
690 #ifdef DDB
691 #include <machine/db_machdep.h>
692 
693 #include <ddb/db_interface.h>
694 #include <ddb/db_output.h>
695 
696 void
697 db_show_all_procs(addr, haddr, count, modif)
698 	db_expr_t addr;
699 	int haddr;
700 	db_expr_t count;
701 	char *modif;
702 {
703 	int map = modif[0] == 'm';
704 	int doingzomb = 0;
705 	struct proc *p, *pp;
706 
707 	p = allproc.lh_first;
708 	db_printf("  pid proc     addr     %s comm         wchan\n",
709 	    map ? "map     " : "uid  ppid  pgrp  flag stat em ");
710 	while (p != 0) {
711 		pp = p->p_pptr;
712 		if (p->p_stat) {
713 			db_printf("%5d %p %p ",
714 			    p->p_pid, p, p->p_addr);
715 			if (map)
716 				db_printf("%p %s   ",
717 				    p->p_vmspace, p->p_comm);
718 			else
719 				db_printf("%3d %5d %5d  %06x  %d  %s  %s   ",
720 				    p->p_cred->p_ruid, pp ? pp->p_pid : -1,
721 				    p->p_pgrp->pg_id, p->p_flag, p->p_stat,
722 				    p->p_emul->e_name, p->p_comm);
723 			if (p->p_wchan) {
724 				if (p->p_wmesg)
725 					db_printf("%s ", p->p_wmesg);
726 				db_printf("%p", p->p_wchan);
727 			}
728 			db_printf("\n");
729 		}
730 		p = p->p_list.le_next;
731 		if (p == 0 && doingzomb == 0) {
732 			doingzomb = 1;
733 			p = zombproc.lh_first;
734 		}
735 	}
736 }
737 #endif
738