implemented generic linux sal with heuristics
This commit is contained in:
@@ -17,6 +17,7 @@ pub struct DellXps9380Sal {
|
||||
last_poll: Mutex<Instant>,
|
||||
last_temp: Mutex<f32>,
|
||||
last_fans: Mutex<Vec<u32>>,
|
||||
suppressed_services: Mutex<Vec<String>>,
|
||||
}
|
||||
|
||||
impl DellXps9380Sal {
|
||||
@@ -82,6 +83,7 @@ impl DellXps9380Sal {
|
||||
last_poll: Mutex::new(Instant::now() - Duration::from_secs(2)),
|
||||
last_temp: Mutex::new(0.0),
|
||||
last_fans: Mutex::new(Vec::new()),
|
||||
suppressed_services: Mutex::new(Vec::new()),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -151,44 +153,36 @@ impl PreflightAuditor for DellXps9380Sal {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DellXps9380Guard {
|
||||
stopped_services: Vec<String>,
|
||||
}
|
||||
|
||||
impl DellXps9380Guard {
|
||||
pub fn new() -> Self {
|
||||
Self { stopped_services: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
impl EnvironmentGuard for DellXps9380Guard {
|
||||
impl EnvironmentGuard for DellXps9380Sal {
|
||||
fn suppress(&mut self) -> Result<()> {
|
||||
let services = ["tlp", "thermald", "i8kmon"];
|
||||
let mut suppressed = self.suppressed_services.lock().unwrap();
|
||||
for s in services {
|
||||
if Command::new("systemctl").args(["is-active", "--quiet", s]).status()?.success() {
|
||||
debug!("Suppressing service: {}", s);
|
||||
Command::new("systemctl").args(["stop", s]).status()?;
|
||||
self.stopped_services.push(s.to_string());
|
||||
suppressed.push(s.to_string());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn restore(&mut self) -> Result<()> {
|
||||
for s in &self.stopped_services {
|
||||
let _ = Command::new("systemctl").args(["start", s]).status();
|
||||
let mut suppressed = self.suppressed_services.lock().unwrap();
|
||||
for s in suppressed.drain(..) {
|
||||
let _ = Command::new("systemctl").args(["start", &s]).status();
|
||||
}
|
||||
self.stopped_services.clear();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DellXps9380Guard {
|
||||
impl Drop for DellXps9380Sal {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.restore();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl SensorBus for DellXps9380Sal {
|
||||
fn get_temp(&self) -> Result<f32> {
|
||||
// Enforce 1000ms rate limit for Dell SMM as per GEMINI.md
|
||||
|
||||
218
src/sal/generic_linux.rs
Normal file
218
src/sal/generic_linux.rs
Normal 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();
|
||||
}
|
||||
}
|
||||
185
src/sal/heuristic/discovery.rs
Normal file
185
src/sal/heuristic/discovery.rs
Normal file
@@ -0,0 +1,185 @@
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
use std::time::Duration;
|
||||
use std::thread;
|
||||
use std::sync::mpsc;
|
||||
use crate::sal::heuristic::schema::{SensorDiscovery, ActuatorDiscovery, Conflict};
|
||||
use tracing::{debug, warn};
|
||||
|
||||
/// Strongly-typed findings about the current system.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct SystemFactSheet {
|
||||
pub vendor: String,
|
||||
pub model: String,
|
||||
pub temp_path: Option<PathBuf>,
|
||||
pub fan_paths: Vec<PathBuf>,
|
||||
pub rapl_paths: Vec<PathBuf>,
|
||||
pub active_conflicts: Vec<String>, // List of conflict IDs found active
|
||||
}
|
||||
|
||||
/// Probes the system for hardware sensors, actuators, and service conflicts.
|
||||
pub fn discover_facts(
|
||||
sensors: &SensorDiscovery,
|
||||
actuators: &ActuatorDiscovery,
|
||||
conflicts: &[Conflict]
|
||||
) -> SystemFactSheet {
|
||||
let (vendor, model) = read_dmi_info();
|
||||
|
||||
debug!("DMI Identity: Vendor='{}', Model='{}'", vendor, model);
|
||||
|
||||
let (temp_path, fan_paths) = discover_hwmon(sensors);
|
||||
let rapl_paths = discover_rapl(actuators);
|
||||
|
||||
let mut active_conflicts = Vec::new();
|
||||
for conflict in conflicts {
|
||||
for service in &conflict.services {
|
||||
if is_service_active(service) {
|
||||
debug!("Detected active conflict: {} (Service: {})", conflict.id, service);
|
||||
active_conflicts.push(conflict.id.clone());
|
||||
break; // Found one service in this conflict, move to next conflict
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SystemFactSheet {
|
||||
vendor,
|
||||
model,
|
||||
temp_path,
|
||||
fan_paths,
|
||||
rapl_paths,
|
||||
active_conflicts,
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads DMI information from sysfs with a safety timeout.
|
||||
fn read_dmi_info() -> (String, String) {
|
||||
let vendor = read_sysfs_with_timeout(Path::new("/sys/class/dmi/id/sys_vendor"), Duration::from_millis(100))
|
||||
.unwrap_or_else(|| "Unknown".to_string());
|
||||
let model = read_sysfs_with_timeout(Path::new("/sys/class/dmi/id/product_name"), Duration::from_millis(100))
|
||||
.unwrap_or_else(|| "Unknown".to_string());
|
||||
(vendor, model)
|
||||
}
|
||||
|
||||
/// Discovers hwmon sensors by matching labels and prioritizing drivers.
|
||||
fn discover_hwmon(cfg: &SensorDiscovery) -> (Option<PathBuf>, Vec<PathBuf>) {
|
||||
let mut temp_candidates = Vec::new();
|
||||
let mut fan_candidates = Vec::new();
|
||||
|
||||
let hwmon_base = Path::new("/sys/class/hwmon");
|
||||
let entries = match fs::read_dir(hwmon_base) {
|
||||
Ok(e) => e,
|
||||
Err(e) => {
|
||||
warn!("Could not read /sys/class/hwmon: {}", e);
|
||||
return (None, Vec::new());
|
||||
}
|
||||
};
|
||||
|
||||
for entry in entries.flatten() {
|
||||
let hwmon_path = entry.path();
|
||||
|
||||
let driver_name = read_sysfs_with_timeout(&hwmon_path.join("name"), Duration::from_millis(100))
|
||||
.unwrap_or_default();
|
||||
|
||||
let priority = cfg.hwmon_priority
|
||||
.iter()
|
||||
.position(|p| p == &driver_name)
|
||||
.unwrap_or(usize::MAX);
|
||||
|
||||
if let Ok(hw_entries) = fs::read_dir(&hwmon_path) {
|
||||
for hw_entry in hw_entries.flatten() {
|
||||
let file_name = hw_entry.file_name().into_string().unwrap_or_default();
|
||||
|
||||
// Temperature Sensors
|
||||
if file_name.starts_with("temp") && file_name.ends_with("_label") {
|
||||
if let Some(label) = read_sysfs_with_timeout(&hw_entry.path(), Duration::from_millis(100)) {
|
||||
if cfg.temp_labels.iter().any(|l| label.contains(l)) {
|
||||
let input_path = hwmon_path.join(file_name.replace("_label", "_input"));
|
||||
if input_path.exists() {
|
||||
temp_candidates.push((priority, input_path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fan Sensors
|
||||
if file_name.starts_with("fan") && file_name.ends_with("_label") {
|
||||
if let Some(label) = read_sysfs_with_timeout(&hw_entry.path(), Duration::from_millis(100)) {
|
||||
if cfg.fan_labels.iter().any(|l| label.contains(l)) {
|
||||
let input_path = hwmon_path.join(file_name.replace("_label", "_input"));
|
||||
if input_path.exists() {
|
||||
fan_candidates.push((priority, input_path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
temp_candidates.sort_by_key(|(p, _)| *p);
|
||||
fan_candidates.sort_by_key(|(p, _)| *p);
|
||||
|
||||
let best_temp = temp_candidates.first().map(|(_, p)| p.clone());
|
||||
let best_fans = fan_candidates.into_iter().map(|(_, p)| p).collect();
|
||||
|
||||
(best_temp, best_fans)
|
||||
}
|
||||
|
||||
/// Discovers RAPL powercap paths.
|
||||
fn discover_rapl(cfg: &ActuatorDiscovery) -> Vec<PathBuf> {
|
||||
let mut paths = Vec::new();
|
||||
let powercap_base = Path::new("/sys/class/powercap");
|
||||
|
||||
let entries = match fs::read_dir(powercap_base) {
|
||||
Ok(e) => e,
|
||||
Err(_) => return Vec::new(),
|
||||
};
|
||||
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
let dir_name = entry.file_name().into_string().unwrap_or_default();
|
||||
|
||||
if cfg.rapl_paths.contains(&dir_name) {
|
||||
paths.push(path);
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(name) = read_sysfs_with_timeout(&path.join("name"), Duration::from_millis(100)) {
|
||||
if cfg.rapl_paths.iter().any(|p| p == &name) {
|
||||
paths.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
paths
|
||||
}
|
||||
|
||||
/// Checks if a systemd service is currently active.
|
||||
pub fn is_service_active(service: &str) -> bool {
|
||||
let status = Command::new("systemctl")
|
||||
.arg("is-active")
|
||||
.arg("--quiet")
|
||||
.arg(service)
|
||||
.status();
|
||||
|
||||
match status {
|
||||
Ok(s) => s.success(),
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to read a sysfs file with a timeout.
|
||||
fn read_sysfs_with_timeout(path: &Path, timeout: Duration) -> Option<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(Ok(content)) => Some(content),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
60
src/sal/heuristic/engine.rs
Normal file
60
src/sal/heuristic/engine.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use miette::{Result, IntoDiagnostic, Context};
|
||||
use std::fs;
|
||||
use regex::Regex;
|
||||
use tracing::{info, debug};
|
||||
|
||||
use crate::sal::traits::PlatformSal;
|
||||
use crate::sal::dell_xps_9380::DellXps9380Sal;
|
||||
use crate::sal::generic_linux::GenericLinuxSal;
|
||||
use crate::sal::heuristic::schema::HardwareDb;
|
||||
use crate::sal::heuristic::discovery::{discover_facts};
|
||||
|
||||
pub struct HeuristicEngine;
|
||||
|
||||
impl HeuristicEngine {
|
||||
/// Loads the hardware database, probes the system, and builds the appropriate SAL.
|
||||
pub fn detect_and_build() -> Result<Box<dyn PlatformSal>> {
|
||||
// 1. Load Hardware DB
|
||||
let db_path = "assets/hardware_db.toml";
|
||||
let db_content = fs::read_to_string(db_path)
|
||||
.into_diagnostic()
|
||||
.with_context(|| format!("Failed to read hardware database at {}", db_path))?;
|
||||
let db: HardwareDb = toml::from_str(&db_content)
|
||||
.into_diagnostic()
|
||||
.context("Failed to parse hardware_db.toml")?;
|
||||
|
||||
// 2. Discover Facts
|
||||
let facts = discover_facts(&db.discovery.sensors, &db.discovery.actuators, &db.conflicts);
|
||||
info!("System Identity: {} {}", facts.vendor, facts.model);
|
||||
|
||||
// 3. Routing Logic
|
||||
|
||||
// --- Special Case: Dell XPS 13 9380 ---
|
||||
if is_match(&facts.vendor, "(?i)Dell.*") && is_match(&facts.model, "(?i)XPS.*13.*9380.*") {
|
||||
info!("Specialized SAL Match Found: Dell XPS 13 9380");
|
||||
let sal = DellXps9380Sal::init().map_err(|e| miette::miette!(e))?;
|
||||
return Ok(Box::new(sal));
|
||||
}
|
||||
|
||||
// --- Fallback: Generic Linux SAL ---
|
||||
debug!("No specialized SAL match. Falling back to GenericLinuxSal with DB quirks.");
|
||||
|
||||
// Validation: Ensure we found at least a temperature sensor if required
|
||||
if facts.temp_path.is_none() {
|
||||
return Err(miette::miette!("No temperature sensor discovered. Generic fallback impossible."));
|
||||
}
|
||||
if facts.rapl_paths.is_empty() {
|
||||
return Err(miette::miette!("No RAPL power interface discovered. Generic fallback impossible."));
|
||||
}
|
||||
|
||||
Ok(Box::new(GenericLinuxSal::new(facts, db)))
|
||||
}
|
||||
}
|
||||
|
||||
fn is_match(input: &str, pattern: &str) -> bool {
|
||||
if let Ok(re) = Regex::new(pattern) {
|
||||
re.is_match(input)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
3
src/sal/heuristic/mod.rs
Normal file
3
src/sal/heuristic/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod schema;
|
||||
pub mod discovery;
|
||||
pub mod engine;
|
||||
90
src/sal/heuristic/schema.rs
Normal file
90
src/sal/heuristic/schema.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct HardwareDb {
|
||||
pub metadata: Metadata,
|
||||
pub conflicts: Vec<Conflict>,
|
||||
pub ecosystems: HashMap<String, Ecosystem>,
|
||||
pub quirks: Vec<Quirk>,
|
||||
pub discovery: Discovery,
|
||||
pub preflight_checks: Vec<PreflightCheck>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Metadata {
|
||||
pub version: String,
|
||||
pub updated: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Conflict {
|
||||
pub id: String,
|
||||
pub services: Vec<String>,
|
||||
pub contention: String,
|
||||
pub severity: String,
|
||||
pub fix_action: String,
|
||||
pub help_text: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Ecosystem {
|
||||
pub vendor_regex: String,
|
||||
pub polling_cap_ms: Option<u64>,
|
||||
pub drivers: Option<Vec<String>>,
|
||||
pub fan_manual_mode_cmd: Option<String>,
|
||||
pub fan_auto_mode_cmd: Option<String>,
|
||||
pub safety_register: Option<String>,
|
||||
pub lap_mode_path: Option<String>,
|
||||
pub profiles_path: Option<String>,
|
||||
pub ec_write_required: Option<bool>,
|
||||
pub thermal_policy_path: Option<String>,
|
||||
pub policy_map: Option<HashMap<String, i32>>,
|
||||
pub msr_lock_register: Option<String>,
|
||||
pub msr_lock_bit: Option<u32>,
|
||||
pub fan_boost_path: Option<String>,
|
||||
pub ec_tool: Option<String>,
|
||||
pub optimization: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Quirk {
|
||||
pub model_regex: String,
|
||||
pub id: String,
|
||||
pub issue: String,
|
||||
pub action: String,
|
||||
pub monitor_msr: Option<String>,
|
||||
pub reset_bit: Option<u32>,
|
||||
pub trigger_path: Option<String>,
|
||||
pub trigger_value: Option<String>,
|
||||
pub target_path: Option<String>,
|
||||
pub format: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Discovery {
|
||||
pub sensors: SensorDiscovery,
|
||||
pub actuators: ActuatorDiscovery,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct SensorDiscovery {
|
||||
pub temp_labels: Vec<String>,
|
||||
pub fan_labels: Vec<String>,
|
||||
pub hwmon_priority: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct ActuatorDiscovery {
|
||||
pub rapl_paths: Vec<String>,
|
||||
pub amd_energy_paths: Vec<String>,
|
||||
pub governor_files: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct PreflightCheck {
|
||||
pub name: String,
|
||||
pub check_cmd: String,
|
||||
pub fail_help: String,
|
||||
}
|
||||
@@ -1,8 +1,15 @@
|
||||
use super::traits::{PreflightAuditor, EnvironmentGuard, SensorBus, ActuatorBus, HardwareWatchdog, AuditStep};
|
||||
use anyhow::Result;
|
||||
|
||||
pub struct MockAuditor;
|
||||
impl PreflightAuditor for MockAuditor {
|
||||
pub struct MockSal;
|
||||
|
||||
impl MockSal {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl PreflightAuditor for MockSal {
|
||||
fn audit(&self) -> Box<dyn Iterator<Item = AuditStep> + '_> {
|
||||
let steps = vec![
|
||||
AuditStep {
|
||||
@@ -18,32 +25,16 @@ impl PreflightAuditor for MockAuditor {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MockGuard {
|
||||
pub suppressed: bool,
|
||||
}
|
||||
impl MockGuard {
|
||||
pub fn new() -> Self {
|
||||
Self { suppressed: false }
|
||||
}
|
||||
}
|
||||
impl EnvironmentGuard for MockGuard {
|
||||
impl EnvironmentGuard for MockSal {
|
||||
fn suppress(&mut self) -> Result<()> {
|
||||
self.suppressed = true;
|
||||
Ok(())
|
||||
}
|
||||
fn restore(&mut self) -> Result<()> {
|
||||
self.suppressed = false;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl Drop for MockGuard {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.restore();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MockSensorBus;
|
||||
impl SensorBus for MockSensorBus {
|
||||
impl SensorBus for MockSal {
|
||||
fn get_temp(&self) -> Result<f32> {
|
||||
Ok(42.0)
|
||||
}
|
||||
@@ -58,8 +49,7 @@ impl SensorBus for MockSensorBus {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MockActuatorBus;
|
||||
impl ActuatorBus for MockActuatorBus {
|
||||
impl ActuatorBus for MockSal {
|
||||
fn set_fan_mode(&self, _mode: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
@@ -71,8 +61,7 @@ impl ActuatorBus for MockActuatorBus {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MockWatchdog;
|
||||
impl HardwareWatchdog for MockWatchdog {
|
||||
impl HardwareWatchdog for MockSal {
|
||||
fn check_emergency(&self) -> Result<bool> {
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
pub mod traits;
|
||||
pub mod mock;
|
||||
pub mod dell_xps_9380;
|
||||
pub mod generic_linux;
|
||||
pub mod heuristic;
|
||||
|
||||
@@ -48,7 +48,7 @@ impl<T: PreflightAuditor + ?Sized> PreflightAuditor for Arc<T> {
|
||||
}
|
||||
|
||||
/// Suppresses conflicting daemons (tlp, thermald).
|
||||
pub trait EnvironmentGuard {
|
||||
pub trait EnvironmentGuard: Send + Sync {
|
||||
fn suppress(&mut self) -> Result<()>;
|
||||
fn restore(&mut self) -> Result<()>;
|
||||
}
|
||||
@@ -77,7 +77,7 @@ impl<T: SensorBus + ?Sized> SensorBus for Arc<T> {
|
||||
}
|
||||
|
||||
/// Write-only interface for hardware commands.
|
||||
pub trait ActuatorBus {
|
||||
pub trait ActuatorBus: Send + Sync {
|
||||
fn set_fan_mode(&self, mode: &str) -> Result<()>;
|
||||
fn set_sustained_power_limit(&self, watts: f32) -> Result<()>;
|
||||
fn set_burst_power_limit(&self, watts: f32) -> Result<()>;
|
||||
@@ -96,7 +96,7 @@ impl<T: ActuatorBus + ?Sized> ActuatorBus for Arc<T> {
|
||||
}
|
||||
|
||||
/// Concurrent monitor for catastrophic states.
|
||||
pub trait HardwareWatchdog {
|
||||
pub trait HardwareWatchdog: Send + Sync {
|
||||
fn check_emergency(&self) -> Result<bool>;
|
||||
}
|
||||
|
||||
@@ -105,3 +105,8 @@ impl<T: HardwareWatchdog + ?Sized> HardwareWatchdog for Arc<T> {
|
||||
(**self).check_emergency()
|
||||
}
|
||||
}
|
||||
|
||||
/// Aggregate trait for a complete platform implementation.
|
||||
pub trait PlatformSal: PreflightAuditor + SensorBus + ActuatorBus + EnvironmentGuard + HardwareWatchdog {}
|
||||
|
||||
impl<T: PreflightAuditor + SensorBus + ActuatorBus + EnvironmentGuard + HardwareWatchdog + ?Sized> PlatformSal for T {}
|
||||
|
||||
Reference in New Issue
Block a user