kern_synch.c revision 28268
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.9 (Berkeley) 5/19/95
39 * $Id: kern_synch.c,v 1.34 1997/08/13 19:29:33 julian Exp $
40 */
41
42#include "opt_ktrace.h"
43#include "opt_smp.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/proc.h>
48#include <sys/kernel.h>
49#include <sys/buf.h>
50#include <sys/signalvar.h>
51#include <sys/resourcevar.h>
52#include <sys/signalvar.h>
53#include <sys/vmmeter.h>
54#include <sys/sysctl.h>
55#include <vm/vm.h>
56#include <vm/vm_param.h>
57#include <vm/vm_extern.h>
58#ifdef KTRACE
59#include <sys/ktrace.h>
60#endif
61
62#include <machine/cpu.h>
63
64static void rqinit __P((void *));
65SYSINIT(runqueue, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, rqinit, NULL)
66
67u_char	curpriority;		/* usrpri of curproc */
68int	lbolt;			/* once a second sleep address */
69
70extern void	endtsleep __P((void *));
71extern void	updatepri __P((struct proc *p));
72
73#define MAXIMUM_SCHEDULE_QUANTUM	(1000000) /* arbitrary limit */
74#ifndef DEFAULT_SCHEDULE_QUANTUM
75#define DEFAULT_SCHEDULE_QUANTUM 10
76#endif
77static int quantum = DEFAULT_SCHEDULE_QUANTUM; /* default value */
78
79static int
80sysctl_kern_quantum SYSCTL_HANDLER_ARGS
81{
82	int error;
83	int new_val = quantum;
84
85	new_val = quantum;
86	error = sysctl_handle_int(oidp, &new_val, 0, req);
87	if ((error == 0) && (new_val > 0) && (new_val < MAXIMUM_SCHEDULE_QUANTUM)) {
88		quantum = new_val;
89	}
90	return (error);
91}
92
93SYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW,
94	0, sizeof quantum, sysctl_kern_quantum, "I", "");
95
96/*
97 * Force switch among equal priority processes every 100ms.
98 */
99/* ARGSUSED */
100void
101roundrobin(arg)
102	void *arg;
103{
104
105	need_resched();
106	timeout(roundrobin, NULL, hz / quantum);
107}
108
109/*
110 * Constants for digital decay and forget:
111 *	90% of (p_estcpu) usage in 5 * loadav time
112 *	95% of (p_pctcpu) usage in 60 seconds (load insensitive)
113 *          Note that, as ps(1) mentions, this can let percentages
114 *          total over 100% (I've seen 137.9% for 3 processes).
115 *
116 * Note that statclock updates p_estcpu and p_cpticks independently.
117 *
118 * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
119 * That is, the system wants to compute a value of decay such
120 * that the following for loop:
121 * 	for (i = 0; i < (5 * loadavg); i++)
122 * 		p_estcpu *= decay;
123 * will compute
124 * 	p_estcpu *= 0.1;
125 * for all values of loadavg:
126 *
127 * Mathematically this loop can be expressed by saying:
128 * 	decay ** (5 * loadavg) ~= .1
129 *
130 * The system computes decay as:
131 * 	decay = (2 * loadavg) / (2 * loadavg + 1)
132 *
133 * We wish to prove that the system's computation of decay
134 * will always fulfill the equation:
135 * 	decay ** (5 * loadavg) ~= .1
136 *
137 * If we compute b as:
138 * 	b = 2 * loadavg
139 * then
140 * 	decay = b / (b + 1)
141 *
142 * We now need to prove two things:
143 *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
144 *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
145 *
146 * Facts:
147 *         For x close to zero, exp(x) =~ 1 + x, since
148 *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
149 *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
150 *         For x close to zero, ln(1+x) =~ x, since
151 *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
152 *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
153 *         ln(.1) =~ -2.30
154 *
155 * Proof of (1):
156 *    Solve (factor)**(power) =~ .1 given power (5*loadav):
157 *	solving for factor,
158 *      ln(factor) =~ (-2.30/5*loadav), or
159 *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
160 *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
161 *
162 * Proof of (2):
163 *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
164 *	solving for power,
165 *      power*ln(b/(b+1)) =~ -2.30, or
166 *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
167 *
168 * Actual power values for the implemented algorithm are as follows:
169 *      loadav: 1       2       3       4
170 *      power:  5.68    10.32   14.94   19.55
171 */
172
173/* calculations for digital decay to forget 90% of usage in 5*loadav sec */
174#define	loadfactor(loadav)	(2 * (loadav))
175#define	decay_cpu(loadfac, cpu)	(((loadfac) * (cpu)) / ((loadfac) + FSCALE))
176
177/* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
178fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;		/* exp(-1/20) */
179
180/*
181 * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
182 * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
183 * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
184 *
185 * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
186 *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
187 *
188 * If you dont want to bother with the faster/more-accurate formula, you
189 * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
190 * (more general) method of calculating the %age of CPU used by a process.
191 */
192#define	CCPU_SHIFT	11
193
194/*
195 * Recompute process priorities, every hz ticks.
196 */
197/* ARGSUSED */
198void
199schedcpu(arg)
200	void *arg;
201{
202	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
203	register struct proc *p;
204	register int s, i, j;
205	register unsigned int newcpu;
206
207	wakeup((caddr_t)&lbolt);
208	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
209		/*
210		 * Increment time in/out of memory and sleep time
211		 * (if sleeping).  We ignore overflow; with 16-bit int's
212		 * (remember them?) overflow takes 45 days.
213		 */
214		p->p_swtime++;
215		if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
216			p->p_slptime++;
217		p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
218		/*
219		 * If the process has slept the entire second,
220		 * stop recalculating its priority until it wakes up.
221		 */
222		if (p->p_slptime > 1)
223			continue;
224		s = splhigh();	/* prevent state changes and protect run queue */
225		/*
226		 * p_pctcpu is only for ps.
227		 */
228#if	(FSHIFT >= CCPU_SHIFT)
229		p->p_pctcpu += (hz == 100)?
230			((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
231                	100 * (((fixpt_t) p->p_cpticks)
232				<< (FSHIFT - CCPU_SHIFT)) / hz;
233#else
234		p->p_pctcpu += ((FSCALE - ccpu) *
235			(p->p_cpticks * FSCALE / hz)) >> FSHIFT;
236#endif
237		p->p_cpticks = 0;
238		newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu) + p->p_nice;
239		p->p_estcpu = min(newcpu, UCHAR_MAX);
240		resetpriority(p);
241		if (p->p_priority >= PUSER) {
242#define	PPQ	(128 / NQS)		/* priorities per queue */
243			if ((p != curproc) &&
244#ifdef SMP
245			    (u_char)p->p_oncpu == 0xff && 	/* idle */
246#endif
247			    p->p_stat == SRUN &&
248			    (p->p_flag & P_INMEM) &&
249			    (p->p_priority / PPQ) != (p->p_usrpri / PPQ)) {
250				remrq(p);
251				p->p_priority = p->p_usrpri;
252				setrunqueue(p);
253			} else
254				p->p_priority = p->p_usrpri;
255		}
256		splx(s);
257	not_mine:
258	}
259	vmmeter();
260	timeout(schedcpu, (void *)0, hz);
261}
262
263/*
264 * Recalculate the priority of a process after it has slept for a while.
265 * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
266 * least six times the loadfactor will decay p_estcpu to zero.
267 */
268void
269updatepri(p)
270	register struct proc *p;
271{
272	register unsigned int newcpu = p->p_estcpu;
273	register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
274
275	if (p->p_slptime > 5 * loadfac)
276		p->p_estcpu = 0;
277	else {
278		p->p_slptime--;	/* the first time was done in schedcpu */
279		while (newcpu && --p->p_slptime)
280			newcpu = (int) decay_cpu(loadfac, newcpu);
281		p->p_estcpu = min(newcpu, UCHAR_MAX);
282	}
283	resetpriority(p);
284}
285
286/*
287 * We're only looking at 7 bits of the address; everything is
288 * aligned to 4, lots of things are aligned to greater powers
289 * of 2.  Shift right by 8, i.e. drop the bottom 256 worth.
290 */
291#define TABLESIZE	128
292TAILQ_HEAD(slpquehead, proc) slpque[TABLESIZE];
293#define LOOKUP(x)	(((long)(x) >> 8) & (TABLESIZE - 1))
294
295/*
296 * During autoconfiguration or after a panic, a sleep will simply
297 * lower the priority briefly to allow interrupts, then return.
298 * The priority to be used (safepri) is machine-dependent, thus this
299 * value is initialized and maintained in the machine-dependent layers.
300 * This priority will typically be 0, or the lowest priority
301 * that is safe for use on the interrupt stack; it can be made
302 * higher to block network software interrupts after panics.
303 */
304int safepri;
305
306void
307sleepinit()
308{
309	int i;
310
311	for (i = 0; i < TABLESIZE; i++)
312		TAILQ_INIT(&slpque[i]);
313}
314
315/*
316 * General sleep call.  Suspends the current process until a wakeup is
317 * performed on the specified identifier.  The process will then be made
318 * runnable with the specified priority.  Sleeps at most timo/hz seconds
319 * (0 means no timeout).  If pri includes PCATCH flag, signals are checked
320 * before and after sleeping, else signals are not checked.  Returns 0 if
321 * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
322 * signal needs to be delivered, ERESTART is returned if the current system
323 * call should be restarted if possible, and EINTR is returned if the system
324 * call should be interrupted by the signal (return EINTR).
325 */
326int
327tsleep(ident, priority, wmesg, timo)
328	void *ident;
329	int priority, timo;
330	char *wmesg;
331{
332	struct proc *p = curproc;
333	int s, sig, catch = priority & PCATCH;
334
335#ifdef KTRACE
336	if (KTRPOINT(p, KTR_CSW))
337		ktrcsw(p->p_tracep, 1, 0);
338#endif
339	s = splhigh();
340	if (cold || panicstr) {
341		/*
342		 * After a panic, or during autoconfiguration,
343		 * just give interrupts a chance, then just return;
344		 * don't run any other procs or panic below,
345		 * in case this is the idle process and already asleep.
346		 */
347		splx(safepri);
348		splx(s);
349		return (0);
350	}
351#ifdef DIAGNOSTIC
352	if(p == NULL)
353		panic("tsleep1");
354	if (ident == NULL || p->p_stat != SRUN)
355		panic("tsleep");
356	/* XXX This is not exhaustive, just the most common case */
357	if ((p->p_procq.tqe_next != NULL)
358#if 0 /* XXX this is impossible... the types don't even match */
359	    && (p->p_procq.tqe_next == p->p_procq.tqe_prev)
360#endif
361	    && (*p->p_procq.tqe_prev == p))
362		panic("sleeping process on run queue");
363#endif
364	p->p_wchan = ident;
365	p->p_wmesg = wmesg;
366	p->p_slptime = 0;
367	p->p_priority = priority & PRIMASK;
368	TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_procq);
369	if (timo)
370		timeout(endtsleep, (void *)p, timo);
371	/*
372	 * We put ourselves on the sleep queue and start our timeout
373	 * before calling CURSIG, as we could stop there, and a wakeup
374	 * or a SIGCONT (or both) could occur while we were stopped.
375	 * A SIGCONT would cause us to be marked as SSLEEP
376	 * without resuming us, thus we must be ready for sleep
377	 * when CURSIG is called.  If the wakeup happens while we're
378	 * stopped, p->p_wchan will be 0 upon return from CURSIG.
379	 */
380	if (catch) {
381		p->p_flag |= P_SINTR;
382		if ((sig = CURSIG(p))) {
383			if (p->p_wchan)
384				unsleep(p);
385			p->p_stat = SRUN;
386			goto resume;
387		}
388		if (p->p_wchan == 0) {
389			catch = 0;
390			goto resume;
391		}
392	} else
393		sig = 0;
394	p->p_stat = SSLEEP;
395	p->p_stats->p_ru.ru_nvcsw++;
396	mi_switch();
397resume:
398	curpriority = p->p_usrpri;
399	splx(s);
400	p->p_flag &= ~P_SINTR;
401	if (p->p_flag & P_TIMEOUT) {
402		p->p_flag &= ~P_TIMEOUT;
403		if (sig == 0) {
404#ifdef KTRACE
405			if (KTRPOINT(p, KTR_CSW))
406				ktrcsw(p->p_tracep, 0, 0);
407#endif
408			return (EWOULDBLOCK);
409		}
410	} else if (timo)
411		untimeout(endtsleep, (void *)p);
412	if (catch && (sig != 0 || (sig = CURSIG(p)))) {
413#ifdef KTRACE
414		if (KTRPOINT(p, KTR_CSW))
415			ktrcsw(p->p_tracep, 0, 0);
416#endif
417		if (p->p_sigacts->ps_sigintr & sigmask(sig))
418			return (EINTR);
419		return (ERESTART);
420	}
421#ifdef KTRACE
422	if (KTRPOINT(p, KTR_CSW))
423		ktrcsw(p->p_tracep, 0, 0);
424#endif
425	return (0);
426}
427
428/*
429 * Implement timeout for tsleep.
430 * If process hasn't been awakened (wchan non-zero),
431 * set timeout flag and undo the sleep.  If proc
432 * is stopped, just unsleep so it will remain stopped.
433 */
434void
435endtsleep(arg)
436	void *arg;
437{
438	register struct proc *p;
439	int s;
440
441	p = (struct proc *)arg;
442	s = splhigh();
443	if (p->p_wchan) {
444		if (p->p_stat == SSLEEP)
445			setrunnable(p);
446		else
447			unsleep(p);
448		p->p_flag |= P_TIMEOUT;
449	}
450	splx(s);
451}
452
453/*
454 * Remove a process from its wait queue
455 */
456void
457unsleep(p)
458	register struct proc *p;
459{
460	int s;
461
462	s = splhigh();
463	if (p->p_wchan) {
464		TAILQ_REMOVE(&slpque[LOOKUP(p->p_wchan)], p, p_procq);
465		p->p_wchan = 0;
466	}
467	splx(s);
468}
469
470/*
471 * Make all processes sleeping on the specified identifier runnable.
472 */
473void
474wakeup(ident)
475	register void *ident;
476{
477	register struct slpquehead *qp;
478	register struct proc *p;
479	int s;
480
481	s = splhigh();
482	qp = &slpque[LOOKUP(ident)];
483restart:
484	for (p = qp->tqh_first; p != NULL; p = p->p_procq.tqe_next) {
485#ifdef DIAGNOSTIC
486		if (p->p_stat != SSLEEP && p->p_stat != SSTOP)
487			panic("wakeup");
488#endif
489		if (p->p_wchan == ident) {
490			TAILQ_REMOVE(qp, p, p_procq);
491			p->p_wchan = 0;
492			if (p->p_stat == SSLEEP) {
493				/* OPTIMIZED EXPANSION OF setrunnable(p); */
494				if (p->p_slptime > 1)
495					updatepri(p);
496				p->p_slptime = 0;
497				p->p_stat = SRUN;
498				if (p->p_flag & P_INMEM) {
499					setrunqueue(p);
500					need_resched();
501				} else {
502					p->p_flag |= P_SWAPINREQ;
503					wakeup((caddr_t)&proc0);
504				}
505				/* END INLINE EXPANSION */
506				goto restart;
507			}
508		}
509	}
510	splx(s);
511}
512
513/*
514 * Make a process sleeping on the specified identifier runnable.
515 * May wake more than one process if a target prcoess is currently
516 * swapped out.
517 */
518void
519wakeup_one(ident)
520	register void *ident;
521{
522	register struct slpquehead *qp;
523	register struct proc *p;
524	int s;
525
526	s = splhigh();
527	qp = &slpque[LOOKUP(ident)];
528
529	for (p = qp->tqh_first; p != NULL; p = p->p_procq.tqe_next) {
530#ifdef DIAGNOSTIC
531		if (p->p_stat != SSLEEP && p->p_stat != SSTOP)
532			panic("wakeup_one");
533#endif
534		if (p->p_wchan == ident) {
535			TAILQ_REMOVE(qp, p, p_procq);
536			p->p_wchan = 0;
537			if (p->p_stat == SSLEEP) {
538				/* OPTIMIZED EXPANSION OF setrunnable(p); */
539				if (p->p_slptime > 1)
540					updatepri(p);
541				p->p_slptime = 0;
542				p->p_stat = SRUN;
543				if (p->p_flag & P_INMEM) {
544					setrunqueue(p);
545					need_resched();
546					break;
547				} else {
548					p->p_flag |= P_SWAPINREQ;
549					wakeup((caddr_t)&proc0);
550				}
551				/* END INLINE EXPANSION */
552			}
553		}
554	}
555	splx(s);
556}
557
558/*
559 * The machine independent parts of mi_switch().
560 * Must be called at splstatclock() or higher.
561 */
562void
563mi_switch()
564{
565	register struct proc *p = curproc;	/* XXX */
566	register struct rlimit *rlim;
567	register long s, u;
568	int x;
569	struct timeval tv;
570
571	/*
572	 * XXX this spl is almost unnecessary.  It is partly to allow for
573	 * sloppy callers that don't do it (issignal() via CURSIG() is the
574	 * main offender).  It is partly to work around a bug in the i386
575	 * cpu_switch() (the ipl is not preserved).  We ran for years
576	 * without it.  I think there was only a interrupt latency problem.
577	 * The main caller, tsleep(), does an splx() a couple of instructions
578	 * after calling here.  The buggy caller, issignal(), usually calls
579	 * here at spl0() and sometimes returns at splhigh().  The process
580	 * then runs for a little too long at splhigh().  The ipl gets fixed
581	 * when the process returns to user mode (or earlier).
582	 *
583	 * It would probably be better to always call here at spl0(). Callers
584	 * are prepared to give up control to another process, so they must
585	 * be prepared to be interrupted.  The clock stuff here may not
586	 * actually need splstatclock().
587	 */
588	x = splstatclock();
589
590#ifdef SIMPLELOCK_DEBUG
591	if (p->p_simple_locks)
592		printf("sleep: holding simple lock");
593#endif
594	/*
595	 * Compute the amount of time during which the current
596	 * process was running, and add that to its total so far.
597	 */
598	microtime(&tv);
599	u = p->p_rtime.tv_usec + (tv.tv_usec - runtime.tv_usec);
600	s = p->p_rtime.tv_sec + (tv.tv_sec - runtime.tv_sec);
601	if (u < 0) {
602		u += 1000000;
603		s--;
604	} else if (u >= 1000000) {
605		u -= 1000000;
606		s++;
607	}
608#ifdef SMP
609	if (s < 0)
610		s = u = 0;
611#endif
612	p->p_rtime.tv_usec = u;
613	p->p_rtime.tv_sec = s;
614
615	/*
616	 * Check if the process exceeds its cpu resource allocation.
617	 * If over max, kill it.
618	 */
619	if (p->p_stat != SZOMB) {
620		rlim = &p->p_rlimit[RLIMIT_CPU];
621		if (s >= rlim->rlim_cur) {
622			if (s >= rlim->rlim_max)
623				killproc(p, "exceeded maximum CPU limit");
624			else {
625				psignal(p, SIGXCPU);
626				if (rlim->rlim_cur < rlim->rlim_max)
627					rlim->rlim_cur += 5;
628			}
629		}
630	}
631
632	/*
633	 * Pick a new current process and record its start time.
634	 */
635	cnt.v_swtch++;
636	cpu_switch(p);
637	microtime(&runtime);
638	splx(x);
639}
640
641/*
642 * Initialize the (doubly-linked) run queues
643 * to be empty.
644 */
645/* ARGSUSED*/
646static void
647rqinit(dummy)
648	void *dummy;
649{
650	register int i;
651
652	for (i = 0; i < NQS; i++) {
653		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
654		rtqs[i].ph_link = rtqs[i].ph_rlink = (struct proc *)&rtqs[i];
655		idqs[i].ph_link = idqs[i].ph_rlink = (struct proc *)&idqs[i];
656	}
657}
658
659/*
660 * Change process state to be runnable,
661 * placing it on the run queue if it is in memory,
662 * and awakening the swapper if it isn't in memory.
663 */
664void
665setrunnable(p)
666	register struct proc *p;
667{
668	register int s;
669
670	s = splhigh();
671	switch (p->p_stat) {
672	case 0:
673	case SRUN:
674	case SZOMB:
675	default:
676		panic("setrunnable");
677	case SSTOP:
678	case SSLEEP:
679		unsleep(p);		/* e.g. when sending signals */
680		break;
681
682	case SIDL:
683		break;
684	}
685	p->p_stat = SRUN;
686	if (p->p_flag & P_INMEM)
687		setrunqueue(p);
688	splx(s);
689	if (p->p_slptime > 1)
690		updatepri(p);
691	p->p_slptime = 0;
692	if ((p->p_flag & P_INMEM) == 0) {
693		p->p_flag |= P_SWAPINREQ;
694		wakeup((caddr_t)&proc0);
695	}
696	else if (p->p_priority < curpriority)
697		need_resched();
698}
699
700/*
701 * Compute the priority of a process when running in user mode.
702 * Arrange to reschedule if the resulting priority is better
703 * than that of the current process.
704 */
705void
706resetpriority(p)
707	register struct proc *p;
708{
709	register unsigned int newpriority;
710
711	if (p->p_rtprio.type == RTP_PRIO_NORMAL) {
712		newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice;
713		newpriority = min(newpriority, MAXPRI);
714		p->p_usrpri = newpriority;
715		if (newpriority < curpriority)
716			need_resched();
717	} else {
718		need_resched();
719	}
720}
721