xref: /csrg-svn/sys/kern/kern_synch.c (revision 18363)
1*18363Skarels /*	kern_synch.c	6.6	85/03/18	*/
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))
11933Sbill struct proc *slpque[SQSIZE];
12033Sbill 
12133Sbill /*
12233Sbill  * Give up the processor till a wakeup occurs
12333Sbill  * on chan, at which time the process
12433Sbill  * enters the scheduling queue at priority pri.
12533Sbill  * The most important effect of pri is that when
12633Sbill  * pri<=PZERO a signal cannot disturb the sleep;
12733Sbill  * if pri>PZERO signals will be processed.
12833Sbill  * Callers of this routine must be prepared for
12933Sbill  * premature return, and check that the reason for
13033Sbill  * sleeping has gone away.
13133Sbill  */
13233Sbill sleep(chan, pri)
1338033Sroot 	caddr_t chan;
1348033Sroot 	int pri;
13533Sbill {
136187Sbill 	register struct proc *rp, **hp;
137207Sbill 	register s;
13833Sbill 
13933Sbill 	rp = u.u_procp;
14017541Skarels 	s = splhigh();
141*18363Skarels 	if (panicstr) {
142*18363Skarels 		/*
143*18363Skarels 		 * After a panic, just give interrupts a chance,
144*18363Skarels 		 * then just return; don't run any other procs
145*18363Skarels 		 * or panic below, in case this is the idle process
146*18363Skarels 		 * and already asleep.
147*18363Skarels 		 * The splnet should be spl0 if the network was being used
148*18363Skarels 		 * by the filesystem, but for now avoid network interrupts
149*18363Skarels 		 * that might cause another panic.
150*18363Skarels 		 */
151*18363Skarels 		(void) splnet();
152*18363Skarels 		splx(s);
153*18363Skarels 		return;
154*18363Skarels 	}
155*18363Skarels 	if (chan==0 || rp->p_stat != SRUN || rp->p_rlink)
15633Sbill 		panic("sleep");
15733Sbill 	rp->p_wchan = chan;
15833Sbill 	rp->p_slptime = 0;
15933Sbill 	rp->p_pri = pri;
160187Sbill 	hp = &slpque[HASH(chan)];
161187Sbill 	rp->p_link = *hp;
162187Sbill 	*hp = rp;
1634826Swnj 	if (pri > PZERO) {
1644826Swnj 		if (ISSIG(rp)) {
165187Sbill 			if (rp->p_wchan)
166187Sbill 				unsleep(rp);
16733Sbill 			rp->p_stat = SRUN;
168131Sbill 			(void) spl0();
16933Sbill 			goto psig;
17033Sbill 		}
171187Sbill 		if (rp->p_wchan == 0)
172187Sbill 			goto out;
173187Sbill 		rp->p_stat = SSLEEP;
174131Sbill 		(void) spl0();
1758033Sroot 		u.u_ru.ru_nvcsw++;
17633Sbill 		swtch();
1774826Swnj 		if (ISSIG(rp))
17833Sbill 			goto psig;
17933Sbill 	} else {
180207Sbill 		rp->p_stat = SSLEEP;
181131Sbill 		(void) spl0();
1828033Sroot 		u.u_ru.ru_nvcsw++;
18333Sbill 		swtch();
18433Sbill 	}
18516795Skarels 	curpri = rp->p_usrpri;
186187Sbill out:
18733Sbill 	splx(s);
18833Sbill 	return;
18933Sbill 
19033Sbill 	/*
19133Sbill 	 * If priority was low (>PZERO) and
1924826Swnj 	 * there has been a signal, execute non-local goto through
1938113Sroot 	 * u.u_qsave, aborting the system call in progress (see trap.c)
19433Sbill 	 */
19533Sbill psig:
1968113Sroot 	longjmp(&u.u_qsave);
19733Sbill 	/*NOTREACHED*/
19833Sbill }
19933Sbill 
20033Sbill /*
201181Sbill  * Remove a process from its wait queue
202181Sbill  */
203181Sbill unsleep(p)
2044826Swnj 	register struct proc *p;
205181Sbill {
206181Sbill 	register struct proc **hp;
207181Sbill 	register s;
208181Sbill 
20917541Skarels 	s = splhigh();
210181Sbill 	if (p->p_wchan) {
211181Sbill 		hp = &slpque[HASH(p->p_wchan)];
212181Sbill 		while (*hp != p)
213181Sbill 			hp = &(*hp)->p_link;
214181Sbill 		*hp = p->p_link;
215181Sbill 		p->p_wchan = 0;
216181Sbill 	}
217181Sbill 	splx(s);
218181Sbill }
219181Sbill 
220181Sbill /*
22133Sbill  * Wake up all processes sleeping on chan.
22233Sbill  */
22333Sbill wakeup(chan)
2244826Swnj 	register caddr_t chan;
22533Sbill {
226187Sbill 	register struct proc *p, **q, **h;
22733Sbill 	int s;
22833Sbill 
22917541Skarels 	s = splhigh();
230187Sbill 	h = &slpque[HASH(chan)];
23133Sbill restart:
232187Sbill 	for (q = h; p = *q; ) {
233181Sbill 		if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP)
23433Sbill 			panic("wakeup");
235207Sbill 		if (p->p_wchan==chan) {
23633Sbill 			p->p_wchan = 0;
237187Sbill 			*q = p->p_link;
23817541Skarels 			if (p->p_slptime > 1)
23917541Skarels 				updatepri(p);
24033Sbill 			p->p_slptime = 0;
241181Sbill 			if (p->p_stat == SSLEEP) {
242181Sbill 				/* OPTIMIZED INLINE EXPANSION OF setrun(p) */
243181Sbill 				p->p_stat = SRUN;
2442702Swnj 				if (p->p_flag & SLOAD)
245181Sbill 					setrq(p);
24616795Skarels 				/*
24716795Skarels 				 * Since curpri is a usrpri,
24816795Skarels 				 * p->p_pri is always better than curpri.
24916795Skarels 				 */
25016795Skarels 				runrun++;
25116795Skarels 				aston();
2523545Swnj 				if ((p->p_flag&SLOAD) == 0) {
2533545Swnj 					if (runout != 0) {
2543545Swnj 						runout = 0;
2553545Swnj 						wakeup((caddr_t)&runout);
2563545Swnj 					}
2573545Swnj 					wantin++;
258181Sbill 				}
259181Sbill 				/* END INLINE EXPANSION */
260187Sbill 				goto restart;
26133Sbill 			}
262187Sbill 		} else
263187Sbill 			q = &p->p_link;
26433Sbill 	}
26533Sbill 	splx(s);
26633Sbill }
26733Sbill 
26833Sbill /*
26933Sbill  * Initialize the (doubly-linked) run queues
27033Sbill  * to be empty.
27133Sbill  */
27233Sbill rqinit()
27333Sbill {
27433Sbill 	register int i;
27533Sbill 
27633Sbill 	for (i = 0; i < NQS; i++)
27733Sbill 		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
27833Sbill }
27933Sbill 
28033Sbill /*
28133Sbill  * Set the process running;
28233Sbill  * arrange for it to be swapped in if necessary.
28333Sbill  */
28433Sbill setrun(p)
2854826Swnj 	register struct proc *p;
28633Sbill {
2874826Swnj 	register int s;
28833Sbill 
28917541Skarels 	s = splhigh();
29033Sbill 	switch (p->p_stat) {
29133Sbill 
29233Sbill 	case 0:
29333Sbill 	case SWAIT:
29433Sbill 	case SRUN:
29533Sbill 	case SZOMB:
29633Sbill 	default:
29733Sbill 		panic("setrun");
29833Sbill 
299207Sbill 	case SSTOP:
30033Sbill 	case SSLEEP:
301181Sbill 		unsleep(p);		/* e.g. when sending signals */
30233Sbill 		break;
30333Sbill 
30433Sbill 	case SIDL:
30533Sbill 		break;
30633Sbill 	}
30717541Skarels 	if (p->p_slptime > 1)
30817541Skarels 		updatepri(p);
30933Sbill 	p->p_stat = SRUN;
31033Sbill 	if (p->p_flag & SLOAD)
31133Sbill 		setrq(p);
31233Sbill 	splx(s);
3134826Swnj 	if (p->p_pri < curpri) {
31433Sbill 		runrun++;
3152443Swnj 		aston();
3162443Swnj 	}
3173545Swnj 	if ((p->p_flag&SLOAD) == 0) {
3184826Swnj 		if (runout != 0) {
3193545Swnj 			runout = 0;
3203545Swnj 			wakeup((caddr_t)&runout);
3213545Swnj 		}
3223545Swnj 		wantin++;
32333Sbill 	}
32433Sbill }
32533Sbill 
32633Sbill /*
32733Sbill  * Set user priority.
32833Sbill  * The rescheduling flag (runrun)
32933Sbill  * is set if the priority is better
33033Sbill  * than the currently running process.
33133Sbill  */
33233Sbill setpri(pp)
3334826Swnj 	register struct proc *pp;
33433Sbill {
3354826Swnj 	register int p;
33633Sbill 
3373875Swnj 	p = (pp->p_cpu & 0377)/4;
33817541Skarels 	p += PUSER + 2 * pp->p_nice;
3393530Swnj 	if (pp->p_rssize > pp->p_maxrss && freemem < desfree)
3403530Swnj 		p += 2*4;	/* effectively, nice(4) */
3414826Swnj 	if (p > 127)
34233Sbill 		p = 127;
3434826Swnj 	if (p < curpri) {
34433Sbill 		runrun++;
3452453Swnj 		aston();
3462453Swnj 	}
34733Sbill 	pp->p_usrpri = p;
3484826Swnj 	return (p);
34933Sbill }
350