/*
* Copyright (c) 2007, 2008 Mindaugas Rasiukevicius <rmind at NetBSD org>
* All rights reserved.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
/*
* TODO:
* - Implementation of fair share queue;
* - Support for NUMA;
*/
void
sched_rqinit(void)
{
if (hz < 100) {
panic("sched_rqinit: value of HZ is too low\n");
}
/* Default timing ranges */
min_ts = mstohz(20); /* ~20 ms */
max_ts = mstohz(150); /* ~150 ms */
sched_rrticks = mstohz(100); /* ~100 ms */
sched_precalcts();
#ifdef notyet
/* Need to set the name etc. This does not belong here */
/* Attach the primary CPU here */
sched_cpuattach(curcpu());
#endif
sched_lwp_fork(NULL, &lwp0);
#ifdef notyet
/* without attaching the primary CPU l_mutex does not get initialized */
lwp_lock(&lwp0);
sched_newts(&lwp0);
lwp_unlock(&lwp0);
#else
/* gross */
lwp0.l_sched.timeslice = ts_map[lwp0.l_auxprio];
#endif
}
/* Pre-calculate the time-slices for the priorities */
static void
sched_precalcts(void)
{
pri_t p;
/* Recalculate the time-slice */
void
sched_newts(struct lwp *l)
{
l->l_sched.timeslice = ts_map[lwp_eprio(l)];
}
void
sched_slept(struct lwp *l)
{
/*
* If thread is in time-sharing queue and batch flag is not marked,
* increase the priority, and run with the lower time-quantum.
*/
if (l->l_priority < PRI_HIGHEST_TS && (l->l_flag & LW_BATCH) == 0) {
struct proc *p = l->l_proc;
KASSERT(l->l_class == SCHED_OTHER);
if (__predict_false(p->p_nice < NZERO)) {
const int n = uimax((NZERO - p->p_nice) >> 2, 1);
l->l_priority = uimin(l->l_priority + n, PRI_HIGHEST_TS);
} else {
l->l_priority++;
}
}
}
void
sched_wakeup(struct lwp *l)
{
/* If thread was sleeping a second or more - set a high priority */
if (l->l_slptime >= 1)
l->l_priority = high_pri[l->l_priority];
}
void
sched_pstats_hook(struct lwp *l, int batch)
{
pri_t prio;
/*
* Estimate threads on time-sharing queue only, however,
* exclude the highest priority for performance purposes.
*/
KASSERT(lwp_locked(l, NULL));
if (l->l_priority >= PRI_HIGHEST_TS)
return;
KASSERT(l->l_class == SCHED_OTHER);
/* If it is CPU-bound not a first time - decrease the priority */
prio = l->l_priority;
if (batch && prio != 0)
prio--;
/* If thread was not ran a second or more - set a high priority */
if (l->l_stat == LSRUN) {
if (l->l_rticks && (getticks() - l->l_rticks >= hz))
prio = high_pri[prio];
/* Re-enqueue the thread if priority has changed */
if (prio != l->l_priority)
lwp_changepri(l, prio);
} else {
/* In other states, change the priority directly */
l->l_priority = prio;
}
}
/*
* Called once per time-quantum, with the running LWP lock held (spc_lwplock).
*/
void
sched_tick(struct cpu_info *ci)
{
struct schedstate_percpu *spc = &ci->ci_schedstate;
struct lwp *l = ci->ci_onproc;
struct proc *p;
if (__predict_false(CURCPU_IDLE_P()))
return;
lwp_lock(l);
KASSERT(l->l_mutex != spc->spc_mutex);
switch (l->l_class) {
case SCHED_FIFO:
/*
* Update the time-quantum, and continue running,
* if thread runs on FIFO real-time policy.
*/
KASSERT(l->l_priority > PRI_HIGHEST_TS);
spc->spc_ticks = l->l_sched.timeslice;
lwp_unlock(l);
return;
case SCHED_OTHER:
/*
* If thread is in time-sharing queue, decrease the priority,
* and run with a higher time-quantum.
*/
KASSERT(l->l_priority <= PRI_HIGHEST_TS);
if (l->l_priority == 0)
break;
p = l->l_proc;
if (__predict_false(p->p_nice > NZERO)) {
const int n = uimax((p->p_nice - NZERO) >> 2, 1);
l->l_priority = imax(l->l_priority - n, 0);
} else
l->l_priority--;
break;
}
/*
* If there are higher priority threads or threads in the same queue,
* mark that thread should yield, otherwise, continue running.
*/
if (lwp_eprio(l) <= spc->spc_maxpriority || l->l_target_cpu) {
spc->spc_flags |= SPCF_SHOULDYIELD;
spc_lock(ci);
sched_resched_cpu(ci, MAXPRI_KTHREAD, true);
/* spc now unlocked */
} else
spc->spc_ticks = l->l_sched.timeslice;
lwp_unlock(l);
}
/*
* Sysctl nodes and initialization.
*/
static int
sysctl_sched_rtts(SYSCTLFN_ARGS)
{
struct sysctlnode node;
int rttsms = hztoms(sched_rrticks);