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