Skip to main content

ax_task/sched/system/cpu/
snapshot.rs

1//! Stable deterministic snapshot of one owner CPU.
2
3use super::CpuLocal;
4use crate::{sched::CpuId, thread::ThreadId};
5
6/// Stable, allocation-free scheduler state used by deterministic model tests.
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub struct CpuSnapshot {
9    owner: CpuId,
10    current: Option<ThreadId>,
11    runnable: usize,
12    need_resched: bool,
13}
14
15impl CpuSnapshot {
16    pub(crate) fn capture(cpu: &CpuLocal) -> Self {
17        Self {
18            owner: cpu.owner(),
19            current: cpu.current(),
20            runnable: cpu.runnable_count(),
21            need_resched: cpu.needs_reschedule(),
22        }
23    }
24
25    /// Returns the owner CPU.
26    pub const fn owner(self) -> CpuId {
27        self.owner
28    }
29
30    /// Returns the current thread.
31    pub const fn current(self) -> Option<ThreadId> {
32        self.current
33    }
34
35    /// Returns Linux `rq->nr_running`, including a runnable current thread.
36    pub const fn runnable(self) -> usize {
37        self.runnable
38    }
39
40    /// Returns the sticky preemption state.
41    pub const fn need_resched(self) -> bool {
42        self.need_resched
43    }
44}