2007-03-04 08:59:00 +03:00
|
|
|
/* $NetBSD: kern_synch.c,v 1.186 2007/03/04 06:03:06 christos Exp $ */
|
1999-07-27 03:00:58 +04:00
|
|
|
|
|
|
|
/*-
|
2007-02-10 00:55:00 +03:00
|
|
|
* Copyright (c) 1999, 2000, 2004, 2006, 2007 The NetBSD Foundation, Inc.
|
1999-07-27 03:00:58 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
|
2007-02-10 00:55:00 +03:00
|
|
|
* NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
|
1999-07-27 03:00:58 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1982, 1986, 1990, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-06-29 10:29:24 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1998-03-01 05:20:01 +03:00
|
|
|
* @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2007-03-04 08:59:00 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.186 2007/03/04 06:03:06 christos Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
1998-07-05 02:18:13 +04:00
|
|
|
#include "opt_ddb.h"
|
2002-07-03 00:27:44 +04:00
|
|
|
#include "opt_kstack.h"
|
2000-08-08 02:10:52 +04:00
|
|
|
#include "opt_lockdebug.h"
|
2000-08-21 01:50:06 +04:00
|
|
|
#include "opt_multiprocessor.h"
|
2002-08-07 09:14:47 +04:00
|
|
|
#include "opt_perfctrs.h"
|
1998-02-10 17:08:44 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
#define __MUTEX_PRIVATE
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2000-03-23 09:30:07 +03:00
|
|
|
#include <sys/callout.h>
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/buf.h>
|
2002-08-07 15:13:40 +04:00
|
|
|
#if defined(PERFCTRS)
|
2002-08-07 09:14:47 +04:00
|
|
|
#include <sys/pmc.h>
|
2002-08-07 15:13:40 +04:00
|
|
|
#endif
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/resourcevar.h>
|
Scheduler bug fixes and reorganization
* fix the ancient nice(1) bug, where nice +20 processes incorrectly
steal 10 - 20% of the CPU, (or even more depending on load average)
* provide a new schedclk() mechanism at a new clock at schedhz, so high
platform hz values don't cause nice +0 processes to look like they are
niced
* change the algorithm slightly, and reorganize the code a lot
* fix percent-CPU calculation bugs, and eliminate some no-op code
=== nice bug === Correctly divide the scheduler queues between niced and
compute-bound processes. The current nice weight of two (sort of, see
`algorithm change' below) neatly divides the USRPRI queues in half; this
should have been used to clip p_estcpu, instead of UCHAR_MAX. Besides
being the wrong amount, clipping an unsigned char to UCHAR_MAX is a no-op,
and it was done after decay_cpu() which can only _reduce_ the value. It
has to be kept <= NICE_WEIGHT * PRIO_MAX - PPQ or processes can
scheduler-penalize themselves onto the same queue as nice +20 processes.
(Or even a higher one.)
=== New schedclk() mechansism === Some platforms should be cutting down
stathz before hitting the scheduler, since the scheduler algorithm only
works right in the vicinity of 64 Hz. Rather than prescale hz, then scale
back and forth by 4 every time p_estcpu is touched (each occurance an
abstraction violation), use p_estcpu without scaling and require schedhz
to be generated directly at the right frequency. Use a default stathz (well,
actually, profhz) / 4, so nothing changes unless a platform defines schedhz
and a new clock. Define these for alpha, where hz==1024, and nice was
totally broke.
=== Algorithm change === The nice value used to be added to the
exponentially-decayed scheduler history value p_estcpu, in _addition_ to
be incorporated directly (with greater wieght) into the priority calculation.
At first glance, it appears to be a pointless increase of 1/8 the nice
effect (pri = p_estcpu/4 + nice*2), but it's actually at least 3x that
because it will ramp up linearly but be decayed only exponentially, thus
converging to an additional .75 nice for a loadaverage of one. I killed
this, it makes the behavior hard to control, almost impossible to analyze,
and the effect (~~nothing at for the first second, then somewhat increased
niceness after three seconds or more, depending on load average) pointless.
=== Other bugs === hz -> profhz in the p_pctcpu = f(p_cpticks) calcuation.
Collect scheduler functionality. Try to put each abstraction in just one
place.
1999-02-23 05:56:03 +03:00
|
|
|
#include <sys/sched.h>
|
2007-02-18 19:03:06 +03:00
|
|
|
#include <sys/syscall_stats.h>
|
2006-05-15 01:15:11 +04:00
|
|
|
#include <sys/kauth.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
#include <sys/sleepq.h>
|
|
|
|
#include <sys/lockdebug.h>
|
1998-02-05 10:59:28 +03:00
|
|
|
|
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <machine/cpu.h>
|
|
|
|
|
|
|
|
int lbolt; /* once a second sleep address */
|
2000-08-26 07:34:36 +04:00
|
|
|
int rrticks; /* number of hardclock ticks per roundrobin() */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2000-05-27 01:19:19 +04:00
|
|
|
/*
|
|
|
|
* The global scheduler state.
|
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
kmutex_t sched_mutex; /* global sched state mutex */
|
|
|
|
struct prochd sched_qs[RUNQUE_NQS]; /* run queues */
|
2005-12-26 21:41:36 +03:00
|
|
|
volatile uint32_t sched_whichqs; /* bitmap of non-empty queues */
|
2000-08-21 01:50:06 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
void schedcpu(void *);
|
|
|
|
void updatepri(struct lwp *);
|
1996-02-04 05:15:01 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
void sched_unsleep(struct lwp *);
|
2007-02-27 18:07:28 +03:00
|
|
|
void sched_changepri(struct lwp *, pri_t);
|
|
|
|
void sched_lendpri(struct lwp *, pri_t);
|
1999-07-27 03:00:58 +04:00
|
|
|
|
2004-05-13 00:13:58 +04:00
|
|
|
struct callout schedcpu_ch = CALLOUT_INITIALIZER_SETFUNC(schedcpu, NULL);
|
2005-12-24 15:57:14 +03:00
|
|
|
static unsigned int schedcpu_ticks;
|
2003-01-18 13:06:22 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
syncobj_t sleep_syncobj = {
|
|
|
|
SOBJ_SLEEPQ_SORTED,
|
|
|
|
sleepq_unsleep,
|
2007-02-26 12:20:52 +03:00
|
|
|
sleepq_changepri,
|
|
|
|
sleepq_lendpri,
|
|
|
|
syncobj_noowner,
|
2007-02-10 00:55:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
syncobj_t sched_syncobj = {
|
|
|
|
SOBJ_SLEEPQ_SORTED,
|
|
|
|
sched_unsleep,
|
2007-02-26 12:20:52 +03:00
|
|
|
sched_changepri,
|
|
|
|
sched_lendpri,
|
|
|
|
syncobj_noowner,
|
2007-02-10 00:55:00 +03:00
|
|
|
};
|
2003-01-18 13:06:22 +03:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* Force switch among equal priority processes every 100ms.
|
2000-08-26 07:34:36 +04:00
|
|
|
* Called from hardclock every hz/10 == rrticks hardclock ticks.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2000-08-26 08:01:16 +04:00
|
|
|
roundrobin(struct cpu_info *ci)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2000-08-26 08:01:16 +04:00
|
|
|
struct schedstate_percpu *spc = &ci->ci_schedstate;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2000-08-26 07:34:36 +04:00
|
|
|
spc->spc_rrticks = rrticks;
|
2003-06-26 06:09:27 +04:00
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
if (curlwp != NULL) {
|
2000-05-27 01:19:19 +04:00
|
|
|
if (spc->spc_flags & SPCF_SEENRR) {
|
2000-03-23 23:37:58 +03:00
|
|
|
/*
|
|
|
|
* The process has already been through a roundrobin
|
|
|
|
* without switching and may be hogging the CPU.
|
|
|
|
* Indicate that the process should yield.
|
|
|
|
*/
|
2000-05-27 01:19:19 +04:00
|
|
|
spc->spc_flags |= SPCF_SHOULDYIELD;
|
2000-03-23 23:37:58 +03:00
|
|
|
} else
|
2000-05-27 01:19:19 +04:00
|
|
|
spc->spc_flags |= SPCF_SEENRR;
|
2000-03-23 23:37:58 +03:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
cpu_need_resched(curcpu());
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
2005-11-01 12:07:53 +03:00
|
|
|
#define PPQ (128 / RUNQUE_NQS) /* priorities per queue */
|
|
|
|
#define NICE_WEIGHT 2 /* priorities per nice level */
|
|
|
|
|
|
|
|
#define ESTCPU_SHIFT 11
|
|
|
|
#define ESTCPU_MAX ((NICE_WEIGHT * PRIO_MAX - PPQ) << ESTCPU_SHIFT)
|
|
|
|
#define ESTCPULIM(e) min((e), ESTCPU_MAX)
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* Constants for digital decay and forget:
|
|
|
|
* 90% of (p_estcpu) usage in 5 * loadav time
|
|
|
|
* 95% of (p_pctcpu) usage in 60 seconds (load insensitive)
|
|
|
|
* Note that, as ps(1) mentions, this can let percentages
|
|
|
|
* total over 100% (I've seen 137.9% for 3 processes).
|
|
|
|
*
|
|
|
|
* Note that hardclock updates p_estcpu and p_cpticks independently.
|
|
|
|
*
|
|
|
|
* We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
|
|
|
|
* That is, the system wants to compute a value of decay such
|
|
|
|
* that the following for loop:
|
|
|
|
* for (i = 0; i < (5 * loadavg); i++)
|
|
|
|
* p_estcpu *= decay;
|
|
|
|
* will compute
|
|
|
|
* p_estcpu *= 0.1;
|
|
|
|
* for all values of loadavg:
|
|
|
|
*
|
|
|
|
* Mathematically this loop can be expressed by saying:
|
|
|
|
* decay ** (5 * loadavg) ~= .1
|
|
|
|
*
|
|
|
|
* The system computes decay as:
|
|
|
|
* decay = (2 * loadavg) / (2 * loadavg + 1)
|
|
|
|
*
|
|
|
|
* We wish to prove that the system's computation of decay
|
|
|
|
* will always fulfill the equation:
|
|
|
|
* decay ** (5 * loadavg) ~= .1
|
|
|
|
*
|
|
|
|
* If we compute b as:
|
|
|
|
* b = 2 * loadavg
|
|
|
|
* then
|
|
|
|
* decay = b / (b + 1)
|
|
|
|
*
|
|
|
|
* We now need to prove two things:
|
|
|
|
* 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
|
|
|
|
* 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
|
2003-06-26 06:09:27 +04:00
|
|
|
*
|
1994-06-29 10:29:24 +04:00
|
|
|
* Facts:
|
|
|
|
* For x close to zero, exp(x) =~ 1 + x, since
|
|
|
|
* exp(x) = 0! + x**1/1! + x**2/2! + ... .
|
|
|
|
* therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
|
|
|
|
* For x close to zero, ln(1+x) =~ x, since
|
|
|
|
* ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1
|
|
|
|
* therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
|
|
|
|
* ln(.1) =~ -2.30
|
|
|
|
*
|
|
|
|
* Proof of (1):
|
|
|
|
* Solve (factor)**(power) =~ .1 given power (5*loadav):
|
|
|
|
* solving for factor,
|
|
|
|
* ln(factor) =~ (-2.30/5*loadav), or
|
|
|
|
* factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
|
|
|
|
* exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED
|
|
|
|
*
|
|
|
|
* Proof of (2):
|
|
|
|
* Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
|
|
|
|
* solving for power,
|
|
|
|
* power*ln(b/(b+1)) =~ -2.30, or
|
|
|
|
* power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
|
|
|
|
*
|
|
|
|
* Actual power values for the implemented algorithm are as follows:
|
|
|
|
* loadav: 1 2 3 4
|
|
|
|
* power: 5.68 10.32 14.94 19.55
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* calculations for digital decay to forget 90% of usage in 5*loadav sec */
|
|
|
|
#define loadfactor(loadav) (2 * (loadav))
|
2005-11-01 12:07:53 +03:00
|
|
|
|
|
|
|
static fixpt_t
|
|
|
|
decay_cpu(fixpt_t loadfac, fixpt_t estcpu)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (estcpu == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(_LP64)
|
|
|
|
/* avoid 64bit arithmetics. */
|
|
|
|
#define FIXPT_MAX ((fixpt_t)((UINTMAX_C(1) << sizeof(fixpt_t) * CHAR_BIT) - 1))
|
|
|
|
if (__predict_true(loadfac <= FIXPT_MAX / ESTCPU_MAX)) {
|
|
|
|
return estcpu * loadfac / (loadfac + FSCALE);
|
|
|
|
}
|
|
|
|
#endif /* !defined(_LP64) */
|
|
|
|
|
|
|
|
return (uint64_t)estcpu * loadfac / (loadfac + FSCALE);
|
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2005-12-24 15:57:14 +03:00
|
|
|
/*
|
|
|
|
* For all load averages >= 1 and max p_estcpu of (255 << ESTCPU_SHIFT),
|
|
|
|
* sleeping for at least seven times the loadfactor will decay p_estcpu to
|
|
|
|
* less than (1 << ESTCPU_SHIFT).
|
|
|
|
*
|
|
|
|
* note that our ESTCPU_MAX is actually much smaller than (255 << ESTCPU_SHIFT).
|
|
|
|
*/
|
|
|
|
static fixpt_t
|
|
|
|
decay_cpu_batch(fixpt_t loadfac, fixpt_t estcpu, unsigned int n)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((n << FSHIFT) >= 7 * loadfac) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (estcpu != 0 && n > 1) {
|
|
|
|
estcpu = decay_cpu(loadfac, estcpu);
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return estcpu;
|
|
|
|
}
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
|
|
|
|
fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
|
|
|
|
* faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
|
|
|
|
* and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
|
|
|
|
*
|
|
|
|
* To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
|
|
|
|
* 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
|
|
|
|
*
|
|
|
|
* If you dont want to bother with the faster/more-accurate formula, you
|
|
|
|
* can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
|
|
|
|
* (more general) method of calculating the %age of CPU used by a process.
|
|
|
|
*/
|
|
|
|
#define CCPU_SHIFT 11
|
|
|
|
|
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* schedcpu:
|
|
|
|
*
|
|
|
|
* Recompute process priorities, every hz ticks.
|
|
|
|
*
|
|
|
|
* XXXSMP This needs to be reorganised in order to reduce the locking
|
|
|
|
* burden.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2006-11-01 13:17:58 +03:00
|
|
|
schedcpu(void *arg)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
|
2007-02-10 00:55:00 +03:00
|
|
|
struct rlimit *rlim;
|
2003-01-18 13:06:22 +03:00
|
|
|
struct lwp *l;
|
2000-03-30 13:27:11 +04:00
|
|
|
struct proc *p;
|
2007-02-10 00:55:00 +03:00
|
|
|
int minslp, clkhz, sig;
|
|
|
|
long runtm;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2005-12-24 15:57:14 +03:00
|
|
|
schedcpu_ticks++;
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_enter(&proclist_mutex);
|
2004-10-01 20:30:52 +04:00
|
|
|
PROCLIST_FOREACH(p, &allproc) {
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Increment time in/out of memory and sleep time (if
|
|
|
|
* sleeping). We ignore overflow; with 16-bit int's
|
1994-06-29 10:29:24 +04:00
|
|
|
* (remember them?) overflow takes 45 days.
|
|
|
|
*/
|
2003-01-18 13:06:22 +03:00
|
|
|
minslp = 2;
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_enter(&p->p_smutex);
|
|
|
|
runtm = p->p_rtime.tv_sec;
|
2003-01-18 13:06:22 +03:00
|
|
|
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
|
2007-02-10 00:55:00 +03:00
|
|
|
lwp_lock(l);
|
|
|
|
runtm += l->l_rtime.tv_sec;
|
2003-01-18 13:06:22 +03:00
|
|
|
l->l_swtime++;
|
2003-06-26 06:09:27 +04:00
|
|
|
if (l->l_stat == LSSLEEP || l->l_stat == LSSTOP ||
|
2003-01-18 13:06:22 +03:00
|
|
|
l->l_stat == LSSUSPENDED) {
|
|
|
|
l->l_slptime++;
|
|
|
|
minslp = min(minslp, l->l_slptime);
|
|
|
|
} else
|
|
|
|
minslp = 0;
|
2007-02-10 00:55:00 +03:00
|
|
|
lwp_unlock(l);
|
2003-01-18 13:06:22 +03:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Check if the process exceeds its CPU resource allocation.
|
|
|
|
* If over max, kill it.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
rlim = &p->p_rlimit[RLIMIT_CPU];
|
|
|
|
sig = 0;
|
|
|
|
if (runtm >= rlim->rlim_cur) {
|
|
|
|
if (runtm >= rlim->rlim_max)
|
|
|
|
sig = SIGKILL;
|
|
|
|
else {
|
|
|
|
sig = SIGXCPU;
|
|
|
|
if (rlim->rlim_cur < rlim->rlim_max)
|
|
|
|
rlim->rlim_cur += 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the process has run for more than autonicetime, reduce
|
|
|
|
* priority to give others a chance.
|
|
|
|
*/
|
|
|
|
if (autonicetime && runtm > autonicetime && p->p_nice == NZERO
|
|
|
|
&& kauth_cred_geteuid(p->p_cred)) {
|
|
|
|
mutex_spin_enter(&p->p_stmutex);
|
|
|
|
p->p_nice = autoniceval + NZERO;
|
|
|
|
resetprocpriority(p);
|
|
|
|
mutex_spin_exit(&p->p_stmutex);
|
|
|
|
}
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* If the process has slept the entire second,
|
|
|
|
* stop recalculating its priority until it wakes up.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
if (minslp <= 1) {
|
|
|
|
/*
|
|
|
|
* p_pctcpu is only for ps.
|
|
|
|
*/
|
|
|
|
mutex_spin_enter(&p->p_stmutex);
|
|
|
|
clkhz = stathz != 0 ? stathz : hz;
|
1994-06-29 10:29:24 +04:00
|
|
|
#if (FSHIFT >= CCPU_SHIFT)
|
2007-02-10 00:55:00 +03:00
|
|
|
p->p_pctcpu += (clkhz == 100)?
|
|
|
|
((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
|
|
|
|
100 * (((fixpt_t) p->p_cpticks)
|
|
|
|
<< (FSHIFT - CCPU_SHIFT)) / clkhz;
|
1994-06-29 10:29:24 +04:00
|
|
|
#else
|
2007-02-10 00:55:00 +03:00
|
|
|
p->p_pctcpu += ((FSCALE - ccpu) *
|
|
|
|
(p->p_cpticks * FSCALE / clkhz)) >> FSHIFT;
|
1994-06-29 10:29:24 +04:00
|
|
|
#endif
|
2007-02-10 00:55:00 +03:00
|
|
|
p->p_cpticks = 0;
|
|
|
|
p->p_estcpu = decay_cpu(loadfac, p->p_estcpu);
|
|
|
|
|
|
|
|
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
|
|
|
|
lwp_lock(l);
|
|
|
|
if (l->l_slptime <= 1 &&
|
|
|
|
l->l_priority >= PUSER)
|
|
|
|
resetpriority(l);
|
|
|
|
lwp_unlock(l);
|
2003-01-18 13:06:22 +03:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_spin_exit(&p->p_stmutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_exit(&p->p_smutex);
|
|
|
|
if (sig) {
|
|
|
|
psignal(p, sig);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_exit(&proclist_mutex);
|
1998-02-05 10:59:28 +03:00
|
|
|
uvm_meter();
|
2007-03-04 08:59:00 +03:00
|
|
|
wakeup((void *)&lbolt);
|
2004-05-13 00:13:58 +04:00
|
|
|
callout_schedule(&schedcpu_ch, hz);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recalculate the priority of a process after it has slept for a while.
|
|
|
|
*/
|
|
|
|
void
|
2003-01-18 13:06:22 +03:00
|
|
|
updatepri(struct lwp *l)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2003-01-18 13:06:22 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2000-08-21 01:50:06 +04:00
|
|
|
fixpt_t loadfac;
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
LOCK_ASSERT(lwp_locked(l, NULL));
|
2005-12-24 15:57:14 +03:00
|
|
|
KASSERT(l->l_slptime > 1);
|
2000-08-21 01:50:06 +04:00
|
|
|
|
|
|
|
loadfac = loadfactor(averunnable.ldavg[0]);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2005-12-24 15:57:14 +03:00
|
|
|
l->l_slptime--; /* the first time was done in schedcpu */
|
|
|
|
/* XXX NJWLWP */
|
2007-02-10 00:55:00 +03:00
|
|
|
/* XXXSMP occasionally unlocked, should be per-LWP */
|
2005-12-24 15:57:14 +03:00
|
|
|
p->p_estcpu = decay_cpu_batch(loadfac, p->p_estcpu, l->l_slptime);
|
2003-01-18 13:06:22 +03:00
|
|
|
resetpriority(l);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* During autoconfiguration or after a panic, a sleep will simply lower the
|
|
|
|
* priority briefly to allow interrupts, then return. The priority to be
|
|
|
|
* used (safepri) is machine-dependent, thus this value is initialized and
|
|
|
|
* maintained in the machine-dependent layers. This priority will typically
|
|
|
|
* be 0, or the lowest priority that is safe for use on the interrupt stack;
|
|
|
|
* it can be made higher to block network software interrupts after panics.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
int safepri;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* OBSOLETE INTERFACE
|
|
|
|
*
|
1994-06-29 10:29:24 +04:00
|
|
|
* General sleep call. Suspends the current process until a wakeup is
|
|
|
|
* performed on the specified identifier. The process will then be made
|
2007-02-10 00:55:00 +03:00
|
|
|
* runnable with the specified priority. Sleeps at most timo/hz seconds (0
|
|
|
|
* means no timeout). If pri includes PCATCH flag, signals are checked
|
1994-06-29 10:29:24 +04:00
|
|
|
* before and after sleeping, else signals are not checked. Returns 0 if
|
|
|
|
* awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
|
|
|
|
* signal needs to be delivered, ERESTART is returned if the current system
|
|
|
|
* call should be restarted if possible, and EINTR is returned if the system
|
|
|
|
* call should be interrupted by the signal (return EINTR).
|
2000-06-08 09:50:37 +04:00
|
|
|
*
|
2007-02-10 00:55:00 +03:00
|
|
|
* The interlock is held until we are on a sleep queue. The interlock will
|
|
|
|
* be locked before returning back to the caller unless the PNORELOCK flag
|
|
|
|
* is specified, in which case the interlock will always be unlocked upon
|
|
|
|
* return.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
int
|
2007-02-27 18:07:28 +03:00
|
|
|
ltsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo,
|
2007-02-10 00:55:00 +03:00
|
|
|
volatile struct simplelock *interlock)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2003-01-18 13:06:22 +03:00
|
|
|
struct lwp *l = curlwp;
|
2007-02-10 00:55:00 +03:00
|
|
|
sleepq_t *sq;
|
|
|
|
int error, catch;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (sleepq_dontsleep(l)) {
|
|
|
|
(void)sleepq_abort(NULL, 0);
|
|
|
|
if ((priority & PNORELOCK) != 0)
|
2000-06-08 09:50:37 +04:00
|
|
|
simple_unlock(interlock);
|
2007-02-10 00:55:00 +03:00
|
|
|
return 0;
|
2003-01-18 13:06:22 +03:00
|
|
|
}
|
2000-06-08 09:50:37 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
sq = sleeptab_lookup(&sleeptab, ident);
|
|
|
|
sleepq_enter(sq, l);
|
2000-06-08 09:50:37 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (interlock != NULL) {
|
|
|
|
LOCK_ASSERT(simple_lock_held(interlock));
|
2000-06-08 09:50:37 +04:00
|
|
|
simple_unlock(interlock);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
2003-02-04 23:15:59 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
catch = priority & PCATCH;
|
|
|
|
sleepq_block(sq, priority & PRIMASK, ident, wmesg, timo, catch,
|
|
|
|
&sleep_syncobj);
|
|
|
|
error = sleepq_unblock(timo, catch);
|
2003-11-02 19:26:10 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (interlock != NULL && (priority & PNORELOCK) == 0)
|
|
|
|
simple_lock(interlock);
|
|
|
|
|
|
|
|
return error;
|
2003-11-02 19:26:10 +03:00
|
|
|
}
|
|
|
|
|
1999-07-27 03:00:58 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* General sleep call for situations where a wake-up is not expected.
|
1999-07-27 03:00:58 +04:00
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
int
|
2007-02-22 02:48:10 +03:00
|
|
|
kpause(const char *wmesg, bool intr, int timo, kmutex_t *mtx)
|
2000-08-21 01:50:06 +04:00
|
|
|
{
|
2007-02-10 00:55:00 +03:00
|
|
|
struct lwp *l = curlwp;
|
|
|
|
sleepq_t *sq;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (sleepq_dontsleep(l))
|
|
|
|
return sleepq_abort(NULL, 0);
|
|
|
|
|
|
|
|
if (mtx != NULL)
|
|
|
|
mutex_exit(mtx);
|
|
|
|
sq = sleeptab_lookup(&sleeptab, l);
|
|
|
|
sleepq_enter(sq, l);
|
|
|
|
sleepq_block(sq, sched_kpri(l), l, wmesg, timo, intr, &sleep_syncobj);
|
|
|
|
error = sleepq_unblock(timo, intr);
|
|
|
|
if (mtx != NULL)
|
|
|
|
mutex_enter(mtx);
|
|
|
|
|
|
|
|
return error;
|
2000-08-21 01:50:06 +04:00
|
|
|
}
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* OBSOLETE INTERFACE
|
|
|
|
*
|
1994-06-29 10:29:24 +04:00
|
|
|
* Make all processes sleeping on the specified identifier runnable.
|
|
|
|
*/
|
|
|
|
void
|
2007-02-10 00:55:00 +03:00
|
|
|
wakeup(wchan_t ident)
|
2000-08-21 01:50:06 +04:00
|
|
|
{
|
2007-02-10 00:55:00 +03:00
|
|
|
sleepq_t *sq;
|
2000-08-21 01:50:06 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (cold)
|
|
|
|
return;
|
2000-08-21 01:50:06 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
sq = sleeptab_lookup(&sleeptab, ident);
|
|
|
|
sleepq_wake(sq, ident, (u_int)-1);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
1999-07-27 03:00:58 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* OBSOLETE INTERFACE
|
|
|
|
*
|
1999-07-27 03:00:58 +04:00
|
|
|
* Make the highest priority process first in line on the specified
|
|
|
|
* identifier runnable.
|
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
void
|
|
|
|
wakeup_one(wchan_t ident)
|
1999-07-27 03:00:58 +04:00
|
|
|
{
|
2007-02-10 00:55:00 +03:00
|
|
|
sleepq_t *sq;
|
2000-06-08 09:50:37 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if (cold)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sq = sleeptab_lookup(&sleeptab, ident);
|
|
|
|
sleepq_wake(sq, ident, 1);
|
1999-07-27 03:00:58 +04:00
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2002-12-22 02:52:05 +03:00
|
|
|
/*
|
|
|
|
* General yield call. Puts the current process back on its run queue and
|
|
|
|
* performs a voluntary context switch. Should only be called when the
|
|
|
|
* current process explicitly requests it (eg sched_yield(2) in compat code).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
yield(void)
|
|
|
|
{
|
2003-01-18 13:06:22 +03:00
|
|
|
struct lwp *l = curlwp;
|
2002-12-22 02:52:05 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
|
|
|
|
lwp_lock(l);
|
|
|
|
if (l->l_stat == LSONPROC) {
|
|
|
|
KASSERT(lwp_locked(l, &sched_mutex));
|
|
|
|
l->l_priority = l->l_usrpri;
|
|
|
|
}
|
|
|
|
l->l_nvcsw++;
|
2003-01-18 13:06:22 +03:00
|
|
|
mi_switch(l, NULL);
|
2007-02-10 00:55:00 +03:00
|
|
|
KERNEL_LOCK(l->l_biglocks, l);
|
2002-12-22 02:52:05 +03:00
|
|
|
}
|
|
|
|
|
2000-03-23 23:37:58 +03:00
|
|
|
/*
|
|
|
|
* General preemption call. Puts the current process back on its run queue
|
2005-12-20 22:26:15 +03:00
|
|
|
* and performs an involuntary context switch.
|
2000-03-23 23:37:58 +03:00
|
|
|
*/
|
|
|
|
void
|
2007-02-10 00:55:00 +03:00
|
|
|
preempt(void)
|
2000-03-23 23:37:58 +03:00
|
|
|
{
|
2003-01-18 13:06:22 +03:00
|
|
|
struct lwp *l = curlwp;
|
2003-06-26 06:08:19 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
|
|
|
|
lwp_lock(l);
|
|
|
|
if (l->l_stat == LSONPROC) {
|
|
|
|
KASSERT(lwp_locked(l, &sched_mutex));
|
|
|
|
l->l_priority = l->l_usrpri;
|
|
|
|
}
|
|
|
|
l->l_nivcsw++;
|
|
|
|
(void)mi_switch(l, NULL);
|
|
|
|
KERNEL_LOCK(l->l_biglocks, l);
|
2000-03-23 23:37:58 +03:00
|
|
|
}
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* The machine independent parts of context switch. Switch to "new"
|
|
|
|
* if non-NULL, otherwise let cpu_switch choose the next lwp.
|
2003-06-26 06:09:27 +04:00
|
|
|
*
|
2003-01-18 13:06:22 +03:00
|
|
|
* Returns 1 if another process was actually run.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
2003-01-18 13:06:22 +03:00
|
|
|
int
|
|
|
|
mi_switch(struct lwp *l, struct lwp *newl)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2000-05-31 09:02:31 +04:00
|
|
|
struct schedstate_percpu *spc;
|
1994-06-29 10:29:24 +04:00
|
|
|
struct timeval tv;
|
2007-02-10 00:55:00 +03:00
|
|
|
int retval, oldspl;
|
|
|
|
long s, u;
|
2002-09-22 09:36:48 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
LOCK_ASSERT(lwp_locked(l, NULL));
|
2000-05-31 09:02:31 +04:00
|
|
|
|
1998-11-04 09:19:55 +03:00
|
|
|
#ifdef LOCKDEBUG
|
2005-12-27 07:06:45 +03:00
|
|
|
spinlock_switchcheck();
|
2000-08-08 01:55:22 +04:00
|
|
|
simple_lock_switchcheck();
|
1998-03-01 05:20:01 +03:00
|
|
|
#endif
|
2007-02-10 00:55:00 +03:00
|
|
|
#ifdef KSTACK_CHECK_MAGIC
|
|
|
|
kstack_check_magic(l);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's safe to read the per CPU schedstate unlocked here, as all we
|
|
|
|
* are after is the run time and that's guarenteed to have been last
|
|
|
|
* updated by this CPU.
|
|
|
|
*/
|
|
|
|
KDASSERT(l->l_cpu == curcpu());
|
|
|
|
spc = &l->l_cpu->ci_schedstate;
|
2000-08-08 01:55:22 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* Compute the amount of time during which the current
|
2002-09-22 09:36:48 +04:00
|
|
|
* process was running.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
microtime(&tv);
|
2007-02-10 00:55:00 +03:00
|
|
|
u = l->l_rtime.tv_usec +
|
2003-01-18 13:06:22 +03:00
|
|
|
(tv.tv_usec - spc->spc_runtime.tv_usec);
|
2007-02-10 00:55:00 +03:00
|
|
|
s = l->l_rtime.tv_sec + (tv.tv_sec - spc->spc_runtime.tv_sec);
|
2007-02-20 01:14:15 +03:00
|
|
|
if (u < 0) {
|
|
|
|
u += 1000000;
|
|
|
|
s--;
|
|
|
|
} else if (u >= 1000000) {
|
|
|
|
u -= 1000000;
|
|
|
|
s++;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
l->l_rtime.tv_usec = u;
|
|
|
|
l->l_rtime.tv_sec = s;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2007-02-18 19:58:15 +03:00
|
|
|
/* Count time spent in current system call */
|
|
|
|
SYSCALL_TIME_SLEEP(l);
|
|
|
|
|
2000-03-23 23:37:58 +03:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* XXXSMP If we are using h/w performance counters, save context.
|
2000-03-23 23:37:58 +03:00
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
#if PERFCTRS
|
2007-02-10 05:55:18 +03:00
|
|
|
if (PMC_ENABLED(l->l_proc)) {
|
|
|
|
pmc_save_context(l->l_proc);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2002-07-03 00:27:44 +04:00
|
|
|
#endif
|
|
|
|
|
2002-09-22 09:36:48 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Acquire the sched_mutex if necessary. It will be released by
|
|
|
|
* cpu_switch once it has decided to idle, or picked another LWP
|
|
|
|
* to run.
|
2002-09-22 09:36:48 +04:00
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
#if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
|
|
|
|
if (l->l_mutex != &sched_mutex) {
|
|
|
|
mutex_spin_enter(&sched_mutex);
|
|
|
|
lwp_unlock(l);
|
2006-09-02 10:32:09 +04:00
|
|
|
}
|
2002-08-07 09:14:47 +04:00
|
|
|
#endif
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* If on the CPU and we have gotten this far, then we must yield.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
KASSERT(l->l_stat != LSRUN);
|
|
|
|
if (l->l_stat == LSONPROC) {
|
|
|
|
KASSERT(lwp_locked(l, &sched_mutex));
|
|
|
|
l->l_stat = LSRUN;
|
|
|
|
setrunqueue(l);
|
|
|
|
}
|
2002-09-30 01:11:36 +04:00
|
|
|
uvmexp.swtch++;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process is about to yield the CPU; clear the appropriate
|
|
|
|
* scheduling flags.
|
|
|
|
*/
|
|
|
|
spc->spc_flags &= ~SPCF_SWITCHCLEAR;
|
|
|
|
|
|
|
|
LOCKDEBUG_BARRIER(&sched_mutex, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Switch to the new current LWP. When we run again, we'll
|
|
|
|
* return back here.
|
|
|
|
*/
|
|
|
|
oldspl = MUTEX_SPIN_OLDSPL(l->l_cpu);
|
|
|
|
|
|
|
|
if (newl == NULL || newl->l_back == NULL)
|
2003-01-18 13:06:22 +03:00
|
|
|
retval = cpu_switch(l, NULL);
|
2007-02-10 00:55:00 +03:00
|
|
|
else {
|
|
|
|
KASSERT(lwp_locked(newl, &sched_mutex));
|
2003-01-18 13:06:22 +03:00
|
|
|
remrunqueue(newl);
|
|
|
|
cpu_switchto(l, newl);
|
|
|
|
retval = 0;
|
|
|
|
}
|
2000-05-31 09:02:31 +04:00
|
|
|
|
2002-08-07 09:14:47 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* XXXSMP If we are using h/w performance counters, restore context.
|
2002-08-07 09:14:47 +04:00
|
|
|
*/
|
2002-09-22 09:36:48 +04:00
|
|
|
#if PERFCTRS
|
2007-02-10 05:55:18 +03:00
|
|
|
if (PMC_ENABLED(l->l_proc)) {
|
|
|
|
pmc_restore_context(l->l_proc);
|
2006-09-02 10:32:09 +04:00
|
|
|
}
|
2002-09-22 09:36:48 +04:00
|
|
|
#endif
|
|
|
|
|
2000-05-31 09:02:31 +04:00
|
|
|
/*
|
|
|
|
* We're running again; record our new start time. We might
|
2007-02-10 00:55:00 +03:00
|
|
|
* be running on a new CPU now, so don't use the cached
|
2000-05-31 09:02:31 +04:00
|
|
|
* schedstate_percpu pointer.
|
|
|
|
*/
|
2007-02-18 19:58:15 +03:00
|
|
|
SYSCALL_TIME_WAKEUP(l);
|
2003-01-18 13:06:22 +03:00
|
|
|
KDASSERT(l->l_cpu == curcpu());
|
|
|
|
microtime(&l->l_cpu->ci_schedstate.spc_runtime);
|
2007-02-10 00:55:00 +03:00
|
|
|
splx(oldspl);
|
2006-11-01 12:32:52 +03:00
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
return retval;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the (doubly-linked) run queues
|
|
|
|
* to be empty.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rqinit()
|
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
int i;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2000-05-27 01:19:19 +04:00
|
|
|
for (i = 0; i < RUNQUE_NQS; i++)
|
|
|
|
sched_qs[i].ph_link = sched_qs[i].ph_rlink =
|
2003-01-18 13:06:22 +03:00
|
|
|
(struct lwp *)&sched_qs[i];
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
mutex_init(&sched_mutex, MUTEX_SPIN, IPL_SCHED);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
2005-12-24 22:12:23 +03:00
|
|
|
static inline void
|
2007-02-26 12:20:52 +03:00
|
|
|
resched_lwp(struct lwp *l)
|
2002-12-29 20:40:26 +03:00
|
|
|
{
|
|
|
|
struct cpu_info *ci;
|
2007-02-27 18:07:28 +03:00
|
|
|
const pri_t pri = lwp_eprio(l);
|
2002-12-29 20:40:26 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXXSMP
|
2003-01-18 13:06:22 +03:00
|
|
|
* Since l->l_cpu persists across a context switch,
|
2002-12-29 20:40:26 +03:00
|
|
|
* this gives us *very weak* processor affinity, in
|
|
|
|
* that we notify the CPU on which the process last
|
|
|
|
* ran that it should try to switch.
|
|
|
|
*
|
|
|
|
* This does not guarantee that the process will run on
|
|
|
|
* that processor next, because another processor might
|
|
|
|
* grab it the next time it performs a context switch.
|
|
|
|
*
|
|
|
|
* This also does not handle the case where its last
|
|
|
|
* CPU is running a higher-priority process, but every
|
|
|
|
* other CPU is running a lower-priority process. There
|
|
|
|
* are ways to handle this situation, but they're not
|
|
|
|
* currently very pretty, and we also need to weigh the
|
|
|
|
* cost of moving a process from one CPU to another.
|
|
|
|
*
|
|
|
|
* XXXSMP
|
|
|
|
* There is also the issue of locking the other CPU's
|
|
|
|
* sched state, which we currently do not do.
|
|
|
|
*/
|
2003-01-18 13:06:22 +03:00
|
|
|
ci = (l->l_cpu != NULL) ? l->l_cpu : curcpu();
|
2003-01-15 10:12:20 +03:00
|
|
|
if (pri < ci->ci_schedstate.spc_curpriority)
|
2007-02-10 00:55:00 +03:00
|
|
|
cpu_need_resched(ci);
|
2002-12-29 20:40:26 +03:00
|
|
|
}
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Change process state to be runnable, placing it on the run queue if it is
|
|
|
|
* in memory, and awakening the swapper if it isn't in memory.
|
|
|
|
*
|
|
|
|
* Call with the process and LWP locked. Will return with the LWP unlocked.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
void
|
2003-01-18 13:06:22 +03:00
|
|
|
setrunnable(struct lwp *l)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2003-01-18 13:06:22 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2007-02-10 00:55:00 +03:00
|
|
|
sigset_t *ss;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2007-02-23 19:51:47 +03:00
|
|
|
KASSERT(mutex_owned(&p->p_smutex));
|
|
|
|
KASSERT(lwp_locked(l, NULL));
|
2000-08-21 01:50:06 +04:00
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
switch (l->l_stat) {
|
|
|
|
case LSSTOP:
|
1995-06-09 03:51:01 +04:00
|
|
|
/*
|
|
|
|
* If we're being traced (possibly because someone attached us
|
|
|
|
* while we were stopped), check for a signal from the debugger.
|
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
if ((p->p_slflag & PSL_TRACED) != 0 && p->p_xstat != 0) {
|
|
|
|
if ((sigprop[p->p_xstat] & SA_TOLWP) != 0)
|
|
|
|
ss = &l->l_sigpend.sp_set;
|
|
|
|
else
|
|
|
|
ss = &p->p_sigpend.sp_set;
|
|
|
|
sigaddset(ss, p->p_xstat);
|
|
|
|
signotify(l);
|
1998-09-11 16:50:05 +04:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
p->p_nrlwps++;
|
2003-01-18 13:06:22 +03:00
|
|
|
break;
|
|
|
|
case LSSUSPENDED:
|
2007-02-18 01:31:36 +03:00
|
|
|
l->l_flag &= ~LW_WSUSPEND;
|
2007-02-10 00:55:00 +03:00
|
|
|
p->p_nrlwps++;
|
1994-06-29 10:29:24 +04:00
|
|
|
break;
|
2007-02-10 00:55:00 +03:00
|
|
|
case LSSLEEP:
|
|
|
|
KASSERT(l->l_wchan != NULL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
panic("setrunnable: lwp %p state was %d", l, l->l_stat);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
2003-11-02 19:26:10 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* If the LWP was sleeping interruptably, then it's OK to start it
|
|
|
|
* again. If not, mark it as still sleeping.
|
|
|
|
*/
|
|
|
|
if (l->l_wchan != NULL) {
|
|
|
|
l->l_stat = LSSLEEP;
|
2007-02-23 19:51:47 +03:00
|
|
|
/* lwp_unsleep() will release the lock. */
|
|
|
|
lwp_unsleep(l);
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2003-11-02 19:26:10 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
LOCK_ASSERT(lwp_locked(l, &sched_mutex));
|
2003-01-18 13:06:22 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* If the LWP is still on the CPU, mark it as LSONPROC. It may be
|
|
|
|
* about to call mi_switch(), in which case it will yield.
|
|
|
|
*
|
|
|
|
* XXXSMP Will need to change for preemption.
|
|
|
|
*/
|
|
|
|
#ifdef MULTIPROCESSOR
|
|
|
|
if (l->l_cpu->ci_curlwp == l) {
|
|
|
|
#else
|
|
|
|
if (l == curlwp) {
|
|
|
|
#endif
|
|
|
|
l->l_stat = LSONPROC;
|
|
|
|
l->l_slptime = 0;
|
|
|
|
lwp_unlock(l);
|
|
|
|
return;
|
|
|
|
}
|
2003-01-18 13:06:22 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* Set the LWP runnable. If it's swapped out, we need to wake the swapper
|
|
|
|
* to bring it back in. Otherwise, enter it into a run queue.
|
|
|
|
*/
|
2003-01-18 13:06:22 +03:00
|
|
|
if (l->l_slptime > 1)
|
|
|
|
updatepri(l);
|
2007-02-10 00:55:00 +03:00
|
|
|
l->l_stat = LSRUN;
|
2003-01-18 13:06:22 +03:00
|
|
|
l->l_slptime = 0;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-02-18 01:31:36 +03:00
|
|
|
if (l->l_flag & LW_INMEM) {
|
2007-02-10 00:55:00 +03:00
|
|
|
setrunqueue(l);
|
2007-02-26 12:20:52 +03:00
|
|
|
resched_lwp(l);
|
2007-02-10 00:55:00 +03:00
|
|
|
lwp_unlock(l);
|
|
|
|
} else {
|
|
|
|
lwp_unlock(l);
|
2007-02-15 23:21:13 +03:00
|
|
|
uvm_kick_scheduler();
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the priority of a process when running in user mode.
|
|
|
|
* Arrange to reschedule if the resulting priority is better
|
|
|
|
* than that of the current process.
|
|
|
|
*/
|
|
|
|
void
|
2003-01-18 13:06:22 +03:00
|
|
|
resetpriority(struct lwp *l)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2007-02-27 18:07:28 +03:00
|
|
|
pri_t newpriority;
|
2003-01-18 13:06:22 +03:00
|
|
|
struct proc *p = l->l_proc;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/* XXXSMP LOCK_ASSERT(mutex_owned(&p->p_stmutex)); */
|
|
|
|
LOCK_ASSERT(lwp_locked(l, NULL));
|
|
|
|
|
2007-02-18 01:31:36 +03:00
|
|
|
if ((l->l_flag & LW_SYSTEM) != 0)
|
2007-02-10 00:55:00 +03:00
|
|
|
return;
|
2000-08-21 01:50:06 +04:00
|
|
|
|
2005-11-01 12:07:53 +03:00
|
|
|
newpriority = PUSER + (p->p_estcpu >> ESTCPU_SHIFT) +
|
2007-02-10 00:55:00 +03:00
|
|
|
NICE_WEIGHT * (p->p_nice - NZERO);
|
1994-06-29 10:29:24 +04:00
|
|
|
newpriority = min(newpriority, MAXPRI);
|
2007-02-10 00:55:00 +03:00
|
|
|
lwp_changepri(l, newpriority);
|
2003-01-18 13:06:22 +03:00
|
|
|
}
|
|
|
|
|
2003-06-26 06:09:27 +04:00
|
|
|
/*
|
2003-01-18 13:06:22 +03:00
|
|
|
* Recompute priority for all LWPs in a process.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
resetprocpriority(struct proc *p)
|
|
|
|
{
|
|
|
|
struct lwp *l;
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
LOCK_ASSERT(mutex_owned(&p->p_stmutex));
|
|
|
|
|
|
|
|
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
|
|
|
|
lwp_lock(l);
|
|
|
|
resetpriority(l);
|
|
|
|
lwp_unlock(l);
|
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
Scheduler bug fixes and reorganization
* fix the ancient nice(1) bug, where nice +20 processes incorrectly
steal 10 - 20% of the CPU, (or even more depending on load average)
* provide a new schedclk() mechanism at a new clock at schedhz, so high
platform hz values don't cause nice +0 processes to look like they are
niced
* change the algorithm slightly, and reorganize the code a lot
* fix percent-CPU calculation bugs, and eliminate some no-op code
=== nice bug === Correctly divide the scheduler queues between niced and
compute-bound processes. The current nice weight of two (sort of, see
`algorithm change' below) neatly divides the USRPRI queues in half; this
should have been used to clip p_estcpu, instead of UCHAR_MAX. Besides
being the wrong amount, clipping an unsigned char to UCHAR_MAX is a no-op,
and it was done after decay_cpu() which can only _reduce_ the value. It
has to be kept <= NICE_WEIGHT * PRIO_MAX - PPQ or processes can
scheduler-penalize themselves onto the same queue as nice +20 processes.
(Or even a higher one.)
=== New schedclk() mechansism === Some platforms should be cutting down
stathz before hitting the scheduler, since the scheduler algorithm only
works right in the vicinity of 64 Hz. Rather than prescale hz, then scale
back and forth by 4 every time p_estcpu is touched (each occurance an
abstraction violation), use p_estcpu without scaling and require schedhz
to be generated directly at the right frequency. Use a default stathz (well,
actually, profhz) / 4, so nothing changes unless a platform defines schedhz
and a new clock. Define these for alpha, where hz==1024, and nice was
totally broke.
=== Algorithm change === The nice value used to be added to the
exponentially-decayed scheduler history value p_estcpu, in _addition_ to
be incorporated directly (with greater wieght) into the priority calculation.
At first glance, it appears to be a pointless increase of 1/8 the nice
effect (pri = p_estcpu/4 + nice*2), but it's actually at least 3x that
because it will ramp up linearly but be decayed only exponentially, thus
converging to an additional .75 nice for a loadaverage of one. I killed
this, it makes the behavior hard to control, almost impossible to analyze,
and the effect (~~nothing at for the first second, then somewhat increased
niceness after three seconds or more, depending on load average) pointless.
=== Other bugs === hz -> profhz in the p_pctcpu = f(p_cpticks) calcuation.
Collect scheduler functionality. Try to put each abstraction in just one
place.
1999-02-23 05:56:03 +03:00
|
|
|
|
|
|
|
/*
|
1999-02-28 21:14:57 +03:00
|
|
|
* We adjust the priority of the current process. The priority of a process
|
2004-02-13 14:36:08 +03:00
|
|
|
* gets worse as it accumulates CPU time. The CPU usage estimator (p_estcpu)
|
1999-02-28 21:14:57 +03:00
|
|
|
* is increased here. The formula for computing priorities (in kern_synch.c)
|
|
|
|
* will compute a different value each time p_estcpu increases. This can
|
|
|
|
* cause a switch, but unless the priority crosses a PPQ boundary the actual
|
2004-02-13 14:36:08 +03:00
|
|
|
* queue will not change. The CPU usage estimator ramps up quite quickly
|
1999-02-28 21:14:57 +03:00
|
|
|
* when the process is running (linearly), and decays away exponentially, at
|
|
|
|
* a rate which is proportionally slower when the system is busy. The basic
|
2000-08-03 00:21:36 +04:00
|
|
|
* principle is that the system will 90% forget that the process used a lot
|
1999-02-28 21:14:57 +03:00
|
|
|
* of CPU time in 5 * loadav seconds. This causes the system to favor
|
|
|
|
* processes which haven't run much recently, and to round-robin among other
|
|
|
|
* processes.
|
Scheduler bug fixes and reorganization
* fix the ancient nice(1) bug, where nice +20 processes incorrectly
steal 10 - 20% of the CPU, (or even more depending on load average)
* provide a new schedclk() mechanism at a new clock at schedhz, so high
platform hz values don't cause nice +0 processes to look like they are
niced
* change the algorithm slightly, and reorganize the code a lot
* fix percent-CPU calculation bugs, and eliminate some no-op code
=== nice bug === Correctly divide the scheduler queues between niced and
compute-bound processes. The current nice weight of two (sort of, see
`algorithm change' below) neatly divides the USRPRI queues in half; this
should have been used to clip p_estcpu, instead of UCHAR_MAX. Besides
being the wrong amount, clipping an unsigned char to UCHAR_MAX is a no-op,
and it was done after decay_cpu() which can only _reduce_ the value. It
has to be kept <= NICE_WEIGHT * PRIO_MAX - PPQ or processes can
scheduler-penalize themselves onto the same queue as nice +20 processes.
(Or even a higher one.)
=== New schedclk() mechansism === Some platforms should be cutting down
stathz before hitting the scheduler, since the scheduler algorithm only
works right in the vicinity of 64 Hz. Rather than prescale hz, then scale
back and forth by 4 every time p_estcpu is touched (each occurance an
abstraction violation), use p_estcpu without scaling and require schedhz
to be generated directly at the right frequency. Use a default stathz (well,
actually, profhz) / 4, so nothing changes unless a platform defines schedhz
and a new clock. Define these for alpha, where hz==1024, and nice was
totally broke.
=== Algorithm change === The nice value used to be added to the
exponentially-decayed scheduler history value p_estcpu, in _addition_ to
be incorporated directly (with greater wieght) into the priority calculation.
At first glance, it appears to be a pointless increase of 1/8 the nice
effect (pri = p_estcpu/4 + nice*2), but it's actually at least 3x that
because it will ramp up linearly but be decayed only exponentially, thus
converging to an additional .75 nice for a loadaverage of one. I killed
this, it makes the behavior hard to control, almost impossible to analyze,
and the effect (~~nothing at for the first second, then somewhat increased
niceness after three seconds or more, depending on load average) pointless.
=== Other bugs === hz -> profhz in the p_pctcpu = f(p_cpticks) calcuation.
Collect scheduler functionality. Try to put each abstraction in just one
place.
1999-02-23 05:56:03 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2003-01-18 13:06:22 +03:00
|
|
|
schedclock(struct lwp *l)
|
Scheduler bug fixes and reorganization
* fix the ancient nice(1) bug, where nice +20 processes incorrectly
steal 10 - 20% of the CPU, (or even more depending on load average)
* provide a new schedclk() mechanism at a new clock at schedhz, so high
platform hz values don't cause nice +0 processes to look like they are
niced
* change the algorithm slightly, and reorganize the code a lot
* fix percent-CPU calculation bugs, and eliminate some no-op code
=== nice bug === Correctly divide the scheduler queues between niced and
compute-bound processes. The current nice weight of two (sort of, see
`algorithm change' below) neatly divides the USRPRI queues in half; this
should have been used to clip p_estcpu, instead of UCHAR_MAX. Besides
being the wrong amount, clipping an unsigned char to UCHAR_MAX is a no-op,
and it was done after decay_cpu() which can only _reduce_ the value. It
has to be kept <= NICE_WEIGHT * PRIO_MAX - PPQ or processes can
scheduler-penalize themselves onto the same queue as nice +20 processes.
(Or even a higher one.)
=== New schedclk() mechansism === Some platforms should be cutting down
stathz before hitting the scheduler, since the scheduler algorithm only
works right in the vicinity of 64 Hz. Rather than prescale hz, then scale
back and forth by 4 every time p_estcpu is touched (each occurance an
abstraction violation), use p_estcpu without scaling and require schedhz
to be generated directly at the right frequency. Use a default stathz (well,
actually, profhz) / 4, so nothing changes unless a platform defines schedhz
and a new clock. Define these for alpha, where hz==1024, and nice was
totally broke.
=== Algorithm change === The nice value used to be added to the
exponentially-decayed scheduler history value p_estcpu, in _addition_ to
be incorporated directly (with greater wieght) into the priority calculation.
At first glance, it appears to be a pointless increase of 1/8 the nice
effect (pri = p_estcpu/4 + nice*2), but it's actually at least 3x that
because it will ramp up linearly but be decayed only exponentially, thus
converging to an additional .75 nice for a loadaverage of one. I killed
this, it makes the behavior hard to control, almost impossible to analyze,
and the effect (~~nothing at for the first second, then somewhat increased
niceness after three seconds or more, depending on load average) pointless.
=== Other bugs === hz -> profhz in the p_pctcpu = f(p_cpticks) calcuation.
Collect scheduler functionality. Try to put each abstraction in just one
place.
1999-02-23 05:56:03 +03:00
|
|
|
{
|
2003-01-18 13:06:22 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2000-06-08 09:50:37 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_spin_enter(&p->p_stmutex);
|
2005-11-01 12:07:53 +03:00
|
|
|
p->p_estcpu = ESTCPULIM(p->p_estcpu + (1 << ESTCPU_SHIFT));
|
2007-02-10 00:55:00 +03:00
|
|
|
lwp_lock(l);
|
2003-01-18 13:06:22 +03:00
|
|
|
resetpriority(l);
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_spin_exit(&p->p_stmutex);
|
2007-02-18 01:31:36 +03:00
|
|
|
if ((l->l_flag & LW_SYSTEM) == 0 && l->l_priority >= PUSER)
|
2003-01-18 13:06:22 +03:00
|
|
|
l->l_priority = l->l_usrpri;
|
2007-02-10 00:55:00 +03:00
|
|
|
lwp_unlock(l);
|
Scheduler bug fixes and reorganization
* fix the ancient nice(1) bug, where nice +20 processes incorrectly
steal 10 - 20% of the CPU, (or even more depending on load average)
* provide a new schedclk() mechanism at a new clock at schedhz, so high
platform hz values don't cause nice +0 processes to look like they are
niced
* change the algorithm slightly, and reorganize the code a lot
* fix percent-CPU calculation bugs, and eliminate some no-op code
=== nice bug === Correctly divide the scheduler queues between niced and
compute-bound processes. The current nice weight of two (sort of, see
`algorithm change' below) neatly divides the USRPRI queues in half; this
should have been used to clip p_estcpu, instead of UCHAR_MAX. Besides
being the wrong amount, clipping an unsigned char to UCHAR_MAX is a no-op,
and it was done after decay_cpu() which can only _reduce_ the value. It
has to be kept <= NICE_WEIGHT * PRIO_MAX - PPQ or processes can
scheduler-penalize themselves onto the same queue as nice +20 processes.
(Or even a higher one.)
=== New schedclk() mechansism === Some platforms should be cutting down
stathz before hitting the scheduler, since the scheduler algorithm only
works right in the vicinity of 64 Hz. Rather than prescale hz, then scale
back and forth by 4 every time p_estcpu is touched (each occurance an
abstraction violation), use p_estcpu without scaling and require schedhz
to be generated directly at the right frequency. Use a default stathz (well,
actually, profhz) / 4, so nothing changes unless a platform defines schedhz
and a new clock. Define these for alpha, where hz==1024, and nice was
totally broke.
=== Algorithm change === The nice value used to be added to the
exponentially-decayed scheduler history value p_estcpu, in _addition_ to
be incorporated directly (with greater wieght) into the priority calculation.
At first glance, it appears to be a pointless increase of 1/8 the nice
effect (pri = p_estcpu/4 + nice*2), but it's actually at least 3x that
because it will ramp up linearly but be decayed only exponentially, thus
converging to an additional .75 nice for a loadaverage of one. I killed
this, it makes the behavior hard to control, almost impossible to analyze,
and the effect (~~nothing at for the first second, then somewhat increased
niceness after three seconds or more, depending on load average) pointless.
=== Other bugs === hz -> profhz in the p_pctcpu = f(p_cpticks) calcuation.
Collect scheduler functionality. Try to put each abstraction in just one
place.
1999-02-23 05:56:03 +03:00
|
|
|
}
|
2000-09-05 20:27:51 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* suspendsched:
|
|
|
|
*
|
|
|
|
* Convert all non-L_SYSTEM LSSLEEP or LSRUN LWPs to LSSUSPENDED.
|
|
|
|
*/
|
2000-09-05 20:27:51 +04:00
|
|
|
void
|
2007-02-10 00:55:00 +03:00
|
|
|
suspendsched(void)
|
2000-09-05 20:27:51 +04:00
|
|
|
{
|
2007-02-10 00:55:00 +03:00
|
|
|
#ifdef MULTIPROCESSOR
|
|
|
|
CPU_INFO_ITERATOR cii;
|
|
|
|
struct cpu_info *ci;
|
|
|
|
#endif
|
2003-01-18 13:06:22 +03:00
|
|
|
struct lwp *l;
|
2007-02-10 00:55:00 +03:00
|
|
|
struct proc *p;
|
2000-09-05 20:27:51 +04:00
|
|
|
|
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* We do this by process in order not to violate the locking rules.
|
2000-09-05 20:27:51 +04:00
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_enter(&proclist_mutex);
|
|
|
|
PROCLIST_FOREACH(p, &allproc) {
|
|
|
|
mutex_enter(&p->p_smutex);
|
|
|
|
|
2007-02-18 01:31:36 +03:00
|
|
|
if ((p->p_flag & PK_SYSTEM) != 0) {
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_exit(&p->p_smutex);
|
2000-09-05 20:27:51 +04:00
|
|
|
continue;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p->p_stat = SSTOP;
|
|
|
|
|
|
|
|
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
|
|
|
|
if (l == curlwp)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
lwp_lock(l);
|
2003-01-18 13:06:22 +03:00
|
|
|
|
2000-09-23 05:00:35 +04:00
|
|
|
/*
|
2007-02-10 00:55:00 +03:00
|
|
|
* Set L_WREBOOT so that the LWP will suspend itself
|
|
|
|
* when it tries to return to user mode. We want to
|
|
|
|
* try and get to get as many LWPs as possible to
|
|
|
|
* the user / kernel boundary, so that they will
|
|
|
|
* release any locks that they hold.
|
2000-09-23 05:00:35 +04:00
|
|
|
*/
|
2007-02-18 01:31:36 +03:00
|
|
|
l->l_flag |= (LW_WREBOOT | LW_WSUSPEND);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
if (l->l_stat == LSSLEEP &&
|
2007-02-18 01:31:36 +03:00
|
|
|
(l->l_flag & LW_SINTR) != 0) {
|
2007-02-10 00:55:00 +03:00
|
|
|
/* setrunnable() will release the lock. */
|
|
|
|
setrunnable(l);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwp_unlock(l);
|
2000-09-05 20:27:51 +04:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
mutex_exit(&p->p_smutex);
|
2000-09-05 20:27:51 +04:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_exit(&proclist_mutex);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Kick all CPUs to make them preempt any LWPs running in user mode.
|
|
|
|
* They'll trap into the kernel and suspend themselves in userret().
|
|
|
|
*/
|
|
|
|
sched_lock(0);
|
|
|
|
#ifdef MULTIPROCESSOR
|
|
|
|
for (CPU_INFO_FOREACH(cii, ci))
|
|
|
|
cpu_need_resched(ci);
|
|
|
|
#else
|
|
|
|
cpu_need_resched(curcpu());
|
|
|
|
#endif
|
|
|
|
sched_unlock(0);
|
2000-09-05 20:27:51 +04:00
|
|
|
}
|
2002-09-22 09:36:48 +04:00
|
|
|
|
2005-10-06 11:02:13 +04:00
|
|
|
/*
|
|
|
|
* scheduler_fork_hook:
|
|
|
|
*
|
|
|
|
* Inherit the parent's scheduler history.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
scheduler_fork_hook(struct proc *parent, struct proc *child)
|
|
|
|
{
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
LOCK_ASSERT(mutex_owned(&parent->p_smutex));
|
|
|
|
|
2005-12-24 15:57:14 +03:00
|
|
|
child->p_estcpu = child->p_estcpu_inherited = parent->p_estcpu;
|
|
|
|
child->p_forktime = schedcpu_ticks;
|
2005-10-06 11:02:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* scheduler_wait_hook:
|
|
|
|
*
|
|
|
|
* Chargeback parents for the sins of their children.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
scheduler_wait_hook(struct proc *parent, struct proc *child)
|
|
|
|
{
|
2005-12-24 15:57:14 +03:00
|
|
|
fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
|
|
|
|
fixpt_t estcpu;
|
2005-10-06 11:02:13 +04:00
|
|
|
|
|
|
|
/* XXX Only if parent != init?? */
|
2005-12-24 15:57:14 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_spin_enter(&parent->p_stmutex);
|
2005-12-24 15:57:14 +03:00
|
|
|
estcpu = decay_cpu_batch(loadfac, child->p_estcpu_inherited,
|
|
|
|
schedcpu_ticks - child->p_forktime);
|
2007-02-10 00:55:00 +03:00
|
|
|
if (child->p_estcpu > estcpu)
|
2005-12-24 15:57:14 +03:00
|
|
|
parent->p_estcpu =
|
|
|
|
ESTCPULIM(parent->p_estcpu + child->p_estcpu - estcpu);
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_spin_exit(&parent->p_stmutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sched_kpri:
|
|
|
|
*
|
|
|
|
* Scale a priority level to a kernel priority level, usually
|
|
|
|
* for an LWP that is about to sleep.
|
|
|
|
*/
|
2007-02-27 18:07:28 +03:00
|
|
|
pri_t
|
2007-02-10 00:55:00 +03:00
|
|
|
sched_kpri(struct lwp *l)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Scale user priorities (127 -> 50) up to kernel priorities
|
|
|
|
* in the range (49 -> 8). Reserve the top 8 kernel priorities
|
|
|
|
* for high priority kthreads. Kernel priorities passed in
|
|
|
|
* are left "as is". XXX This is somewhat arbitrary.
|
|
|
|
*/
|
|
|
|
static const uint8_t kpri_tab[] = {
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7,
|
|
|
|
8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
16, 17, 18, 19, 20, 21, 22, 23,
|
|
|
|
24, 25, 26, 27, 28, 29, 30, 31,
|
|
|
|
32, 33, 34, 35, 36, 37, 38, 39,
|
|
|
|
40, 41, 42, 43, 44, 45, 46, 47,
|
|
|
|
48, 49, 8, 8, 9, 9, 10, 10,
|
|
|
|
11, 11, 12, 12, 13, 14, 14, 15,
|
|
|
|
15, 16, 16, 17, 17, 18, 18, 19,
|
|
|
|
20, 20, 21, 21, 22, 22, 23, 23,
|
|
|
|
24, 24, 25, 26, 26, 27, 27, 28,
|
|
|
|
28, 29, 29, 30, 30, 31, 32, 32,
|
|
|
|
33, 33, 34, 34, 35, 35, 36, 36,
|
|
|
|
37, 38, 38, 39, 39, 40, 40, 41,
|
|
|
|
41, 42, 42, 43, 44, 44, 45, 45,
|
|
|
|
46, 46, 47, 47, 48, 48, 49, 49,
|
|
|
|
};
|
|
|
|
|
2007-02-27 18:07:28 +03:00
|
|
|
return (pri_t)kpri_tab[l->l_usrpri];
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sched_unsleep:
|
|
|
|
*
|
|
|
|
* The is called when the LWP has not been awoken normally but instead
|
|
|
|
* interrupted: for example, if the sleep timed out. Because of this,
|
|
|
|
* it's not a valid action for running or idle LWPs.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sched_unsleep(struct lwp *l)
|
|
|
|
{
|
|
|
|
|
|
|
|
lwp_unlock(l);
|
|
|
|
panic("sched_unsleep");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sched_changepri:
|
|
|
|
*
|
|
|
|
* Adjust the priority of an LWP.
|
|
|
|
*/
|
|
|
|
void
|
2007-02-27 18:07:28 +03:00
|
|
|
sched_changepri(struct lwp *l, pri_t pri)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
LOCK_ASSERT(lwp_locked(l, &sched_mutex));
|
|
|
|
|
|
|
|
l->l_usrpri = pri;
|
|
|
|
if (l->l_priority < PUSER)
|
|
|
|
return;
|
2007-02-26 12:20:52 +03:00
|
|
|
|
|
|
|
if (l->l_stat != LSRUN || (l->l_flag & LW_INMEM) == 0) {
|
2007-02-10 00:55:00 +03:00
|
|
|
l->l_priority = pri;
|
|
|
|
return;
|
2005-12-24 15:57:14 +03:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
remrunqueue(l);
|
|
|
|
l->l_priority = pri;
|
|
|
|
setrunqueue(l);
|
2007-02-26 12:20:52 +03:00
|
|
|
resched_lwp(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-02-27 18:07:28 +03:00
|
|
|
sched_lendpri(struct lwp *l, pri_t pri)
|
2007-02-26 12:20:52 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
LOCK_ASSERT(lwp_locked(l, &sched_mutex));
|
|
|
|
|
|
|
|
if (l->l_stat != LSRUN || (l->l_flag & LW_INMEM) == 0) {
|
|
|
|
l->l_inheritedprio = pri;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
remrunqueue(l);
|
|
|
|
l->l_inheritedprio = pri;
|
|
|
|
setrunqueue(l);
|
|
|
|
resched_lwp(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct lwp *
|
|
|
|
syncobj_noowner(wchan_t wchan)
|
|
|
|
{
|
|
|
|
|
|
|
|
return NULL;
|
2005-10-06 11:02:13 +04:00
|
|
|
}
|
|
|
|
|
2002-09-22 09:36:48 +04:00
|
|
|
/*
|
|
|
|
* Low-level routines to access the run queue. Optimised assembler
|
|
|
|
* routines can override these.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __HAVE_MD_RUNQUEUE
|
|
|
|
|
2003-07-18 05:02:31 +04:00
|
|
|
/*
|
|
|
|
* On some architectures, it's faster to use a MSB ordering for the priorites
|
|
|
|
* than the traditional LSB ordering.
|
|
|
|
*/
|
|
|
|
#ifdef __HAVE_BIGENDIAN_BITOPS
|
|
|
|
#define RQMASK(n) (0x80000000 >> (n))
|
|
|
|
#else
|
|
|
|
#define RQMASK(n) (0x00000001 << (n))
|
|
|
|
#endif
|
|
|
|
|
2003-06-26 06:09:27 +04:00
|
|
|
/*
|
2002-11-03 16:59:12 +03:00
|
|
|
* The primitives that manipulate the run queues. whichqs tells which
|
|
|
|
* of the 32 queues qs have processes in them. Setrunqueue puts processes
|
|
|
|
* into queues, remrunqueue removes them from queues. The running process is
|
|
|
|
* on no queue, other processes are on a queue related to p->p_priority,
|
|
|
|
* divided by 4 actually to shrink the 0-127 range of priorities into the 32
|
|
|
|
* available queues.
|
2003-06-26 06:09:27 +04:00
|
|
|
*/
|
2004-12-10 00:52:24 +03:00
|
|
|
#ifdef RQDEBUG
|
|
|
|
static void
|
|
|
|
checkrunqueue(int whichq, struct lwp *l)
|
|
|
|
{
|
|
|
|
const struct prochd * const rq = &sched_qs[whichq];
|
|
|
|
struct lwp *l2;
|
|
|
|
int found = 0;
|
|
|
|
int die = 0;
|
|
|
|
int empty = 1;
|
2006-08-17 21:11:27 +04:00
|
|
|
for (l2 = rq->ph_link; l2 != (const void*) rq; l2 = l2->l_forw) {
|
2004-12-10 00:52:24 +03:00
|
|
|
if (l2->l_stat != LSRUN) {
|
|
|
|
printf("checkrunqueue[%d]: lwp %p state (%d) "
|
|
|
|
" != LSRUN\n", whichq, l2, l2->l_stat);
|
|
|
|
}
|
|
|
|
if (l2->l_back->l_forw != l2) {
|
|
|
|
printf("checkrunqueue[%d]: lwp %p back-qptr (%p) "
|
|
|
|
"corrupt %p\n", whichq, l2, l2->l_back,
|
|
|
|
l2->l_back->l_forw);
|
|
|
|
die = 1;
|
|
|
|
}
|
|
|
|
if (l2->l_forw->l_back != l2) {
|
|
|
|
printf("checkrunqueue[%d]: lwp %p forw-qptr (%p) "
|
|
|
|
"corrupt %p\n", whichq, l2, l2->l_forw,
|
|
|
|
l2->l_forw->l_back);
|
|
|
|
die = 1;
|
|
|
|
}
|
|
|
|
if (l2 == l)
|
|
|
|
found = 1;
|
|
|
|
empty = 0;
|
|
|
|
}
|
|
|
|
if (empty && (sched_whichqs & RQMASK(whichq)) != 0) {
|
|
|
|
printf("checkrunqueue[%d]: bit set for empty run-queue %p\n",
|
|
|
|
whichq, rq);
|
|
|
|
die = 1;
|
|
|
|
} else if (!empty && (sched_whichqs & RQMASK(whichq)) == 0) {
|
|
|
|
printf("checkrunqueue[%d]: bit clear for non-empty "
|
|
|
|
"run-queue %p\n", whichq, rq);
|
|
|
|
die = 1;
|
|
|
|
}
|
|
|
|
if (l != NULL && (sched_whichqs & RQMASK(whichq)) == 0) {
|
|
|
|
printf("checkrunqueue[%d]: bit clear for active lwp %p\n",
|
|
|
|
whichq, l);
|
|
|
|
die = 1;
|
|
|
|
}
|
|
|
|
if (l != NULL && empty) {
|
|
|
|
printf("checkrunqueue[%d]: empty run-queue %p with "
|
|
|
|
"active lwp %p\n", whichq, rq, l);
|
|
|
|
die = 1;
|
|
|
|
}
|
|
|
|
if (l != NULL && !found) {
|
|
|
|
printf("checkrunqueue[%d]: lwp %p not in runqueue %p!",
|
|
|
|
whichq, l, rq);
|
|
|
|
die = 1;
|
|
|
|
}
|
|
|
|
if (die)
|
|
|
|
panic("checkrunqueue: inconsistency found");
|
|
|
|
}
|
|
|
|
#endif /* RQDEBUG */
|
|
|
|
|
2002-09-22 09:36:48 +04:00
|
|
|
void
|
2003-01-18 13:06:22 +03:00
|
|
|
setrunqueue(struct lwp *l)
|
2002-09-22 09:36:48 +04:00
|
|
|
{
|
|
|
|
struct prochd *rq;
|
2003-01-18 13:06:22 +03:00
|
|
|
struct lwp *prev;
|
2007-02-26 12:20:52 +03:00
|
|
|
const int whichq = lwp_eprio(l) / PPQ;
|
2002-09-22 09:36:48 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
LOCK_ASSERT(lwp_locked(l, &sched_mutex));
|
|
|
|
|
2004-12-10 00:52:24 +03:00
|
|
|
#ifdef RQDEBUG
|
|
|
|
checkrunqueue(whichq, NULL);
|
|
|
|
#endif
|
2002-09-22 09:36:48 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
2007-02-10 00:55:00 +03:00
|
|
|
if (l->l_back != NULL || l->l_stat != LSRUN)
|
2002-09-22 09:36:48 +04:00
|
|
|
panic("setrunqueue");
|
|
|
|
#endif
|
2003-07-18 05:02:31 +04:00
|
|
|
sched_whichqs |= RQMASK(whichq);
|
2002-09-22 09:36:48 +04:00
|
|
|
rq = &sched_qs[whichq];
|
|
|
|
prev = rq->ph_rlink;
|
2003-01-18 13:06:22 +03:00
|
|
|
l->l_forw = (struct lwp *)rq;
|
|
|
|
rq->ph_rlink = l;
|
|
|
|
prev->l_forw = l;
|
|
|
|
l->l_back = prev;
|
2004-12-10 00:52:24 +03:00
|
|
|
#ifdef RQDEBUG
|
|
|
|
checkrunqueue(whichq, l);
|
|
|
|
#endif
|
2002-09-22 09:36:48 +04:00
|
|
|
}
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* XXXSMP When LWP dispatch (cpu_switch()) is changed to use remrunqueue(),
|
|
|
|
* drop of the effective priority level from kernel to user needs to be
|
|
|
|
* moved here from userret(). The assignment in userret() is currently
|
|
|
|
* done unlocked.
|
|
|
|
*/
|
2002-09-22 09:36:48 +04:00
|
|
|
void
|
2003-01-18 13:06:22 +03:00
|
|
|
remrunqueue(struct lwp *l)
|
2002-09-22 09:36:48 +04:00
|
|
|
{
|
2003-01-18 13:06:22 +03:00
|
|
|
struct lwp *prev, *next;
|
2007-02-26 12:20:52 +03:00
|
|
|
const int whichq = lwp_eprio(l) / PPQ;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
LOCK_ASSERT(lwp_locked(l, &sched_mutex));
|
|
|
|
|
2004-12-10 00:52:24 +03:00
|
|
|
#ifdef RQDEBUG
|
|
|
|
checkrunqueue(whichq, l);
|
|
|
|
#endif
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
#if defined(DIAGNOSTIC)
|
|
|
|
if (((sched_whichqs & RQMASK(whichq)) == 0) || l->l_back == NULL) {
|
|
|
|
/* Shouldn't happen - interrupts disabled. */
|
2004-12-10 00:52:24 +03:00
|
|
|
panic("remrunqueue: bit %d not set", whichq);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2002-09-22 09:36:48 +04:00
|
|
|
#endif
|
2003-01-18 13:06:22 +03:00
|
|
|
prev = l->l_back;
|
|
|
|
l->l_back = NULL;
|
|
|
|
next = l->l_forw;
|
|
|
|
prev->l_forw = next;
|
|
|
|
next->l_back = prev;
|
2002-09-22 09:36:48 +04:00
|
|
|
if (prev == next)
|
2003-07-18 05:02:31 +04:00
|
|
|
sched_whichqs &= ~RQMASK(whichq);
|
2004-12-10 00:52:24 +03:00
|
|
|
#ifdef RQDEBUG
|
|
|
|
checkrunqueue(whichq, NULL);
|
|
|
|
#endif
|
2002-09-22 09:36:48 +04:00
|
|
|
}
|
|
|
|
|
2003-07-18 05:02:31 +04:00
|
|
|
#undef RQMASK
|
|
|
|
#endif /* !defined(__HAVE_MD_RUNQUEUE) */
|