xref: /freebsd/sys/kern/kern_synch.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
1 /*-
2  * Copyright (c) 1982, 1986, 1990, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_synch.c	8.6 (Berkeley) 1/21/94
39  * $Id: kern_synch.c,v 1.17 1996/01/03 21:42:12 wollman Exp $
40  */
41 
42 #include "opt_ktrace.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/kernel.h>
48 #include <sys/buf.h>
49 #include <sys/signalvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/signalvar.h>
52 #include <sys/vmmeter.h>
53 #include <vm/vm.h>
54 #include <vm/vm_param.h>
55 #include <vm/vm_extern.h>
56 #ifdef KTRACE
57 #include <sys/ktrace.h>
58 #endif
59 
60 #include <machine/cpu.h>
61 
62 static void rqinit __P((void *));
63 SYSINIT(runqueue, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, rqinit, NULL)
64 
65 u_char	curpriority;		/* usrpri of curproc */
66 int	lbolt;			/* once a second sleep address */
67 
68 extern void	endtsleep __P((void *));
69 extern void	updatepri __P((struct proc *p));
70 
71 /*
72  * Force switch among equal priority processes every 100ms.
73  */
74 /* ARGSUSED */
75 void
76 roundrobin(arg)
77 	void *arg;
78 {
79 
80 	need_resched();
81 	timeout(roundrobin, NULL, hz / 10);
82 }
83 
84 /*
85  * Constants for digital decay and forget:
86  *	90% of (p_estcpu) usage in 5 * loadav time
87  *	95% of (p_pctcpu) usage in 60 seconds (load insensitive)
88  *          Note that, as ps(1) mentions, this can let percentages
89  *          total over 100% (I've seen 137.9% for 3 processes).
90  *
91  * Note that hardclock updates p_estcpu and p_cpticks independently.
92  *
93  * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
94  * That is, the system wants to compute a value of decay such
95  * that the following for loop:
96  * 	for (i = 0; i < (5 * loadavg); i++)
97  * 		p_estcpu *= decay;
98  * will compute
99  * 	p_estcpu *= 0.1;
100  * for all values of loadavg:
101  *
102  * Mathematically this loop can be expressed by saying:
103  * 	decay ** (5 * loadavg) ~= .1
104  *
105  * The system computes decay as:
106  * 	decay = (2 * loadavg) / (2 * loadavg + 1)
107  *
108  * We wish to prove that the system's computation of decay
109  * will always fulfill the equation:
110  * 	decay ** (5 * loadavg) ~= .1
111  *
112  * If we compute b as:
113  * 	b = 2 * loadavg
114  * then
115  * 	decay = b / (b + 1)
116  *
117  * We now need to prove two things:
118  *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
119  *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
120  *
121  * Facts:
122  *         For x close to zero, exp(x) =~ 1 + x, since
123  *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
124  *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
125  *         For x close to zero, ln(1+x) =~ x, since
126  *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
127  *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
128  *         ln(.1) =~ -2.30
129  *
130  * Proof of (1):
131  *    Solve (factor)**(power) =~ .1 given power (5*loadav):
132  *	solving for factor,
133  *      ln(factor) =~ (-2.30/5*loadav), or
134  *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
135  *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
136  *
137  * Proof of (2):
138  *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
139  *	solving for power,
140  *      power*ln(b/(b+1)) =~ -2.30, or
141  *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
142  *
143  * Actual power values for the implemented algorithm are as follows:
144  *      loadav: 1       2       3       4
145  *      power:  5.68    10.32   14.94   19.55
146  */
147 
148 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
149 #define	loadfactor(loadav)	(2 * (loadav))
150 #define	decay_cpu(loadfac, cpu)	(((loadfac) * (cpu)) / ((loadfac) + FSCALE))
151 
152 /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
153 fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;		/* exp(-1/20) */
154 
155 /*
156  * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
157  * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
158  * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
159  *
160  * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
161  *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
162  *
163  * If you dont want to bother with the faster/more-accurate formula, you
164  * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
165  * (more general) method of calculating the %age of CPU used by a process.
166  */
167 #define	CCPU_SHIFT	11
168 
169 /*
170  * Recompute process priorities, every hz ticks.
171  */
172 /* ARGSUSED */
173 void
174 schedcpu(arg)
175 	void *arg;
176 {
177 	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
178 	register struct proc *p;
179 	register int s;
180 	register unsigned int newcpu;
181 
182 	wakeup((caddr_t)&lbolt);
183 	for (p = (struct proc *)allproc; p != NULL; p = p->p_next) {
184 		/*
185 		 * Increment time in/out of memory and sleep time
186 		 * (if sleeping).  We ignore overflow; with 16-bit int's
187 		 * (remember them?) overflow takes 45 days.
188 		 */
189 		p->p_swtime++;
190 		if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
191 			p->p_slptime++;
192 		p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
193 		/*
194 		 * If the process has slept the entire second,
195 		 * stop recalculating its priority until it wakes up.
196 		 */
197 		if (p->p_slptime > 1)
198 			continue;
199 		s = splstatclock();	/* prevent state changes */
200 		/*
201 		 * p_pctcpu is only for ps.
202 		 */
203 #if	(FSHIFT >= CCPU_SHIFT)
204 		p->p_pctcpu += (hz == 100)?
205 			((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
206                 	100 * (((fixpt_t) p->p_cpticks)
207 				<< (FSHIFT - CCPU_SHIFT)) / hz;
208 #else
209 		p->p_pctcpu += ((FSCALE - ccpu) *
210 			(p->p_cpticks * FSCALE / hz)) >> FSHIFT;
211 #endif
212 		p->p_cpticks = 0;
213 		newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu) + p->p_nice;
214 		p->p_estcpu = min(newcpu, UCHAR_MAX);
215 		resetpriority(p);
216 		if (p->p_priority >= PUSER) {
217 #define	PPQ	(128 / NQS)		/* priorities per queue */
218 			if ((p != curproc) &&
219 			    p->p_stat == SRUN &&
220 			    (p->p_flag & P_INMEM) &&
221 			    (p->p_priority / PPQ) != (p->p_usrpri / PPQ)) {
222 				remrq(p);
223 				p->p_priority = p->p_usrpri;
224 				setrunqueue(p);
225 			} else
226 				p->p_priority = p->p_usrpri;
227 		}
228 		splx(s);
229 	}
230 	vmmeter();
231 	timeout(schedcpu, (void *)0, hz);
232 }
233 
234 /*
235  * Recalculate the priority of a process after it has slept for a while.
236  * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
237  * least six times the loadfactor will decay p_estcpu to zero.
238  */
239 void
240 updatepri(p)
241 	register struct proc *p;
242 {
243 	register unsigned int newcpu = p->p_estcpu;
244 	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
245 
246 	if (p->p_slptime > 5 * loadfac)
247 		p->p_estcpu = 0;
248 	else {
249 		p->p_slptime--;	/* the first time was done in schedcpu */
250 		while (newcpu && --p->p_slptime)
251 			newcpu = (int) decay_cpu(loadfac, newcpu);
252 		p->p_estcpu = min(newcpu, UCHAR_MAX);
253 	}
254 	resetpriority(p);
255 }
256 
257 /*
258  * We're only looking at 7 bits of the address; everything is
259  * aligned to 4, lots of things are aligned to greater powers
260  * of 2.  Shift right by 8, i.e. drop the bottom 256 worth.
261  */
262 #define TABLESIZE	128
263 #define LOOKUP(x)	(((int)(x) >> 8) & (TABLESIZE - 1))
264 struct slpque {
265 	struct proc *sq_head;
266 	struct proc **sq_tailp;
267 } slpque[TABLESIZE];
268 
269 /*
270  * During autoconfiguration or after a panic, a sleep will simply
271  * lower the priority briefly to allow interrupts, then return.
272  * The priority to be used (safepri) is machine-dependent, thus this
273  * value is initialized and maintained in the machine-dependent layers.
274  * This priority will typically be 0, or the lowest priority
275  * that is safe for use on the interrupt stack; it can be made
276  * higher to block network software interrupts after panics.
277  */
278 int safepri;
279 
280 /*
281  * General sleep call.  Suspends the current process until a wakeup is
282  * performed on the specified identifier.  The process will then be made
283  * runnable with the specified priority.  Sleeps at most timo/hz seconds
284  * (0 means no timeout).  If pri includes PCATCH flag, signals are checked
285  * before and after sleeping, else signals are not checked.  Returns 0 if
286  * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
287  * signal needs to be delivered, ERESTART is returned if the current system
288  * call should be restarted if possible, and EINTR is returned if the system
289  * call should be interrupted by the signal (return EINTR).
290  */
291 int
292 tsleep(ident, priority, wmesg, timo)
293 	void *ident;
294 	int priority, timo;
295 	char *wmesg;
296 {
297 	register struct proc *p = curproc;
298 	register struct slpque *qp;
299 	register s;
300 	int sig, catch = priority & PCATCH;
301 
302 #ifdef KTRACE
303 	if (KTRPOINT(p, KTR_CSW))
304 		ktrcsw(p->p_tracep, 1, 0);
305 #endif
306 	s = splhigh();
307 	if (cold || panicstr) {
308 		/*
309 		 * After a panic, or during autoconfiguration,
310 		 * just give interrupts a chance, then just return;
311 		 * don't run any other procs or panic below,
312 		 * in case this is the idle process and already asleep.
313 		 */
314 		splx(safepri);
315 		splx(s);
316 		return (0);
317 	}
318 #ifdef DIAGNOSTIC
319 	if (ident == NULL || p->p_stat != SRUN || p->p_back)
320 		panic("tsleep");
321 #endif
322 	p->p_wchan = ident;
323 	p->p_wmesg = wmesg;
324 	p->p_slptime = 0;
325 	p->p_priority = priority & PRIMASK;
326 	qp = &slpque[LOOKUP(ident)];
327 	if (qp->sq_head == 0)
328 		qp->sq_head = p;
329 	else
330 		*qp->sq_tailp = p;
331 	*(qp->sq_tailp = &p->p_forw) = 0;
332 	if (timo)
333 		timeout(endtsleep, (void *)p, timo);
334 	/*
335 	 * We put ourselves on the sleep queue and start our timeout
336 	 * before calling CURSIG, as we could stop there, and a wakeup
337 	 * or a SIGCONT (or both) could occur while we were stopped.
338 	 * A SIGCONT would cause us to be marked as SSLEEP
339 	 * without resuming us, thus we must be ready for sleep
340 	 * when CURSIG is called.  If the wakeup happens while we're
341 	 * stopped, p->p_wchan will be 0 upon return from CURSIG.
342 	 */
343 	if (catch) {
344 		p->p_flag |= P_SINTR;
345 		if ((sig = CURSIG(p))) {
346 			if (p->p_wchan)
347 				unsleep(p);
348 			p->p_stat = SRUN;
349 			goto resume;
350 		}
351 		if (p->p_wchan == 0) {
352 			catch = 0;
353 			goto resume;
354 		}
355 	} else
356 		sig = 0;
357 	p->p_stat = SSLEEP;
358 	p->p_stats->p_ru.ru_nvcsw++;
359 	mi_switch();
360 resume:
361 	curpriority = p->p_usrpri;
362 	splx(s);
363 	p->p_flag &= ~P_SINTR;
364 	if (p->p_flag & P_TIMEOUT) {
365 		p->p_flag &= ~P_TIMEOUT;
366 		if (sig == 0) {
367 #ifdef KTRACE
368 			if (KTRPOINT(p, KTR_CSW))
369 				ktrcsw(p->p_tracep, 0, 0);
370 #endif
371 			return (EWOULDBLOCK);
372 		}
373 	} else if (timo)
374 		untimeout(endtsleep, (void *)p);
375 	if (catch && (sig != 0 || (sig = CURSIG(p)))) {
376 #ifdef KTRACE
377 		if (KTRPOINT(p, KTR_CSW))
378 			ktrcsw(p->p_tracep, 0, 0);
379 #endif
380 		if (p->p_sigacts->ps_sigintr & sigmask(sig))
381 			return (EINTR);
382 		return (ERESTART);
383 	}
384 #ifdef KTRACE
385 	if (KTRPOINT(p, KTR_CSW))
386 		ktrcsw(p->p_tracep, 0, 0);
387 #endif
388 	return (0);
389 }
390 
391 /*
392  * Implement timeout for tsleep.
393  * If process hasn't been awakened (wchan non-zero),
394  * set timeout flag and undo the sleep.  If proc
395  * is stopped, just unsleep so it will remain stopped.
396  */
397 void
398 endtsleep(arg)
399 	void *arg;
400 {
401 	register struct proc *p;
402 	int s;
403 
404 	p = (struct proc *)arg;
405 	s = splhigh();
406 	if (p->p_wchan) {
407 		if (p->p_stat == SSLEEP)
408 			setrunnable(p);
409 		else
410 			unsleep(p);
411 		p->p_flag |= P_TIMEOUT;
412 	}
413 	splx(s);
414 }
415 
416 /*
417  * Short-term, non-interruptable sleep.
418  */
419 void
420 sleep(ident, priority)
421 	void *ident;
422 	int priority;
423 {
424 	register struct proc *p = curproc;
425 	register struct slpque *qp;
426 	register s;
427 
428 #ifdef DIAGNOSTIC
429 	if (priority > PZERO) {
430 		printf("sleep called with priority %d > PZERO, wchan: %p\n",
431 		    priority, ident);
432 		panic("old sleep");
433 	}
434 #endif
435 	s = splhigh();
436 	if (cold || panicstr) {
437 		/*
438 		 * After a panic, or during autoconfiguration,
439 		 * just give interrupts a chance, then just return;
440 		 * don't run any other procs or panic below,
441 		 * in case this is the idle process and already asleep.
442 		 */
443 		splx(safepri);
444 		splx(s);
445 		return;
446 	}
447 #ifdef DIAGNOSTIC
448 	if (ident == NULL || p->p_stat != SRUN || p->p_back)
449 		panic("sleep");
450 #endif
451 	p->p_wchan = ident;
452 	p->p_wmesg = NULL;
453 	p->p_slptime = 0;
454 	p->p_priority = priority;
455 	qp = &slpque[LOOKUP(ident)];
456 	if (qp->sq_head == 0)
457 		qp->sq_head = p;
458 	else
459 		*qp->sq_tailp = p;
460 	*(qp->sq_tailp = &p->p_forw) = 0;
461 	p->p_stat = SSLEEP;
462 	p->p_stats->p_ru.ru_nvcsw++;
463 #ifdef KTRACE
464 	if (KTRPOINT(p, KTR_CSW))
465 		ktrcsw(p->p_tracep, 1, 0);
466 #endif
467 	mi_switch();
468 #ifdef KTRACE
469 	if (KTRPOINT(p, KTR_CSW))
470 		ktrcsw(p->p_tracep, 0, 0);
471 #endif
472 	curpriority = p->p_usrpri;
473 	splx(s);
474 }
475 
476 /*
477  * Remove a process from its wait queue
478  */
479 void
480 unsleep(p)
481 	register struct proc *p;
482 {
483 	register struct slpque *qp;
484 	register struct proc **hp;
485 	int s;
486 
487 	s = splhigh();
488 	if (p->p_wchan) {
489 		hp = &(qp = &slpque[LOOKUP(p->p_wchan)])->sq_head;
490 		while (*hp != p)
491 			hp = &(*hp)->p_forw;
492 		*hp = p->p_forw;
493 		if (qp->sq_tailp == &p->p_forw)
494 			qp->sq_tailp = hp;
495 		p->p_wchan = 0;
496 	}
497 	splx(s);
498 }
499 
500 /*
501  * Make all processes sleeping on the specified identifier runnable.
502  */
503 void
504 wakeup(ident)
505 	register void *ident;
506 {
507 	register struct slpque *qp;
508 	register struct proc *p, **q;
509 	int s;
510 
511 	s = splhigh();
512 	qp = &slpque[LOOKUP(ident)];
513 restart:
514 	for (q = &qp->sq_head; *q; ) {
515 		p = *q;
516 #ifdef DIAGNOSTIC
517 		if (p->p_back || (p->p_stat != SSLEEP && p->p_stat != SSTOP))
518 			panic("wakeup");
519 #endif
520 		if (p->p_wchan == ident) {
521 			p->p_wchan = 0;
522 			*q = p->p_forw;
523 			if (qp->sq_tailp == &p->p_forw)
524 				qp->sq_tailp = q;
525 			if (p->p_stat == SSLEEP) {
526 				/* OPTIMIZED EXPANSION OF setrunnable(p); */
527 				if (p->p_slptime > 1)
528 					updatepri(p);
529 				p->p_slptime = 0;
530 				p->p_stat = SRUN;
531 				if (p->p_flag & P_INMEM)
532 					setrunqueue(p);
533 				/*
534 				 * Since curpriority is a user priority,
535 				 * p->p_priority is always better than
536 				 * curpriority.
537 				 */
538 				if ((p->p_flag & P_INMEM) == 0)
539 					wakeup((caddr_t)&proc0);
540 				else
541 					need_resched();
542 				/* END INLINE EXPANSION */
543 				goto restart;
544 			}
545 		} else
546 			q = &p->p_forw;
547 	}
548 	splx(s);
549 }
550 
551 /*
552  * The machine independent parts of mi_switch().
553  * Must be called at splstatclock() or higher.
554  */
555 void
556 mi_switch()
557 {
558 	register struct proc *p = curproc;	/* XXX */
559 	register struct rlimit *rlim;
560 	register long s, u;
561 	struct timeval tv;
562 
563 	/*
564 	 * Compute the amount of time during which the current
565 	 * process was running, and add that to its total so far.
566 	 */
567 	microtime(&tv);
568 	u = p->p_rtime.tv_usec + (tv.tv_usec - runtime.tv_usec);
569 	s = p->p_rtime.tv_sec + (tv.tv_sec - runtime.tv_sec);
570 	if (u < 0) {
571 		u += 1000000;
572 		s--;
573 	} else if (u >= 1000000) {
574 		u -= 1000000;
575 		s++;
576 	}
577 	p->p_rtime.tv_usec = u;
578 	p->p_rtime.tv_sec = s;
579 
580 	/*
581 	 * Check if the process exceeds its cpu resource allocation.
582 	 * If over max, kill it.  In any case, if it has run for more
583 	 * than 10 minutes, reduce priority to give others a chance.
584 	 */
585 	if (p->p_stat != SZOMB) {
586 		rlim = &p->p_rlimit[RLIMIT_CPU];
587 		if (s >= rlim->rlim_cur) {
588 			if (s >= rlim->rlim_max)
589 				killproc(p, "exceeded maximum CPU limit");
590 			else {
591 				psignal(p, SIGXCPU);
592 				if (rlim->rlim_cur < rlim->rlim_max)
593 					rlim->rlim_cur += 5;
594 			}
595 		}
596 		if (s > 10 * 60 && p->p_ucred->cr_uid && p->p_nice == NZERO) {
597 			p->p_nice = NZERO + 4;
598 			resetpriority(p);
599 		}
600 	}
601 
602 	/*
603 	 * Pick a new current process and record its start time.
604 	 */
605 	cnt.v_swtch++;
606 	cpu_switch(p);
607 	microtime(&runtime);
608 }
609 
610 /*
611  * Initialize the (doubly-linked) run queues
612  * to be empty.
613  */
614 /* ARGSUSED*/
615 static void
616 rqinit(dummy)
617 	void *dummy;
618 {
619 	register int i;
620 
621 	for (i = 0; i < NQS; i++) {
622 		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
623 		rtqs[i].ph_link = rtqs[i].ph_rlink = (struct proc *)&rtqs[i];
624 		idqs[i].ph_link = idqs[i].ph_rlink = (struct proc *)&idqs[i];
625 	}
626 }
627 
628 /*
629  * Change process state to be runnable,
630  * placing it on the run queue if it is in memory,
631  * and awakening the swapper if it isn't in memory.
632  */
633 void
634 setrunnable(p)
635 	register struct proc *p;
636 {
637 	register int s;
638 
639 	s = splhigh();
640 	switch (p->p_stat) {
641 	case 0:
642 	case SRUN:
643 	case SZOMB:
644 	default:
645 		panic("setrunnable");
646 	case SSTOP:
647 	case SSLEEP:
648 		unsleep(p);		/* e.g. when sending signals */
649 		break;
650 
651 	case SIDL:
652 		break;
653 	}
654 	p->p_stat = SRUN;
655 	if (p->p_flag & P_INMEM)
656 		setrunqueue(p);
657 	splx(s);
658 	if (p->p_slptime > 1)
659 		updatepri(p);
660 	p->p_slptime = 0;
661 	if ((p->p_flag & P_INMEM) == 0)
662 		wakeup((caddr_t)&proc0);
663 	else if (p->p_priority < curpriority)
664 		need_resched();
665 }
666 
667 /*
668  * Compute the priority of a process when running in user mode.
669  * Arrange to reschedule if the resulting priority is better
670  * than that of the current process.
671  */
672 void
673 resetpriority(p)
674 	register struct proc *p;
675 {
676 	register unsigned int newpriority;
677 
678 	if (p->p_rtprio.type == RTP_PRIO_NORMAL) {
679 		newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice;
680 		newpriority = min(newpriority, MAXPRI);
681 		p->p_usrpri = newpriority;
682 		if (newpriority < curpriority)
683 			need_resched();
684 	} else {
685 		need_resched();
686 	}
687 }
688