implemented generic linux sal with heuristics

This commit is contained in:
2026-02-26 15:16:37 +01:00
parent 48c3b46a0c
commit f87efa1d24
13 changed files with 686 additions and 125 deletions

218
src/sal/generic_linux.rs Normal file
View File

@@ -0,0 +1,218 @@
use anyhow::{Result, anyhow};
use std::path::Path;
use std::fs;
use std::time::{Duration, Instant};
use std::thread;
use std::process::Command;
use tracing::{debug};
use std::sync::mpsc;
use crate::sal::traits::{SensorBus, ActuatorBus, EnvironmentGuard, HardwareWatchdog, PreflightAuditor, AuditStep, AuditError};
use crate::sal::heuristic::discovery::SystemFactSheet;
use crate::sal::heuristic::schema::HardwareDb;
pub struct GenericLinuxSal {
fact_sheet: SystemFactSheet,
db: HardwareDb,
suppressed_services: Vec<String>,
}
impl GenericLinuxSal {
pub fn new(fact_sheet: SystemFactSheet, db: HardwareDb) -> Self {
Self {
fact_sheet,
db,
suppressed_services: Vec::new(),
}
}
fn is_dell(&self) -> bool {
self.fact_sheet.vendor.to_lowercase().contains("dell")
}
fn read_sysfs_timeout(&self, path: &Path, timeout: Duration) -> Result<String> {
let (tx, rx) = mpsc::channel();
let path_buf = path.to_path_buf();
thread::spawn(move || {
let res = fs::read_to_string(path_buf).map(|s| s.trim().to_string());
let _ = tx.send(res);
});
match rx.recv_timeout(timeout) {
Ok(res) => res.map_err(|e| anyhow!("Failed to read sysfs: {}", e)),
Err(_) => Err(anyhow!("Timeout reading sysfs path: {:?}", path)),
}
}
}
impl PreflightAuditor for GenericLinuxSal {
fn audit(&self) -> Box<dyn Iterator<Item = AuditStep> + '_> {
let mut steps = Vec::new();
// 1. Static DB checks
for check in &self.db.preflight_checks {
let status = Command::new("sh")
.arg("-c")
.arg(&check.check_cmd)
.status();
steps.push(AuditStep {
description: check.name.clone(),
outcome: match status {
Ok(s) if s.success() => Ok(()),
_ => Err(AuditError::KernelIncompatible(check.fail_help.clone())),
}
});
}
// 2. Conflict checks (Critical only)
for conflict_id in &self.fact_sheet.active_conflicts {
if let Some(conflict) = self.db.conflicts.iter().find(|c| &c.id == conflict_id) {
if conflict.severity == "Critical" {
steps.push(AuditStep {
description: format!("Conflict: {}", conflict.id),
outcome: Err(AuditError::ToolMissing(conflict.help_text.clone())),
});
}
}
}
Box::new(steps.into_iter())
}
}
impl SensorBus for GenericLinuxSal {
fn get_temp(&self) -> Result<f32> {
let path = self.fact_sheet.temp_path.as_ref()
.ok_or_else(|| anyhow!("No temperature sensor path found"))?;
let content = self.read_sysfs_timeout(path, Duration::from_millis(200))?;
let milli_celsius: f32 = content.parse()?;
Ok(milli_celsius / 1000.0)
}
fn get_power_w(&self) -> Result<f32> {
let rapl_path = self.fact_sheet.rapl_paths.first()
.ok_or_else(|| anyhow!("No RAPL path found"))?;
let energy_path = rapl_path.join("energy_uj");
let e1: u64 = self.read_sysfs_timeout(&energy_path, Duration::from_millis(200))?.parse()?;
let t1 = Instant::now();
thread::sleep(Duration::from_millis(100));
let e2: u64 = self.read_sysfs_timeout(&energy_path, Duration::from_millis(200))?.parse()?;
let t2 = Instant::now();
let delta_e = e2.wrapping_sub(e1);
let delta_t = t2.duration_since(t1).as_secs_f32();
Ok((delta_e as f32 / 1_000_000.0) / delta_t)
}
fn get_fan_rpms(&self) -> Result<Vec<u32>> {
let mut rpms = Vec::new();
for path in &self.fact_sheet.fan_paths {
if let Ok(content) = self.read_sysfs_timeout(path, Duration::from_millis(200)) {
if let Ok(rpm) = content.parse() { rpms.push(rpm); }
}
}
Ok(rpms)
}
fn get_freq_mhz(&self) -> Result<f32> {
let path = Path::new("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
if path.exists() {
let khz: f32 = self.read_sysfs_timeout(path, Duration::from_millis(200))?.parse()?;
Ok(khz / 1000.0)
} else {
// Fallback: parse /proc/cpuinfo
let cpuinfo = fs::read_to_string("/proc/cpuinfo")?;
for line in cpuinfo.lines() {
if line.starts_with("cpu MHz") {
if let Some((_, mhz)) = line.split_once(':') {
return Ok(mhz.trim().parse()?);
}
}
}
Err(anyhow!("Could not determine CPU frequency"))
}
}
}
impl ActuatorBus for GenericLinuxSal {
fn set_fan_mode(&self, mode: &str) -> Result<()> {
if self.is_dell() {
let cmd = match mode {
"manual" | "max" => self.db.ecosystems.get("dell").and_then(|e| e.fan_manual_mode_cmd.as_ref()),
"auto" => self.db.ecosystems.get("dell").and_then(|e| e.fan_auto_mode_cmd.as_ref()),
_ => return Err(anyhow!("Unsupported fan mode: {}", mode)),
};
if let Some(cmd_str) = cmd {
let parts: Vec<&str> = cmd_str.split_whitespace().collect();
Command::new(parts[0]).args(&parts[1..]).status()?;
Ok(())
} else { Err(anyhow!("Dell fan command missing in DB")) }
} else {
debug!("Fan control not implemented for non-Dell systems yet");
Ok(())
}
}
fn set_sustained_power_limit(&self, watts: f32) -> Result<()> {
let rapl_path = self.fact_sheet.rapl_paths.first()
.ok_or_else(|| anyhow!("No RAPL path found for PL1"))?;
let path = rapl_path.join("constraint_0_power_limit_uw");
fs::write(path, ((watts * 1_000_000.0) as u64).to_string())?;
Ok(())
}
fn set_burst_power_limit(&self, watts: f32) -> Result<()> {
let rapl_path = self.fact_sheet.rapl_paths.first()
.ok_or_else(|| anyhow!("No RAPL path found for PL2"))?;
let path = rapl_path.join("constraint_1_power_limit_uw");
fs::write(path, ((watts * 1_000_000.0) as u64).to_string())?;
Ok(())
}
}
impl EnvironmentGuard for GenericLinuxSal {
fn suppress(&mut self) -> Result<()> {
for conflict_id in &self.fact_sheet.active_conflicts {
if let Some(conflict) = self.db.conflicts.iter().find(|c| &c.id == conflict_id) {
for service in &conflict.services {
debug!("Stopping service: {}", service);
if Command::new("systemctl").arg("stop").arg(service).status()?.success() {
self.suppressed_services.push(service.clone());
}
}
}
}
Ok(())
}
fn restore(&mut self) -> Result<()> {
for service in self.suppressed_services.drain(..) {
debug!("Starting service: {}", service);
let _ = Command::new("systemctl").arg("start").arg(service).status();
}
if self.is_dell() {
let _ = self.set_fan_mode("auto");
}
Ok(())
}
}
impl HardwareWatchdog for GenericLinuxSal {
fn check_emergency(&self) -> Result<bool> {
if let Ok(temp) = self.get_temp() {
if temp > 100.0 {
return Ok(true);
}
}
Ok(false)
}
}
impl Drop for GenericLinuxSal {
fn drop(&mut self) {
let _ = self.restore();
}
}