#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sched_m2.c,v 1.41 2026/01/04 02:10:01 riastradh Exp $");
#include <sys/param.h>
#include <sys/cpu.h>
#include <sys/callout.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/lwp.h>
#include <sys/mutex.h>
#include <sys/pool.h>
#include <sys/proc.h>
#include <sys/pset.h>
#include <sys/resource.h>
#include <sys/resourcevar.h>
#include <sys/sched.h>
#include <sys/sdt.h>
#include <sys/syscallargs.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#define PRI_TS_COUNT (NPRI_USER)
#define PRI_RT_COUNT (PRI_COUNT - PRI_TS_COUNT)
#define PRI_HTS_RANGE (PRI_TS_COUNT / 10)
#define PRI_HIGHEST_TS (MAXPRI_USER)
static u_int min_ts;
static u_int max_ts;
static u_int ts_map[PRI_COUNT];
static pri_t high_pri[PRI_COUNT];
u_int sched_rrticks;
static void sched_precalcts(void);
void
sched_rqinit(void)
{
if (hz < 100) {
panic("sched_rqinit: value of HZ is too low\n");
}
min_ts = mstohz(20);
max_ts = mstohz(150);
sched_rrticks = mstohz(100);
sched_precalcts();
#ifdef notyet
sched_cpuattach(curcpu());
#endif
sched_lwp_fork(NULL, &lwp0);
#ifdef notyet
lwp_lock(&lwp0);
sched_newts(&lwp0);
lwp_unlock(&lwp0);
#else
lwp0.l_sched.timeslice = ts_map[lwp0.l_auxprio];
#endif
}
static void
sched_precalcts(void)
{
pri_t p;
for (p = 0; p <= PRI_HIGHEST_TS; p++) {
ts_map[p] = max_ts -
(p * 100 / (PRI_TS_COUNT - 1) * (max_ts - min_ts) / 100);
high_pri[p] = (PRI_HIGHEST_TS - PRI_HTS_RANGE) +
((p * PRI_HTS_RANGE) / (PRI_TS_COUNT - 1));
}
for (p = (PRI_HIGHEST_TS + 1); p < PRI_COUNT; p++) {
ts_map[p] = sched_rrticks;
high_pri[p] = p;
}
}
void
sched_proc_fork(struct proc *parent, struct proc *child)
{
struct lwp *l;
LIST_FOREACH(l, &child->p_lwps, l_sibling) {
lwp_lock(l);
sched_newts(l);
lwp_unlock(l);
}
}
void
sched_proc_exit(struct proc *child, struct proc *parent)
{
}
void
sched_lwp_fork(struct lwp *l1, struct lwp *l2)
{
}
void
sched_lwp_collect(struct lwp *l)
{
}
void
sched_setrunnable(struct lwp *l)
{
}
void
sched_schedclock(struct lwp *l)
{
}
void
sched_nice(struct proc *p, int prio)
{
struct lwp *l;
int n;
KASSERT(mutex_owned(p->p_lock));
p->p_nice = prio;
n = (prio - NZERO) >> 2;
if (n == 0)
return;
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
lwp_lock(l);
if (l->l_class == SCHED_OTHER) {
pri_t pri = l->l_priority - n;
pri = (n < 0) ? uimin(pri, PRI_HIGHEST_TS) : imax(pri, 0);
lwp_changepri(l, pri);
}
lwp_unlock(l);
}
}
void
sched_newts(struct lwp *l)
{
l->l_sched.timeslice = ts_map[lwp_eprio(l)];
}
void
sched_slept(struct lwp *l)
{
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 (l->l_slptime >= 1)
l->l_priority = high_pri[l->l_priority];
}
void
sched_pstats_hook(struct lwp *l, int batch)
{
pri_t prio;
KASSERT(lwp_locked(l, NULL));
if (l->l_priority >= PRI_HIGHEST_TS)
return;
KASSERT(l->l_class == SCHED_OTHER);
prio = l->l_priority;
if (batch && prio != 0)
prio--;
if (l->l_stat == LSRUN) {
if (l->l_rticks && (getticks() - l->l_rticks >= hz))
prio = high_pri[prio];
if (prio != l->l_priority)
lwp_changepri(l, prio);
} else {
l->l_priority = prio;
}
}
void
sched_oncpu(lwp_t *l)
{
struct schedstate_percpu *spc = &l->l_cpu->ci_schedstate;
KASSERT(l->l_sched.timeslice >= min_ts);
KASSERT(l->l_sched.timeslice <= max_ts);
spc->spc_ticks = l->l_sched.timeslice;
}
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:
KASSERT(l->l_priority > PRI_HIGHEST_TS);
spc->spc_ticks = l->l_sched.timeslice;
lwp_unlock(l);
return;
case SCHED_OTHER:
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 (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);
} else
spc->spc_ticks = l->l_sched.timeslice;
lwp_unlock(l);
}
static int
sysctl_sched_rtts(SYSCTLFN_ARGS)
{
struct sysctlnode node;
int rttsms = hztoms(sched_rrticks);
node = *rnode;
node.sysctl_data = &rttsms;
return sysctl_lookup(SYSCTLFN_CALL(&node));
}
static int
sysctl_sched_mints(SYSCTLFN_ARGS)
{
struct sysctlnode node;
struct cpu_info *ci;
int error, newsize;
CPU_INFO_ITERATOR cii;
node = *rnode;
node.sysctl_data = &newsize;
newsize = hztoms(min_ts);
error = sysctl_lookup(SYSCTLFN_CALL(&node));
if (error || newp == NULL)
return error;
newsize = mstohz(newsize);
if (newsize < 1 || newsize > hz || newsize >= max_ts)
return SET_ERROR(EINVAL);
for (CPU_INFO_FOREACH(cii, ci))
spc_lock(ci);
min_ts = newsize;
sched_precalcts();
for (CPU_INFO_FOREACH(cii, ci))
spc_unlock(ci);
return 0;
}
static int
sysctl_sched_maxts(SYSCTLFN_ARGS)
{
struct sysctlnode node;
struct cpu_info *ci;
int error, newsize;
CPU_INFO_ITERATOR cii;
node = *rnode;
node.sysctl_data = &newsize;
newsize = hztoms(max_ts);
error = sysctl_lookup(SYSCTLFN_CALL(&node));
if (error || newp == NULL)
return error;
newsize = mstohz(newsize);
if (newsize < 10 || newsize > hz || newsize <= min_ts)
return SET_ERROR(EINVAL);
for (CPU_INFO_FOREACH(cii, ci))
spc_lock(ci);
max_ts = newsize;
sched_precalcts();
for (CPU_INFO_FOREACH(cii, ci))
spc_unlock(ci);
return 0;
}
SYSCTL_SETUP(sysctl_sched_m2_setup, "sysctl sched setup")
{
const struct sysctlnode *node = NULL;
sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "sched",
SYSCTL_DESCR("Scheduler options"),
NULL, 0, NULL, 0,
CTL_KERN, CTL_CREATE, CTL_EOL);
if (node == NULL)
return;
sysctl_createv(NULL, 0, &node, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_STRING, "name", NULL,
NULL, 0, __UNCONST("M2"), 0,
CTL_CREATE, CTL_EOL);
sysctl_createv(NULL, 0, &node, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_INT, "rtts",
SYSCTL_DESCR("Round-robin time quantum (in milliseconds)"),
sysctl_sched_rtts, 0, NULL, 0,
CTL_CREATE, CTL_EOL);
sysctl_createv(NULL, 0, &node, NULL,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
CTLTYPE_INT, "maxts",
SYSCTL_DESCR("Maximal time quantum (in milliseconds)"),
sysctl_sched_maxts, 0, &max_ts, 0,
CTL_CREATE, CTL_EOL);
sysctl_createv(NULL, 0, &node, NULL,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
CTLTYPE_INT, "mints",
SYSCTL_DESCR("Minimal time quantum (in milliseconds)"),
sysctl_sched_mints, 0, &min_ts, 0,
CTL_CREATE, CTL_EOL);
}