Skip to main content

ax_task/
sched.rs

1//! Scheduling policy, CPU placement and runtime accounting.
2
3use crate::{
4    runtime::context::{runtime_task_system, validate_task_context},
5    thread::TaskError,
6};
7pub use crate::{
8    sched::{
9        affinity::ThreadAffinityChange,
10        algorithm::SchedulerTimestamp,
11        policy::{DeadlineFlags, DeadlinePolicy, FairMode, Nice, RtPriority, SchedulePolicy},
12    },
13    thread::spec::CpuSet,
14};
15
16/// A logical processor identifier in the configured topology.
17#[repr(transparent)]
18#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
19pub struct CpuId(u32);
20
21impl CpuId {
22    /// Creates a logical processor identifier.
23    pub const fn new(value: u32) -> Self {
24        Self(value)
25    }
26
27    /// Returns the numeric identifier.
28    pub const fn as_u32(self) -> u32 {
29        self.0
30    }
31
32    /// Returns the identifier as an array index.
33    pub const fn as_usize(self) -> usize {
34        self.0 as usize
35    }
36}
37
38/// Returns cumulative non-idle runtime charged by one online CPU.
39pub fn cpu_busy_runtime_ns(cpu: CpuId) -> Result<u64, TaskError> {
40    runtime_task_system()?.cpu_busy_runtime_ns(cpu)
41}
42
43/// Returns the fixed topology width accepted by scheduler affinity masks.
44pub fn cpu_topology_len() -> Result<usize, TaskError> {
45    Ok(runtime_task_system()?.cpu_topology_len())
46}
47
48/// Returns the CPUs that currently accept runnable placement.
49///
50/// Unlike [`cpu_topology_len`], this snapshot excludes possible CPUs that have
51/// not completed scheduler online publication or no longer accept new work.
52pub fn active_cpu_set() -> Result<CpuSet, TaskError> {
53    validate_task_context()?;
54    Ok(runtime_task_system()?.active_cpu_set())
55}
56
57pub(crate) mod algorithm;
58
59pub(crate) mod system;
60
61pub(crate) mod policy;
62
63pub(crate) mod affinity;
64
65pub use crate::sched::system::{
66    ChargeOutcome, DeadlineActivity, DeadlineActivitySnapshot, DeadlineBandwidthSnapshot,
67    DeadlineRuntimeSnapshot, SchedulingClass,
68};