Clan OS Scope 5 uses a preemptive, round-robin context scheduler for kernel tasks.
- Quantum:
SCHED_QUANTUM_TICKS = 5 - Runtime quantum override API:
set_scheduler_quantum_ticks(ticks) - Runtime fairness-interval API:
set_fairness_check_interval_ticks(ticks) - Atomic runtime config API:
apply_runtime_config(config) - Trigger: timer tick sets reschedule flag each quantum boundary
- Switch path: deferred preemption checkpoint in task loops (
preempt_if_irq_pending) - Selection: round-robin over runnable context tasks
- Timer IRQ increments scheduler ticks (
on_timer_tick) - Quantum expiry sets
NEED_RESCHEDULEand IRQ preempt pending flag - Running task reaches checkpoint (
preempt_if_requested/preempt_if_irq_pending) - Scheduler selects next runnable task pair
switch_contextsaves current CPU context and restores next
- Per-task metrics (
TaskMetrics): switches, cpu ticks, preemption attempts/successes - Kernel fairness counters:
KERNEL_TASK_1..4_COUNT - Runtime fairness monitor:
log_preemption_fairness - Fairness violation threshold: score >
1.10
Fairness score is computed as:
A score close to 1.0 indicates balanced scheduling.
Scope 5 observability components:
- Global counters:
- process creations / terminations
- total preemptions
- fairness violations
- scheduler lock contention
- Event ring buffer (
EVENT_LOG_CAPACITY = 256):- event type
- tick timestamp
- pid/task id
- Performance snapshot (
PerformanceCounters::read) includes:- timer ticks
- total preemptions
- scheduler lock contention
- fairness violations
Main public APIs in task::scheduler:
on_timer_tick()try_context_reschedule()preempt_if_requested()preempt_if_irq_pending()spawn_context_task(name, entry)spawn_preemption_lab_tasks()stats()andcontext_stats()get_task_metrics(id)andget_all_task_metrics()scheduler_lock_contention()scheduler_quantum_ticks()/set_scheduler_quantum_ticks()fairness_check_interval_ticks()/set_fairness_check_interval_ticks()runtime_config()/apply_runtime_config()
Preemptive mode:
cargo run -p kernel --features preemptionIntegration validation:
cargo test -p kernel --features preemption --test preemption_integrationBudgeted matrix validation:
python scripts/validation_matrix.py --soak-duration 30 --latency-duration 30 --boot-wait 90 --smoke-timeout 180Current enforced budgets in matrix mode:
- fairness score
<= 1.10 - max estimated preemption latency
<= 300ms(matrix default) - scope-6 runtime smoke line must report all true flags
Preemptive context switch applies the next runnable process user CR3 via apply_scheduler_cr3_for_next. Process records store cr3_phys when hardware page tables are built.
smp::init() records CPU and parked AP counts and provides TLB flush hooks. Scheduling still runs on the bootstrap processor only. See SMP.md.