2002-10-12 05:32:24 +00: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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University 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 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.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-10-12 05:32:24 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/ktr.h>
|
|
|
|
#include <sys/lock.h>
|
2003-12-26 17:07:29 +00:00
|
|
|
#include <sys/kthread.h>
|
2002-10-12 05:32:24 +00:00
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/sched.h>
|
|
|
|
#include <sys/smp.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/sx.h>
|
|
|
|
|
2004-01-25 08:00:04 +00:00
|
|
|
#define KTR_4BSD 0x0
|
|
|
|
|
2002-11-21 09:14:13 +00:00
|
|
|
/*
|
|
|
|
* INVERSE_ESTCPU_WEIGHT is only suitable for statclock() frequencies in
|
|
|
|
* the range 100-256 Hz (approximately).
|
|
|
|
*/
|
|
|
|
#define ESTCPULIM(e) \
|
|
|
|
min((e), INVERSE_ESTCPU_WEIGHT * (NICE_WEIGHT * (PRIO_MAX - PRIO_MIN) - \
|
|
|
|
RQ_PPQ) + INVERSE_ESTCPU_WEIGHT - 1)
|
2003-11-09 13:45:54 +00:00
|
|
|
#ifdef SMP
|
|
|
|
#define INVERSE_ESTCPU_WEIGHT (8 * smp_cpus)
|
|
|
|
#else
|
2002-11-21 09:14:13 +00:00
|
|
|
#define INVERSE_ESTCPU_WEIGHT 8 /* 1 / (priorities per estcpu level). */
|
2003-11-09 13:45:54 +00:00
|
|
|
#endif
|
2002-11-21 09:14:13 +00:00
|
|
|
#define NICE_WEIGHT 1 /* Priorities per nice level. */
|
|
|
|
|
2003-01-12 19:04:49 +00:00
|
|
|
struct ke_sched {
|
2004-01-25 08:00:04 +00:00
|
|
|
int ske_cpticks; /* (j) Ticks of cpu time. */
|
|
|
|
struct runq *ske_runq; /* runq the kse is currently on */
|
2003-01-12 19:04:49 +00:00
|
|
|
};
|
2004-01-25 08:00:04 +00:00
|
|
|
#define ke_runq ke_sched->ske_runq
|
|
|
|
#define KEF_BOUND KEF_SCHED1
|
|
|
|
|
|
|
|
#define SKE_RUNQ_PCPU(ke) \
|
|
|
|
((ke)->ke_runq != 0 && (ke)->ke_runq != &runq)
|
2003-01-12 19:04:49 +00:00
|
|
|
|
2004-01-25 08:00:04 +00:00
|
|
|
/*
|
|
|
|
* KSE_CAN_MIGRATE macro returns true if the kse can migrate between
|
|
|
|
* cpus. Currently ithread cpu binding is disabled on x86 due to a
|
|
|
|
* bug in the Xeon round-robin interrupt delivery that delivers all
|
|
|
|
* interrupts to cpu 0.
|
|
|
|
*/
|
|
|
|
#ifdef __i386__
|
|
|
|
#define KSE_CAN_MIGRATE(ke) \
|
|
|
|
((ke)->ke_thread->td_pinned == 0 && ((ke)->ke_flags & KEF_BOUND) == 0)
|
|
|
|
#else
|
|
|
|
#define KSE_CAN_MIGRATE(ke) \
|
|
|
|
PRI_BASE((ke)->ke_ksegrp->kg_pri_class) != PRI_ITHD && \
|
|
|
|
((ke)->ke_thread->td_pinned == 0 &&((ke)->ke_flags & KEF_BOUND) == 0)
|
|
|
|
#endif
|
2003-04-30 12:57:40 +00:00
|
|
|
static struct ke_sched ke_sched;
|
2003-01-12 19:04:49 +00:00
|
|
|
|
|
|
|
struct ke_sched *kse0_sched = &ke_sched;
|
2002-11-21 01:22:38 +00:00
|
|
|
struct kg_sched *ksegrp0_sched = NULL;
|
|
|
|
struct p_sched *proc0_sched = NULL;
|
|
|
|
struct td_sched *thread0_sched = NULL;
|
2002-10-12 05:32:24 +00:00
|
|
|
|
|
|
|
static int sched_quantum; /* Roundrobin scheduling quantum in ticks. */
|
2003-03-24 15:16:21 +00:00
|
|
|
#define SCHED_QUANTUM (hz / 10) /* Default sched quantum */
|
2002-10-12 05:32:24 +00:00
|
|
|
|
|
|
|
static struct callout roundrobin_callout;
|
|
|
|
|
2004-01-25 08:00:04 +00:00
|
|
|
static void setup_runqs(void);
|
2002-10-12 05:32:24 +00:00
|
|
|
static void roundrobin(void *arg);
|
2003-12-26 17:07:29 +00:00
|
|
|
static void schedcpu(void);
|
2004-01-25 08:00:04 +00:00
|
|
|
static void schedcpu_thread(void);
|
2002-10-12 05:32:24 +00:00
|
|
|
static void sched_setup(void *dummy);
|
|
|
|
static void maybe_resched(struct thread *td);
|
|
|
|
static void updatepri(struct ksegrp *kg);
|
|
|
|
static void resetpriority(struct ksegrp *kg);
|
|
|
|
|
2004-01-25 08:00:04 +00:00
|
|
|
static struct kproc_desc sched_kp = {
|
|
|
|
"schedcpu",
|
|
|
|
schedcpu_thread,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
SYSINIT(schedcpu, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, kproc_start, &sched_kp)
|
|
|
|
SYSINIT(sched_setup, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, sched_setup, NULL)
|
2002-10-12 05:32:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Global run queue.
|
|
|
|
*/
|
|
|
|
static struct runq runq;
|
2004-01-25 08:00:04 +00:00
|
|
|
|
|
|
|
#ifdef SMP
|
|
|
|
/*
|
|
|
|
* Per-CPU run queues
|
|
|
|
*/
|
|
|
|
static struct runq runq_pcpu[MAXCPU];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_runqs(void)
|
|
|
|
{
|
|
|
|
#ifdef SMP
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAXCPU; ++i)
|
|
|
|
runq_init(&runq_pcpu[i]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
runq_init(&runq);
|
|
|
|
}
|
2002-10-12 05:32:24 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
sysctl_kern_quantum(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
int error, new_val;
|
|
|
|
|
|
|
|
new_val = sched_quantum * tick;
|
|
|
|
error = sysctl_handle_int(oidp, &new_val, 0, req);
|
|
|
|
if (error != 0 || req->newptr == NULL)
|
|
|
|
return (error);
|
|
|
|
if (new_val < tick)
|
|
|
|
return (EINVAL);
|
|
|
|
sched_quantum = new_val / tick;
|
|
|
|
hogticks = 2 * sched_quantum;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW,
|
|
|
|
0, sizeof sched_quantum, sysctl_kern_quantum, "I",
|
|
|
|
"Roundrobin scheduling quantum in microseconds");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Arrange to reschedule if necessary, taking the priorities and
|
|
|
|
* schedulers into account.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
maybe_resched(struct thread *td)
|
|
|
|
{
|
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2002-12-28 01:23:07 +00:00
|
|
|
if (td->td_priority < curthread->td_priority && curthread->td_kse)
|
2003-02-17 09:55:10 +00:00
|
|
|
curthread->td_flags |= TDF_NEEDRESCHED;
|
2002-10-12 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Force switch among equal priority processes every 100ms.
|
|
|
|
* We don't actually need to force a context switch of the current process.
|
|
|
|
* The act of firing the event triggers a context switch to softclock() and
|
|
|
|
* then switching back out again which is equivalent to a preemption, thus
|
|
|
|
* no further work is needed on the local CPU.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
|
|
|
roundrobin(void *arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef SMP
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
forward_roundrobin();
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
callout_reset(&roundrobin_callout, sched_quantum, roundrobin, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constants for digital decay and forget:
|
2003-08-15 21:29:06 +00:00
|
|
|
* 90% of (kg_estcpu) usage in 5 * loadav time
|
|
|
|
* 95% of (ke_pctcpu) usage in 60 seconds (load insensitive)
|
2002-10-12 05:32:24 +00:00
|
|
|
* Note that, as ps(1) mentions, this can let percentages
|
|
|
|
* total over 100% (I've seen 137.9% for 3 processes).
|
|
|
|
*
|
2003-08-15 21:29:06 +00:00
|
|
|
* Note that schedclock() updates kg_estcpu and p_cpticks asynchronously.
|
2002-10-12 05:32:24 +00:00
|
|
|
*
|
2003-08-15 21:29:06 +00:00
|
|
|
* We wish to decay away 90% of kg_estcpu in (5 * loadavg) seconds.
|
2002-10-12 05:32:24 +00:00
|
|
|
* That is, the system wants to compute a value of decay such
|
|
|
|
* that the following for loop:
|
|
|
|
* for (i = 0; i < (5 * loadavg); i++)
|
2003-08-15 21:29:06 +00:00
|
|
|
* kg_estcpu *= decay;
|
2002-10-12 05:32:24 +00:00
|
|
|
* will compute
|
2003-08-15 21:29:06 +00:00
|
|
|
* kg_estcpu *= 0.1;
|
2002-10-12 05:32:24 +00:00
|
|
|
* 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)
|
|
|
|
*
|
|
|
|
* 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))
|
|
|
|
#define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE))
|
|
|
|
|
2003-08-15 21:29:06 +00:00
|
|
|
/* decay 95% of `ke_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
|
2002-10-12 05:32:24 +00:00
|
|
|
static fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */
|
|
|
|
SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 don't 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
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recompute process priorities, every hz ticks.
|
|
|
|
* MP-safe, called without the Giant mutex.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
2003-12-26 17:07:29 +00:00
|
|
|
schedcpu(void)
|
2002-10-12 05:32:24 +00:00
|
|
|
{
|
|
|
|
register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
|
|
|
|
struct thread *td;
|
|
|
|
struct proc *p;
|
|
|
|
struct kse *ke;
|
|
|
|
struct ksegrp *kg;
|
2003-08-15 21:29:06 +00:00
|
|
|
int awake, realstathz;
|
2002-10-12 05:32:24 +00:00
|
|
|
|
|
|
|
realstathz = stathz ? stathz : hz;
|
|
|
|
sx_slock(&allproc_lock);
|
|
|
|
FOREACH_PROC_IN_SYSTEM(p) {
|
2003-08-15 21:29:06 +00:00
|
|
|
/*
|
|
|
|
* Prevent state changes and protect run queue.
|
|
|
|
*/
|
2002-10-12 05:32:24 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2003-08-15 21:29:06 +00:00
|
|
|
/*
|
|
|
|
* Increment time in/out of memory. We ignore overflow; with
|
|
|
|
* 16-bit int's (remember them?) overflow takes 45 days.
|
|
|
|
*/
|
2002-10-12 05:32:24 +00:00
|
|
|
p->p_swtime++;
|
|
|
|
FOREACH_KSEGRP_IN_PROC(p, kg) {
|
|
|
|
awake = 0;
|
|
|
|
FOREACH_KSE_IN_GROUP(kg, ke) {
|
|
|
|
/*
|
2003-08-15 21:29:06 +00:00
|
|
|
* Increment sleep time (if sleeping). We
|
|
|
|
* ignore overflow, as above.
|
2002-10-12 05:32:24 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The kse slptimes are not touched in wakeup
|
|
|
|
* because the thread may not HAVE a KSE.
|
|
|
|
*/
|
|
|
|
if (ke->ke_state == KES_ONRUNQ) {
|
|
|
|
awake = 1;
|
|
|
|
ke->ke_flags &= ~KEF_DIDRUN;
|
|
|
|
} else if ((ke->ke_state == KES_THREAD) &&
|
|
|
|
(TD_IS_RUNNING(ke->ke_thread))) {
|
|
|
|
awake = 1;
|
|
|
|
/* Do not clear KEF_DIDRUN */
|
|
|
|
} else if (ke->ke_flags & KEF_DIDRUN) {
|
|
|
|
awake = 1;
|
|
|
|
ke->ke_flags &= ~KEF_DIDRUN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-08-15 21:29:06 +00:00
|
|
|
* ke_pctcpu is only for ps and ttyinfo().
|
|
|
|
* Do it per kse, and add them up at the end?
|
2002-10-12 05:32:24 +00:00
|
|
|
* XXXKSE
|
|
|
|
*/
|
2003-08-15 21:29:06 +00:00
|
|
|
ke->ke_pctcpu = (ke->ke_pctcpu * ccpu) >>
|
2003-01-12 19:04:49 +00:00
|
|
|
FSHIFT;
|
2002-10-12 05:32:24 +00:00
|
|
|
/*
|
|
|
|
* If the kse has been idle the entire second,
|
|
|
|
* stop recalculating its priority until
|
|
|
|
* it wakes up.
|
|
|
|
*/
|
2003-01-12 19:04:49 +00:00
|
|
|
if (ke->ke_sched->ske_cpticks == 0)
|
2002-10-12 05:32:24 +00:00
|
|
|
continue;
|
|
|
|
#if (FSHIFT >= CCPU_SHIFT)
|
2003-01-13 03:42:41 +00:00
|
|
|
ke->ke_pctcpu += (realstathz == 100)
|
2003-01-12 19:04:49 +00:00
|
|
|
? ((fixpt_t) ke->ke_sched->ske_cpticks) <<
|
2002-10-12 05:32:24 +00:00
|
|
|
(FSHIFT - CCPU_SHIFT) :
|
2003-01-12 19:04:49 +00:00
|
|
|
100 * (((fixpt_t) ke->ke_sched->ske_cpticks)
|
|
|
|
<< (FSHIFT - CCPU_SHIFT)) / realstathz;
|
2002-10-12 05:32:24 +00:00
|
|
|
#else
|
2003-01-13 03:42:41 +00:00
|
|
|
ke->ke_pctcpu += ((FSCALE - ccpu) *
|
2003-01-12 19:04:49 +00:00
|
|
|
(ke->ke_sched->ske_cpticks *
|
|
|
|
FSCALE / realstathz)) >> FSHIFT;
|
2002-10-12 05:32:24 +00:00
|
|
|
#endif
|
2003-01-12 19:04:49 +00:00
|
|
|
ke->ke_sched->ske_cpticks = 0;
|
2002-10-12 05:32:24 +00:00
|
|
|
} /* end of kse loop */
|
|
|
|
/*
|
|
|
|
* If there are ANY running threads in this KSEGRP,
|
|
|
|
* then don't count it as sleeping.
|
|
|
|
*/
|
|
|
|
if (awake) {
|
|
|
|
if (kg->kg_slptime > 1) {
|
|
|
|
/*
|
|
|
|
* In an ideal world, this should not
|
|
|
|
* happen, because whoever woke us
|
|
|
|
* up from the long sleep should have
|
|
|
|
* unwound the slptime and reset our
|
|
|
|
* priority before we run at the stale
|
|
|
|
* priority. Should KASSERT at some
|
|
|
|
* point when all the cases are fixed.
|
|
|
|
*/
|
|
|
|
updatepri(kg);
|
|
|
|
}
|
|
|
|
kg->kg_slptime = 0;
|
2003-08-15 21:29:06 +00:00
|
|
|
} else
|
2002-10-12 05:32:24 +00:00
|
|
|
kg->kg_slptime++;
|
|
|
|
if (kg->kg_slptime > 1)
|
|
|
|
continue;
|
|
|
|
kg->kg_estcpu = decay_cpu(loadfac, kg->kg_estcpu);
|
|
|
|
resetpriority(kg);
|
|
|
|
FOREACH_THREAD_IN_GROUP(kg, td) {
|
|
|
|
if (td->td_priority >= PUSER) {
|
2002-10-14 20:34:31 +00:00
|
|
|
sched_prio(td, kg->kg_user_pri);
|
2002-10-12 05:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* end of ksegrp loop */
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
} /* end of process loop */
|
|
|
|
sx_sunlock(&allproc_lock);
|
2003-12-26 17:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main loop for a kthread that executes schedcpu once a second.
|
|
|
|
*/
|
|
|
|
static void
|
2004-01-25 08:00:04 +00:00
|
|
|
schedcpu_thread(void)
|
2003-12-26 17:07:29 +00:00
|
|
|
{
|
|
|
|
int nowake;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
schedcpu();
|
|
|
|
tsleep(&nowake, curthread->td_priority, "-", hz);
|
|
|
|
}
|
2002-10-12 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recalculate the priority of a process after it has slept for a while.
|
2003-08-15 21:29:06 +00:00
|
|
|
* For all load averages >= 1 and max kg_estcpu of 255, sleeping for at
|
|
|
|
* least six times the loadfactor will decay kg_estcpu to zero.
|
2002-10-12 05:32:24 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
updatepri(struct ksegrp *kg)
|
|
|
|
{
|
2003-08-15 21:29:06 +00:00
|
|
|
register fixpt_t loadfac;
|
2002-10-12 05:32:24 +00:00
|
|
|
register unsigned int newcpu;
|
|
|
|
|
2003-08-15 21:29:06 +00:00
|
|
|
loadfac = loadfactor(averunnable.ldavg[0]);
|
2002-10-12 05:32:24 +00:00
|
|
|
if (kg->kg_slptime > 5 * loadfac)
|
|
|
|
kg->kg_estcpu = 0;
|
|
|
|
else {
|
2003-08-15 21:29:06 +00:00
|
|
|
newcpu = kg->kg_estcpu;
|
|
|
|
kg->kg_slptime--; /* was incremented in schedcpu() */
|
2002-10-12 05:32:24 +00:00
|
|
|
while (newcpu && --kg->kg_slptime)
|
|
|
|
newcpu = decay_cpu(loadfac, newcpu);
|
|
|
|
kg->kg_estcpu = newcpu;
|
|
|
|
}
|
|
|
|
resetpriority(kg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
resetpriority(struct ksegrp *kg)
|
|
|
|
{
|
|
|
|
register unsigned int newpriority;
|
|
|
|
struct thread *td;
|
|
|
|
|
|
|
|
if (kg->kg_pri_class == PRI_TIMESHARE) {
|
|
|
|
newpriority = PUSER + kg->kg_estcpu / INVERSE_ESTCPU_WEIGHT +
|
|
|
|
NICE_WEIGHT * (kg->kg_nice - PRIO_MIN);
|
|
|
|
newpriority = min(max(newpriority, PRI_MIN_TIMESHARE),
|
|
|
|
PRI_MAX_TIMESHARE);
|
|
|
|
kg->kg_user_pri = newpriority;
|
|
|
|
}
|
|
|
|
FOREACH_THREAD_IN_GROUP(kg, td) {
|
|
|
|
maybe_resched(td); /* XXXKSE silly */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
|
|
|
sched_setup(void *dummy)
|
|
|
|
{
|
2004-01-25 08:00:04 +00:00
|
|
|
setup_runqs();
|
2003-08-15 21:29:06 +00:00
|
|
|
|
2002-10-12 05:32:24 +00:00
|
|
|
if (sched_quantum == 0)
|
|
|
|
sched_quantum = SCHED_QUANTUM;
|
|
|
|
hogticks = 2 * sched_quantum;
|
|
|
|
|
|
|
|
callout_init(&roundrobin_callout, 0);
|
|
|
|
|
|
|
|
/* Kick off timeout driven events by calling first time. */
|
|
|
|
roundrobin(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* External interfaces start here */
|
|
|
|
int
|
|
|
|
sched_runnable(void)
|
|
|
|
{
|
2004-01-25 08:00:04 +00:00
|
|
|
#ifdef SMP
|
|
|
|
return runq_check(&runq) + runq_check(&runq_pcpu[PCPU_GET(cpuid)]);
|
|
|
|
#else
|
|
|
|
return runq_check(&runq);
|
|
|
|
#endif
|
2002-10-12 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sched_rr_interval(void)
|
|
|
|
{
|
|
|
|
if (sched_quantum == 0)
|
|
|
|
sched_quantum = SCHED_QUANTUM;
|
|
|
|
return (sched_quantum);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We adjust the priority of the current process. The priority of
|
|
|
|
* a process gets worse as it accumulates CPU time. The cpu usage
|
2003-08-15 21:29:06 +00:00
|
|
|
* estimator (kg_estcpu) is increased here. resetpriority() will
|
|
|
|
* compute a different priority each time kg_estcpu increases by
|
2002-10-12 05:32:24 +00:00
|
|
|
* INVERSE_ESTCPU_WEIGHT
|
|
|
|
* (until MAXPRI is reached). The cpu usage estimator ramps up
|
|
|
|
* quite quickly when the process is running (linearly), and decays
|
|
|
|
* away exponentially, at a rate which is proportionally slower when
|
|
|
|
* the system is busy. The basic principle is that the system will
|
|
|
|
* 90% forget that the process used a lot 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.
|
|
|
|
*/
|
|
|
|
void
|
2003-10-16 08:39:15 +00:00
|
|
|
sched_clock(struct thread *td)
|
2002-10-12 05:32:24 +00:00
|
|
|
{
|
|
|
|
struct ksegrp *kg;
|
2003-10-16 08:39:15 +00:00
|
|
|
struct kse *ke;
|
2003-04-11 03:39:48 +00:00
|
|
|
|
2003-04-23 18:51:05 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2003-10-16 08:39:15 +00:00
|
|
|
kg = td->td_ksegrp;
|
|
|
|
ke = td->td_kse;
|
2002-10-12 05:32:24 +00:00
|
|
|
|
2003-01-12 19:04:49 +00:00
|
|
|
ke->ke_sched->ske_cpticks++;
|
2002-10-12 05:32:24 +00:00
|
|
|
kg->kg_estcpu = ESTCPULIM(kg->kg_estcpu + 1);
|
|
|
|
if ((kg->kg_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) {
|
|
|
|
resetpriority(kg);
|
|
|
|
if (td->td_priority >= PUSER)
|
|
|
|
td->td_priority = kg->kg_user_pri;
|
|
|
|
}
|
|
|
|
}
|
2003-08-15 21:29:06 +00:00
|
|
|
|
2002-10-12 05:32:24 +00:00
|
|
|
/*
|
|
|
|
* charge childs scheduling cpu usage to parent.
|
|
|
|
*
|
|
|
|
* XXXKSE assume only one thread & kse & ksegrp keep estcpu in each ksegrp.
|
|
|
|
* Charge it to the ksegrp that did the wait since process estcpu is sum of
|
|
|
|
* all ksegrps, this is strictly as expected. Assume that the child process
|
|
|
|
* aggregated all the estcpu into the 'built-in' ksegrp.
|
|
|
|
*/
|
|
|
|
void
|
2003-04-11 03:39:48 +00:00
|
|
|
sched_exit(struct proc *p, struct proc *p1)
|
|
|
|
{
|
|
|
|
sched_exit_kse(FIRST_KSE_IN_PROC(p), FIRST_KSE_IN_PROC(p1));
|
|
|
|
sched_exit_ksegrp(FIRST_KSEGRP_IN_PROC(p), FIRST_KSEGRP_IN_PROC(p1));
|
|
|
|
sched_exit_thread(FIRST_THREAD_IN_PROC(p), FIRST_THREAD_IN_PROC(p1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sched_exit_kse(struct kse *ke, struct kse *child)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sched_exit_ksegrp(struct ksegrp *kg, struct ksegrp *child)
|
2002-10-12 05:32:24 +00:00
|
|
|
{
|
2003-04-23 18:51:05 +00:00
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2002-10-12 05:32:24 +00:00
|
|
|
kg->kg_estcpu = ESTCPULIM(kg->kg_estcpu + child->kg_estcpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-04-11 03:39:48 +00:00
|
|
|
sched_exit_thread(struct thread *td, struct thread *child)
|
2002-10-12 05:32:24 +00:00
|
|
|
{
|
2003-04-11 03:39:48 +00:00
|
|
|
}
|
2003-01-12 19:04:49 +00:00
|
|
|
|
2003-04-11 03:39:48 +00:00
|
|
|
void
|
|
|
|
sched_fork(struct proc *p, struct proc *p1)
|
|
|
|
{
|
|
|
|
sched_fork_kse(FIRST_KSE_IN_PROC(p), FIRST_KSE_IN_PROC(p1));
|
|
|
|
sched_fork_ksegrp(FIRST_KSEGRP_IN_PROC(p), FIRST_KSEGRP_IN_PROC(p1));
|
|
|
|
sched_fork_thread(FIRST_THREAD_IN_PROC(p), FIRST_THREAD_IN_PROC(p1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sched_fork_kse(struct kse *ke, struct kse *child)
|
|
|
|
{
|
|
|
|
child->ke_sched->ske_cpticks = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sched_fork_ksegrp(struct ksegrp *kg, struct ksegrp *child)
|
|
|
|
{
|
2003-04-23 18:51:05 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2002-10-12 05:32:24 +00:00
|
|
|
child->kg_estcpu = kg->kg_estcpu;
|
2003-04-11 03:39:48 +00:00
|
|
|
}
|
2003-01-12 19:04:49 +00:00
|
|
|
|
2003-04-11 03:39:48 +00:00
|
|
|
void
|
|
|
|
sched_fork_thread(struct thread *td, struct thread *child)
|
|
|
|
{
|
2002-10-12 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sched_nice(struct ksegrp *kg, int nice)
|
|
|
|
{
|
2003-04-22 20:50:38 +00:00
|
|
|
|
|
|
|
PROC_LOCK_ASSERT(kg->kg_proc, MA_OWNED);
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2002-10-12 05:32:24 +00:00
|
|
|
kg->kg_nice = nice;
|
|
|
|
resetpriority(kg);
|
|
|
|
}
|
|
|
|
|
2003-04-11 03:39:48 +00:00
|
|
|
void
|
|
|
|
sched_class(struct ksegrp *kg, int class)
|
|
|
|
{
|
2003-04-23 18:51:05 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2003-04-11 03:39:48 +00:00
|
|
|
kg->kg_pri_class = class;
|
|
|
|
}
|
|
|
|
|
2002-10-14 20:34:31 +00:00
|
|
|
/*
|
|
|
|
* Adjust the priority of a thread.
|
|
|
|
* This may include moving the thread within the KSEGRP,
|
|
|
|
* changing the assignment of a kse to the thread,
|
|
|
|
* and moving a KSE in the system run queue.
|
|
|
|
*/
|
2002-10-12 05:32:24 +00:00
|
|
|
void
|
|
|
|
sched_prio(struct thread *td, u_char prio)
|
|
|
|
{
|
|
|
|
|
2003-04-23 18:51:05 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2002-10-12 05:32:24 +00:00
|
|
|
if (TD_ON_RUNQ(td)) {
|
2002-10-14 20:34:31 +00:00
|
|
|
adjustrunqueue(td, prio);
|
|
|
|
} else {
|
|
|
|
td->td_priority = prio;
|
2002-10-12 05:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sched_sleep(struct thread *td, u_char prio)
|
|
|
|
{
|
2003-04-23 18:51:05 +00:00
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2002-10-12 05:32:24 +00:00
|
|
|
td->td_ksegrp->kg_slptime = 0;
|
|
|
|
td->td_priority = prio;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-10-16 08:53:46 +00:00
|
|
|
sched_switch(struct thread *td)
|
2002-10-12 05:32:24 +00:00
|
|
|
{
|
2003-10-16 08:53:46 +00:00
|
|
|
struct thread *newtd;
|
2002-10-12 05:32:24 +00:00
|
|
|
struct kse *ke;
|
|
|
|
struct proc *p;
|
|
|
|
|
|
|
|
ke = td->td_kse;
|
|
|
|
p = td->td_proc;
|
|
|
|
|
2003-04-23 18:51:05 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2004-01-25 08:21:46 +00:00
|
|
|
KASSERT((ke->ke_state == KES_THREAD), ("sched_switch: kse state?"));
|
2002-10-12 05:32:24 +00:00
|
|
|
|
2003-04-10 17:35:44 +00:00
|
|
|
td->td_lastcpu = td->td_oncpu;
|
2002-10-14 20:34:31 +00:00
|
|
|
td->td_last_kse = ke;
|
2003-04-10 17:35:44 +00:00
|
|
|
td->td_oncpu = NOCPU;
|
2003-02-17 09:55:10 +00:00
|
|
|
td->td_flags &= ~TDF_NEEDRESCHED;
|
2002-10-12 05:32:24 +00:00
|
|
|
/*
|
|
|
|
* At the last moment, if this thread is still marked RUNNING,
|
|
|
|
* then put it back on the run queue as it has not been suspended
|
|
|
|
* or stopped or any thing else similar.
|
|
|
|
*/
|
|
|
|
if (TD_IS_RUNNING(td)) {
|
|
|
|
/* Put us back on the run queue (kse and all). */
|
|
|
|
setrunqueue(td);
|
2003-06-15 00:31:24 +00:00
|
|
|
} else if (p->p_flag & P_SA) {
|
2002-10-12 05:32:24 +00:00
|
|
|
/*
|
|
|
|
* We will not be on the run queue. So we must be
|
|
|
|
* sleeping or similar. As it's available,
|
|
|
|
* someone else can use the KSE if they need it.
|
|
|
|
*/
|
|
|
|
kse_reassign(ke);
|
|
|
|
}
|
2003-10-16 08:53:46 +00:00
|
|
|
newtd = choosethread();
|
|
|
|
if (td != newtd)
|
|
|
|
cpu_switch(td, newtd);
|
|
|
|
sched_lock.mtx_lock = (uintptr_t)td;
|
|
|
|
td->td_oncpu = PCPU_GET(cpuid);
|
2002-10-12 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sched_wakeup(struct thread *td)
|
|
|
|
{
|
|
|
|
struct ksegrp *kg;
|
|
|
|
|
2003-04-23 18:51:05 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2002-10-12 05:32:24 +00:00
|
|
|
kg = td->td_ksegrp;
|
|
|
|
if (kg->kg_slptime > 1)
|
|
|
|
updatepri(kg);
|
|
|
|
kg->kg_slptime = 0;
|
|
|
|
setrunqueue(td);
|
|
|
|
maybe_resched(td);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-10-16 08:39:15 +00:00
|
|
|
sched_add(struct thread *td)
|
2002-10-12 05:32:24 +00:00
|
|
|
{
|
2003-10-16 08:39:15 +00:00
|
|
|
struct kse *ke;
|
|
|
|
|
|
|
|
ke = td->td_kse;
|
2002-10-12 05:32:24 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2004-01-25 08:21:46 +00:00
|
|
|
KASSERT((ke->ke_thread != NULL), ("sched_add: No thread on KSE"));
|
2002-10-12 05:32:24 +00:00
|
|
|
KASSERT((ke->ke_thread->td_kse != NULL),
|
2004-01-25 08:21:46 +00:00
|
|
|
("sched_add: No KSE on thread"));
|
2002-10-12 05:32:24 +00:00
|
|
|
KASSERT(ke->ke_state != KES_ONRUNQ,
|
2004-01-25 08:21:46 +00:00
|
|
|
("sched_add: kse %p (%s) already in run queue", ke,
|
2002-10-12 05:32:24 +00:00
|
|
|
ke->ke_proc->p_comm));
|
|
|
|
KASSERT(ke->ke_proc->p_sflag & PS_INMEM,
|
2004-01-25 08:21:46 +00:00
|
|
|
("sched_add: process swapped out"));
|
2002-10-12 05:32:24 +00:00
|
|
|
ke->ke_ksegrp->kg_runq_kses++;
|
|
|
|
ke->ke_state = KES_ONRUNQ;
|
|
|
|
|
2004-01-25 08:00:04 +00:00
|
|
|
#ifdef SMP
|
|
|
|
if (KSE_CAN_MIGRATE(ke)) {
|
|
|
|
CTR1(KTR_4BSD, "adding kse:%p to gbl runq", ke);
|
|
|
|
ke->ke_runq = &runq;
|
|
|
|
} else {
|
|
|
|
CTR1(KTR_4BSD, "adding kse:%p to pcpu runq", ke);
|
|
|
|
if (!SKE_RUNQ_PCPU(ke))
|
|
|
|
ke->ke_runq = &runq_pcpu[PCPU_GET(cpuid)];
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ke->ke_runq = &runq;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
runq_add(ke->ke_runq, ke);
|
2002-10-12 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-10-16 08:39:15 +00:00
|
|
|
sched_rem(struct thread *td)
|
2002-10-12 05:32:24 +00:00
|
|
|
{
|
2003-10-16 08:39:15 +00:00
|
|
|
struct kse *ke;
|
|
|
|
|
|
|
|
ke = td->td_kse;
|
2002-10-12 05:32:24 +00:00
|
|
|
KASSERT(ke->ke_proc->p_sflag & PS_INMEM,
|
2004-01-25 08:21:46 +00:00
|
|
|
("sched_rem: process swapped out"));
|
|
|
|
KASSERT((ke->ke_state == KES_ONRUNQ),
|
|
|
|
("sched_rem: KSE not on run queue"));
|
2002-10-12 05:32:24 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
|
|
|
|
2004-01-25 08:00:04 +00:00
|
|
|
runq_remove(ke->ke_sched->ske_runq, ke);
|
|
|
|
|
2002-10-12 05:32:24 +00:00
|
|
|
ke->ke_state = KES_THREAD;
|
|
|
|
ke->ke_ksegrp->kg_runq_kses--;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct kse *
|
|
|
|
sched_choose(void)
|
|
|
|
{
|
|
|
|
struct kse *ke;
|
2004-01-25 08:00:04 +00:00
|
|
|
struct runq *rq;
|
|
|
|
|
|
|
|
#ifdef SMP
|
|
|
|
struct kse *kecpu;
|
2002-10-12 05:32:24 +00:00
|
|
|
|
2004-01-25 08:00:04 +00:00
|
|
|
rq = &runq;
|
|
|
|
ke = runq_choose(&runq);
|
|
|
|
kecpu = runq_choose(&runq_pcpu[PCPU_GET(cpuid)]);
|
|
|
|
|
|
|
|
if (ke == NULL ||
|
|
|
|
(kecpu != NULL &&
|
|
|
|
kecpu->ke_thread->td_priority < ke->ke_thread->td_priority)) {
|
|
|
|
CTR2(KTR_4BSD, "choosing kse %p from pcpu runq %d", kecpu,
|
|
|
|
PCPU_GET(cpuid));
|
|
|
|
ke = kecpu;
|
|
|
|
rq = &runq_pcpu[PCPU_GET(cpuid)];
|
|
|
|
} else {
|
|
|
|
CTR1(KTR_4BSD, "choosing kse %p from main runq", ke);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
rq = &runq;
|
2002-10-12 05:32:24 +00:00
|
|
|
ke = runq_choose(&runq);
|
2004-01-25 08:00:04 +00:00
|
|
|
#endif
|
2002-10-12 05:32:24 +00:00
|
|
|
|
|
|
|
if (ke != NULL) {
|
2004-01-25 08:00:04 +00:00
|
|
|
runq_remove(rq, ke);
|
2002-10-12 05:32:24 +00:00
|
|
|
ke->ke_state = KES_THREAD;
|
|
|
|
|
|
|
|
KASSERT((ke->ke_thread != NULL),
|
2004-01-25 08:21:46 +00:00
|
|
|
("sched_choose: No thread on KSE"));
|
2002-10-12 05:32:24 +00:00
|
|
|
KASSERT((ke->ke_thread->td_kse != NULL),
|
2004-01-25 08:21:46 +00:00
|
|
|
("sched_choose: No KSE on thread"));
|
2002-10-12 05:32:24 +00:00
|
|
|
KASSERT(ke->ke_proc->p_sflag & PS_INMEM,
|
2004-01-25 08:21:46 +00:00
|
|
|
("sched_choose: process swapped out"));
|
2002-10-12 05:32:24 +00:00
|
|
|
}
|
|
|
|
return (ke);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sched_userret(struct thread *td)
|
|
|
|
{
|
|
|
|
struct ksegrp *kg;
|
|
|
|
/*
|
|
|
|
* XXX we cheat slightly on the locking here to avoid locking in
|
|
|
|
* the usual case. Setting td_priority here is essentially an
|
|
|
|
* incomplete workaround for not setting it properly elsewhere.
|
|
|
|
* Now that some interrupt handlers are threads, not setting it
|
|
|
|
* properly elsewhere can clobber it in the window between setting
|
|
|
|
* it here and returning to user mode, so don't waste time setting
|
|
|
|
* it perfectly here.
|
|
|
|
*/
|
|
|
|
kg = td->td_ksegrp;
|
|
|
|
if (td->td_priority != kg->kg_user_pri) {
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
td->td_priority = kg->kg_user_pri;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
}
|
|
|
|
}
|
2002-11-21 01:22:38 +00:00
|
|
|
|
2004-01-25 08:00:04 +00:00
|
|
|
void
|
|
|
|
sched_bind(struct thread *td, int cpu)
|
|
|
|
{
|
|
|
|
struct kse *ke;
|
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
|
|
|
KASSERT(TD_IS_RUNNING(td),
|
|
|
|
("sched_bind: cannot bind non-running thread"));
|
|
|
|
|
|
|
|
ke = td->td_kse;
|
|
|
|
|
|
|
|
ke->ke_flags |= KEF_BOUND;
|
|
|
|
#ifdef SMP
|
|
|
|
ke->ke_runq = &runq_pcpu[cpu];
|
|
|
|
if (PCPU_GET(cpuid) == cpu)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ke->ke_state = KES_THREAD;
|
|
|
|
|
|
|
|
mi_switch(SW_VOL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sched_unbind(struct thread* td)
|
|
|
|
{
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
|
|
|
td->td_kse->ke_flags &= ~KEF_BOUND;
|
|
|
|
}
|
|
|
|
|
2002-11-21 01:22:38 +00:00
|
|
|
int
|
|
|
|
sched_sizeof_kse(void)
|
|
|
|
{
|
2003-01-12 19:04:49 +00:00
|
|
|
return (sizeof(struct kse) + sizeof(struct ke_sched));
|
2002-11-21 01:22:38 +00:00
|
|
|
}
|
|
|
|
int
|
|
|
|
sched_sizeof_ksegrp(void)
|
|
|
|
{
|
|
|
|
return (sizeof(struct ksegrp));
|
|
|
|
}
|
|
|
|
int
|
|
|
|
sched_sizeof_proc(void)
|
|
|
|
{
|
|
|
|
return (sizeof(struct proc));
|
|
|
|
}
|
|
|
|
int
|
|
|
|
sched_sizeof_thread(void)
|
|
|
|
{
|
|
|
|
return (sizeof(struct thread));
|
|
|
|
}
|
2002-11-21 09:30:55 +00:00
|
|
|
|
|
|
|
fixpt_t
|
2003-10-16 08:39:15 +00:00
|
|
|
sched_pctcpu(struct thread *td)
|
2002-11-21 09:30:55 +00:00
|
|
|
{
|
2003-10-16 21:13:14 +00:00
|
|
|
struct kse *ke;
|
|
|
|
|
|
|
|
ke = td->td_kse;
|
2003-11-08 03:03:17 +00:00
|
|
|
if (ke == NULL)
|
|
|
|
ke = td->td_last_kse;
|
2003-10-16 21:13:14 +00:00
|
|
|
if (ke)
|
|
|
|
return (ke->ke_pctcpu);
|
|
|
|
|
|
|
|
return (0);
|
2002-11-21 09:30:55 +00:00
|
|
|
}
|