kern_synch.c revision 28176
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.33 1997/08/08 22:48:57 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	&&  (p->p_procq.tqe_next == p->p_procq.tqe_prev)
359	&&  (*p->p_procq.tqe_prev == p))
360		panic("sleeping process on run queue");
361#endif
362	p->p_wchan = ident;
363	p->p_wmesg = wmesg;
364	p->p_slptime = 0;
365	p->p_priority = priority & PRIMASK;
366	TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_procq);
367	if (timo)
368		timeout(endtsleep, (void *)p, timo);
369	/*
370	 * We put ourselves on the sleep queue and start our timeout
371	 * before calling CURSIG, as we could stop there, and a wakeup
372	 * or a SIGCONT (or both) could occur while we were stopped.
373	 * A SIGCONT would cause us to be marked as SSLEEP
374	 * without resuming us, thus we must be ready for sleep
375	 * when CURSIG is called.  If the wakeup happens while we're
376	 * stopped, p->p_wchan will be 0 upon return from CURSIG.
377	 */
378	if (catch) {
379		p->p_flag |= P_SINTR;
380		if ((sig = CURSIG(p))) {
381			if (p->p_wchan)
382				unsleep(p);
383			p->p_stat = SRUN;
384			goto resume;
385		}
386		if (p->p_wchan == 0) {
387			catch = 0;
388			goto resume;
389		}
390	} else
391		sig = 0;
392	p->p_stat = SSLEEP;
393	p->p_stats->p_ru.ru_nvcsw++;
394	mi_switch();
395resume:
396	curpriority = p->p_usrpri;
397	splx(s);
398	p->p_flag &= ~P_SINTR;
399	if (p->p_flag & P_TIMEOUT) {
400		p->p_flag &= ~P_TIMEOUT;
401		if (sig == 0) {
402#ifdef KTRACE
403			if (KTRPOINT(p, KTR_CSW))
404				ktrcsw(p->p_tracep, 0, 0);
405#endif
406			return (EWOULDBLOCK);
407		}
408	} else if (timo)
409		untimeout(endtsleep, (void *)p);
410	if (catch && (sig != 0 || (sig = CURSIG(p)))) {
411#ifdef KTRACE
412		if (KTRPOINT(p, KTR_CSW))
413			ktrcsw(p->p_tracep, 0, 0);
414#endif
415		if (p->p_sigacts->ps_sigintr & sigmask(sig))
416			return (EINTR);
417		return (ERESTART);
418	}
419#ifdef KTRACE
420	if (KTRPOINT(p, KTR_CSW))
421		ktrcsw(p->p_tracep, 0, 0);
422#endif
423	return (0);
424}
425
426/*
427 * Implement timeout for tsleep.
428 * If process hasn't been awakened (wchan non-zero),
429 * set timeout flag and undo the sleep.  If proc
430 * is stopped, just unsleep so it will remain stopped.
431 */
432void
433endtsleep(arg)
434	void *arg;
435{
436	register struct proc *p;
437	int s;
438
439	p = (struct proc *)arg;
440	s = splhigh();
441	if (p->p_wchan) {
442		if (p->p_stat == SSLEEP)
443			setrunnable(p);
444		else
445			unsleep(p);
446		p->p_flag |= P_TIMEOUT;
447	}
448	splx(s);
449}
450
451/*
452 * Remove a process from its wait queue
453 */
454void
455unsleep(p)
456	register struct proc *p;
457{
458	int s;
459
460	s = splhigh();
461	if (p->p_wchan) {
462		TAILQ_REMOVE(&slpque[LOOKUP(p->p_wchan)], p, p_procq);
463		p->p_wchan = 0;
464	}
465	splx(s);
466}
467
468/*
469 * Make all processes sleeping on the specified identifier runnable.
470 */
471void
472wakeup(ident)
473	register void *ident;
474{
475	register struct slpquehead *qp;
476	register struct proc *p;
477	int s;
478
479	s = splhigh();
480	qp = &slpque[LOOKUP(ident)];
481restart:
482	for (p = qp->tqh_first; p != NULL; p = p->p_procq.tqe_next) {
483#ifdef DIAGNOSTIC
484		if (p->p_stat != SSLEEP && p->p_stat != SSTOP)
485			panic("wakeup");
486#endif
487		if (p->p_wchan == ident) {
488			TAILQ_REMOVE(qp, p, p_procq);
489			p->p_wchan = 0;
490			if (p->p_stat == SSLEEP) {
491				/* OPTIMIZED EXPANSION OF setrunnable(p); */
492				if (p->p_slptime > 1)
493					updatepri(p);
494				p->p_slptime = 0;
495				p->p_stat = SRUN;
496				if (p->p_flag & P_INMEM) {
497					setrunqueue(p);
498					need_resched();
499				} else {
500					p->p_flag |= P_SWAPINREQ;
501					wakeup((caddr_t)&proc0);
502				}
503				/* END INLINE EXPANSION */
504				goto restart;
505			}
506		}
507	}
508	splx(s);
509}
510
511/*
512 * Make a process sleeping on the specified identifier runnable.
513 * May wake more than one process if a target prcoess is currently
514 * swapped out.
515 */
516void
517wakeup_one(ident)
518	register void *ident;
519{
520	register struct slpquehead *qp;
521	register struct proc *p;
522	int s;
523
524	s = splhigh();
525	qp = &slpque[LOOKUP(ident)];
526
527	for (p = qp->tqh_first; p != NULL; p = p->p_procq.tqe_next) {
528#ifdef DIAGNOSTIC
529		if (p->p_stat != SSLEEP && p->p_stat != SSTOP)
530			panic("wakeup_one");
531#endif
532		if (p->p_wchan == ident) {
533			TAILQ_REMOVE(qp, p, p_procq);
534			p->p_wchan = 0;
535			if (p->p_stat == SSLEEP) {
536				/* OPTIMIZED EXPANSION OF setrunnable(p); */
537				if (p->p_slptime > 1)
538					updatepri(p);
539				p->p_slptime = 0;
540				p->p_stat = SRUN;
541				if (p->p_flag & P_INMEM) {
542					setrunqueue(p);
543					need_resched();
544					break;
545				} else {
546					p->p_flag |= P_SWAPINREQ;
547					wakeup((caddr_t)&proc0);
548				}
549				/* END INLINE EXPANSION */
550			}
551		}
552	}
553	splx(s);
554}
555
556/*
557 * The machine independent parts of mi_switch().
558 * Must be called at splstatclock() or higher.
559 */
560void
561mi_switch()
562{
563	register struct proc *p = curproc;	/* XXX */
564	register struct rlimit *rlim;
565	register long s, u;
566	int x;
567	struct timeval tv;
568
569	/*
570	 * XXX this spl is almost unnecessary.  It is partly to allow for
571	 * sloppy callers that don't do it (issignal() via CURSIG() is the
572	 * main offender).  It is partly to work around a bug in the i386
573	 * cpu_switch() (the ipl is not preserved).  We ran for years
574	 * without it.  I think there was only a interrupt latency problem.
575	 * The main caller, tsleep(), does an splx() a couple of instructions
576	 * after calling here.  The buggy caller, issignal(), usually calls
577	 * here at spl0() and sometimes returns at splhigh().  The process
578	 * then runs for a little too long at splhigh().  The ipl gets fixed
579	 * when the process returns to user mode (or earlier).
580	 *
581	 * It would probably be better to always call here at spl0(). Callers
582	 * are prepared to give up control to another process, so they must
583	 * be prepared to be interrupted.  The clock stuff here may not
584	 * actually need splstatclock().
585	 */
586	x = splstatclock();
587
588#ifdef SIMPLELOCK_DEBUG
589	if (p->p_simple_locks)
590		printf("sleep: holding simple lock");
591#endif
592	/*
593	 * Compute the amount of time during which the current
594	 * process was running, and add that to its total so far.
595	 */
596	microtime(&tv);
597	u = p->p_rtime.tv_usec + (tv.tv_usec - runtime.tv_usec);
598	s = p->p_rtime.tv_sec + (tv.tv_sec - runtime.tv_sec);
599	if (u < 0) {
600		u += 1000000;
601		s--;
602	} else if (u >= 1000000) {
603		u -= 1000000;
604		s++;
605	}
606#ifdef SMP
607	if (s < 0)
608		s = u = 0;
609#endif
610	p->p_rtime.tv_usec = u;
611	p->p_rtime.tv_sec = s;
612
613	/*
614	 * Check if the process exceeds its cpu resource allocation.
615	 * If over max, kill it.
616	 */
617	if (p->p_stat != SZOMB) {
618		rlim = &p->p_rlimit[RLIMIT_CPU];
619		if (s >= rlim->rlim_cur) {
620			if (s >= rlim->rlim_max)
621				killproc(p, "exceeded maximum CPU limit");
622			else {
623				psignal(p, SIGXCPU);
624				if (rlim->rlim_cur < rlim->rlim_max)
625					rlim->rlim_cur += 5;
626			}
627		}
628	}
629
630	/*
631	 * Pick a new current process and record its start time.
632	 */
633	cnt.v_swtch++;
634	cpu_switch(p);
635	microtime(&runtime);
636	splx(x);
637}
638
639/*
640 * Initialize the (doubly-linked) run queues
641 * to be empty.
642 */
643/* ARGSUSED*/
644static void
645rqinit(dummy)
646	void *dummy;
647{
648	register int i;
649
650	for (i = 0; i < NQS; i++) {
651		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
652		rtqs[i].ph_link = rtqs[i].ph_rlink = (struct proc *)&rtqs[i];
653		idqs[i].ph_link = idqs[i].ph_rlink = (struct proc *)&idqs[i];
654	}
655}
656
657/*
658 * Change process state to be runnable,
659 * placing it on the run queue if it is in memory,
660 * and awakening the swapper if it isn't in memory.
661 */
662void
663setrunnable(p)
664	register struct proc *p;
665{
666	register int s;
667
668	s = splhigh();
669	switch (p->p_stat) {
670	case 0:
671	case SRUN:
672	case SZOMB:
673	default:
674		panic("setrunnable");
675	case SSTOP:
676	case SSLEEP:
677		unsleep(p);		/* e.g. when sending signals */
678		break;
679
680	case SIDL:
681		break;
682	}
683	p->p_stat = SRUN;
684	if (p->p_flag & P_INMEM)
685		setrunqueue(p);
686	splx(s);
687	if (p->p_slptime > 1)
688		updatepri(p);
689	p->p_slptime = 0;
690	if ((p->p_flag & P_INMEM) == 0) {
691		p->p_flag |= P_SWAPINREQ;
692		wakeup((caddr_t)&proc0);
693	}
694	else if (p->p_priority < curpriority)
695		need_resched();
696}
697
698/*
699 * Compute the priority of a process when running in user mode.
700 * Arrange to reschedule if the resulting priority is better
701 * than that of the current process.
702 */
703void
704resetpriority(p)
705	register struct proc *p;
706{
707	register unsigned int newpriority;
708
709	if (p->p_rtprio.type == RTP_PRIO_NORMAL) {
710		newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice;
711		newpriority = min(newpriority, MAXPRI);
712		p->p_usrpri = newpriority;
713		if (newpriority < curpriority)
714			need_resched();
715	} else {
716		need_resched();
717	}
718}
719