Home | History | Annotate | Line # | Download | only in kern
sched_4bsd.c revision 1.1.2.10
      1  1.1.2.10  rmind /*	$NetBSD: sched_4bsd.c,v 1.1.2.10 2007/03/10 13:40:49 rmind Exp $	*/
      2   1.1.2.1  rmind 
      3   1.1.2.1  rmind /*-
      4   1.1.2.1  rmind  * Copyright (c) 1999, 2000, 2004, 2006, 2007 The NetBSD Foundation, Inc.
      5   1.1.2.1  rmind  * All rights reserved.
      6   1.1.2.1  rmind  *
      7   1.1.2.1  rmind  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1.2.1  rmind  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1.2.1  rmind  * NASA Ames Research Center, by Charles M. Hannum, Andrew Doran, and
     10   1.1.2.1  rmind  * Daniel Sieger.
     11   1.1.2.1  rmind  *
     12   1.1.2.1  rmind  * Redistribution and use in source and binary forms, with or without
     13   1.1.2.1  rmind  * modification, are permitted provided that the following conditions
     14   1.1.2.1  rmind  * are met:
     15   1.1.2.1  rmind  * 1. Redistributions of source code must retain the above copyright
     16   1.1.2.1  rmind  *    notice, this list of conditions and the following disclaimer.
     17   1.1.2.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1.2.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     19   1.1.2.1  rmind  *    documentation and/or other materials provided with the distribution.
     20   1.1.2.1  rmind  * 3. All advertising materials mentioning features or use of this software
     21   1.1.2.1  rmind  *    must display the following acknowledgement:
     22   1.1.2.1  rmind  *	This product includes software developed by the NetBSD
     23   1.1.2.1  rmind  *	Foundation, Inc. and its contributors.
     24   1.1.2.1  rmind  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25   1.1.2.1  rmind  *    contributors may be used to endorse or promote products derived
     26   1.1.2.1  rmind  *    from this software without specific prior written permission.
     27   1.1.2.1  rmind  *
     28   1.1.2.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29   1.1.2.1  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30   1.1.2.1  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31   1.1.2.1  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32   1.1.2.1  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33   1.1.2.1  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34   1.1.2.1  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35   1.1.2.1  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36   1.1.2.1  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37   1.1.2.1  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38   1.1.2.1  rmind  * POSSIBILITY OF SUCH DAMAGE.
     39   1.1.2.1  rmind  */
     40   1.1.2.1  rmind 
     41   1.1.2.1  rmind /*-
     42   1.1.2.1  rmind  * Copyright (c) 1982, 1986, 1990, 1991, 1993
     43   1.1.2.1  rmind  *	The Regents of the University of California.  All rights reserved.
     44   1.1.2.1  rmind  * (c) UNIX System Laboratories, Inc.
     45   1.1.2.1  rmind  * All or some portions of this file are derived from material licensed
     46   1.1.2.1  rmind  * to the University of California by American Telephone and Telegraph
     47   1.1.2.1  rmind  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     48   1.1.2.1  rmind  * the permission of UNIX System Laboratories, Inc.
     49   1.1.2.1  rmind  *
     50   1.1.2.1  rmind  * Redistribution and use in source and binary forms, with or without
     51   1.1.2.1  rmind  * modification, are permitted provided that the following conditions
     52   1.1.2.1  rmind  * are met:
     53   1.1.2.1  rmind  * 1. Redistributions of source code must retain the above copyright
     54   1.1.2.1  rmind  *    notice, this list of conditions and the following disclaimer.
     55   1.1.2.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     56   1.1.2.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     57   1.1.2.1  rmind  *    documentation and/or other materials provided with the distribution.
     58   1.1.2.1  rmind  * 3. Neither the name of the University nor the names of its contributors
     59   1.1.2.1  rmind  *    may be used to endorse or promote products derived from this software
     60   1.1.2.1  rmind  *    without specific prior written permission.
     61   1.1.2.1  rmind  *
     62   1.1.2.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63   1.1.2.1  rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64   1.1.2.1  rmind  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65   1.1.2.1  rmind  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66   1.1.2.1  rmind  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67   1.1.2.1  rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68   1.1.2.1  rmind  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69   1.1.2.1  rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70   1.1.2.1  rmind  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71   1.1.2.1  rmind  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72   1.1.2.1  rmind  * SUCH DAMAGE.
     73   1.1.2.1  rmind  *
     74   1.1.2.1  rmind  *	@(#)kern_synch.c	8.9 (Berkeley) 5/19/95
     75   1.1.2.1  rmind  */
     76   1.1.2.1  rmind 
     77   1.1.2.1  rmind #include <sys/cdefs.h>
     78  1.1.2.10  rmind __KERNEL_RCSID(0, "$NetBSD: sched_4bsd.c,v 1.1.2.10 2007/03/10 13:40:49 rmind Exp $");
     79   1.1.2.1  rmind 
     80   1.1.2.1  rmind #include "opt_ddb.h"
     81   1.1.2.1  rmind #include "opt_lockdebug.h"
     82   1.1.2.1  rmind #include "opt_perfctrs.h"
     83   1.1.2.1  rmind 
     84   1.1.2.1  rmind #define	__MUTEX_PRIVATE
     85   1.1.2.1  rmind 
     86   1.1.2.1  rmind #include <sys/param.h>
     87   1.1.2.1  rmind #include <sys/systm.h>
     88   1.1.2.1  rmind #include <sys/callout.h>
     89   1.1.2.6   yamt #include <sys/cpu.h>
     90   1.1.2.1  rmind #include <sys/proc.h>
     91   1.1.2.1  rmind #include <sys/kernel.h>
     92   1.1.2.1  rmind #include <sys/signalvar.h>
     93   1.1.2.1  rmind #include <sys/resourcevar.h>
     94   1.1.2.1  rmind #include <sys/sched.h>
     95   1.1.2.9  rmind #include <sys/sysctl.h>
     96   1.1.2.1  rmind #include <sys/kauth.h>
     97   1.1.2.1  rmind #include <sys/lockdebug.h>
     98   1.1.2.1  rmind 
     99   1.1.2.1  rmind #include <uvm/uvm_extern.h>
    100   1.1.2.1  rmind 
    101   1.1.2.1  rmind /*
    102   1.1.2.1  rmind  * Run queues.
    103   1.1.2.1  rmind  *
    104   1.1.2.1  rmind  * We have 32 run queues in descending priority of 0..31.  We maintain
    105   1.1.2.1  rmind  * a bitmask of non-empty queues in order speed up finding the first
    106   1.1.2.1  rmind  * runnable process.  The bitmask is maintained only by machine-dependent
    107   1.1.2.1  rmind  * code, allowing the most efficient instructions to be used to find the
    108   1.1.2.1  rmind  * first non-empty queue.
    109   1.1.2.1  rmind  */
    110   1.1.2.1  rmind 
    111   1.1.2.1  rmind 
    112   1.1.2.1  rmind #define	RUNQUE_NQS		32      /* number of runqueues */
    113   1.1.2.1  rmind #define	PPQ	(128 / RUNQUE_NQS)	/* priorities per queue */
    114   1.1.2.1  rmind 
    115   1.1.2.1  rmind struct prochd {
    116   1.1.2.1  rmind 	struct lwp *ph_link;
    117   1.1.2.1  rmind 	struct lwp *ph_rlink;
    118   1.1.2.1  rmind };
    119   1.1.2.1  rmind 
    120   1.1.2.1  rmind struct prochd sched_qs[RUNQUE_NQS];	/* run queues */
    121   1.1.2.1  rmind volatile uint32_t sched_whichqs;	/* bitmap of non-empty queues */
    122   1.1.2.1  rmind 
    123   1.1.2.1  rmind void schedcpu(void *);
    124   1.1.2.1  rmind void updatepri(struct lwp *);
    125   1.1.2.3   yamt void resetpriority(struct lwp *);
    126   1.1.2.1  rmind void resetprocpriority(struct proc *);
    127   1.1.2.1  rmind 
    128   1.1.2.1  rmind struct callout schedcpu_ch = CALLOUT_INITIALIZER_SETFUNC(schedcpu, NULL);
    129   1.1.2.1  rmind static unsigned int schedcpu_ticks;
    130   1.1.2.1  rmind 
    131   1.1.2.1  rmind int rrticks; /* number of hardclock ticks per sched_tick() */
    132   1.1.2.1  rmind 
    133   1.1.2.1  rmind /*
    134   1.1.2.1  rmind  * Force switch among equal priority processes every 100ms.
    135   1.1.2.1  rmind  * Called from hardclock every hz/10 == rrticks hardclock ticks.
    136   1.1.2.1  rmind  */
    137   1.1.2.1  rmind /* ARGSUSED */
    138   1.1.2.1  rmind void
    139   1.1.2.1  rmind sched_tick(struct cpu_info *ci)
    140   1.1.2.1  rmind {
    141   1.1.2.1  rmind 	struct schedstate_percpu *spc = &ci->ci_schedstate;
    142   1.1.2.1  rmind 
    143   1.1.2.1  rmind 	spc->spc_ticks = rrticks;
    144   1.1.2.1  rmind 
    145   1.1.2.1  rmind 	if (!CURCPU_IDLE_P()) {
    146   1.1.2.1  rmind 		if (spc->spc_flags & SPCF_SEENRR) {
    147   1.1.2.1  rmind 			/*
    148   1.1.2.1  rmind 			 * The process has already been through a roundrobin
    149   1.1.2.1  rmind 			 * without switching and may be hogging the CPU.
    150   1.1.2.1  rmind 			 * Indicate that the process should yield.
    151   1.1.2.1  rmind 			 */
    152   1.1.2.1  rmind 			spc->spc_flags |= SPCF_SHOULDYIELD;
    153   1.1.2.1  rmind 		} else
    154   1.1.2.1  rmind 			spc->spc_flags |= SPCF_SEENRR;
    155   1.1.2.1  rmind 	}
    156   1.1.2.7   yamt 	cpu_need_resched(curcpu(), 0);
    157   1.1.2.1  rmind }
    158   1.1.2.1  rmind 
    159   1.1.2.1  rmind #define	NICE_WEIGHT 2			/* priorities per nice level */
    160   1.1.2.1  rmind 
    161   1.1.2.1  rmind #define	ESTCPU_SHIFT	11
    162   1.1.2.1  rmind #define	ESTCPU_MAX	((NICE_WEIGHT * PRIO_MAX - PPQ) << ESTCPU_SHIFT)
    163   1.1.2.1  rmind #define	ESTCPULIM(e)	min((e), ESTCPU_MAX)
    164   1.1.2.1  rmind 
    165   1.1.2.1  rmind /*
    166   1.1.2.1  rmind  * Constants for digital decay and forget:
    167   1.1.2.1  rmind  *	90% of (p_estcpu) usage in 5 * loadav time
    168   1.1.2.1  rmind  *	95% of (p_pctcpu) usage in 60 seconds (load insensitive)
    169   1.1.2.1  rmind  *          Note that, as ps(1) mentions, this can let percentages
    170   1.1.2.1  rmind  *          total over 100% (I've seen 137.9% for 3 processes).
    171   1.1.2.1  rmind  *
    172   1.1.2.1  rmind  * Note that hardclock updates p_estcpu and p_cpticks independently.
    173   1.1.2.1  rmind  *
    174   1.1.2.1  rmind  * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
    175   1.1.2.1  rmind  * That is, the system wants to compute a value of decay such
    176   1.1.2.1  rmind  * that the following for loop:
    177   1.1.2.1  rmind  * 	for (i = 0; i < (5 * loadavg); i++)
    178   1.1.2.1  rmind  * 		p_estcpu *= decay;
    179   1.1.2.1  rmind  * will compute
    180   1.1.2.1  rmind  * 	p_estcpu *= 0.1;
    181   1.1.2.1  rmind  * for all values of loadavg:
    182   1.1.2.1  rmind  *
    183   1.1.2.1  rmind  * Mathematically this loop can be expressed by saying:
    184   1.1.2.1  rmind  * 	decay ** (5 * loadavg) ~= .1
    185   1.1.2.1  rmind  *
    186   1.1.2.1  rmind  * The system computes decay as:
    187   1.1.2.1  rmind  * 	decay = (2 * loadavg) / (2 * loadavg + 1)
    188   1.1.2.1  rmind  *
    189   1.1.2.1  rmind  * We wish to prove that the system's computation of decay
    190   1.1.2.1  rmind  * will always fulfill the equation:
    191   1.1.2.1  rmind  * 	decay ** (5 * loadavg) ~= .1
    192   1.1.2.1  rmind  *
    193   1.1.2.1  rmind  * If we compute b as:
    194   1.1.2.1  rmind  * 	b = 2 * loadavg
    195   1.1.2.1  rmind  * then
    196   1.1.2.1  rmind  * 	decay = b / (b + 1)
    197   1.1.2.1  rmind  *
    198   1.1.2.1  rmind  * We now need to prove two things:
    199   1.1.2.1  rmind  *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
    200   1.1.2.1  rmind  *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
    201   1.1.2.1  rmind  *
    202   1.1.2.1  rmind  * Facts:
    203   1.1.2.1  rmind  *         For x close to zero, exp(x) =~ 1 + x, since
    204   1.1.2.1  rmind  *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
    205   1.1.2.1  rmind  *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
    206   1.1.2.1  rmind  *         For x close to zero, ln(1+x) =~ x, since
    207   1.1.2.1  rmind  *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
    208   1.1.2.1  rmind  *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
    209   1.1.2.1  rmind  *         ln(.1) =~ -2.30
    210   1.1.2.1  rmind  *
    211   1.1.2.1  rmind  * Proof of (1):
    212   1.1.2.1  rmind  *    Solve (factor)**(power) =~ .1 given power (5*loadav):
    213   1.1.2.1  rmind  *	solving for factor,
    214   1.1.2.1  rmind  *      ln(factor) =~ (-2.30/5*loadav), or
    215   1.1.2.1  rmind  *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
    216   1.1.2.1  rmind  *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
    217   1.1.2.1  rmind  *
    218   1.1.2.1  rmind  * Proof of (2):
    219   1.1.2.1  rmind  *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
    220   1.1.2.1  rmind  *	solving for power,
    221   1.1.2.1  rmind  *      power*ln(b/(b+1)) =~ -2.30, or
    222   1.1.2.1  rmind  *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
    223   1.1.2.1  rmind  *
    224   1.1.2.1  rmind  * Actual power values for the implemented algorithm are as follows:
    225   1.1.2.1  rmind  *      loadav: 1       2       3       4
    226   1.1.2.1  rmind  *      power:  5.68    10.32   14.94   19.55
    227   1.1.2.1  rmind  */
    228   1.1.2.1  rmind 
    229   1.1.2.1  rmind /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
    230   1.1.2.1  rmind #define	loadfactor(loadav)	(2 * (loadav))
    231   1.1.2.1  rmind 
    232   1.1.2.1  rmind static fixpt_t
    233   1.1.2.1  rmind decay_cpu(fixpt_t loadfac, fixpt_t estcpu)
    234   1.1.2.1  rmind {
    235   1.1.2.1  rmind 
    236   1.1.2.1  rmind 	if (estcpu == 0) {
    237   1.1.2.1  rmind 		return 0;
    238   1.1.2.1  rmind 	}
    239   1.1.2.1  rmind 
    240   1.1.2.1  rmind #if !defined(_LP64)
    241   1.1.2.1  rmind 	/* avoid 64bit arithmetics. */
    242   1.1.2.1  rmind #define	FIXPT_MAX ((fixpt_t)((UINTMAX_C(1) << sizeof(fixpt_t) * CHAR_BIT) - 1))
    243   1.1.2.1  rmind 	if (__predict_true(loadfac <= FIXPT_MAX / ESTCPU_MAX)) {
    244   1.1.2.1  rmind 		return estcpu * loadfac / (loadfac + FSCALE);
    245   1.1.2.1  rmind 	}
    246   1.1.2.1  rmind #endif /* !defined(_LP64) */
    247   1.1.2.1  rmind 
    248   1.1.2.1  rmind 	return (uint64_t)estcpu * loadfac / (loadfac + FSCALE);
    249   1.1.2.1  rmind }
    250   1.1.2.1  rmind 
    251   1.1.2.1  rmind /*
    252   1.1.2.1  rmind  * For all load averages >= 1 and max p_estcpu of (255 << ESTCPU_SHIFT),
    253   1.1.2.1  rmind  * sleeping for at least seven times the loadfactor will decay p_estcpu to
    254   1.1.2.1  rmind  * less than (1 << ESTCPU_SHIFT).
    255   1.1.2.1  rmind  *
    256   1.1.2.1  rmind  * note that our ESTCPU_MAX is actually much smaller than (255 << ESTCPU_SHIFT).
    257   1.1.2.1  rmind  */
    258   1.1.2.1  rmind static fixpt_t
    259   1.1.2.1  rmind decay_cpu_batch(fixpt_t loadfac, fixpt_t estcpu, unsigned int n)
    260   1.1.2.1  rmind {
    261   1.1.2.1  rmind 
    262   1.1.2.1  rmind 	if ((n << FSHIFT) >= 7 * loadfac) {
    263   1.1.2.1  rmind 		return 0;
    264   1.1.2.1  rmind 	}
    265   1.1.2.1  rmind 
    266   1.1.2.1  rmind 	while (estcpu != 0 && n > 1) {
    267   1.1.2.1  rmind 		estcpu = decay_cpu(loadfac, estcpu);
    268   1.1.2.1  rmind 		n--;
    269   1.1.2.1  rmind 	}
    270   1.1.2.1  rmind 
    271   1.1.2.1  rmind 	return estcpu;
    272   1.1.2.1  rmind }
    273   1.1.2.1  rmind 
    274   1.1.2.1  rmind /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
    275   1.1.2.1  rmind fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;		/* exp(-1/20) */
    276   1.1.2.1  rmind 
    277   1.1.2.1  rmind /*
    278   1.1.2.1  rmind  * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
    279   1.1.2.1  rmind  * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
    280   1.1.2.1  rmind  * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
    281   1.1.2.1  rmind  *
    282   1.1.2.1  rmind  * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
    283   1.1.2.1  rmind  *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
    284   1.1.2.1  rmind  *
    285   1.1.2.1  rmind  * If you dont want to bother with the faster/more-accurate formula, you
    286   1.1.2.1  rmind  * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
    287   1.1.2.1  rmind  * (more general) method of calculating the %age of CPU used by a process.
    288   1.1.2.1  rmind  */
    289   1.1.2.1  rmind #define	CCPU_SHIFT	11
    290   1.1.2.1  rmind 
    291   1.1.2.1  rmind /*
    292   1.1.2.1  rmind  * schedcpu:
    293   1.1.2.1  rmind  *
    294   1.1.2.1  rmind  *	Recompute process priorities, every hz ticks.
    295   1.1.2.1  rmind  *
    296   1.1.2.1  rmind  *	XXXSMP This needs to be reorganised in order to reduce the locking
    297   1.1.2.1  rmind  *	burden.
    298   1.1.2.1  rmind  */
    299   1.1.2.1  rmind /* ARGSUSED */
    300   1.1.2.1  rmind void
    301   1.1.2.1  rmind schedcpu(void *arg)
    302   1.1.2.1  rmind {
    303   1.1.2.1  rmind 	fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
    304   1.1.2.1  rmind 	struct rlimit *rlim;
    305   1.1.2.1  rmind 	struct lwp *l;
    306   1.1.2.1  rmind 	struct proc *p;
    307   1.1.2.1  rmind 	int minslp, clkhz, sig;
    308   1.1.2.1  rmind 	long runtm;
    309   1.1.2.1  rmind 
    310   1.1.2.1  rmind 	schedcpu_ticks++;
    311   1.1.2.1  rmind 
    312   1.1.2.1  rmind 	mutex_enter(&proclist_mutex);
    313   1.1.2.1  rmind 	PROCLIST_FOREACH(p, &allproc) {
    314   1.1.2.1  rmind 		/*
    315   1.1.2.1  rmind 		 * Increment time in/out of memory and sleep time (if
    316   1.1.2.1  rmind 		 * sleeping).  We ignore overflow; with 16-bit int's
    317   1.1.2.1  rmind 		 * (remember them?) overflow takes 45 days.
    318   1.1.2.1  rmind 		 */
    319   1.1.2.1  rmind 		minslp = 2;
    320   1.1.2.1  rmind 		mutex_enter(&p->p_smutex);
    321   1.1.2.1  rmind 		runtm = p->p_rtime.tv_sec;
    322   1.1.2.1  rmind 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    323   1.1.2.8   yamt 			if ((l->l_flag & LW_IDLE) != 0)
    324   1.1.2.1  rmind 				continue;
    325   1.1.2.1  rmind 			lwp_lock(l);
    326   1.1.2.1  rmind 			runtm += l->l_rtime.tv_sec;
    327   1.1.2.1  rmind 			l->l_swtime++;
    328   1.1.2.1  rmind 			if (l->l_stat == LSSLEEP || l->l_stat == LSSTOP ||
    329   1.1.2.1  rmind 			    l->l_stat == LSSUSPENDED) {
    330   1.1.2.1  rmind 				l->l_slptime++;
    331   1.1.2.1  rmind 				minslp = min(minslp, l->l_slptime);
    332   1.1.2.1  rmind 			} else
    333   1.1.2.1  rmind 				minslp = 0;
    334   1.1.2.1  rmind 			lwp_unlock(l);
    335   1.1.2.1  rmind 		}
    336   1.1.2.1  rmind 		p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
    337   1.1.2.1  rmind 
    338   1.1.2.1  rmind 		/*
    339   1.1.2.1  rmind 		 * Check if the process exceeds its CPU resource allocation.
    340   1.1.2.1  rmind 		 * If over max, kill it.
    341   1.1.2.1  rmind 		 */
    342   1.1.2.1  rmind 		rlim = &p->p_rlimit[RLIMIT_CPU];
    343   1.1.2.1  rmind 		sig = 0;
    344   1.1.2.1  rmind 		if (runtm >= rlim->rlim_cur) {
    345   1.1.2.1  rmind 			if (runtm >= rlim->rlim_max)
    346   1.1.2.1  rmind 				sig = SIGKILL;
    347   1.1.2.1  rmind 			else {
    348   1.1.2.1  rmind 				sig = SIGXCPU;
    349   1.1.2.1  rmind 				if (rlim->rlim_cur < rlim->rlim_max)
    350   1.1.2.1  rmind 					rlim->rlim_cur += 5;
    351   1.1.2.1  rmind 			}
    352   1.1.2.1  rmind 		}
    353   1.1.2.1  rmind 
    354   1.1.2.1  rmind 		/*
    355   1.1.2.1  rmind 		 * If the process has run for more than autonicetime, reduce
    356   1.1.2.1  rmind 		 * priority to give others a chance.
    357   1.1.2.1  rmind 		 */
    358   1.1.2.1  rmind 		if (autonicetime && runtm > autonicetime && p->p_nice == NZERO
    359   1.1.2.1  rmind 		    && kauth_cred_geteuid(p->p_cred)) {
    360   1.1.2.1  rmind 			mutex_spin_enter(&p->p_stmutex);
    361   1.1.2.1  rmind 			p->p_nice = autoniceval + NZERO;
    362   1.1.2.1  rmind 			resetprocpriority(p);
    363   1.1.2.1  rmind 			mutex_spin_exit(&p->p_stmutex);
    364   1.1.2.1  rmind 		}
    365   1.1.2.1  rmind 
    366   1.1.2.1  rmind 		/*
    367   1.1.2.1  rmind 		 * If the process has slept the entire second,
    368   1.1.2.1  rmind 		 * stop recalculating its priority until it wakes up.
    369   1.1.2.1  rmind 		 */
    370   1.1.2.1  rmind 		if (minslp <= 1) {
    371   1.1.2.1  rmind 			/*
    372   1.1.2.1  rmind 			 * p_pctcpu is only for ps.
    373   1.1.2.1  rmind 			 */
    374   1.1.2.1  rmind 			mutex_spin_enter(&p->p_stmutex);
    375   1.1.2.1  rmind 			clkhz = stathz != 0 ? stathz : hz;
    376   1.1.2.1  rmind #if	(FSHIFT >= CCPU_SHIFT)
    377   1.1.2.1  rmind 			p->p_pctcpu += (clkhz == 100)?
    378   1.1.2.1  rmind 			    ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
    379   1.1.2.1  rmind 			    100 * (((fixpt_t) p->p_cpticks)
    380   1.1.2.1  rmind 			    << (FSHIFT - CCPU_SHIFT)) / clkhz;
    381   1.1.2.1  rmind #else
    382   1.1.2.1  rmind 			p->p_pctcpu += ((FSCALE - ccpu) *
    383   1.1.2.1  rmind 			    (p->p_cpticks * FSCALE / clkhz)) >> FSHIFT;
    384   1.1.2.1  rmind #endif
    385   1.1.2.1  rmind 			p->p_cpticks = 0;
    386   1.1.2.1  rmind 			p->p_estcpu = decay_cpu(loadfac, p->p_estcpu);
    387   1.1.2.1  rmind 
    388   1.1.2.1  rmind 			LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    389   1.1.2.8   yamt 				if ((l->l_flag & LW_IDLE) != 0)
    390   1.1.2.1  rmind 					continue;
    391   1.1.2.1  rmind 				lwp_lock(l);
    392   1.1.2.1  rmind 				if (l->l_slptime <= 1 &&
    393   1.1.2.1  rmind 				    l->l_priority >= PUSER)
    394   1.1.2.1  rmind 					resetpriority(l);
    395   1.1.2.1  rmind 				lwp_unlock(l);
    396   1.1.2.1  rmind 			}
    397   1.1.2.1  rmind 			mutex_spin_exit(&p->p_stmutex);
    398   1.1.2.1  rmind 		}
    399   1.1.2.1  rmind 
    400   1.1.2.1  rmind 		mutex_exit(&p->p_smutex);
    401   1.1.2.1  rmind 		if (sig) {
    402   1.1.2.1  rmind 			psignal(p, sig);
    403   1.1.2.1  rmind 		}
    404   1.1.2.1  rmind 	}
    405   1.1.2.1  rmind 	mutex_exit(&proclist_mutex);
    406   1.1.2.1  rmind 	uvm_meter();
    407   1.1.2.1  rmind 	wakeup((caddr_t)&lbolt);
    408   1.1.2.1  rmind 	callout_schedule(&schedcpu_ch, hz);
    409   1.1.2.1  rmind }
    410   1.1.2.1  rmind 
    411   1.1.2.1  rmind /*
    412   1.1.2.1  rmind  * Recalculate the priority of a process after it has slept for a while.
    413   1.1.2.1  rmind  */
    414   1.1.2.1  rmind void
    415   1.1.2.1  rmind updatepri(struct lwp *l)
    416   1.1.2.1  rmind {
    417   1.1.2.1  rmind 	struct proc *p = l->l_proc;
    418   1.1.2.1  rmind 	fixpt_t loadfac;
    419   1.1.2.1  rmind 
    420   1.1.2.1  rmind 	LOCK_ASSERT(lwp_locked(l, NULL));
    421   1.1.2.1  rmind 	KASSERT(l->l_slptime > 1);
    422   1.1.2.1  rmind 
    423   1.1.2.1  rmind 	loadfac = loadfactor(averunnable.ldavg[0]);
    424   1.1.2.1  rmind 
    425   1.1.2.1  rmind 	l->l_slptime--; /* the first time was done in schedcpu */
    426   1.1.2.1  rmind 	/* XXX NJWLWP */
    427   1.1.2.1  rmind 	/* XXXSMP occasionally unlocked, should be per-LWP */
    428   1.1.2.1  rmind 	p->p_estcpu = decay_cpu_batch(loadfac, p->p_estcpu, l->l_slptime);
    429   1.1.2.1  rmind 	resetpriority(l);
    430   1.1.2.1  rmind }
    431   1.1.2.1  rmind 
    432   1.1.2.1  rmind /*
    433   1.1.2.1  rmind  * Initialize the (doubly-linked) run queues
    434   1.1.2.1  rmind  * to be empty.
    435   1.1.2.1  rmind  */
    436   1.1.2.1  rmind void
    437   1.1.2.1  rmind sched_rqinit()
    438   1.1.2.1  rmind {
    439   1.1.2.1  rmind 	int i;
    440   1.1.2.1  rmind 
    441   1.1.2.1  rmind 	for (i = 0; i < RUNQUE_NQS; i++)
    442   1.1.2.1  rmind 		sched_qs[i].ph_link = sched_qs[i].ph_rlink =
    443   1.1.2.1  rmind 		    (struct lwp *)&sched_qs[i];
    444   1.1.2.1  rmind 
    445   1.1.2.1  rmind 	mutex_init(&sched_mutex, MUTEX_SPIN, IPL_SCHED);
    446   1.1.2.1  rmind }
    447   1.1.2.1  rmind 
    448   1.1.2.1  rmind void
    449   1.1.2.1  rmind sched_setup()
    450   1.1.2.1  rmind {
    451   1.1.2.1  rmind 	rrticks = hz / 10;
    452   1.1.2.1  rmind 
    453   1.1.2.1  rmind 	schedcpu(NULL);
    454   1.1.2.1  rmind }
    455   1.1.2.1  rmind 
    456   1.1.2.1  rmind void
    457   1.1.2.1  rmind sched_setrunnable(struct lwp *l)
    458   1.1.2.1  rmind {
    459   1.1.2.1  rmind  	if (l->l_slptime > 1)
    460   1.1.2.1  rmind  		updatepri(l);
    461   1.1.2.1  rmind }
    462   1.1.2.1  rmind 
    463   1.1.2.9  rmind bool
    464   1.1.2.1  rmind sched_curcpu_runnable_p(void)
    465   1.1.2.1  rmind {
    466   1.1.2.1  rmind 
    467   1.1.2.1  rmind 	return sched_whichqs != 0;
    468   1.1.2.1  rmind }
    469   1.1.2.1  rmind 
    470   1.1.2.1  rmind void
    471   1.1.2.1  rmind sched_nice(struct proc *chgp, int n)
    472   1.1.2.1  rmind {
    473   1.1.2.1  rmind 	chgp->p_nice = n;
    474   1.1.2.1  rmind 	(void)resetprocpriority(chgp);
    475   1.1.2.1  rmind }
    476   1.1.2.1  rmind 
    477   1.1.2.1  rmind /*
    478   1.1.2.1  rmind  * Compute the priority of a process when running in user mode.
    479   1.1.2.1  rmind  * Arrange to reschedule if the resulting priority is better
    480   1.1.2.1  rmind  * than that of the current process.
    481   1.1.2.1  rmind  */
    482   1.1.2.1  rmind void
    483   1.1.2.1  rmind resetpriority(struct lwp *l)
    484   1.1.2.1  rmind {
    485   1.1.2.1  rmind 	unsigned int newpriority;
    486   1.1.2.1  rmind 	struct proc *p = l->l_proc;
    487   1.1.2.1  rmind 
    488   1.1.2.1  rmind 	/* XXXSMP LOCK_ASSERT(mutex_owned(&p->p_stmutex)); */
    489   1.1.2.1  rmind 	LOCK_ASSERT(lwp_locked(l, NULL));
    490   1.1.2.1  rmind 
    491   1.1.2.8   yamt 	if ((l->l_flag & LW_SYSTEM) != 0)
    492   1.1.2.1  rmind 		return;
    493   1.1.2.1  rmind 
    494   1.1.2.1  rmind 	newpriority = PUSER + (p->p_estcpu >> ESTCPU_SHIFT) +
    495   1.1.2.1  rmind 	    NICE_WEIGHT * (p->p_nice - NZERO);
    496   1.1.2.1  rmind 	newpriority = min(newpriority, MAXPRI);
    497   1.1.2.1  rmind 	lwp_changepri(l, newpriority);
    498   1.1.2.1  rmind }
    499   1.1.2.1  rmind 
    500   1.1.2.1  rmind /*
    501   1.1.2.1  rmind  * Recompute priority for all LWPs in a process.
    502   1.1.2.1  rmind  */
    503   1.1.2.1  rmind void
    504   1.1.2.1  rmind resetprocpriority(struct proc *p)
    505   1.1.2.1  rmind {
    506   1.1.2.1  rmind 	struct lwp *l;
    507   1.1.2.1  rmind 
    508   1.1.2.1  rmind 	LOCK_ASSERT(mutex_owned(&p->p_stmutex));
    509   1.1.2.1  rmind 
    510   1.1.2.1  rmind 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    511   1.1.2.1  rmind 		lwp_lock(l);
    512   1.1.2.1  rmind 		resetpriority(l);
    513   1.1.2.1  rmind 		lwp_unlock(l);
    514   1.1.2.1  rmind 	}
    515   1.1.2.1  rmind }
    516   1.1.2.1  rmind 
    517   1.1.2.1  rmind /*
    518   1.1.2.1  rmind  * We adjust the priority of the current process.  The priority of a process
    519   1.1.2.1  rmind  * gets worse as it accumulates CPU time.  The CPU usage estimator (p_estcpu)
    520   1.1.2.1  rmind  * is increased here.  The formula for computing priorities (in kern_synch.c)
    521   1.1.2.1  rmind  * will compute a different value each time p_estcpu increases. This can
    522   1.1.2.1  rmind  * cause a switch, but unless the priority crosses a PPQ boundary the actual
    523   1.1.2.1  rmind  * queue will not change.  The CPU usage estimator ramps up quite quickly
    524   1.1.2.1  rmind  * when the process is running (linearly), and decays away exponentially, at
    525   1.1.2.1  rmind  * a rate which is proportionally slower when the system is busy.  The basic
    526   1.1.2.1  rmind  * principle is that the system will 90% forget that the process used a lot
    527   1.1.2.1  rmind  * of CPU time in 5 * loadav seconds.  This causes the system to favor
    528   1.1.2.1  rmind  * processes which haven't run much recently, and to round-robin among other
    529   1.1.2.1  rmind  * processes.
    530   1.1.2.1  rmind  */
    531   1.1.2.1  rmind 
    532   1.1.2.1  rmind void
    533   1.1.2.5   yamt sched_clock(struct lwp *l)
    534   1.1.2.1  rmind {
    535   1.1.2.1  rmind 	struct proc *p = l->l_proc;
    536   1.1.2.1  rmind 
    537   1.1.2.1  rmind 	KASSERT(!CURCPU_IDLE_P());
    538   1.1.2.1  rmind 	mutex_spin_enter(&p->p_stmutex);
    539   1.1.2.1  rmind 	p->p_estcpu = ESTCPULIM(p->p_estcpu + (1 << ESTCPU_SHIFT));
    540   1.1.2.1  rmind 	lwp_lock(l);
    541   1.1.2.1  rmind 	resetpriority(l);
    542   1.1.2.1  rmind 	mutex_spin_exit(&p->p_stmutex);
    543   1.1.2.8   yamt 	if ((l->l_flag & LW_SYSTEM) == 0 && l->l_priority >= PUSER)
    544   1.1.2.1  rmind 		l->l_priority = l->l_usrpri;
    545   1.1.2.1  rmind 	lwp_unlock(l);
    546   1.1.2.1  rmind }
    547   1.1.2.1  rmind 
    548   1.1.2.1  rmind /*
    549   1.1.2.1  rmind  * scheduler_fork_hook:
    550   1.1.2.1  rmind  *
    551   1.1.2.1  rmind  *	Inherit the parent's scheduler history.
    552   1.1.2.1  rmind  */
    553   1.1.2.1  rmind void
    554   1.1.2.1  rmind sched_proc_fork(struct proc *parent, struct proc *child)
    555   1.1.2.1  rmind {
    556   1.1.2.1  rmind 
    557   1.1.2.1  rmind 	LOCK_ASSERT(mutex_owned(&parent->p_smutex));
    558   1.1.2.1  rmind 
    559   1.1.2.1  rmind 	child->p_estcpu = child->p_estcpu_inherited = parent->p_estcpu;
    560   1.1.2.1  rmind 	child->p_forktime = schedcpu_ticks;
    561   1.1.2.1  rmind }
    562   1.1.2.1  rmind 
    563   1.1.2.1  rmind /*
    564   1.1.2.1  rmind  * scheduler_wait_hook:
    565   1.1.2.1  rmind  *
    566   1.1.2.1  rmind  *	Chargeback parents for the sins of their children.
    567   1.1.2.1  rmind  */
    568   1.1.2.1  rmind void
    569   1.1.2.1  rmind sched_proc_exit(struct proc *parent, struct proc *child)
    570   1.1.2.1  rmind {
    571   1.1.2.1  rmind 	fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
    572   1.1.2.1  rmind 	fixpt_t estcpu;
    573   1.1.2.1  rmind 
    574   1.1.2.1  rmind 	/* XXX Only if parent != init?? */
    575   1.1.2.1  rmind 
    576   1.1.2.1  rmind 	mutex_spin_enter(&parent->p_stmutex);
    577   1.1.2.1  rmind 	estcpu = decay_cpu_batch(loadfac, child->p_estcpu_inherited,
    578   1.1.2.1  rmind 	    schedcpu_ticks - child->p_forktime);
    579   1.1.2.1  rmind 	if (child->p_estcpu > estcpu)
    580   1.1.2.1  rmind 		parent->p_estcpu =
    581   1.1.2.1  rmind 		    ESTCPULIM(parent->p_estcpu + child->p_estcpu - estcpu);
    582   1.1.2.1  rmind 	mutex_spin_exit(&parent->p_stmutex);
    583   1.1.2.1  rmind }
    584   1.1.2.1  rmind 
    585   1.1.2.1  rmind /*
    586   1.1.2.1  rmind  * On some architectures, it's faster to use a MSB ordering for the priorites
    587   1.1.2.1  rmind  * than the traditional LSB ordering.
    588   1.1.2.1  rmind  */
    589   1.1.2.1  rmind #ifdef __HAVE_BIGENDIAN_BITOPS
    590   1.1.2.1  rmind #define	RQMASK(n) (0x80000000 >> (n))
    591   1.1.2.1  rmind #else
    592   1.1.2.1  rmind #define	RQMASK(n) (0x00000001 << (n))
    593   1.1.2.1  rmind #endif
    594   1.1.2.1  rmind 
    595   1.1.2.1  rmind /*
    596   1.1.2.1  rmind  * Low-level routines to access the run queue.  Optimised assembler
    597   1.1.2.1  rmind  * routines can override these.
    598   1.1.2.1  rmind  */
    599   1.1.2.1  rmind 
    600   1.1.2.1  rmind #ifndef __HAVE_MD_RUNQUEUE
    601   1.1.2.1  rmind 
    602   1.1.2.1  rmind /*
    603   1.1.2.1  rmind  * The primitives that manipulate the run queues.  whichqs tells which
    604   1.1.2.4   yamt  * of the 32 queues qs have processes in them.  sched_enqueue puts processes
    605   1.1.2.4   yamt  * into queues, sched_dequeue removes them from queues.  The running process is
    606   1.1.2.1  rmind  * on no queue, other processes are on a queue related to p->p_priority,
    607   1.1.2.1  rmind  * divided by 4 actually to shrink the 0-127 range of priorities into the 32
    608   1.1.2.1  rmind  * available queues.
    609   1.1.2.1  rmind  */
    610   1.1.2.1  rmind #ifdef RQDEBUG
    611   1.1.2.1  rmind static void
    612   1.1.2.1  rmind checkrunqueue(int whichq, struct lwp *l)
    613   1.1.2.1  rmind {
    614   1.1.2.1  rmind 	const struct prochd * const rq = &sched_qs[whichq];
    615   1.1.2.1  rmind 	struct lwp *l2;
    616   1.1.2.1  rmind 	int found = 0;
    617   1.1.2.1  rmind 	int die = 0;
    618   1.1.2.1  rmind 	int empty = 1;
    619   1.1.2.1  rmind 	for (l2 = rq->ph_link; l2 != (const void*) rq; l2 = l2->l_forw) {
    620   1.1.2.1  rmind 		if (l2->l_stat != LSRUN) {
    621   1.1.2.1  rmind 			printf("checkrunqueue[%d]: lwp %p state (%d) "
    622   1.1.2.1  rmind 			    " != LSRUN\n", whichq, l2, l2->l_stat);
    623   1.1.2.1  rmind 		}
    624   1.1.2.1  rmind 		if (l2->l_back->l_forw != l2) {
    625   1.1.2.1  rmind 			printf("checkrunqueue[%d]: lwp %p back-qptr (%p) "
    626   1.1.2.1  rmind 			    "corrupt %p\n", whichq, l2, l2->l_back,
    627   1.1.2.1  rmind 			    l2->l_back->l_forw);
    628   1.1.2.1  rmind 			die = 1;
    629   1.1.2.1  rmind 		}
    630   1.1.2.1  rmind 		if (l2->l_forw->l_back != l2) {
    631   1.1.2.1  rmind 			printf("checkrunqueue[%d]: lwp %p forw-qptr (%p) "
    632   1.1.2.1  rmind 			    "corrupt %p\n", whichq, l2, l2->l_forw,
    633   1.1.2.1  rmind 			    l2->l_forw->l_back);
    634   1.1.2.1  rmind 			die = 1;
    635   1.1.2.1  rmind 		}
    636   1.1.2.1  rmind 		if (l2 == l)
    637   1.1.2.1  rmind 			found = 1;
    638   1.1.2.1  rmind 		empty = 0;
    639   1.1.2.1  rmind 	}
    640   1.1.2.1  rmind 	if (empty && (sched_whichqs & RQMASK(whichq)) != 0) {
    641   1.1.2.1  rmind 		printf("checkrunqueue[%d]: bit set for empty run-queue %p\n",
    642   1.1.2.1  rmind 		    whichq, rq);
    643   1.1.2.1  rmind 		die = 1;
    644   1.1.2.1  rmind 	} else if (!empty && (sched_whichqs & RQMASK(whichq)) == 0) {
    645   1.1.2.1  rmind 		printf("checkrunqueue[%d]: bit clear for non-empty "
    646   1.1.2.1  rmind 		    "run-queue %p\n", whichq, rq);
    647   1.1.2.1  rmind 		die = 1;
    648   1.1.2.1  rmind 	}
    649   1.1.2.1  rmind 	if (l != NULL && (sched_whichqs & RQMASK(whichq)) == 0) {
    650   1.1.2.1  rmind 		printf("checkrunqueue[%d]: bit clear for active lwp %p\n",
    651   1.1.2.1  rmind 		    whichq, l);
    652   1.1.2.1  rmind 		die = 1;
    653   1.1.2.1  rmind 	}
    654   1.1.2.1  rmind 	if (l != NULL && empty) {
    655   1.1.2.1  rmind 		printf("checkrunqueue[%d]: empty run-queue %p with "
    656   1.1.2.1  rmind 		    "active lwp %p\n", whichq, rq, l);
    657   1.1.2.1  rmind 		die = 1;
    658   1.1.2.1  rmind 	}
    659   1.1.2.1  rmind 	if (l != NULL && !found) {
    660   1.1.2.1  rmind 		printf("checkrunqueue[%d]: lwp %p not in runqueue %p!",
    661   1.1.2.1  rmind 		    whichq, l, rq);
    662   1.1.2.1  rmind 		die = 1;
    663   1.1.2.1  rmind 	}
    664   1.1.2.1  rmind 	if (die)
    665   1.1.2.1  rmind 		panic("checkrunqueue: inconsistency found");
    666   1.1.2.1  rmind }
    667   1.1.2.1  rmind #endif /* RQDEBUG */
    668   1.1.2.1  rmind 
    669   1.1.2.1  rmind void
    670   1.1.2.1  rmind sched_enqueue(struct lwp *l)
    671   1.1.2.1  rmind {
    672   1.1.2.9  rmind 	struct prochd *rq;
    673   1.1.2.1  rmind 	struct lwp *prev;
    674   1.1.2.8   yamt 	const int whichq = lwp_eprio(l) / PPQ;
    675   1.1.2.1  rmind 
    676   1.1.2.1  rmind 	LOCK_ASSERT(lwp_locked(l, &sched_mutex));
    677   1.1.2.1  rmind 
    678   1.1.2.1  rmind #ifdef RQDEBUG
    679   1.1.2.1  rmind 	checkrunqueue(whichq, NULL);
    680   1.1.2.1  rmind #endif
    681   1.1.2.1  rmind #ifdef DIAGNOSTIC
    682   1.1.2.1  rmind 	if (l->l_back != NULL || l->l_stat != LSRUN)
    683   1.1.2.4   yamt 		panic("sched_enqueue");
    684   1.1.2.1  rmind #endif
    685   1.1.2.1  rmind 	sched_whichqs |= RQMASK(whichq);
    686   1.1.2.1  rmind 	rq = &sched_qs[whichq];
    687   1.1.2.1  rmind 	prev = rq->ph_rlink;
    688   1.1.2.1  rmind 	l->l_forw = (struct lwp *)rq;
    689   1.1.2.1  rmind 	rq->ph_rlink = l;
    690   1.1.2.1  rmind 	prev->l_forw = l;
    691   1.1.2.1  rmind 	l->l_back = prev;
    692   1.1.2.1  rmind #ifdef RQDEBUG
    693   1.1.2.1  rmind 	checkrunqueue(whichq, l);
    694   1.1.2.1  rmind #endif
    695   1.1.2.1  rmind }
    696   1.1.2.1  rmind 
    697   1.1.2.1  rmind /*
    698   1.1.2.4   yamt  * XXXSMP When LWP dispatch (cpu_switch()) is changed to use sched_dequeue(),
    699   1.1.2.1  rmind  * drop of the effective priority level from kernel to user needs to be
    700   1.1.2.1  rmind  * moved here from userret().  The assignment in userret() is currently
    701   1.1.2.1  rmind  * done unlocked.
    702   1.1.2.1  rmind  */
    703   1.1.2.1  rmind void
    704   1.1.2.1  rmind sched_dequeue(struct lwp *l)
    705   1.1.2.1  rmind {
    706   1.1.2.1  rmind 	struct lwp *prev, *next;
    707   1.1.2.8   yamt 	const int whichq = lwp_eprio(l) / PPQ;
    708   1.1.2.1  rmind 
    709   1.1.2.1  rmind 	LOCK_ASSERT(lwp_locked(l, &sched_mutex));
    710   1.1.2.1  rmind 
    711   1.1.2.1  rmind #ifdef RQDEBUG
    712   1.1.2.1  rmind 	checkrunqueue(whichq, l);
    713   1.1.2.1  rmind #endif
    714   1.1.2.1  rmind 
    715   1.1.2.1  rmind #if defined(DIAGNOSTIC)
    716   1.1.2.1  rmind 	if (((sched_whichqs & RQMASK(whichq)) == 0) || l->l_back == NULL) {
    717   1.1.2.1  rmind 		/* Shouldn't happen - interrupts disabled. */
    718   1.1.2.4   yamt 		panic("sched_dequeue: bit %d not set", whichq);
    719   1.1.2.1  rmind 	}
    720   1.1.2.1  rmind #endif
    721   1.1.2.1  rmind 	prev = l->l_back;
    722   1.1.2.1  rmind 	l->l_back = NULL;
    723   1.1.2.1  rmind 	next = l->l_forw;
    724   1.1.2.1  rmind 	prev->l_forw = next;
    725   1.1.2.1  rmind 	next->l_back = prev;
    726   1.1.2.1  rmind 	if (prev == next)
    727   1.1.2.1  rmind 		sched_whichqs &= ~RQMASK(whichq);
    728   1.1.2.1  rmind #ifdef RQDEBUG
    729   1.1.2.1  rmind 	checkrunqueue(whichq, NULL);
    730   1.1.2.1  rmind #endif
    731   1.1.2.1  rmind }
    732   1.1.2.1  rmind 
    733   1.1.2.1  rmind struct lwp *
    734   1.1.2.9  rmind sched_switch(struct lwp *l)
    735   1.1.2.1  rmind {
    736   1.1.2.1  rmind 	const struct prochd *rq;
    737   1.1.2.1  rmind 	int whichq;
    738   1.1.2.1  rmind 
    739   1.1.2.9  rmind 	KASSERT(l != NULL);
    740   1.1.2.9  rmind 	KASSERT(l->l_stat != LSRUN);
    741   1.1.2.9  rmind 
    742   1.1.2.9  rmind 	if (l->l_stat == LSONPROC) {
    743   1.1.2.9  rmind 		KASSERT(lwp_locked(l, &sched_mutex));
    744   1.1.2.9  rmind 		l->l_stat = LSRUN;
    745   1.1.2.9  rmind 		if ((l->l_flag & LW_IDLE) == 0) {
    746   1.1.2.9  rmind 			sched_enqueue(l);
    747   1.1.2.9  rmind 		}
    748   1.1.2.9  rmind 	}
    749   1.1.2.9  rmind 
    750   1.1.2.1  rmind 	if (sched_whichqs == 0) {
    751   1.1.2.1  rmind 		return NULL;
    752   1.1.2.1  rmind 	}
    753   1.1.2.1  rmind #ifdef __HAVE_BIGENDIAN_BITOPS
    754   1.1.2.1  rmind 	for (whichq = 0; ; whichq++) {
    755   1.1.2.1  rmind 		if ((sched_whichqs & RQMASK(whichq)) != 0) {
    756   1.1.2.1  rmind 			break;
    757   1.1.2.1  rmind 		}
    758   1.1.2.1  rmind 	}
    759   1.1.2.1  rmind #else
    760   1.1.2.1  rmind 	whichq = ffs(sched_whichqs) - 1;
    761   1.1.2.1  rmind #endif
    762   1.1.2.1  rmind 	rq = &sched_qs[whichq];
    763   1.1.2.9  rmind 	return rq->ph_link;
    764   1.1.2.1  rmind }
    765   1.1.2.1  rmind 
    766   1.1.2.1  rmind #endif /* !defined(__HAVE_MD_RUNQUEUE) */
    767   1.1.2.1  rmind 
    768   1.1.2.9  rmind /* Dummy */
    769   1.1.2.9  rmind void sched_lwp_fork(struct lwp *l)
    770   1.1.2.9  rmind {
    771   1.1.2.9  rmind 
    772   1.1.2.9  rmind }
    773   1.1.2.9  rmind 
    774   1.1.2.9  rmind void sched_lwp_exit(struct lwp *l)
    775   1.1.2.9  rmind {
    776   1.1.2.9  rmind 
    777   1.1.2.9  rmind }
    778   1.1.2.9  rmind 
    779   1.1.2.9  rmind void sched_slept(struct lwp *l)
    780   1.1.2.9  rmind {
    781   1.1.2.9  rmind 
    782   1.1.2.9  rmind }
    783   1.1.2.9  rmind 
    784   1.1.2.9  rmind /* SysCtl */
    785   1.1.2.9  rmind 
    786   1.1.2.9  rmind SYSCTL_SETUP(sysctl_sched_setup, "sysctl kern.sched subtree setup") {
    787   1.1.2.9  rmind 	sysctl_createv(clog, 0, NULL, NULL,
    788   1.1.2.9  rmind 		CTLFLAG_PERMANENT,
    789   1.1.2.9  rmind 		CTLTYPE_NODE, "kern", NULL,
    790   1.1.2.9  rmind 		NULL, 0, NULL, 0,
    791   1.1.2.9  rmind 		CTL_KERN, CTL_EOL);
    792   1.1.2.9  rmind 	sysctl_createv(clog, 0, NULL, NULL,
    793   1.1.2.9  rmind 		CTLFLAG_PERMANENT,
    794   1.1.2.9  rmind 		CTLTYPE_NODE, "sched",
    795   1.1.2.9  rmind 		SYSCTL_DESCR("Scheduler options"),
    796   1.1.2.9  rmind 		NULL, 0, NULL, 0,
    797   1.1.2.9  rmind 		CTL_KERN, KERN_SCHED, CTL_EOL);
    798   1.1.2.9  rmind 	sysctl_createv(clog, 0, NULL, NULL,
    799   1.1.2.9  rmind 		CTLFLAG_PERMANENT,
    800   1.1.2.9  rmind 		CTLTYPE_STRING, "name", NULL,
    801   1.1.2.9  rmind 		NULL, 0, __UNCONST("4.4BSD"), 0,
    802  1.1.2.10  rmind 		CTL_KERN, KERN_SCHED, CTL_CREATE, CTL_EOL);
    803   1.1.2.9  rmind 	sysctl_createv(clog, 0, NULL, NULL,
    804   1.1.2.9  rmind 		CTLFLAG_PERMANENT,
    805   1.1.2.9  rmind 		CTLTYPE_INT, "ccpu",
    806   1.1.2.9  rmind 		SYSCTL_DESCR("Scheduler exponential decay value"),
    807   1.1.2.9  rmind 		NULL, 0, &ccpu, 0,
    808   1.1.2.9  rmind 		CTL_KERN, KERN_SCHED, CTL_CREATE, CTL_EOL);
    809   1.1.2.9  rmind }
    810   1.1.2.9  rmind 
    811   1.1.2.1  rmind #if defined(DDB)
    812   1.1.2.1  rmind void
    813   1.1.2.1  rmind sched_print_runqueue(void (*pr)(const char *, ...))
    814   1.1.2.1  rmind {
    815   1.1.2.1  rmind 	struct prochd *ph;
    816   1.1.2.1  rmind 	struct lwp *l;
    817   1.1.2.1  rmind 	int i, first;
    818   1.1.2.1  rmind 
    819   1.1.2.1  rmind 	for (i = 0; i < RUNQUE_NQS; i++)
    820   1.1.2.1  rmind 	{
    821   1.1.2.1  rmind 		first = 1;
    822   1.1.2.1  rmind 		ph = &sched_qs[i];
    823   1.1.2.1  rmind 		for (l = ph->ph_link; l != (void *)ph; l = l->l_forw) {
    824   1.1.2.1  rmind 			if (first) {
    825   1.1.2.1  rmind 				(*pr)("%c%d",
    826   1.1.2.1  rmind 				    (sched_whichqs & RQMASK(i))
    827   1.1.2.1  rmind 				    ? ' ' : '!', i);
    828   1.1.2.1  rmind 				first = 0;
    829   1.1.2.1  rmind 			}
    830   1.1.2.1  rmind 			(*pr)("\t%d.%d (%s) pri=%d usrpri=%d\n",
    831   1.1.2.1  rmind 			    l->l_proc->p_pid,
    832   1.1.2.1  rmind 			    l->l_lid, l->l_proc->p_comm,
    833   1.1.2.1  rmind 			    (int)l->l_priority, (int)l->l_usrpri);
    834   1.1.2.1  rmind 		}
    835   1.1.2.1  rmind 	}
    836   1.1.2.1  rmind }
    837   1.1.2.1  rmind #endif /* defined(DDB) */
    838   1.1.2.1  rmind #undef RQMASK
    839