pub mod sysfs;
#[cfg(target_os = "linux")]
mod affinity;
#[cfg(target_os = "linux")]
pub use linux_platform::LinuxPlatform;
#[cfg(target_os = "linux")]
mod linux_platform {
use std::process::{Child, Command};
use super::{affinity, sysfs::Sysfs};
use crate::platform::{Platform, SwitchCounters};
use crate::topology::{LogicalId, Topology};
use corescout_core::cpuset::CpuSet;
use corescout_core::error::Result;
pub struct LinuxPlatform {
sysfs: Sysfs,
}
impl LinuxPlatform {
pub fn new() -> Self {
LinuxPlatform {
sysfs: Sysfs::system(),
}
}
pub fn with_sysfs(sysfs: Sysfs) -> Self {
LinuxPlatform { sysfs }
}
}
impl Default for LinuxPlatform {
fn default() -> Self {
Self::new()
}
}
impl Platform for LinuxPlatform {
fn name(&self) -> &'static str {
"linux"
}
fn discover_topology(&self) -> Result<Topology> {
let affinity = affinity::process_affinity().ok();
self.sysfs.read_topology(affinity)
}
fn pin_current_thread(&self, cpu: LogicalId) -> Result<()> {
affinity::pin_current_thread(cpu)
}
fn set_current_thread_affinity(&self, cpus: &CpuSet) -> Result<()> {
affinity::set_current_thread_affinity(cpus)
}
fn current_thread_affinity(&self) -> Result<CpuSet> {
affinity::current_thread_affinity()
}
fn process_affinity(&self) -> Result<CpuSet> {
affinity::process_affinity()
}
fn thread_switch_counters(&self) -> Option<SwitchCounters> {
affinity::thread_switch_counters()
}
fn spawn_with_affinity(&self, command: &mut Command, cpus: &CpuSet) -> Result<Child> {
affinity::spawn_with_affinity(command, cpus)
}
}
}