xref: /csrg-svn/sys/kern/kern_synch.c (revision 49594)
1*49594Sbostic /*-
2*49594Sbostic  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
3*49594Sbostic  * Copyright (c) 1991 The Regents of the University of California.
4*49594Sbostic  * All rights reserved.
523376Smckusick  *
6*49594Sbostic  * %sccs.include.redist.c%
7*49594Sbostic  *
8*49594Sbostic  *	@(#)kern_synch.c	7.17 (Berkeley) 05/09/91
923376Smckusick  */
1033Sbill 
1117093Sbloom #include "param.h"
1217093Sbloom #include "systm.h"
1317093Sbloom #include "proc.h"
1417093Sbloom #include "kernel.h"
1517093Sbloom #include "buf.h"
1649095Skarels #include "signalvar.h"
1749095Skarels #include "resourcevar.h"
189756Ssam 
1947544Skarels #include "machine/cpu.h"
2045742Smckusick 
2149226Skarels u_char	curpri;			/* usrpri of curproc */
2249226Skarels 
238102Sroot /*
248102Sroot  * Force switch among equal priority processes every 100ms.
258102Sroot  */
268102Sroot roundrobin()
278102Sroot {
288102Sroot 
2947544Skarels 	need_resched();
308624Sroot 	timeout(roundrobin, (caddr_t)0, hz / 10);
318102Sroot }
328102Sroot 
3332908Smckusick /*
3432908Smckusick  * constants for digital decay and forget
3532908Smckusick  *	90% of (p_cpu) usage in 5*loadav time
3632908Smckusick  *	95% of (p_pctcpu) usage in 60 seconds (load insensitive)
3732908Smckusick  *          Note that, as ps(1) mentions, this can let percentages
3832908Smckusick  *          total over 100% (I've seen 137.9% for 3 processes).
3932908Smckusick  *
4032908Smckusick  * Note that hardclock updates p_cpu and p_cpticks independently.
4132908Smckusick  *
4232908Smckusick  * We wish to decay away 90% of p_cpu in (5 * loadavg) seconds.
4332908Smckusick  * That is, the system wants to compute a value of decay such
4432908Smckusick  * that the following for loop:
4532908Smckusick  * 	for (i = 0; i < (5 * loadavg); i++)
4632908Smckusick  * 		p_cpu *= decay;
4732908Smckusick  * will compute
4832908Smckusick  * 	p_cpu *= 0.1;
4932908Smckusick  * for all values of loadavg:
5032908Smckusick  *
5132908Smckusick  * Mathematically this loop can be expressed by saying:
5232908Smckusick  * 	decay ** (5 * loadavg) ~= .1
5332908Smckusick  *
5432908Smckusick  * The system computes decay as:
5532908Smckusick  * 	decay = (2 * loadavg) / (2 * loadavg + 1)
5632908Smckusick  *
5732908Smckusick  * We wish to prove that the system's computation of decay
5832908Smckusick  * will always fulfill the equation:
5932908Smckusick  * 	decay ** (5 * loadavg) ~= .1
6032908Smckusick  *
6132908Smckusick  * If we compute b as:
6232908Smckusick  * 	b = 2 * loadavg
6332908Smckusick  * then
6432908Smckusick  * 	decay = b / (b + 1)
6532908Smckusick  *
6632908Smckusick  * We now need to prove two things:
6732908Smckusick  *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
6832908Smckusick  *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
6932908Smckusick  *
7032908Smckusick  * Facts:
7132908Smckusick  *         For x close to zero, exp(x) =~ 1 + x, since
7232908Smckusick  *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
7332908Smckusick  *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
7432908Smckusick  *         For x close to zero, ln(1+x) =~ x, since
7532908Smckusick  *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
7632908Smckusick  *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
7732908Smckusick  *         ln(.1) =~ -2.30
7832908Smckusick  *
7932908Smckusick  * Proof of (1):
8032908Smckusick  *    Solve (factor)**(power) =~ .1 given power (5*loadav):
8132908Smckusick  *	solving for factor,
8232908Smckusick  *      ln(factor) =~ (-2.30/5*loadav), or
8347544Skarels  *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
8432908Smckusick  *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
8532908Smckusick  *
8632908Smckusick  * Proof of (2):
8732908Smckusick  *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
8832908Smckusick  *	solving for power,
8932908Smckusick  *      power*ln(b/(b+1)) =~ -2.30, or
9032908Smckusick  *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
9132908Smckusick  *
9232908Smckusick  * Actual power values for the implemented algorithm are as follows:
9332908Smckusick  *      loadav: 1       2       3       4
9432908Smckusick  *      power:  5.68    10.32   14.94   19.55
9532908Smckusick  */
9617541Skarels 
9738164Smckusick /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
9847544Skarels #define	loadfactor(loadav)	(2 * (loadav))
9947544Skarels #define	decay_cpu(loadfac, cpu)	(((loadfac) * (cpu)) / ((loadfac) + FSCALE))
1008102Sroot 
10138164Smckusick /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
10238164Smckusick fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;		/* exp(-1/20) */
10338164Smckusick 
1048102Sroot /*
10538164Smckusick  * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
10638164Smckusick  * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
10738164Smckusick  * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
10838164Smckusick  *
10938164Smckusick  * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
11038164Smckusick  *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
11138164Smckusick  *
11238164Smckusick  * If you dont want to bother with the faster/more-accurate formula, you
11338164Smckusick  * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
11438164Smckusick  * (more general) method of calculating the %age of CPU used by a process.
11538164Smckusick  */
11638164Smckusick #define	CCPU_SHIFT	11
11738164Smckusick 
11838164Smckusick /*
1198102Sroot  * Recompute process priorities, once a second
1208102Sroot  */
1218102Sroot schedcpu()
1228102Sroot {
12347544Skarels 	register fixpt_t loadfac = loadfactor(averunnable[0]);
1248102Sroot 	register struct proc *p;
12547544Skarels 	register int s;
12647544Skarels 	register unsigned int newcpu;
1278102Sroot 
1288102Sroot 	wakeup((caddr_t)&lbolt);
12916532Skarels 	for (p = allproc; p != NULL; p = p->p_nxt) {
13047544Skarels 		/*
13147544Skarels 		 * Increment time in/out of memory and sleep time
13247544Skarels 		 * (if sleeping).  We ignore overflow; with 16-bit int's
13347544Skarels 		 * (remember them?) overflow takes 45 days.
13447544Skarels 		 */
13547544Skarels 		p->p_time++;
13647544Skarels 		if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
13747544Skarels 			p->p_slptime++;
13838164Smckusick 		p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
13917541Skarels 		/*
14017541Skarels 		 * If the process has slept the entire second,
14117541Skarels 		 * stop recalculating its priority until it wakes up.
14217541Skarels 		 */
14338164Smckusick 		if (p->p_slptime > 1)
14417541Skarels 			continue;
14517541Skarels 		/*
14617541Skarels 		 * p_pctcpu is only for ps.
14717541Skarels 		 */
14838164Smckusick #if	(FSHIFT >= CCPU_SHIFT)
14938164Smckusick 		p->p_pctcpu += (hz == 100)?
15038164Smckusick 			((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
15138164Smckusick                 	100 * (((fixpt_t) p->p_cpticks)
15238164Smckusick 				<< (FSHIFT - CCPU_SHIFT)) / hz;
15338164Smckusick #else
15438164Smckusick 		p->p_pctcpu += ((FSCALE - ccpu) *
15538164Smckusick 			(p->p_cpticks * FSCALE / hz)) >> FSHIFT;
15638164Smckusick #endif
1578102Sroot 		p->p_cpticks = 0;
15847544Skarels 		newcpu = (u_int) decay_cpu(loadfac, p->p_cpu) + p->p_nice;
15947544Skarels 		p->p_cpu = min(newcpu, UCHAR_MAX);
16047544Skarels 		setpri(p);
16117541Skarels 		s = splhigh();	/* prevent state changes */
1628102Sroot 		if (p->p_pri >= PUSER) {
16347544Skarels #define	PPQ	(128 / NQS)		/* priorities per queue */
16449095Skarels 			if ((p != curproc) &&
1658102Sroot 			    p->p_stat == SRUN &&
1668102Sroot 			    (p->p_flag & SLOAD) &&
16716795Skarels 			    (p->p_pri / PPQ) != (p->p_usrpri / PPQ)) {
1688102Sroot 				remrq(p);
1698102Sroot 				p->p_pri = p->p_usrpri;
1708102Sroot 				setrq(p);
1718102Sroot 			} else
1728102Sroot 				p->p_pri = p->p_usrpri;
1738102Sroot 		}
1748102Sroot 		splx(s);
1758102Sroot 	}
1768102Sroot 	vmmeter();
1778102Sroot 	if (bclnlist != NULL)
17847544Skarels 		wakeup((caddr_t)pageproc);
1798624Sroot 	timeout(schedcpu, (caddr_t)0, hz);
1808102Sroot }
1818102Sroot 
18217541Skarels /*
18317541Skarels  * Recalculate the priority of a process after it has slept for a while.
18447544Skarels  * For all load averages >= 1 and max p_cpu of 255, sleeping for at least
18547544Skarels  * six times the loadfactor will decay p_cpu to zero.
18617541Skarels  */
18717541Skarels updatepri(p)
18817541Skarels 	register struct proc *p;
18917541Skarels {
19047544Skarels 	register unsigned int newcpu = p->p_cpu;
19147544Skarels 	register fixpt_t loadfac = loadfactor(averunnable[0]);
19217541Skarels 
19347544Skarels 	if (p->p_slptime > 5 * loadfac)
19447544Skarels 		p->p_cpu = 0;
19547544Skarels 	else {
19647544Skarels 		p->p_slptime--;	/* the first time was done in schedcpu */
19747544Skarels 		while (newcpu && --p->p_slptime)
19847544Skarels 			newcpu = (int) decay_cpu(loadfac, newcpu);
19947544Skarels 		p->p_cpu = min(newcpu, UCHAR_MAX);
20047544Skarels 	}
20147544Skarels 	setpri(p);
20217541Skarels }
20317541Skarels 
20433Sbill #define SQSIZE 0100	/* Must be power of 2 */
20533Sbill #define HASH(x)	(( (int) x >> 5) & (SQSIZE-1))
20621099Smckusick struct slpque {
20721099Smckusick 	struct proc *sq_head;
20821099Smckusick 	struct proc **sq_tailp;
20921099Smckusick } slpque[SQSIZE];
21033Sbill 
21133Sbill /*
21245671Skarels  * During autoconfiguration or after a panic, a sleep will simply
21345671Skarels  * lower the priority briefly to allow interrupts, then return.
21445671Skarels  * The priority to be used (safepri) is machine-dependent, thus this
21545671Skarels  * value is initialized and maintained in the machine-dependent layers.
21645671Skarels  * This priority will typically be 0, or the lowest priority
21745671Skarels  * that is safe for use on the interrupt stack; it can be made
21845671Skarels  * higher to block network software interrupts after panics.
21945671Skarels  */
22045671Skarels int safepri;
22145671Skarels 
22245671Skarels /*
22340711Skarels  * General sleep call.
22440711Skarels  * Suspends current process until a wakeup is made on chan.
22540711Skarels  * The process will then be made runnable with priority pri.
22640711Skarels  * Sleeps at most timo/hz seconds (0 means no timeout).
22740711Skarels  * If pri includes PCATCH flag, signals are checked
22840711Skarels  * before and after sleeping, else signals are not checked.
22940711Skarels  * Returns 0 if awakened, EWOULDBLOCK if the timeout expires.
23040711Skarels  * If PCATCH is set and a signal needs to be delivered,
23140711Skarels  * ERESTART is returned if the current system call should be restarted
23240711Skarels  * if possible, and EINTR is returned if the system call should
23340711Skarels  * be interrupted by the signal (return EINTR).
23433Sbill  */
23540711Skarels tsleep(chan, pri, wmesg, timo)
23640710Smarc 	caddr_t chan;
23740710Smarc 	int pri;
23840710Smarc 	char *wmesg;
23940710Smarc 	int timo;
24040710Smarc {
24149095Skarels 	register struct proc *p = curproc;
24240710Smarc 	register struct slpque *qp;
24340710Smarc 	register s;
24440711Skarels 	int sig, catch = pri & PCATCH;
24540710Smarc 	extern int cold;
24640710Smarc 	int endtsleep();
24740710Smarc 
24840710Smarc 	s = splhigh();
24940710Smarc 	if (cold || panicstr) {
25040710Smarc 		/*
25140710Smarc 		 * After a panic, or during autoconfiguration,
25240710Smarc 		 * just give interrupts a chance, then just return;
25340710Smarc 		 * don't run any other procs or panic below,
25440710Smarc 		 * in case this is the idle process and already asleep.
25540710Smarc 		 */
25645671Skarels 		splx(safepri);
25740710Smarc 		splx(s);
25840710Smarc 		return (0);
25940710Smarc 	}
26040710Smarc #ifdef DIAGNOSTIC
26147544Skarels 	if (chan == 0 || p->p_stat != SRUN || p->p_rlink)
26240711Skarels 		panic("tsleep");
26340710Smarc #endif
26447544Skarels 	p->p_wchan = chan;
26547544Skarels 	p->p_wmesg = wmesg;
26647544Skarels 	p->p_slptime = 0;
26747544Skarels 	p->p_pri = pri & PRIMASK;
26840710Smarc 	qp = &slpque[HASH(chan)];
26940710Smarc 	if (qp->sq_head == 0)
27047544Skarels 		qp->sq_head = p;
27140710Smarc 	else
27247544Skarels 		*qp->sq_tailp = p;
27347544Skarels 	*(qp->sq_tailp = &p->p_link) = 0;
27445671Skarels 	if (timo)
27547544Skarels 		timeout(endtsleep, (caddr_t)p, timo);
27640710Smarc 	/*
27747544Skarels 	 * We put ourselves on the sleep queue and start our timeout
27847544Skarels 	 * before calling CURSIG, as we could stop there, and a wakeup
27947544Skarels 	 * or a SIGCONT (or both) could occur while we were stopped.
28045671Skarels 	 * A SIGCONT would cause us to be marked as SSLEEP
28145671Skarels 	 * without resuming us, thus we must be ready for sleep
28245671Skarels 	 * when CURSIG is called.  If the wakeup happens while we're
28347544Skarels 	 * stopped, p->p_wchan will be 0 upon return from CURSIG.
28440710Smarc 	 */
28540711Skarels 	if (catch) {
28647544Skarels 		p->p_flag |= SSINTR;
28747544Skarels 		if (sig = CURSIG(p)) {
28847544Skarels 			if (p->p_wchan)
28947544Skarels 				unsleep(p);
29047544Skarels 			p->p_stat = SRUN;
29145671Skarels 			goto resume;
29240711Skarels 		}
29347544Skarels 		if (p->p_wchan == 0) {
29445671Skarels 			catch = 0;
29545671Skarels 			goto resume;
29640711Skarels 		}
29740710Smarc 	}
29847544Skarels 	p->p_stat = SSLEEP;
29940710Smarc 	(void) spl0();
30047544Skarels 	p->p_stats->p_ru.ru_nvcsw++;
30140710Smarc 	swtch();
30245671Skarels resume:
30347544Skarels 	curpri = p->p_usrpri;
30440710Smarc 	splx(s);
30547544Skarels 	p->p_flag &= ~SSINTR;
30647544Skarels 	if (p->p_flag & STIMO) {
30747544Skarels 		p->p_flag &= ~STIMO;
30845671Skarels 		if (catch == 0 || sig == 0)
30945671Skarels 			return (EWOULDBLOCK);
31045671Skarels 	} else if (timo)
31147544Skarels 		untimeout(endtsleep, (caddr_t)p);
31247544Skarels 	if (catch && (sig != 0 || (sig = CURSIG(p)))) {
31347544Skarels 		if (p->p_sigacts->ps_sigintr & sigmask(sig))
31440711Skarels 			return (EINTR);
31540711Skarels 		return (ERESTART);
31640711Skarels 	}
31740710Smarc 	return (0);
31840710Smarc }
31940710Smarc 
32040710Smarc /*
32140710Smarc  * Implement timeout for tsleep.
32240710Smarc  * If process hasn't been awakened (wchan non-zero),
32340710Smarc  * set timeout flag and undo the sleep.  If proc
32440710Smarc  * is stopped, just unsleep so it will remain stopped.
32540710Smarc  */
32640710Smarc endtsleep(p)
32740710Smarc 	register struct proc *p;
32840710Smarc {
32940710Smarc 	int s = splhigh();
33040710Smarc 
33140710Smarc 	if (p->p_wchan) {
33240710Smarc 		if (p->p_stat == SSLEEP)
33340710Smarc 			setrun(p);
33440710Smarc 		else
33540710Smarc 			unsleep(p);
33640710Smarc 		p->p_flag |= STIMO;
33740710Smarc 	}
33840710Smarc 	splx(s);
33940710Smarc }
34040710Smarc 
34140711Skarels /*
34240711Skarels  * Short-term, non-interruptable sleep.
34340711Skarels  */
34433Sbill sleep(chan, pri)
3458033Sroot 	caddr_t chan;
3468033Sroot 	int pri;
34733Sbill {
34849095Skarels 	register struct proc *p = curproc;
34921099Smckusick 	register struct slpque *qp;
350207Sbill 	register s;
35130532Skarels 	extern int cold;
35233Sbill 
35340711Skarels #ifdef DIAGNOSTIC
35440711Skarels 	if (pri > PZERO) {
35540711Skarels 		printf("sleep called with pri %d > PZERO, wchan: %x\n",
35640711Skarels 			pri, chan);
35740711Skarels 		panic("old sleep");
35840711Skarels 	}
35940711Skarels #endif
36017541Skarels 	s = splhigh();
36130532Skarels 	if (cold || panicstr) {
36218363Skarels 		/*
36330532Skarels 		 * After a panic, or during autoconfiguration,
36430532Skarels 		 * just give interrupts a chance, then just return;
36530532Skarels 		 * don't run any other procs or panic below,
36630532Skarels 		 * in case this is the idle process and already asleep.
36718363Skarels 		 */
36845671Skarels 		splx(safepri);
36918363Skarels 		splx(s);
37018363Skarels 		return;
37118363Skarels 	}
37240710Smarc #ifdef DIAGNOSTIC
37347544Skarels 	if (chan==0 || p->p_stat != SRUN || p->p_rlink)
37433Sbill 		panic("sleep");
37540710Smarc #endif
37647544Skarels 	p->p_wchan = chan;
37747544Skarels 	p->p_wmesg = NULL;
37847544Skarels 	p->p_slptime = 0;
37947544Skarels 	p->p_pri = pri;
38021099Smckusick 	qp = &slpque[HASH(chan)];
38121099Smckusick 	if (qp->sq_head == 0)
38247544Skarels 		qp->sq_head = p;
38321099Smckusick 	else
38447544Skarels 		*qp->sq_tailp = p;
38547544Skarels 	*(qp->sq_tailp = &p->p_link) = 0;
38647544Skarels 	p->p_stat = SSLEEP;
38740711Skarels 	(void) spl0();
38847544Skarels 	p->p_stats->p_ru.ru_nvcsw++;
38940711Skarels 	swtch();
39047544Skarels 	curpri = p->p_usrpri;
39133Sbill 	splx(s);
39233Sbill }
39333Sbill 
39433Sbill /*
395181Sbill  * Remove a process from its wait queue
396181Sbill  */
397181Sbill unsleep(p)
3984826Swnj 	register struct proc *p;
399181Sbill {
40021099Smckusick 	register struct slpque *qp;
401181Sbill 	register struct proc **hp;
40221099Smckusick 	int s;
403181Sbill 
40417541Skarels 	s = splhigh();
405181Sbill 	if (p->p_wchan) {
40621099Smckusick 		hp = &(qp = &slpque[HASH(p->p_wchan)])->sq_head;
407181Sbill 		while (*hp != p)
408181Sbill 			hp = &(*hp)->p_link;
409181Sbill 		*hp = p->p_link;
41021099Smckusick 		if (qp->sq_tailp == &p->p_link)
41121099Smckusick 			qp->sq_tailp = hp;
412181Sbill 		p->p_wchan = 0;
413181Sbill 	}
414181Sbill 	splx(s);
415181Sbill }
416181Sbill 
417181Sbill /*
41847544Skarels  * Wakeup on "chan"; set all processes
41947544Skarels  * sleeping on chan to run state.
42033Sbill  */
42133Sbill wakeup(chan)
4224826Swnj 	register caddr_t chan;
42333Sbill {
42421099Smckusick 	register struct slpque *qp;
42521099Smckusick 	register struct proc *p, **q;
42633Sbill 	int s;
42733Sbill 
42817541Skarels 	s = splhigh();
42921099Smckusick 	qp = &slpque[HASH(chan)];
43033Sbill restart:
43121099Smckusick 	for (q = &qp->sq_head; p = *q; ) {
43240710Smarc #ifdef DIAGNOSTIC
433181Sbill 		if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP)
43433Sbill 			panic("wakeup");
43540710Smarc #endif
43647544Skarels 		if (p->p_wchan == chan) {
43733Sbill 			p->p_wchan = 0;
438187Sbill 			*q = p->p_link;
43921099Smckusick 			if (qp->sq_tailp == &p->p_link)
44021099Smckusick 				qp->sq_tailp = q;
441181Sbill 			if (p->p_stat == SSLEEP) {
442181Sbill 				/* OPTIMIZED INLINE EXPANSION OF setrun(p) */
44321763Skarels 				if (p->p_slptime > 1)
44421763Skarels 					updatepri(p);
44547544Skarels 				p->p_slptime = 0;
446181Sbill 				p->p_stat = SRUN;
4472702Swnj 				if (p->p_flag & SLOAD)
448181Sbill 					setrq(p);
44916795Skarels 				/*
45016795Skarels 				 * Since curpri is a usrpri,
45116795Skarels 				 * p->p_pri is always better than curpri.
45216795Skarels 				 */
45347544Skarels 				if ((p->p_flag&SLOAD) == 0)
45447544Skarels 					wakeup((caddr_t)&proc0);
45547544Skarels 				else
45647544Skarels 					need_resched();
457181Sbill 				/* END INLINE EXPANSION */
458187Sbill 				goto restart;
45933Sbill 			}
460187Sbill 		} else
461187Sbill 			q = &p->p_link;
46233Sbill 	}
46333Sbill 	splx(s);
46433Sbill }
46533Sbill 
46633Sbill /*
46733Sbill  * Initialize the (doubly-linked) run queues
46833Sbill  * to be empty.
46933Sbill  */
47033Sbill rqinit()
47133Sbill {
47233Sbill 	register int i;
47333Sbill 
47433Sbill 	for (i = 0; i < NQS; i++)
47533Sbill 		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
47633Sbill }
47733Sbill 
47833Sbill /*
47947544Skarels  * Change process state to be runnable,
48047544Skarels  * placing it on the run queue if it is in memory,
48147544Skarels  * and awakening the swapper if it isn't in memory.
48233Sbill  */
48333Sbill setrun(p)
4844826Swnj 	register struct proc *p;
48533Sbill {
4864826Swnj 	register int s;
48733Sbill 
48817541Skarels 	s = splhigh();
48933Sbill 	switch (p->p_stat) {
49033Sbill 
49133Sbill 	case 0:
49233Sbill 	case SWAIT:
49333Sbill 	case SRUN:
49433Sbill 	case SZOMB:
49533Sbill 	default:
49633Sbill 		panic("setrun");
49733Sbill 
498207Sbill 	case SSTOP:
49933Sbill 	case SSLEEP:
500181Sbill 		unsleep(p);		/* e.g. when sending signals */
50133Sbill 		break;
50233Sbill 
50333Sbill 	case SIDL:
50433Sbill 		break;
50533Sbill 	}
50633Sbill 	p->p_stat = SRUN;
50733Sbill 	if (p->p_flag & SLOAD)
50833Sbill 		setrq(p);
50933Sbill 	splx(s);
51030232Skarels 	if (p->p_slptime > 1)
51130232Skarels 		updatepri(p);
51247544Skarels 	p->p_slptime = 0;
51347544Skarels 	if ((p->p_flag&SLOAD) == 0)
51447544Skarels 		wakeup((caddr_t)&proc0);
51547544Skarels 	else if (p->p_pri < curpri)
51647544Skarels 		need_resched();
51733Sbill }
51833Sbill 
51933Sbill /*
52047544Skarels  * Compute priority of process when running in user mode.
52147544Skarels  * Arrange to reschedule if the resulting priority
52247544Skarels  * is better than that of the current process.
52333Sbill  */
52447544Skarels setpri(p)
52547544Skarels 	register struct proc *p;
52633Sbill {
52747544Skarels 	register unsigned int newpri;
52833Sbill 
52947544Skarels 	newpri = PUSER + p->p_cpu / 4 + 2 * p->p_nice;
53047544Skarels 	newpri = min(newpri, MAXPRI);
53147544Skarels 	p->p_usrpri = newpri;
53247544Skarels 	if (newpri < curpri)
53347544Skarels 		need_resched();
53433Sbill }
535