xref: /csrg-svn/sys/kern/kern_synch.c (revision 29946)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)kern_synch.c	7.2 (Berkeley) 11/03/86
7  */
8 
9 #include "../machine/pte.h"
10 #include "../machine/psl.h"
11 #include "../machine/mtpr.h"
12 
13 #include "param.h"
14 #include "systm.h"
15 #include "dir.h"
16 #include "user.h"
17 #include "proc.h"
18 #include "file.h"
19 #include "inode.h"
20 #include "vm.h"
21 #include "kernel.h"
22 #include "buf.h"
23 
24 /*
25  * Force switch among equal priority processes every 100ms.
26  */
27 roundrobin()
28 {
29 
30 	runrun++;
31 	aston();
32 	timeout(roundrobin, (caddr_t)0, hz / 10);
33 }
34 
35 /* fraction for digital decay to forget 90% of usage in 5*loadav sec */
36 #define	filter(loadav) ((2 * (loadav)) / (2 * (loadav) + 1))
37 
38 double	ccpu = 0.95122942450071400909;		/* exp(-1/20) */
39 
40 /*
41  * Recompute process priorities, once a second
42  */
43 schedcpu()
44 {
45 	register double ccpu1 = (1.0 - ccpu) / (double)hz;
46 	register struct proc *p;
47 	register int s, a;
48 	float scale = filter(avenrun[0]);
49 
50 	wakeup((caddr_t)&lbolt);
51 	for (p = allproc; p != NULL; p = p->p_nxt) {
52 		if (p->p_time != 127)
53 			p->p_time++;
54 		if (p->p_stat==SSLEEP || p->p_stat==SSTOP)
55 			if (p->p_slptime != 127)
56 				p->p_slptime++;
57 		/*
58 		 * If the process has slept the entire second,
59 		 * stop recalculating its priority until it wakes up.
60 		 */
61 		if (p->p_slptime > 1) {
62 			p->p_pctcpu *= ccpu;
63 			continue;
64 		}
65 		/*
66 		 * p_pctcpu is only for ps.
67 		 */
68 		p->p_pctcpu = ccpu * p->p_pctcpu + ccpu1 * p->p_cpticks;
69 		p->p_cpticks = 0;
70 		a = (int) (scale * (p->p_cpu & 0377)) + p->p_nice;
71 		if (a < 0)
72 			a = 0;
73 		if (a > 255)
74 			a = 255;
75 		p->p_cpu = a;
76 		(void) setpri(p);
77 		s = splhigh();	/* prevent state changes */
78 		if (p->p_pri >= PUSER) {
79 #define	PPQ	(128 / NQS)
80 			if ((p != u.u_procp || noproc) &&
81 			    p->p_stat == SRUN &&
82 			    (p->p_flag & SLOAD) &&
83 			    (p->p_pri / PPQ) != (p->p_usrpri / PPQ)) {
84 				remrq(p);
85 				p->p_pri = p->p_usrpri;
86 				setrq(p);
87 			} else
88 				p->p_pri = p->p_usrpri;
89 		}
90 		splx(s);
91 	}
92 	vmmeter();
93 	if (runin!=0) {
94 		runin = 0;
95 		wakeup((caddr_t)&runin);
96 	}
97 	if (bclnlist != NULL)
98 		wakeup((caddr_t)&proc[2]);
99 	timeout(schedcpu, (caddr_t)0, hz);
100 }
101 
102 /*
103  * Recalculate the priority of a process after it has slept for a while.
104  */
105 updatepri(p)
106 	register struct proc *p;
107 {
108 	register int a = p->p_cpu & 0377;
109 	float scale = filter(avenrun[0]);
110 
111 	p->p_slptime--;		/* the first time was done in schedcpu */
112 	while (a && --p->p_slptime)
113 		a = (int) (scale * a) /* + p->p_nice */;
114 	if (a < 0)
115 		a = 0;
116 	if (a > 255)
117 		a = 255;
118 	p->p_cpu = a;
119 	(void) setpri(p);
120 }
121 
122 #define SQSIZE 0100	/* Must be power of 2 */
123 #define HASH(x)	(( (int) x >> 5) & (SQSIZE-1))
124 struct slpque {
125 	struct proc *sq_head;
126 	struct proc **sq_tailp;
127 } slpque[SQSIZE];
128 
129 /*
130  * Give up the processor till a wakeup occurs
131  * on chan, at which time the process
132  * enters the scheduling queue at priority pri.
133  * The most important effect of pri is that when
134  * pri<=PZERO a signal cannot disturb the sleep;
135  * if pri>PZERO signals will be processed.
136  * Callers of this routine must be prepared for
137  * premature return, and check that the reason for
138  * sleeping has gone away.
139  */
140 sleep(chan, pri)
141 	caddr_t chan;
142 	int pri;
143 {
144 	register struct proc *rp;
145 	register struct slpque *qp;
146 	register s;
147 
148 	rp = u.u_procp;
149 	s = splhigh();
150 	if (panicstr) {
151 		/*
152 		 * After a panic, just give interrupts a chance,
153 		 * then just return; don't run any other procs
154 		 * or panic below, in case this is the idle process
155 		 * and already asleep.
156 		 * The splnet should be spl0 if the network was being used
157 		 * by the filesystem, but for now avoid network interrupts
158 		 * that might cause another panic.
159 		 */
160 		(void) splnet();
161 		splx(s);
162 		return;
163 	}
164 	if (chan==0 || rp->p_stat != SRUN || rp->p_rlink)
165 		panic("sleep");
166 	rp->p_wchan = chan;
167 	rp->p_slptime = 0;
168 	rp->p_pri = pri;
169 	qp = &slpque[HASH(chan)];
170 	if (qp->sq_head == 0)
171 		qp->sq_head = rp;
172 	else
173 		*qp->sq_tailp = rp;
174 	*(qp->sq_tailp = &rp->p_link) = 0;
175 	if (pri > PZERO) {
176 		/*
177 		 * If we stop in issig(), wakeup may already have happened
178 		 * when we return (rp->p_wchan will then be 0).
179 		 */
180 		if (ISSIG(rp)) {
181 			if (rp->p_wchan)
182 				unsleep(rp);
183 			rp->p_stat = SRUN;
184 			(void) spl0();
185 			goto psig;
186 		}
187 		if (rp->p_wchan == 0)
188 			goto out;
189 		rp->p_stat = SSLEEP;
190 		(void) spl0();
191 		u.u_ru.ru_nvcsw++;
192 		swtch();
193 		if (ISSIG(rp))
194 			goto psig;
195 	} else {
196 		rp->p_stat = SSLEEP;
197 		(void) spl0();
198 		u.u_ru.ru_nvcsw++;
199 		swtch();
200 	}
201 	curpri = rp->p_usrpri;
202 out:
203 	splx(s);
204 	return;
205 
206 	/*
207 	 * If priority was low (>PZERO) and
208 	 * there has been a signal, execute non-local goto through
209 	 * u.u_qsave, aborting the system call in progress (see trap.c)
210 	 */
211 psig:
212 	longjmp(&u.u_qsave);
213 	/*NOTREACHED*/
214 }
215 
216 /*
217  * Remove a process from its wait queue
218  */
219 unsleep(p)
220 	register struct proc *p;
221 {
222 	register struct slpque *qp;
223 	register struct proc **hp;
224 	int s;
225 
226 	s = splhigh();
227 	if (p->p_wchan) {
228 		hp = &(qp = &slpque[HASH(p->p_wchan)])->sq_head;
229 		while (*hp != p)
230 			hp = &(*hp)->p_link;
231 		*hp = p->p_link;
232 		if (qp->sq_tailp == &p->p_link)
233 			qp->sq_tailp = hp;
234 		p->p_wchan = 0;
235 	}
236 	splx(s);
237 }
238 
239 /*
240  * Wake up all processes sleeping on chan.
241  */
242 wakeup(chan)
243 	register caddr_t chan;
244 {
245 	register struct slpque *qp;
246 	register struct proc *p, **q;
247 	int s;
248 
249 	s = splhigh();
250 	qp = &slpque[HASH(chan)];
251 restart:
252 	for (q = &qp->sq_head; p = *q; ) {
253 		if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP)
254 			panic("wakeup");
255 		if (p->p_wchan==chan) {
256 			p->p_wchan = 0;
257 			*q = p->p_link;
258 			if (qp->sq_tailp == &p->p_link)
259 				qp->sq_tailp = q;
260 			if (p->p_stat == SSLEEP) {
261 				/* OPTIMIZED INLINE EXPANSION OF setrun(p) */
262 				if (p->p_slptime > 1)
263 					updatepri(p);
264 				p->p_slptime = 0;
265 				p->p_stat = SRUN;
266 				if (p->p_flag & SLOAD)
267 					setrq(p);
268 				/*
269 				 * Since curpri is a usrpri,
270 				 * p->p_pri is always better than curpri.
271 				 */
272 				runrun++;
273 				aston();
274 				if ((p->p_flag&SLOAD) == 0) {
275 					if (runout != 0) {
276 						runout = 0;
277 						wakeup((caddr_t)&runout);
278 					}
279 					wantin++;
280 				}
281 				/* END INLINE EXPANSION */
282 				goto restart;
283 			}
284 			p->p_slptime = 0;
285 		} else
286 			q = &p->p_link;
287 	}
288 	splx(s);
289 }
290 
291 /*
292  * Initialize the (doubly-linked) run queues
293  * to be empty.
294  */
295 rqinit()
296 {
297 	register int i;
298 
299 	for (i = 0; i < NQS; i++)
300 		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
301 }
302 
303 /*
304  * Set the process running;
305  * arrange for it to be swapped in if necessary.
306  */
307 setrun(p)
308 	register struct proc *p;
309 {
310 	register int s;
311 
312 	s = splhigh();
313 	switch (p->p_stat) {
314 
315 	case 0:
316 	case SWAIT:
317 	case SRUN:
318 	case SZOMB:
319 	default:
320 		panic("setrun");
321 
322 	case SSTOP:
323 	case SSLEEP:
324 		unsleep(p);		/* e.g. when sending signals */
325 		break;
326 
327 	case SIDL:
328 		break;
329 	}
330 	if (p->p_slptime > 1)
331 		updatepri(p);
332 	p->p_stat = SRUN;
333 	if (p->p_flag & SLOAD)
334 		setrq(p);
335 	splx(s);
336 	if (p->p_pri < curpri) {
337 		runrun++;
338 		aston();
339 	}
340 	if ((p->p_flag&SLOAD) == 0) {
341 		if (runout != 0) {
342 			runout = 0;
343 			wakeup((caddr_t)&runout);
344 		}
345 		wantin++;
346 	}
347 }
348 
349 /*
350  * Set user priority.
351  * The rescheduling flag (runrun)
352  * is set if the priority is better
353  * than the currently running process.
354  */
355 setpri(pp)
356 	register struct proc *pp;
357 {
358 	register int p;
359 
360 	p = (pp->p_cpu & 0377)/4;
361 	p += PUSER + 2 * pp->p_nice;
362 	if (pp->p_rssize > pp->p_maxrss && freemem < desfree)
363 		p += 2*4;	/* effectively, nice(4) */
364 	if (p > 127)
365 		p = 127;
366 	if (p < curpri) {
367 		runrun++;
368 		aston();
369 	}
370 	pp->p_usrpri = p;
371 	return (p);
372 }
373