use std::process::{Child, Command};
use crate::topology::{LogicalId, Topology};
use corescout_core::cpuset::CpuSet;
use corescout_core::error::Result;
pub mod linux;
pub mod cpuid;
pub mod stub;
#[cfg(target_os = "windows")]
pub mod windows;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SwitchCounters {
pub voluntary: u64,
pub involuntary: u64,
}
impl SwitchCounters {
pub fn delta(self, earlier: SwitchCounters) -> SwitchCounters {
SwitchCounters {
voluntary: self.voluntary.saturating_sub(earlier.voluntary),
involuntary: self.involuntary.saturating_sub(earlier.involuntary),
}
}
}
pub trait Platform: Send + Sync {
fn name(&self) -> &'static str;
fn discover_topology(&self) -> Result<Topology>;
fn pin_current_thread(&self, cpu: LogicalId) -> Result<()>;
fn set_current_thread_affinity(&self, cpus: &CpuSet) -> Result<()>;
fn current_thread_affinity(&self) -> Result<CpuSet>;
fn process_affinity(&self) -> Result<CpuSet>;
fn thread_switch_counters(&self) -> Option<SwitchCounters>;
fn thread_cycles(&self) -> Option<u64> {
None
}
fn spawn_with_affinity(&self, command: &mut Command, cpus: &CpuSet) -> Result<Child>;
}
pub fn detect() -> Box<dyn Platform> {
#[cfg(target_os = "windows")]
{
Box::new(windows::WindowsPlatform::new())
}
#[cfg(target_os = "linux")]
{
Box::new(linux::LinuxPlatform::new())
}
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
{
Box::new(stub::StubPlatform::new())
}
}