xref: /csrg-svn/sys/kern/kern_synch.c (revision 49226)
123376Smckusick /*
249095Skarels  * Copyright (c) 1982, 1986, 1990, 1991 Regents of the University of California.
323376Smckusick  * All rights reserved.  The Berkeley software License Agreement
423376Smckusick  * specifies the terms and conditions for redistribution.
523376Smckusick  *
6*49226Skarels  *	@(#)kern_synch.c	7.16 (Berkeley) 05/06/91
723376Smckusick  */
833Sbill 
917093Sbloom #include "param.h"
1017093Sbloom #include "systm.h"
1117093Sbloom #include "proc.h"
1217093Sbloom #include "kernel.h"
1317093Sbloom #include "buf.h"
1449095Skarels #include "signalvar.h"
1549095Skarels #include "resourcevar.h"
169756Ssam 
1747544Skarels #include "machine/cpu.h"
1845742Smckusick 
19*49226Skarels u_char	curpri;			/* usrpri of curproc */
20*49226Skarels 
218102Sroot /*
228102Sroot  * Force switch among equal priority processes every 100ms.
238102Sroot  */
248102Sroot roundrobin()
258102Sroot {
268102Sroot 
2747544Skarels 	need_resched();
288624Sroot 	timeout(roundrobin, (caddr_t)0, hz / 10);
298102Sroot }
308102Sroot 
3132908Smckusick /*
3232908Smckusick  * constants for digital decay and forget
3332908Smckusick  *	90% of (p_cpu) usage in 5*loadav time
3432908Smckusick  *	95% of (p_pctcpu) usage in 60 seconds (load insensitive)
3532908Smckusick  *          Note that, as ps(1) mentions, this can let percentages
3632908Smckusick  *          total over 100% (I've seen 137.9% for 3 processes).
3732908Smckusick  *
3832908Smckusick  * Note that hardclock updates p_cpu and p_cpticks independently.
3932908Smckusick  *
4032908Smckusick  * We wish to decay away 90% of p_cpu in (5 * loadavg) seconds.
4132908Smckusick  * That is, the system wants to compute a value of decay such
4232908Smckusick  * that the following for loop:
4332908Smckusick  * 	for (i = 0; i < (5 * loadavg); i++)
4432908Smckusick  * 		p_cpu *= decay;
4532908Smckusick  * will compute
4632908Smckusick  * 	p_cpu *= 0.1;
4732908Smckusick  * for all values of loadavg:
4832908Smckusick  *
4932908Smckusick  * Mathematically this loop can be expressed by saying:
5032908Smckusick  * 	decay ** (5 * loadavg) ~= .1
5132908Smckusick  *
5232908Smckusick  * The system computes decay as:
5332908Smckusick  * 	decay = (2 * loadavg) / (2 * loadavg + 1)
5432908Smckusick  *
5532908Smckusick  * We wish to prove that the system's computation of decay
5632908Smckusick  * will always fulfill the equation:
5732908Smckusick  * 	decay ** (5 * loadavg) ~= .1
5832908Smckusick  *
5932908Smckusick  * If we compute b as:
6032908Smckusick  * 	b = 2 * loadavg
6132908Smckusick  * then
6232908Smckusick  * 	decay = b / (b + 1)
6332908Smckusick  *
6432908Smckusick  * We now need to prove two things:
6532908Smckusick  *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
6632908Smckusick  *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
6732908Smckusick  *
6832908Smckusick  * Facts:
6932908Smckusick  *         For x close to zero, exp(x) =~ 1 + x, since
7032908Smckusick  *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
7132908Smckusick  *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
7232908Smckusick  *         For x close to zero, ln(1+x) =~ x, since
7332908Smckusick  *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
7432908Smckusick  *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
7532908Smckusick  *         ln(.1) =~ -2.30
7632908Smckusick  *
7732908Smckusick  * Proof of (1):
7832908Smckusick  *    Solve (factor)**(power) =~ .1 given power (5*loadav):
7932908Smckusick  *	solving for factor,
8032908Smckusick  *      ln(factor) =~ (-2.30/5*loadav), or
8147544Skarels  *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
8232908Smckusick  *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
8332908Smckusick  *
8432908Smckusick  * Proof of (2):
8532908Smckusick  *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
8632908Smckusick  *	solving for power,
8732908Smckusick  *      power*ln(b/(b+1)) =~ -2.30, or
8832908Smckusick  *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
8932908Smckusick  *
9032908Smckusick  * Actual power values for the implemented algorithm are as follows:
9132908Smckusick  *      loadav: 1       2       3       4
9232908Smckusick  *      power:  5.68    10.32   14.94   19.55
9332908Smckusick  */
9417541Skarels 
9538164Smckusick /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
9647544Skarels #define	loadfactor(loadav)	(2 * (loadav))
9747544Skarels #define	decay_cpu(loadfac, cpu)	(((loadfac) * (cpu)) / ((loadfac) + FSCALE))
988102Sroot 
9938164Smckusick /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
10038164Smckusick fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;		/* exp(-1/20) */
10138164Smckusick 
1028102Sroot /*
10338164Smckusick  * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
10438164Smckusick  * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
10538164Smckusick  * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
10638164Smckusick  *
10738164Smckusick  * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
10838164Smckusick  *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
10938164Smckusick  *
11038164Smckusick  * If you dont want to bother with the faster/more-accurate formula, you
11138164Smckusick  * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
11238164Smckusick  * (more general) method of calculating the %age of CPU used by a process.
11338164Smckusick  */
11438164Smckusick #define	CCPU_SHIFT	11
11538164Smckusick 
11638164Smckusick /*
1178102Sroot  * Recompute process priorities, once a second
1188102Sroot  */
1198102Sroot schedcpu()
1208102Sroot {
12147544Skarels 	register fixpt_t loadfac = loadfactor(averunnable[0]);
1228102Sroot 	register struct proc *p;
12347544Skarels 	register int s;
12447544Skarels 	register unsigned int newcpu;
1258102Sroot 
1268102Sroot 	wakeup((caddr_t)&lbolt);
12716532Skarels 	for (p = allproc; p != NULL; p = p->p_nxt) {
12847544Skarels 		/*
12947544Skarels 		 * Increment time in/out of memory and sleep time
13047544Skarels 		 * (if sleeping).  We ignore overflow; with 16-bit int's
13147544Skarels 		 * (remember them?) overflow takes 45 days.
13247544Skarels 		 */
13347544Skarels 		p->p_time++;
13447544Skarels 		if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
13547544Skarels 			p->p_slptime++;
13638164Smckusick 		p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
13717541Skarels 		/*
13817541Skarels 		 * If the process has slept the entire second,
13917541Skarels 		 * stop recalculating its priority until it wakes up.
14017541Skarels 		 */
14138164Smckusick 		if (p->p_slptime > 1)
14217541Skarels 			continue;
14317541Skarels 		/*
14417541Skarels 		 * p_pctcpu is only for ps.
14517541Skarels 		 */
14638164Smckusick #if	(FSHIFT >= CCPU_SHIFT)
14738164Smckusick 		p->p_pctcpu += (hz == 100)?
14838164Smckusick 			((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
14938164Smckusick                 	100 * (((fixpt_t) p->p_cpticks)
15038164Smckusick 				<< (FSHIFT - CCPU_SHIFT)) / hz;
15138164Smckusick #else
15238164Smckusick 		p->p_pctcpu += ((FSCALE - ccpu) *
15338164Smckusick 			(p->p_cpticks * FSCALE / hz)) >> FSHIFT;
15438164Smckusick #endif
1558102Sroot 		p->p_cpticks = 0;
15647544Skarels 		newcpu = (u_int) decay_cpu(loadfac, p->p_cpu) + p->p_nice;
15747544Skarels 		p->p_cpu = min(newcpu, UCHAR_MAX);
15847544Skarels 		setpri(p);
15917541Skarels 		s = splhigh();	/* prevent state changes */
1608102Sroot 		if (p->p_pri >= PUSER) {
16147544Skarels #define	PPQ	(128 / NQS)		/* priorities per queue */
16249095Skarels 			if ((p != curproc) &&
1638102Sroot 			    p->p_stat == SRUN &&
1648102Sroot 			    (p->p_flag & SLOAD) &&
16516795Skarels 			    (p->p_pri / PPQ) != (p->p_usrpri / PPQ)) {
1668102Sroot 				remrq(p);
1678102Sroot 				p->p_pri = p->p_usrpri;
1688102Sroot 				setrq(p);
1698102Sroot 			} else
1708102Sroot 				p->p_pri = p->p_usrpri;
1718102Sroot 		}
1728102Sroot 		splx(s);
1738102Sroot 	}
1748102Sroot 	vmmeter();
1758102Sroot 	if (bclnlist != NULL)
17647544Skarels 		wakeup((caddr_t)pageproc);
1778624Sroot 	timeout(schedcpu, (caddr_t)0, hz);
1788102Sroot }
1798102Sroot 
18017541Skarels /*
18117541Skarels  * Recalculate the priority of a process after it has slept for a while.
18247544Skarels  * For all load averages >= 1 and max p_cpu of 255, sleeping for at least
18347544Skarels  * six times the loadfactor will decay p_cpu to zero.
18417541Skarels  */
18517541Skarels updatepri(p)
18617541Skarels 	register struct proc *p;
18717541Skarels {
18847544Skarels 	register unsigned int newcpu = p->p_cpu;
18947544Skarels 	register fixpt_t loadfac = loadfactor(averunnable[0]);
19017541Skarels 
19147544Skarels 	if (p->p_slptime > 5 * loadfac)
19247544Skarels 		p->p_cpu = 0;
19347544Skarels 	else {
19447544Skarels 		p->p_slptime--;	/* the first time was done in schedcpu */
19547544Skarels 		while (newcpu && --p->p_slptime)
19647544Skarels 			newcpu = (int) decay_cpu(loadfac, newcpu);
19747544Skarels 		p->p_cpu = min(newcpu, UCHAR_MAX);
19847544Skarels 	}
19947544Skarels 	setpri(p);
20017541Skarels }
20117541Skarels 
20233Sbill #define SQSIZE 0100	/* Must be power of 2 */
20333Sbill #define HASH(x)	(( (int) x >> 5) & (SQSIZE-1))
20421099Smckusick struct slpque {
20521099Smckusick 	struct proc *sq_head;
20621099Smckusick 	struct proc **sq_tailp;
20721099Smckusick } slpque[SQSIZE];
20833Sbill 
20933Sbill /*
21045671Skarels  * During autoconfiguration or after a panic, a sleep will simply
21145671Skarels  * lower the priority briefly to allow interrupts, then return.
21245671Skarels  * The priority to be used (safepri) is machine-dependent, thus this
21345671Skarels  * value is initialized and maintained in the machine-dependent layers.
21445671Skarels  * This priority will typically be 0, or the lowest priority
21545671Skarels  * that is safe for use on the interrupt stack; it can be made
21645671Skarels  * higher to block network software interrupts after panics.
21745671Skarels  */
21845671Skarels int safepri;
21945671Skarels 
22045671Skarels /*
22140711Skarels  * General sleep call.
22240711Skarels  * Suspends current process until a wakeup is made on chan.
22340711Skarels  * The process will then be made runnable with priority pri.
22440711Skarels  * Sleeps at most timo/hz seconds (0 means no timeout).
22540711Skarels  * If pri includes PCATCH flag, signals are checked
22640711Skarels  * before and after sleeping, else signals are not checked.
22740711Skarels  * Returns 0 if awakened, EWOULDBLOCK if the timeout expires.
22840711Skarels  * If PCATCH is set and a signal needs to be delivered,
22940711Skarels  * ERESTART is returned if the current system call should be restarted
23040711Skarels  * if possible, and EINTR is returned if the system call should
23140711Skarels  * be interrupted by the signal (return EINTR).
23233Sbill  */
23340711Skarels tsleep(chan, pri, wmesg, timo)
23440710Smarc 	caddr_t chan;
23540710Smarc 	int pri;
23640710Smarc 	char *wmesg;
23740710Smarc 	int timo;
23840710Smarc {
23949095Skarels 	register struct proc *p = curproc;
24040710Smarc 	register struct slpque *qp;
24140710Smarc 	register s;
24240711Skarels 	int sig, catch = pri & PCATCH;
24340710Smarc 	extern int cold;
24440710Smarc 	int endtsleep();
24540710Smarc 
24640710Smarc 	s = splhigh();
24740710Smarc 	if (cold || panicstr) {
24840710Smarc 		/*
24940710Smarc 		 * After a panic, or during autoconfiguration,
25040710Smarc 		 * just give interrupts a chance, then just return;
25140710Smarc 		 * don't run any other procs or panic below,
25240710Smarc 		 * in case this is the idle process and already asleep.
25340710Smarc 		 */
25445671Skarels 		splx(safepri);
25540710Smarc 		splx(s);
25640710Smarc 		return (0);
25740710Smarc 	}
25840710Smarc #ifdef DIAGNOSTIC
25947544Skarels 	if (chan == 0 || p->p_stat != SRUN || p->p_rlink)
26040711Skarels 		panic("tsleep");
26140710Smarc #endif
26247544Skarels 	p->p_wchan = chan;
26347544Skarels 	p->p_wmesg = wmesg;
26447544Skarels 	p->p_slptime = 0;
26547544Skarels 	p->p_pri = pri & PRIMASK;
26640710Smarc 	qp = &slpque[HASH(chan)];
26740710Smarc 	if (qp->sq_head == 0)
26847544Skarels 		qp->sq_head = p;
26940710Smarc 	else
27047544Skarels 		*qp->sq_tailp = p;
27147544Skarels 	*(qp->sq_tailp = &p->p_link) = 0;
27245671Skarels 	if (timo)
27347544Skarels 		timeout(endtsleep, (caddr_t)p, timo);
27440710Smarc 	/*
27547544Skarels 	 * We put ourselves on the sleep queue and start our timeout
27647544Skarels 	 * before calling CURSIG, as we could stop there, and a wakeup
27747544Skarels 	 * or a SIGCONT (or both) could occur while we were stopped.
27845671Skarels 	 * A SIGCONT would cause us to be marked as SSLEEP
27945671Skarels 	 * without resuming us, thus we must be ready for sleep
28045671Skarels 	 * when CURSIG is called.  If the wakeup happens while we're
28147544Skarels 	 * stopped, p->p_wchan will be 0 upon return from CURSIG.
28240710Smarc 	 */
28340711Skarels 	if (catch) {
28447544Skarels 		p->p_flag |= SSINTR;
28547544Skarels 		if (sig = CURSIG(p)) {
28647544Skarels 			if (p->p_wchan)
28747544Skarels 				unsleep(p);
28847544Skarels 			p->p_stat = SRUN;
28945671Skarels 			goto resume;
29040711Skarels 		}
29147544Skarels 		if (p->p_wchan == 0) {
29245671Skarels 			catch = 0;
29345671Skarels 			goto resume;
29440711Skarels 		}
29540710Smarc 	}
29647544Skarels 	p->p_stat = SSLEEP;
29740710Smarc 	(void) spl0();
29847544Skarels 	p->p_stats->p_ru.ru_nvcsw++;
29940710Smarc 	swtch();
30045671Skarels resume:
30147544Skarels 	curpri = p->p_usrpri;
30240710Smarc 	splx(s);
30347544Skarels 	p->p_flag &= ~SSINTR;
30447544Skarels 	if (p->p_flag & STIMO) {
30547544Skarels 		p->p_flag &= ~STIMO;
30645671Skarels 		if (catch == 0 || sig == 0)
30745671Skarels 			return (EWOULDBLOCK);
30845671Skarels 	} else if (timo)
30947544Skarels 		untimeout(endtsleep, (caddr_t)p);
31047544Skarels 	if (catch && (sig != 0 || (sig = CURSIG(p)))) {
31147544Skarels 		if (p->p_sigacts->ps_sigintr & sigmask(sig))
31240711Skarels 			return (EINTR);
31340711Skarels 		return (ERESTART);
31440711Skarels 	}
31540710Smarc 	return (0);
31640710Smarc }
31740710Smarc 
31840710Smarc /*
31940710Smarc  * Implement timeout for tsleep.
32040710Smarc  * If process hasn't been awakened (wchan non-zero),
32140710Smarc  * set timeout flag and undo the sleep.  If proc
32240710Smarc  * is stopped, just unsleep so it will remain stopped.
32340710Smarc  */
32440710Smarc endtsleep(p)
32540710Smarc 	register struct proc *p;
32640710Smarc {
32740710Smarc 	int s = splhigh();
32840710Smarc 
32940710Smarc 	if (p->p_wchan) {
33040710Smarc 		if (p->p_stat == SSLEEP)
33140710Smarc 			setrun(p);
33240710Smarc 		else
33340710Smarc 			unsleep(p);
33440710Smarc 		p->p_flag |= STIMO;
33540710Smarc 	}
33640710Smarc 	splx(s);
33740710Smarc }
33840710Smarc 
33940711Skarels /*
34040711Skarels  * Short-term, non-interruptable sleep.
34140711Skarels  */
34233Sbill sleep(chan, pri)
3438033Sroot 	caddr_t chan;
3448033Sroot 	int pri;
34533Sbill {
34649095Skarels 	register struct proc *p = curproc;
34721099Smckusick 	register struct slpque *qp;
348207Sbill 	register s;
34930532Skarels 	extern int cold;
35033Sbill 
35140711Skarels #ifdef DIAGNOSTIC
35240711Skarels 	if (pri > PZERO) {
35340711Skarels 		printf("sleep called with pri %d > PZERO, wchan: %x\n",
35440711Skarels 			pri, chan);
35540711Skarels 		panic("old sleep");
35640711Skarels 	}
35740711Skarels #endif
35817541Skarels 	s = splhigh();
35930532Skarels 	if (cold || panicstr) {
36018363Skarels 		/*
36130532Skarels 		 * After a panic, or during autoconfiguration,
36230532Skarels 		 * just give interrupts a chance, then just return;
36330532Skarels 		 * don't run any other procs or panic below,
36430532Skarels 		 * in case this is the idle process and already asleep.
36518363Skarels 		 */
36645671Skarels 		splx(safepri);
36718363Skarels 		splx(s);
36818363Skarels 		return;
36918363Skarels 	}
37040710Smarc #ifdef DIAGNOSTIC
37147544Skarels 	if (chan==0 || p->p_stat != SRUN || p->p_rlink)
37233Sbill 		panic("sleep");
37340710Smarc #endif
37447544Skarels 	p->p_wchan = chan;
37547544Skarels 	p->p_wmesg = NULL;
37647544Skarels 	p->p_slptime = 0;
37747544Skarels 	p->p_pri = pri;
37821099Smckusick 	qp = &slpque[HASH(chan)];
37921099Smckusick 	if (qp->sq_head == 0)
38047544Skarels 		qp->sq_head = p;
38121099Smckusick 	else
38247544Skarels 		*qp->sq_tailp = p;
38347544Skarels 	*(qp->sq_tailp = &p->p_link) = 0;
38447544Skarels 	p->p_stat = SSLEEP;
38540711Skarels 	(void) spl0();
38647544Skarels 	p->p_stats->p_ru.ru_nvcsw++;
38740711Skarels 	swtch();
38847544Skarels 	curpri = p->p_usrpri;
38933Sbill 	splx(s);
39033Sbill }
39133Sbill 
39233Sbill /*
393181Sbill  * Remove a process from its wait queue
394181Sbill  */
395181Sbill unsleep(p)
3964826Swnj 	register struct proc *p;
397181Sbill {
39821099Smckusick 	register struct slpque *qp;
399181Sbill 	register struct proc **hp;
40021099Smckusick 	int s;
401181Sbill 
40217541Skarels 	s = splhigh();
403181Sbill 	if (p->p_wchan) {
40421099Smckusick 		hp = &(qp = &slpque[HASH(p->p_wchan)])->sq_head;
405181Sbill 		while (*hp != p)
406181Sbill 			hp = &(*hp)->p_link;
407181Sbill 		*hp = p->p_link;
40821099Smckusick 		if (qp->sq_tailp == &p->p_link)
40921099Smckusick 			qp->sq_tailp = hp;
410181Sbill 		p->p_wchan = 0;
411181Sbill 	}
412181Sbill 	splx(s);
413181Sbill }
414181Sbill 
415181Sbill /*
41647544Skarels  * Wakeup on "chan"; set all processes
41747544Skarels  * sleeping on chan to run state.
41833Sbill  */
41933Sbill wakeup(chan)
4204826Swnj 	register caddr_t chan;
42133Sbill {
42221099Smckusick 	register struct slpque *qp;
42321099Smckusick 	register struct proc *p, **q;
42433Sbill 	int s;
42533Sbill 
42617541Skarels 	s = splhigh();
42721099Smckusick 	qp = &slpque[HASH(chan)];
42833Sbill restart:
42921099Smckusick 	for (q = &qp->sq_head; p = *q; ) {
43040710Smarc #ifdef DIAGNOSTIC
431181Sbill 		if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP)
43233Sbill 			panic("wakeup");
43340710Smarc #endif
43447544Skarels 		if (p->p_wchan == chan) {
43533Sbill 			p->p_wchan = 0;
436187Sbill 			*q = p->p_link;
43721099Smckusick 			if (qp->sq_tailp == &p->p_link)
43821099Smckusick 				qp->sq_tailp = q;
439181Sbill 			if (p->p_stat == SSLEEP) {
440181Sbill 				/* OPTIMIZED INLINE EXPANSION OF setrun(p) */
44121763Skarels 				if (p->p_slptime > 1)
44221763Skarels 					updatepri(p);
44347544Skarels 				p->p_slptime = 0;
444181Sbill 				p->p_stat = SRUN;
4452702Swnj 				if (p->p_flag & SLOAD)
446181Sbill 					setrq(p);
44716795Skarels 				/*
44816795Skarels 				 * Since curpri is a usrpri,
44916795Skarels 				 * p->p_pri is always better than curpri.
45016795Skarels 				 */
45147544Skarels 				if ((p->p_flag&SLOAD) == 0)
45247544Skarels 					wakeup((caddr_t)&proc0);
45347544Skarels 				else
45447544Skarels 					need_resched();
455181Sbill 				/* END INLINE EXPANSION */
456187Sbill 				goto restart;
45733Sbill 			}
458187Sbill 		} else
459187Sbill 			q = &p->p_link;
46033Sbill 	}
46133Sbill 	splx(s);
46233Sbill }
46333Sbill 
46433Sbill /*
46533Sbill  * Initialize the (doubly-linked) run queues
46633Sbill  * to be empty.
46733Sbill  */
46833Sbill rqinit()
46933Sbill {
47033Sbill 	register int i;
47133Sbill 
47233Sbill 	for (i = 0; i < NQS; i++)
47333Sbill 		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
47433Sbill }
47533Sbill 
47633Sbill /*
47747544Skarels  * Change process state to be runnable,
47847544Skarels  * placing it on the run queue if it is in memory,
47947544Skarels  * and awakening the swapper if it isn't in memory.
48033Sbill  */
48133Sbill setrun(p)
4824826Swnj 	register struct proc *p;
48333Sbill {
4844826Swnj 	register int s;
48533Sbill 
48617541Skarels 	s = splhigh();
48733Sbill 	switch (p->p_stat) {
48833Sbill 
48933Sbill 	case 0:
49033Sbill 	case SWAIT:
49133Sbill 	case SRUN:
49233Sbill 	case SZOMB:
49333Sbill 	default:
49433Sbill 		panic("setrun");
49533Sbill 
496207Sbill 	case SSTOP:
49733Sbill 	case SSLEEP:
498181Sbill 		unsleep(p);		/* e.g. when sending signals */
49933Sbill 		break;
50033Sbill 
50133Sbill 	case SIDL:
50233Sbill 		break;
50333Sbill 	}
50433Sbill 	p->p_stat = SRUN;
50533Sbill 	if (p->p_flag & SLOAD)
50633Sbill 		setrq(p);
50733Sbill 	splx(s);
50830232Skarels 	if (p->p_slptime > 1)
50930232Skarels 		updatepri(p);
51047544Skarels 	p->p_slptime = 0;
51147544Skarels 	if ((p->p_flag&SLOAD) == 0)
51247544Skarels 		wakeup((caddr_t)&proc0);
51347544Skarels 	else if (p->p_pri < curpri)
51447544Skarels 		need_resched();
51533Sbill }
51633Sbill 
51733Sbill /*
51847544Skarels  * Compute priority of process when running in user mode.
51947544Skarels  * Arrange to reschedule if the resulting priority
52047544Skarels  * is better than that of the current process.
52133Sbill  */
52247544Skarels setpri(p)
52347544Skarels 	register struct proc *p;
52433Sbill {
52547544Skarels 	register unsigned int newpri;
52633Sbill 
52747544Skarels 	newpri = PUSER + p->p_cpu / 4 + 2 * p->p_nice;
52847544Skarels 	newpri = min(newpri, MAXPRI);
52947544Skarels 	p->p_usrpri = newpri;
53047544Skarels 	if (newpri < curpri)
53147544Skarels 		need_resched();
53233Sbill }
533