xref: /csrg-svn/sys/kern/kern_synch.c (revision 21099)
1*21099Smckusick /*	kern_synch.c	6.7	85/05/27	*/
233Sbill 
39756Ssam #include "../machine/pte.h"
49756Ssam 
517093Sbloom #include "param.h"
617093Sbloom #include "systm.h"
717093Sbloom #include "dir.h"
817093Sbloom #include "user.h"
917093Sbloom #include "proc.h"
1017093Sbloom #include "file.h"
1117093Sbloom #include "inode.h"
1217093Sbloom #include "vm.h"
1317093Sbloom #include "kernel.h"
1417093Sbloom #include "buf.h"
159756Ssam 
169756Ssam #ifdef vax
178445Sroot #include "../vax/mtpr.h"	/* XXX */
189756Ssam #endif
198102Sroot /*
208102Sroot  * Force switch among equal priority processes every 100ms.
218102Sroot  */
228102Sroot roundrobin()
238102Sroot {
248102Sroot 
258102Sroot 	runrun++;
268102Sroot 	aston();
278624Sroot 	timeout(roundrobin, (caddr_t)0, hz / 10);
288102Sroot }
298102Sroot 
3017541Skarels /* fraction for digital decay to forget 90% of usage in 5*loadav sec */
3117541Skarels #define	filter(loadav) ((2 * (loadav)) / (2 * (loadav) + 1))
3217541Skarels 
338102Sroot double	ccpu = 0.95122942450071400909;		/* exp(-1/20) */
348102Sroot 
358102Sroot /*
368102Sroot  * Recompute process priorities, once a second
378102Sroot  */
388102Sroot schedcpu()
398102Sroot {
4016795Skarels 	register double ccpu1 = (1.0 - ccpu) / (double)hz;
418102Sroot 	register struct proc *p;
428102Sroot 	register int s, a;
4317541Skarels 	float scale = filter(avenrun[0]);
448102Sroot 
458102Sroot 	wakeup((caddr_t)&lbolt);
4616532Skarels 	for (p = allproc; p != NULL; p = p->p_nxt) {
478102Sroot 		if (p->p_time != 127)
488102Sroot 			p->p_time++;
498102Sroot 		if (p->p_stat==SSLEEP || p->p_stat==SSTOP)
508102Sroot 			if (p->p_slptime != 127)
518102Sroot 				p->p_slptime++;
5217541Skarels 		/*
5317541Skarels 		 * If the process has slept the entire second,
5417541Skarels 		 * stop recalculating its priority until it wakes up.
5517541Skarels 		 */
5617541Skarels 		if (p->p_slptime > 1) {
5717541Skarels 			p->p_pctcpu *= ccpu;
5817541Skarels 			continue;
5917541Skarels 		}
6017541Skarels 		/*
6117541Skarels 		 * p_pctcpu is only for ps.
6217541Skarels 		 */
6317541Skarels 		p->p_pctcpu = ccpu * p->p_pctcpu + ccpu1 * p->p_cpticks;
648102Sroot 		p->p_cpticks = 0;
6517541Skarels 		a = (int) (scale * (p->p_cpu & 0377)) + p->p_nice;
668102Sroot 		if (a < 0)
678102Sroot 			a = 0;
688102Sroot 		if (a > 255)
698102Sroot 			a = 255;
708102Sroot 		p->p_cpu = a;
718102Sroot 		(void) setpri(p);
7217541Skarels 		s = splhigh();	/* prevent state changes */
738102Sroot 		if (p->p_pri >= PUSER) {
7416795Skarels #define	PPQ	(128 / NQS)
758102Sroot 			if ((p != u.u_procp || noproc) &&
768102Sroot 			    p->p_stat == SRUN &&
778102Sroot 			    (p->p_flag & SLOAD) &&
7816795Skarels 			    (p->p_pri / PPQ) != (p->p_usrpri / PPQ)) {
798102Sroot 				remrq(p);
808102Sroot 				p->p_pri = p->p_usrpri;
818102Sroot 				setrq(p);
828102Sroot 			} else
838102Sroot 				p->p_pri = p->p_usrpri;
848102Sroot 		}
858102Sroot 		splx(s);
868102Sroot 	}
878102Sroot 	vmmeter();
888102Sroot 	if (runin!=0) {
898102Sroot 		runin = 0;
908102Sroot 		wakeup((caddr_t)&runin);
918102Sroot 	}
928102Sroot 	if (bclnlist != NULL)
938102Sroot 		wakeup((caddr_t)&proc[2]);
948624Sroot 	timeout(schedcpu, (caddr_t)0, hz);
958102Sroot }
968102Sroot 
9717541Skarels /*
9817541Skarels  * Recalculate the priority of a process after it has slept for a while.
9917541Skarels  */
10017541Skarels updatepri(p)
10117541Skarels 	register struct proc *p;
10217541Skarels {
10317541Skarels 	register int a = p->p_cpu & 0377;
10417541Skarels 	float scale = filter(avenrun[0]);
10517541Skarels 
10617541Skarels 	p->p_slptime--;		/* the first time was done in schedcpu */
10717541Skarels 	while (a && --p->p_slptime)
10817541Skarels 		a = (int) (scale * a) /* + p->p_nice */;
10917541Skarels 	if (a < 0)
11017541Skarels 		a = 0;
11117541Skarels 	if (a > 255)
11217541Skarels 		a = 255;
11317541Skarels 	p->p_cpu = a;
11417541Skarels 	(void) setpri(p);
11517541Skarels }
11617541Skarels 
11733Sbill #define SQSIZE 0100	/* Must be power of 2 */
11833Sbill #define HASH(x)	(( (int) x >> 5) & (SQSIZE-1))
119*21099Smckusick struct slpque {
120*21099Smckusick 	struct proc *sq_head;
121*21099Smckusick 	struct proc **sq_tailp;
122*21099Smckusick } slpque[SQSIZE];
12333Sbill 
12433Sbill /*
12533Sbill  * Give up the processor till a wakeup occurs
12633Sbill  * on chan, at which time the process
12733Sbill  * enters the scheduling queue at priority pri.
12833Sbill  * The most important effect of pri is that when
12933Sbill  * pri<=PZERO a signal cannot disturb the sleep;
13033Sbill  * if pri>PZERO signals will be processed.
13133Sbill  * Callers of this routine must be prepared for
13233Sbill  * premature return, and check that the reason for
13333Sbill  * sleeping has gone away.
13433Sbill  */
13533Sbill sleep(chan, pri)
1368033Sroot 	caddr_t chan;
1378033Sroot 	int pri;
13833Sbill {
139*21099Smckusick 	register struct proc *rp;
140*21099Smckusick 	register struct slpque *qp;
141207Sbill 	register s;
14233Sbill 
14333Sbill 	rp = u.u_procp;
14417541Skarels 	s = splhigh();
14518363Skarels 	if (panicstr) {
14618363Skarels 		/*
14718363Skarels 		 * After a panic, just give interrupts a chance,
14818363Skarels 		 * then just return; don't run any other procs
14918363Skarels 		 * or panic below, in case this is the idle process
15018363Skarels 		 * and already asleep.
15118363Skarels 		 * The splnet should be spl0 if the network was being used
15218363Skarels 		 * by the filesystem, but for now avoid network interrupts
15318363Skarels 		 * that might cause another panic.
15418363Skarels 		 */
15518363Skarels 		(void) splnet();
15618363Skarels 		splx(s);
15718363Skarels 		return;
15818363Skarels 	}
15918363Skarels 	if (chan==0 || rp->p_stat != SRUN || rp->p_rlink)
16033Sbill 		panic("sleep");
16133Sbill 	rp->p_wchan = chan;
16233Sbill 	rp->p_slptime = 0;
16333Sbill 	rp->p_pri = pri;
164*21099Smckusick 	qp = &slpque[HASH(chan)];
165*21099Smckusick 	if (qp->sq_head == 0)
166*21099Smckusick 		qp->sq_head = rp;
167*21099Smckusick 	else
168*21099Smckusick 		*qp->sq_tailp = rp;
169*21099Smckusick 	*(qp->sq_tailp = &rp->p_link) = 0;
1704826Swnj 	if (pri > PZERO) {
1714826Swnj 		if (ISSIG(rp)) {
172187Sbill 			if (rp->p_wchan)
173187Sbill 				unsleep(rp);
17433Sbill 			rp->p_stat = SRUN;
175131Sbill 			(void) spl0();
17633Sbill 			goto psig;
17733Sbill 		}
178187Sbill 		if (rp->p_wchan == 0)
179187Sbill 			goto out;
180187Sbill 		rp->p_stat = SSLEEP;
181131Sbill 		(void) spl0();
1828033Sroot 		u.u_ru.ru_nvcsw++;
18333Sbill 		swtch();
1844826Swnj 		if (ISSIG(rp))
18533Sbill 			goto psig;
18633Sbill 	} else {
187207Sbill 		rp->p_stat = SSLEEP;
188131Sbill 		(void) spl0();
1898033Sroot 		u.u_ru.ru_nvcsw++;
19033Sbill 		swtch();
19133Sbill 	}
19216795Skarels 	curpri = rp->p_usrpri;
193187Sbill out:
19433Sbill 	splx(s);
19533Sbill 	return;
19633Sbill 
19733Sbill 	/*
19833Sbill 	 * If priority was low (>PZERO) and
1994826Swnj 	 * there has been a signal, execute non-local goto through
2008113Sroot 	 * u.u_qsave, aborting the system call in progress (see trap.c)
20133Sbill 	 */
20233Sbill psig:
2038113Sroot 	longjmp(&u.u_qsave);
20433Sbill 	/*NOTREACHED*/
20533Sbill }
20633Sbill 
20733Sbill /*
208181Sbill  * Remove a process from its wait queue
209181Sbill  */
210181Sbill unsleep(p)
2114826Swnj 	register struct proc *p;
212181Sbill {
213*21099Smckusick 	register struct slpque *qp;
214181Sbill 	register struct proc **hp;
215*21099Smckusick 	int s;
216181Sbill 
21717541Skarels 	s = splhigh();
218181Sbill 	if (p->p_wchan) {
219*21099Smckusick 		hp = &(qp = &slpque[HASH(p->p_wchan)])->sq_head;
220181Sbill 		while (*hp != p)
221181Sbill 			hp = &(*hp)->p_link;
222181Sbill 		*hp = p->p_link;
223*21099Smckusick 		if (qp->sq_tailp == &p->p_link)
224*21099Smckusick 			qp->sq_tailp = hp;
225181Sbill 		p->p_wchan = 0;
226181Sbill 	}
227181Sbill 	splx(s);
228181Sbill }
229181Sbill 
230181Sbill /*
23133Sbill  * Wake up all processes sleeping on chan.
23233Sbill  */
23333Sbill wakeup(chan)
2344826Swnj 	register caddr_t chan;
23533Sbill {
236*21099Smckusick 	register struct slpque *qp;
237*21099Smckusick 	register struct proc *p, **q;
23833Sbill 	int s;
23933Sbill 
24017541Skarels 	s = splhigh();
241*21099Smckusick 	qp = &slpque[HASH(chan)];
24233Sbill restart:
243*21099Smckusick 	for (q = &qp->sq_head; p = *q; ) {
244181Sbill 		if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP)
24533Sbill 			panic("wakeup");
246207Sbill 		if (p->p_wchan==chan) {
24733Sbill 			p->p_wchan = 0;
248187Sbill 			*q = p->p_link;
24917541Skarels 			if (p->p_slptime > 1)
25017541Skarels 				updatepri(p);
251*21099Smckusick 			if (qp->sq_tailp == &p->p_link)
252*21099Smckusick 				qp->sq_tailp = q;
25333Sbill 			p->p_slptime = 0;
254181Sbill 			if (p->p_stat == SSLEEP) {
255181Sbill 				/* OPTIMIZED INLINE EXPANSION OF setrun(p) */
256181Sbill 				p->p_stat = SRUN;
2572702Swnj 				if (p->p_flag & SLOAD)
258181Sbill 					setrq(p);
25916795Skarels 				/*
26016795Skarels 				 * Since curpri is a usrpri,
26116795Skarels 				 * p->p_pri is always better than curpri.
26216795Skarels 				 */
26316795Skarels 				runrun++;
26416795Skarels 				aston();
2653545Swnj 				if ((p->p_flag&SLOAD) == 0) {
2663545Swnj 					if (runout != 0) {
2673545Swnj 						runout = 0;
2683545Swnj 						wakeup((caddr_t)&runout);
2693545Swnj 					}
2703545Swnj 					wantin++;
271181Sbill 				}
272181Sbill 				/* END INLINE EXPANSION */
273187Sbill 				goto restart;
27433Sbill 			}
275187Sbill 		} else
276187Sbill 			q = &p->p_link;
27733Sbill 	}
27833Sbill 	splx(s);
27933Sbill }
28033Sbill 
28133Sbill /*
28233Sbill  * Initialize the (doubly-linked) run queues
28333Sbill  * to be empty.
28433Sbill  */
28533Sbill rqinit()
28633Sbill {
28733Sbill 	register int i;
28833Sbill 
28933Sbill 	for (i = 0; i < NQS; i++)
29033Sbill 		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
29133Sbill }
29233Sbill 
29333Sbill /*
29433Sbill  * Set the process running;
29533Sbill  * arrange for it to be swapped in if necessary.
29633Sbill  */
29733Sbill setrun(p)
2984826Swnj 	register struct proc *p;
29933Sbill {
3004826Swnj 	register int s;
30133Sbill 
30217541Skarels 	s = splhigh();
30333Sbill 	switch (p->p_stat) {
30433Sbill 
30533Sbill 	case 0:
30633Sbill 	case SWAIT:
30733Sbill 	case SRUN:
30833Sbill 	case SZOMB:
30933Sbill 	default:
31033Sbill 		panic("setrun");
31133Sbill 
312207Sbill 	case SSTOP:
31333Sbill 	case SSLEEP:
314181Sbill 		unsleep(p);		/* e.g. when sending signals */
31533Sbill 		break;
31633Sbill 
31733Sbill 	case SIDL:
31833Sbill 		break;
31933Sbill 	}
32017541Skarels 	if (p->p_slptime > 1)
32117541Skarels 		updatepri(p);
32233Sbill 	p->p_stat = SRUN;
32333Sbill 	if (p->p_flag & SLOAD)
32433Sbill 		setrq(p);
32533Sbill 	splx(s);
3264826Swnj 	if (p->p_pri < curpri) {
32733Sbill 		runrun++;
3282443Swnj 		aston();
3292443Swnj 	}
3303545Swnj 	if ((p->p_flag&SLOAD) == 0) {
3314826Swnj 		if (runout != 0) {
3323545Swnj 			runout = 0;
3333545Swnj 			wakeup((caddr_t)&runout);
3343545Swnj 		}
3353545Swnj 		wantin++;
33633Sbill 	}
33733Sbill }
33833Sbill 
33933Sbill /*
34033Sbill  * Set user priority.
34133Sbill  * The rescheduling flag (runrun)
34233Sbill  * is set if the priority is better
34333Sbill  * than the currently running process.
34433Sbill  */
34533Sbill setpri(pp)
3464826Swnj 	register struct proc *pp;
34733Sbill {
3484826Swnj 	register int p;
34933Sbill 
3503875Swnj 	p = (pp->p_cpu & 0377)/4;
35117541Skarels 	p += PUSER + 2 * pp->p_nice;
3523530Swnj 	if (pp->p_rssize > pp->p_maxrss && freemem < desfree)
3533530Swnj 		p += 2*4;	/* effectively, nice(4) */
3544826Swnj 	if (p > 127)
35533Sbill 		p = 127;
3564826Swnj 	if (p < curpri) {
35733Sbill 		runrun++;
3582453Swnj 		aston();
3592453Swnj 	}
36033Sbill 	pp->p_usrpri = p;
3614826Swnj 	return (p);
36233Sbill }
363