diff --git a/src/context/context.rs b/src/context/context.rs index 6d723f498f..4f795841c0 100644 --- a/src/context/context.rs +++ b/src/context/context.rs @@ -18,7 +18,8 @@ use crate::{ cpu_stats, ipi::{ipi, IpiKind, IpiTarget}, memory::{ - allocate_p2frame, deallocate_p2frame, Enomem, Frame, RaiiFrame, RmmA, RmmArch, PAGE_SIZE, + allocate_p2frame, deallocate_p2frame, Enomem, Frame, PhysicalAddress, RaiiFrame, RmmA, + RmmArch, PAGE_SIZE, }, percpu::PercpuBlock, scheme::{CallerCtx, FileHandle, SchemeId}, @@ -62,6 +63,38 @@ impl Status { } } +pub const SCHED_PRIORITY_LEVELS: usize = 40; +pub const DEFAULT_SCHED_OTHER_PRIORITY: usize = 20; +pub const DEFAULT_SCHED_RR_QUANTUM: u128 = 100_000_000; + +#[repr(u8)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SchedPolicy { + Fifo = 0, + RoundRobin = 1, + Other = 2, +} + +impl SchedPolicy { + pub fn try_from_raw(raw: u8) -> Option { + match raw { + 0 => Some(Self::Fifo), + 1 => Some(Self::RoundRobin), + 2 => Some(Self::Other), + _ => None, + } + } +} + +pub fn rt_priority_to_kernel_prio(rt_priority: u8) -> usize { + (SCHED_PRIORITY_LEVELS - 1) + .saturating_sub((usize::from(rt_priority.min(99)) * (SCHED_PRIORITY_LEVELS - 1)) / 99) +} + +fn clamp_sched_other_prio(prio: usize) -> usize { + prio.min(SCHED_PRIORITY_LEVELS - 1) +} + #[derive(Clone, Debug)] pub enum HardBlockedReason { /// "SIGSTOP", only procmgr is allowed to switch contexts this state @@ -96,6 +129,7 @@ pub struct Context { pub running: bool, /// Current CPU ID pub cpu_id: Option, + pub last_cpu: Option, /// Time this context was switched to pub switch_time: u128, /// Amount of CPU time used @@ -140,6 +174,20 @@ pub struct Context { pub fmap_ret: Option, /// Priority pub prio: usize, + pub sched_policy: SchedPolicy, + pub sched_rt_priority: u8, + pub sched_rr_ticks_consumed: u32, + pub sched_static_prio: usize, + pub sched_rr_quantum: u128, + /// Virtual runtime for SCHED_OTHER fair scheduling. + /// CPU-bound threads accumulate vruntime faster; I/O-bound stay lower. + pub vruntime: u128, + #[allow(dead_code)] + pub futex_pi_boost: bool, + #[allow(dead_code)] + pub futex_pi_original_prio: usize, + #[allow(dead_code)] + pub futex_pi_waiters: Vec, // TODO: id can reappear after wraparound? pub owner_proc_id: Option, @@ -184,6 +232,7 @@ impl Context { status_reason: "", running: false, cpu_id: None, + last_cpu: None, switch_time: 0, cpu_time: 0, sched_affinity: LogicalCpuSet::all(), @@ -200,6 +249,15 @@ impl Context { userspace: false, fmap_ret: None, prio: 20, + sched_policy: SchedPolicy::Other, + sched_rt_priority: 0, + sched_rr_ticks_consumed: 0, + sched_static_prio: 20, + sched_rr_quantum: DEFAULT_SCHED_RR_QUANTUM, + vruntime: 0, + futex_pi_boost: false, + futex_pi_original_prio: 0, + futex_pi_waiters: Vec::new(), being_sigkilled: false, owner_proc_id,