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