Home | History | Annotate | Line # | Download | only in kern
sched_4bsd.c revision 1.33.6.2
      1  1.33.6.2    martin /*	$NetBSD: sched_4bsd.c,v 1.33.6.2 2020/04/08 14:08:51 martin Exp $	*/
      2       1.2      yamt 
      3      1.31      maxv /*
      4  1.33.6.2    martin  * Copyright (c) 1999, 2000, 2004, 2006, 2007, 2008, 2019, 2020
      5  1.33.6.2    martin  *     The NetBSD Foundation, Inc.
      6       1.2      yamt  * All rights reserved.
      7       1.2      yamt  *
      8       1.2      yamt  * This code is derived from software contributed to The NetBSD Foundation
      9       1.2      yamt  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10       1.2      yamt  * NASA Ames Research Center, by Charles M. Hannum, Andrew Doran, and
     11       1.2      yamt  * Daniel Sieger.
     12       1.2      yamt  *
     13       1.2      yamt  * Redistribution and use in source and binary forms, with or without
     14       1.2      yamt  * modification, are permitted provided that the following conditions
     15       1.2      yamt  * are met:
     16       1.2      yamt  * 1. Redistributions of source code must retain the above copyright
     17       1.2      yamt  *    notice, this list of conditions and the following disclaimer.
     18       1.2      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     19       1.2      yamt  *    notice, this list of conditions and the following disclaimer in the
     20       1.2      yamt  *    documentation and/or other materials provided with the distribution.
     21       1.2      yamt  *
     22       1.2      yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23       1.2      yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24       1.2      yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25       1.2      yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26       1.2      yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27       1.2      yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28       1.2      yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29       1.2      yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30       1.2      yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31       1.2      yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32       1.2      yamt  * POSSIBILITY OF SUCH DAMAGE.
     33       1.2      yamt  */
     34       1.2      yamt 
     35      1.31      maxv /*
     36       1.2      yamt  * Copyright (c) 1982, 1986, 1990, 1991, 1993
     37       1.2      yamt  *	The Regents of the University of California.  All rights reserved.
     38       1.2      yamt  * (c) UNIX System Laboratories, Inc.
     39       1.2      yamt  * All or some portions of this file are derived from material licensed
     40       1.2      yamt  * to the University of California by American Telephone and Telegraph
     41       1.2      yamt  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     42       1.2      yamt  * the permission of UNIX System Laboratories, Inc.
     43       1.2      yamt  *
     44       1.2      yamt  * Redistribution and use in source and binary forms, with or without
     45       1.2      yamt  * modification, are permitted provided that the following conditions
     46       1.2      yamt  * are met:
     47       1.2      yamt  * 1. Redistributions of source code must retain the above copyright
     48       1.2      yamt  *    notice, this list of conditions and the following disclaimer.
     49       1.2      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     50       1.2      yamt  *    notice, this list of conditions and the following disclaimer in the
     51       1.2      yamt  *    documentation and/or other materials provided with the distribution.
     52       1.2      yamt  * 3. Neither the name of the University nor the names of its contributors
     53       1.2      yamt  *    may be used to endorse or promote products derived from this software
     54       1.2      yamt  *    without specific prior written permission.
     55       1.2      yamt  *
     56       1.2      yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57       1.2      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58       1.2      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59       1.2      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60       1.2      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61       1.2      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62       1.2      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63       1.2      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64       1.2      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65       1.2      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66       1.2      yamt  * SUCH DAMAGE.
     67       1.2      yamt  *
     68       1.2      yamt  *	@(#)kern_synch.c	8.9 (Berkeley) 5/19/95
     69       1.2      yamt  */
     70       1.2      yamt 
     71       1.2      yamt #include <sys/cdefs.h>
     72  1.33.6.2    martin __KERNEL_RCSID(0, "$NetBSD: sched_4bsd.c,v 1.33.6.2 2020/04/08 14:08:51 martin Exp $");
     73       1.2      yamt 
     74       1.2      yamt #include "opt_ddb.h"
     75       1.2      yamt #include "opt_lockdebug.h"
     76       1.2      yamt 
     77       1.2      yamt #include <sys/param.h>
     78       1.2      yamt #include <sys/systm.h>
     79       1.2      yamt #include <sys/callout.h>
     80       1.2      yamt #include <sys/cpu.h>
     81       1.2      yamt #include <sys/proc.h>
     82       1.2      yamt #include <sys/kernel.h>
     83       1.2      yamt #include <sys/resourcevar.h>
     84       1.2      yamt #include <sys/sched.h>
     85       1.2      yamt #include <sys/sysctl.h>
     86       1.2      yamt #include <sys/lockdebug.h>
     87       1.5        ad #include <sys/intr.h>
     88  1.33.6.2    martin #include <sys/atomic.h>
     89       1.2      yamt 
     90       1.2      yamt static void updatepri(struct lwp *);
     91       1.2      yamt static void resetpriority(struct lwp *);
     92       1.2      yamt 
     93       1.2      yamt extern unsigned int sched_pstats_ticks; /* defined in kern_synch.c */
     94       1.2      yamt 
     95       1.2      yamt /* Number of hardclock ticks per sched_tick() */
     96      1.31      maxv static int rrticks __read_mostly;
     97       1.2      yamt 
     98       1.2      yamt /*
     99       1.2      yamt  * Force switch among equal priority processes every 100ms.
    100       1.2      yamt  * Called from hardclock every hz/10 == rrticks hardclock ticks.
    101       1.2      yamt  */
    102       1.2      yamt /* ARGSUSED */
    103       1.2      yamt void
    104       1.2      yamt sched_tick(struct cpu_info *ci)
    105       1.2      yamt {
    106       1.2      yamt 	struct schedstate_percpu *spc = &ci->ci_schedstate;
    107  1.33.6.2    martin 	pri_t pri = PRI_NONE;
    108      1.23        ad 	lwp_t *l;
    109       1.2      yamt 
    110       1.2      yamt 	spc->spc_ticks = rrticks;
    111       1.2      yamt 
    112      1.15        ad 	if (CURCPU_IDLE_P()) {
    113  1.33.6.2    martin 		spc_lock(ci);
    114  1.33.6.2    martin 		sched_resched_cpu(ci, MAXPRI_KTHREAD, true);
    115  1.33.6.2    martin 		/* spc now unlocked */
    116       1.7     rmind 		return;
    117      1.15        ad 	}
    118  1.33.6.2    martin 	l = ci->ci_onproc;
    119      1.23        ad 	if (l == NULL) {
    120      1.19      yamt 		return;
    121      1.19      yamt 	}
    122  1.33.6.2    martin 	/*
    123  1.33.6.2    martin 	 * Can only be spc_lwplock or a turnstile lock at this point
    124  1.33.6.2    martin 	 * (if we interrupted priority inheritance trylock dance).
    125  1.33.6.2    martin 	 */
    126  1.33.6.2    martin 	KASSERT(l->l_mutex != spc->spc_mutex);
    127      1.23        ad 	switch (l->l_class) {
    128      1.23        ad 	case SCHED_FIFO:
    129      1.23        ad 		/* No timeslicing for FIFO jobs. */
    130      1.23        ad 		break;
    131      1.23        ad 	case SCHED_RR:
    132      1.23        ad 		/* Force it into mi_switch() to look for other jobs to run. */
    133  1.33.6.2    martin 		pri = MAXPRI_KERNEL_RT;
    134      1.23        ad 		break;
    135      1.23        ad 	default:
    136      1.23        ad 		if (spc->spc_flags & SPCF_SHOULDYIELD) {
    137      1.23        ad 			/*
    138      1.23        ad 			 * Process is stuck in kernel somewhere, probably
    139      1.31      maxv 			 * due to buggy or inefficient code.  Force a
    140      1.23        ad 			 * kernel preemption.
    141      1.23        ad 			 */
    142  1.33.6.2    martin 			pri = MAXPRI_KERNEL_RT;
    143      1.23        ad 		} else if (spc->spc_flags & SPCF_SEENRR) {
    144      1.23        ad 			/*
    145      1.23        ad 			 * The process has already been through a roundrobin
    146      1.23        ad 			 * without switching and may be hogging the CPU.
    147      1.23        ad 			 * Indicate that the process should yield.
    148      1.23        ad 			 */
    149  1.33.6.2    martin 			pri = MAXPRI_KTHREAD;
    150  1.33.6.2    martin 			spc->spc_flags |= SPCF_SHOULDYIELD;
    151  1.33.6.2    martin 		} else if ((spc->spc_flags & SPCF_1STCLASS) == 0) {
    152  1.33.6.2    martin 			/*
    153  1.33.6.2    martin 			 * For SMT or assymetric systems push a little
    154  1.33.6.2    martin 			 * harder: if this is not a 1st class CPU, try to
    155  1.33.6.2    martin 			 * find a better one to run this LWP.
    156  1.33.6.2    martin 			 */
    157  1.33.6.2    martin 			pri = MAXPRI_KTHREAD;
    158      1.23        ad 			spc->spc_flags |= SPCF_SHOULDYIELD;
    159      1.23        ad 		} else {
    160      1.23        ad 			spc->spc_flags |= SPCF_SEENRR;
    161      1.23        ad 		}
    162      1.23        ad 		break;
    163      1.23        ad 	}
    164  1.33.6.2    martin 
    165  1.33.6.2    martin 	if (pri != PRI_NONE) {
    166  1.33.6.2    martin 		spc_lock(ci);
    167  1.33.6.2    martin 		sched_resched_cpu(ci, pri, true);
    168  1.33.6.2    martin 		/* spc now unlocked */
    169  1.33.6.2    martin 	}
    170       1.2      yamt }
    171       1.2      yamt 
    172       1.8        ad /*
    173       1.8        ad  * Why PRIO_MAX - 2? From setpriority(2):
    174       1.8        ad  *
    175       1.8        ad  *	prio is a value in the range -20 to 20.  The default priority is
    176       1.8        ad  *	0; lower priorities cause more favorable scheduling.  A value of
    177       1.8        ad  *	19 or 20 will schedule a process only when nothing at priority <=
    178       1.8        ad  *	0 is runnable.
    179       1.8        ad  *
    180       1.8        ad  * This gives estcpu influence over 18 priority levels, and leaves nice
    181       1.8        ad  * with 40 levels.  One way to think about it is that nice has 20 levels
    182       1.8        ad  * either side of estcpu's 18.
    183       1.8        ad  */
    184       1.2      yamt #define	ESTCPU_SHIFT	11
    185       1.8        ad #define	ESTCPU_MAX	((PRIO_MAX - 2) << ESTCPU_SHIFT)
    186       1.8        ad #define	ESTCPU_ACCUM	(1 << (ESTCPU_SHIFT - 1))
    187  1.33.6.1  christos #define	ESTCPULIM(e)	uimin((e), ESTCPU_MAX)
    188       1.2      yamt 
    189       1.2      yamt /*
    190      1.31      maxv  * The main parameter used by this algorithm is 'l_estcpu'. It is an estimate
    191      1.31      maxv  * of the recent CPU utilization of the thread.
    192      1.31      maxv  *
    193      1.31      maxv  * l_estcpu is:
    194      1.31      maxv  *  - increased each time the hardclock ticks and the thread is found to
    195      1.31      maxv  *    be executing, in sched_schedclock() called from hardclock()
    196      1.31      maxv  *  - decreased (filtered) on each sched tick, in sched_pstats_hook()
    197      1.31      maxv  * If the lwp is sleeping for more than a second, we don't touch l_estcpu: it
    198      1.31      maxv  * will be updated in sched_setrunnable() when the lwp wakes up, in burst mode
    199      1.31      maxv  * (ie, we decrease it n times).
    200       1.2      yamt  *
    201       1.8        ad  * Note that hardclock updates l_estcpu and l_cpticks independently.
    202       1.2      yamt  *
    203      1.31      maxv  * -----------------------------------------------------------------------------
    204      1.31      maxv  *
    205      1.31      maxv  * Here we describe how l_estcpu is decreased.
    206      1.31      maxv  *
    207      1.31      maxv  * Constants for digital decay (filter):
    208      1.31      maxv  *     90% of l_estcpu usage in (5 * loadavg) seconds
    209      1.31      maxv  *
    210      1.31      maxv  * We wish to decay away 90% of l_estcpu in (5 * loadavg) seconds. That is, we
    211      1.31      maxv  * want to compute a value of decay such that the following loop:
    212      1.31      maxv  *     for (i = 0; i < (5 * loadavg); i++)
    213      1.31      maxv  *         l_estcpu *= decay;
    214      1.31      maxv  * will result in
    215      1.31      maxv  *     l_estcpu *= 0.1;
    216      1.31      maxv  * for all values of loadavg.
    217       1.2      yamt  *
    218       1.2      yamt  * Mathematically this loop can be expressed by saying:
    219      1.31      maxv  *     decay ** (5 * loadavg) ~= .1
    220      1.31      maxv  *
    221      1.31      maxv  * And finally, the corresponding value of decay we're using is:
    222      1.31      maxv  *     decay = (2 * loadavg) / (2 * loadavg + 1)
    223       1.2      yamt  *
    224      1.31      maxv  * -----------------------------------------------------------------------------
    225       1.2      yamt  *
    226      1.31      maxv  * Now, let's prove that the value of decay stated above will always fulfill
    227      1.31      maxv  * the equation:
    228      1.31      maxv  *     decay ** (5 * loadavg) ~= .1
    229       1.2      yamt  *
    230       1.2      yamt  * If we compute b as:
    231      1.31      maxv  *     b = 2 * loadavg
    232       1.2      yamt  * then
    233      1.31      maxv  *     decay = b / (b + 1)
    234       1.2      yamt  *
    235       1.2      yamt  * We now need to prove two things:
    236      1.31      maxv  *     1) Given [factor ** (5 * loadavg) =~ .1], prove [factor == b/(b+1)].
    237      1.31      maxv  *     2) Given [b/(b+1) ** power =~ .1], prove [power == (5 * loadavg)].
    238       1.2      yamt  *
    239       1.2      yamt  * Facts:
    240      1.31      maxv  *   * For x real: exp(x) = 0! + x**1/1! + x**2/2! + ...
    241      1.31      maxv  *     Therefore, for x close to zero, exp(x) =~ 1 + x.
    242      1.31      maxv  *     In turn, for b large enough, exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
    243      1.31      maxv  *
    244      1.31      maxv  *   * For b large enough, (b-1)/b =~ b/(b+1).
    245      1.31      maxv  *
    246      1.31      maxv  *   * For x belonging to [-1;1[, ln(1-x) = - x - x**2/2 - x**3/3 - ...
    247      1.31      maxv  *     Therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
    248      1.31      maxv  *
    249      1.31      maxv  *   * ln(0.1) =~ -2.30
    250       1.2      yamt  *
    251       1.2      yamt  * Proof of (1):
    252      1.31      maxv  *     factor ** (5 * loadavg) =~ 0.1
    253      1.31      maxv  *  => ln(factor) =~ -2.30 / (5 * loadavg)
    254      1.31      maxv  *  => factor =~ exp(-1 / ((5 / 2.30) * loadavg))
    255      1.31      maxv  *            =~ exp(-1 / (2 * loadavg))
    256      1.31      maxv  *            =~ exp(-1 / b)
    257      1.31      maxv  *            =~ (b - 1) / b
    258      1.31      maxv  *            =~ b / (b + 1)
    259      1.31      maxv  *            =~ (2 * loadavg) / ((2 * loadavg) + 1)
    260       1.2      yamt  *
    261       1.2      yamt  * Proof of (2):
    262      1.31      maxv  *     (b / (b + 1)) ** power =~ .1
    263      1.31      maxv  *  => power * ln(b / (b + 1)) =~ -2.30
    264      1.31      maxv  *  => power * (-1 / (b + 1)) =~ -2.30
    265      1.31      maxv  *  => power =~ 2.30 * (b + 1)
    266      1.31      maxv  *  => power =~ 4.60 * loadavg + 2.30
    267      1.31      maxv  *  => power =~ 5 * loadavg
    268      1.31      maxv  *
    269      1.31      maxv  * Conclusion: decay = (2 * loadavg) / (2 * loadavg + 1)
    270       1.2      yamt  */
    271       1.2      yamt 
    272      1.31      maxv /* See calculations above */
    273      1.32      maxv #define	loadfactor(loadavg)  (2 * (loadavg))
    274       1.2      yamt 
    275      1.17      yamt static fixpt_t
    276       1.2      yamt decay_cpu(fixpt_t loadfac, fixpt_t estcpu)
    277       1.2      yamt {
    278       1.2      yamt 
    279       1.2      yamt 	if (estcpu == 0) {
    280       1.2      yamt 		return 0;
    281       1.2      yamt 	}
    282       1.2      yamt 
    283       1.2      yamt #if !defined(_LP64)
    284       1.2      yamt 	/* avoid 64bit arithmetics. */
    285       1.2      yamt #define	FIXPT_MAX ((fixpt_t)((UINTMAX_C(1) << sizeof(fixpt_t) * CHAR_BIT) - 1))
    286       1.2      yamt 	if (__predict_true(loadfac <= FIXPT_MAX / ESTCPU_MAX)) {
    287       1.2      yamt 		return estcpu * loadfac / (loadfac + FSCALE);
    288       1.2      yamt 	}
    289      1.31      maxv #endif
    290       1.2      yamt 
    291       1.2      yamt 	return (uint64_t)estcpu * loadfac / (loadfac + FSCALE);
    292       1.2      yamt }
    293       1.2      yamt 
    294       1.2      yamt static fixpt_t
    295       1.2      yamt decay_cpu_batch(fixpt_t loadfac, fixpt_t estcpu, unsigned int n)
    296       1.2      yamt {
    297       1.2      yamt 
    298      1.31      maxv 	/*
    299      1.31      maxv 	 * For all load averages >= 1 and max l_estcpu of (255 << ESTCPU_SHIFT),
    300      1.31      maxv 	 * if we slept for at least seven times the loadfactor, we will decay
    301      1.31      maxv 	 * l_estcpu to less than (1 << ESTCPU_SHIFT), and therefore we can
    302      1.31      maxv 	 * return zero directly.
    303      1.31      maxv 	 *
    304      1.31      maxv 	 * Note that our ESTCPU_MAX is actually much smaller than
    305      1.31      maxv 	 * (255 << ESTCPU_SHIFT).
    306      1.31      maxv 	 */
    307       1.2      yamt 	if ((n << FSHIFT) >= 7 * loadfac) {
    308       1.2      yamt 		return 0;
    309       1.2      yamt 	}
    310       1.2      yamt 
    311       1.2      yamt 	while (estcpu != 0 && n > 1) {
    312       1.2      yamt 		estcpu = decay_cpu(loadfac, estcpu);
    313       1.2      yamt 		n--;
    314       1.2      yamt 	}
    315       1.2      yamt 
    316       1.2      yamt 	return estcpu;
    317       1.2      yamt }
    318       1.2      yamt 
    319       1.2      yamt /*
    320       1.2      yamt  * sched_pstats_hook:
    321       1.2      yamt  *
    322       1.2      yamt  * Periodically called from sched_pstats(); used to recalculate priorities.
    323       1.2      yamt  */
    324       1.2      yamt void
    325      1.22     rmind sched_pstats_hook(struct lwp *l, int batch)
    326       1.2      yamt {
    327      1.25      yamt 	fixpt_t loadfac;
    328       1.2      yamt 
    329       1.8        ad 	/*
    330       1.8        ad 	 * If the LWP has slept an entire second, stop recalculating
    331       1.8        ad 	 * its priority until it wakes up.
    332       1.8        ad 	 */
    333      1.24     rmind 	KASSERT(lwp_locked(l, NULL));
    334      1.25      yamt 	if (l->l_stat == LSSLEEP || l->l_stat == LSSTOP ||
    335      1.25      yamt 	    l->l_stat == LSSUSPENDED) {
    336      1.25      yamt 		if (l->l_slptime > 1) {
    337      1.25      yamt 			return;
    338      1.25      yamt 		}
    339       1.8        ad 	}
    340      1.33      maxv 
    341      1.33      maxv 	loadfac = loadfactor(averunnable.ldavg[0]);
    342      1.25      yamt 	l->l_estcpu = decay_cpu(loadfac, l->l_estcpu);
    343      1.25      yamt 	resetpriority(l);
    344       1.2      yamt }
    345       1.2      yamt 
    346       1.2      yamt /*
    347      1.31      maxv  * Recalculate the priority of an LWP after it has slept for a while.
    348       1.2      yamt  */
    349       1.2      yamt static void
    350       1.2      yamt updatepri(struct lwp *l)
    351       1.2      yamt {
    352       1.2      yamt 	fixpt_t loadfac;
    353       1.2      yamt 
    354       1.3        ad 	KASSERT(lwp_locked(l, NULL));
    355       1.2      yamt 	KASSERT(l->l_slptime > 1);
    356       1.2      yamt 
    357       1.2      yamt 	loadfac = loadfactor(averunnable.ldavg[0]);
    358       1.2      yamt 
    359       1.2      yamt 	l->l_slptime--; /* the first time was done in sched_pstats */
    360       1.8        ad 	l->l_estcpu = decay_cpu_batch(loadfac, l->l_estcpu, l->l_slptime);
    361       1.2      yamt 	resetpriority(l);
    362       1.2      yamt }
    363       1.2      yamt 
    364       1.2      yamt void
    365      1.14      matt sched_rqinit(void)
    366       1.2      yamt {
    367       1.2      yamt 
    368       1.2      yamt }
    369       1.2      yamt 
    370       1.2      yamt void
    371       1.2      yamt sched_setrunnable(struct lwp *l)
    372       1.2      yamt {
    373       1.2      yamt 
    374       1.2      yamt  	if (l->l_slptime > 1)
    375       1.2      yamt  		updatepri(l);
    376       1.2      yamt }
    377       1.2      yamt 
    378       1.2      yamt void
    379       1.8        ad sched_nice(struct proc *p, int n)
    380       1.2      yamt {
    381       1.8        ad 	struct lwp *l;
    382       1.8        ad 
    383      1.20        ad 	KASSERT(mutex_owned(p->p_lock));
    384       1.2      yamt 
    385       1.8        ad 	p->p_nice = n;
    386       1.8        ad 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    387       1.8        ad 		lwp_lock(l);
    388       1.8        ad 		resetpriority(l);
    389       1.8        ad 		lwp_unlock(l);
    390       1.8        ad 	}
    391       1.2      yamt }
    392       1.2      yamt 
    393       1.2      yamt /*
    394       1.8        ad  * Recompute the priority of an LWP.  Arrange to reschedule if
    395       1.8        ad  * the resulting priority is better than that of the current LWP.
    396       1.2      yamt  */
    397       1.2      yamt static void
    398       1.2      yamt resetpriority(struct lwp *l)
    399       1.2      yamt {
    400       1.8        ad 	pri_t pri;
    401       1.2      yamt 	struct proc *p = l->l_proc;
    402       1.2      yamt 
    403       1.8        ad 	KASSERT(lwp_locked(l, NULL));
    404       1.2      yamt 
    405       1.8        ad 	if (l->l_class != SCHED_OTHER)
    406       1.2      yamt 		return;
    407       1.2      yamt 
    408       1.8        ad 	/* See comments above ESTCPU_SHIFT definition. */
    409       1.8        ad 	pri = (PRI_KERNEL - 1) - (l->l_estcpu >> ESTCPU_SHIFT) - p->p_nice;
    410       1.8        ad 	pri = imax(pri, 0);
    411       1.8        ad 	if (pri != l->l_priority)
    412       1.8        ad 		lwp_changepri(l, pri);
    413       1.2      yamt }
    414       1.2      yamt 
    415       1.2      yamt /*
    416      1.28      yamt  * We adjust the priority of the current LWP.  The priority of a LWP
    417       1.8        ad  * gets worse as it accumulates CPU time.  The CPU usage estimator (l_estcpu)
    418      1.28      yamt  * is increased here.  The formula for computing priorities will compute a
    419      1.28      yamt  * different value each time l_estcpu increases. This can cause a switch,
    420      1.28      yamt  * but unless the priority crosses a PPQ boundary the actual queue will not
    421      1.28      yamt  * change.  The CPU usage estimator ramps up quite quickly when the process
    422      1.28      yamt  * is running (linearly), and decays away exponentially, at a rate which is
    423      1.28      yamt  * proportionally slower when the system is busy.  The basic principle is
    424      1.28      yamt  * that the system will 90% forget that the process used a lot of CPU time
    425      1.31      maxv  * in (5 * loadavg) seconds.  This causes the system to favor processes which
    426      1.28      yamt  * haven't run much recently, and to round-robin among other processes.
    427       1.2      yamt  */
    428       1.2      yamt void
    429       1.2      yamt sched_schedclock(struct lwp *l)
    430       1.2      yamt {
    431       1.8        ad 
    432       1.8        ad 	if (l->l_class != SCHED_OTHER)
    433       1.8        ad 		return;
    434       1.2      yamt 
    435       1.2      yamt 	KASSERT(!CURCPU_IDLE_P());
    436       1.8        ad 	l->l_estcpu = ESTCPULIM(l->l_estcpu + ESTCPU_ACCUM);
    437       1.2      yamt 	lwp_lock(l);
    438       1.2      yamt 	resetpriority(l);
    439       1.2      yamt 	lwp_unlock(l);
    440       1.2      yamt }
    441       1.2      yamt 
    442       1.2      yamt /*
    443       1.2      yamt  * sched_proc_fork:
    444       1.2      yamt  *
    445       1.2      yamt  *	Inherit the parent's scheduler history.
    446       1.2      yamt  */
    447       1.2      yamt void
    448       1.2      yamt sched_proc_fork(struct proc *parent, struct proc *child)
    449       1.2      yamt {
    450       1.8        ad 	lwp_t *pl;
    451       1.2      yamt 
    452      1.20        ad 	KASSERT(mutex_owned(parent->p_lock));
    453       1.2      yamt 
    454       1.8        ad 	pl = LIST_FIRST(&parent->p_lwps);
    455       1.8        ad 	child->p_estcpu_inherited = pl->l_estcpu;
    456       1.2      yamt 	child->p_forktime = sched_pstats_ticks;
    457       1.2      yamt }
    458       1.2      yamt 
    459       1.2      yamt /*
    460       1.2      yamt  * sched_proc_exit:
    461       1.2      yamt  *
    462       1.2      yamt  *	Chargeback parents for the sins of their children.
    463       1.2      yamt  */
    464       1.2      yamt void
    465       1.2      yamt sched_proc_exit(struct proc *parent, struct proc *child)
    466       1.2      yamt {
    467       1.2      yamt 	fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
    468       1.2      yamt 	fixpt_t estcpu;
    469       1.8        ad 	lwp_t *pl, *cl;
    470       1.2      yamt 
    471       1.2      yamt 	/* XXX Only if parent != init?? */
    472       1.2      yamt 
    473      1.20        ad 	mutex_enter(parent->p_lock);
    474       1.8        ad 	pl = LIST_FIRST(&parent->p_lwps);
    475       1.8        ad 	cl = LIST_FIRST(&child->p_lwps);
    476       1.2      yamt 	estcpu = decay_cpu_batch(loadfac, child->p_estcpu_inherited,
    477       1.2      yamt 	    sched_pstats_ticks - child->p_forktime);
    478       1.8        ad 	if (cl->l_estcpu > estcpu) {
    479       1.8        ad 		lwp_lock(pl);
    480       1.8        ad 		pl->l_estcpu = ESTCPULIM(pl->l_estcpu + cl->l_estcpu - estcpu);
    481       1.8        ad 		lwp_unlock(pl);
    482       1.8        ad 	}
    483      1.20        ad 	mutex_exit(parent->p_lock);
    484       1.2      yamt }
    485       1.2      yamt 
    486       1.2      yamt void
    487       1.6     rmind sched_wakeup(struct lwp *l)
    488       1.6     rmind {
    489       1.6     rmind 
    490       1.6     rmind }
    491       1.6     rmind 
    492       1.6     rmind void
    493       1.6     rmind sched_slept(struct lwp *l)
    494       1.6     rmind {
    495       1.6     rmind 
    496       1.6     rmind }
    497       1.6     rmind 
    498       1.2      yamt void
    499       1.8        ad sched_lwp_fork(struct lwp *l1, struct lwp *l2)
    500       1.2      yamt {
    501       1.2      yamt 
    502       1.8        ad 	l2->l_estcpu = l1->l_estcpu;
    503       1.2      yamt }
    504       1.2      yamt 
    505       1.2      yamt void
    506       1.8        ad sched_lwp_collect(struct lwp *t)
    507       1.8        ad {
    508       1.8        ad 	lwp_t *l;
    509       1.8        ad 
    510       1.8        ad 	/* Absorb estcpu value of collected LWP. */
    511       1.8        ad 	l = curlwp;
    512       1.8        ad 	lwp_lock(l);
    513       1.8        ad 	l->l_estcpu += t->l_estcpu;
    514       1.8        ad 	lwp_unlock(l);
    515       1.8        ad }
    516       1.8        ad 
    517      1.16        ad void
    518      1.16        ad sched_oncpu(lwp_t *l)
    519      1.16        ad {
    520      1.16        ad 
    521      1.16        ad }
    522      1.16        ad 
    523      1.16        ad void
    524      1.16        ad sched_newts(lwp_t *l)
    525      1.16        ad {
    526      1.16        ad 
    527      1.16        ad }
    528      1.16        ad 
    529       1.5        ad /*
    530      1.12     rmind  * Sysctl nodes and initialization.
    531       1.5        ad  */
    532      1.12     rmind 
    533      1.12     rmind static int
    534      1.12     rmind sysctl_sched_rtts(SYSCTLFN_ARGS)
    535      1.12     rmind {
    536      1.12     rmind 	struct sysctlnode node;
    537      1.12     rmind 	int rttsms = hztoms(rrticks);
    538      1.12     rmind 
    539      1.12     rmind 	node = *rnode;
    540      1.12     rmind 	node.sysctl_data = &rttsms;
    541      1.12     rmind 	return sysctl_lookup(SYSCTLFN_CALL(&node));
    542      1.12     rmind }
    543      1.12     rmind 
    544      1.16        ad SYSCTL_SETUP(sysctl_sched_4bsd_setup, "sysctl sched setup")
    545       1.2      yamt {
    546       1.2      yamt 	const struct sysctlnode *node = NULL;
    547       1.2      yamt 
    548       1.2      yamt 	sysctl_createv(clog, 0, NULL, &node,
    549       1.2      yamt 		CTLFLAG_PERMANENT,
    550       1.2      yamt 		CTLTYPE_NODE, "sched",
    551       1.2      yamt 		SYSCTL_DESCR("Scheduler options"),
    552       1.2      yamt 		NULL, 0, NULL, 0,
    553       1.2      yamt 		CTL_KERN, CTL_CREATE, CTL_EOL);
    554       1.2      yamt 
    555      1.16        ad 	if (node == NULL)
    556      1.16        ad 		return;
    557       1.5        ad 
    558      1.16        ad 	rrticks = hz / 10;
    559      1.16        ad 
    560      1.16        ad 	sysctl_createv(NULL, 0, &node, NULL,
    561       1.5        ad 		CTLFLAG_PERMANENT,
    562       1.5        ad 		CTLTYPE_STRING, "name", NULL,
    563       1.5        ad 		NULL, 0, __UNCONST("4.4BSD"), 0,
    564       1.5        ad 		CTL_CREATE, CTL_EOL);
    565      1.16        ad 	sysctl_createv(NULL, 0, &node, NULL,
    566      1.12     rmind 		CTLFLAG_PERMANENT,
    567      1.12     rmind 		CTLTYPE_INT, "rtts",
    568      1.30      maxv 		SYSCTL_DESCR("Round-robin time quantum (in milliseconds)"),
    569      1.12     rmind 		sysctl_sched_rtts, 0, NULL, 0,
    570      1.12     rmind 		CTL_CREATE, CTL_EOL);
    571       1.2      yamt }
    572