impemented mock testing

This commit is contained in:
2026-02-26 17:11:42 +01:00
parent f76acd6256
commit 667d94af7a
21 changed files with 480 additions and 110 deletions

View File

@@ -5,7 +5,7 @@ use std::time::{Duration};
use std::thread;
use std::sync::mpsc;
use std::collections::HashMap;
use crate::sal::heuristic::schema::{SensorDiscovery, ActuatorDiscovery, Conflict, Discovery};
use crate::sal::heuristic::schema::{SensorDiscovery, ActuatorDiscovery, Conflict, Discovery, Benchmarking};
use tracing::{debug, warn};
/// Registry of dynamically discovered paths for configs and tools.
@@ -25,19 +25,22 @@ pub struct SystemFactSheet {
pub rapl_paths: Vec<PathBuf>,
pub active_conflicts: Vec<String>,
pub paths: PathRegistry,
pub bench_config: Option<Benchmarking>,
}
/// Probes the system for hardware sensors, actuators, service conflicts, and paths.
pub fn discover_facts(
base_path: &Path,
discovery: &Discovery,
conflicts: &[Conflict]
conflicts: &[Conflict],
bench_config: Benchmarking,
) -> SystemFactSheet {
let (vendor, model) = read_dmi_info();
let (vendor, model) = read_dmi_info(base_path);
debug!("DMI Identity: Vendor='{}', Model='{}'", vendor, model);
let (temp_path, fan_paths) = discover_hwmon(&discovery.sensors);
let rapl_paths = discover_rapl(&discovery.actuators);
let (temp_path, fan_paths) = discover_hwmon(base_path, &discovery.sensors);
let rapl_paths = discover_rapl(base_path, &discovery.actuators);
let mut active_conflicts = Vec::new();
for conflict in conflicts {
@@ -50,7 +53,7 @@ pub fn discover_facts(
}
}
let paths = discover_paths(discovery);
let paths = discover_paths(base_path, discovery);
SystemFactSheet {
vendor,
@@ -60,10 +63,11 @@ pub fn discover_facts(
rapl_paths,
active_conflicts,
paths,
bench_config: Some(bench_config),
}
}
fn discover_paths(discovery: &Discovery) -> PathRegistry {
fn discover_paths(base_path: &Path, discovery: &Discovery) -> PathRegistry {
let mut registry = PathRegistry::default();
// 1. Discover Tools via PATH
@@ -77,7 +81,12 @@ fn discover_paths(discovery: &Discovery) -> PathRegistry {
// 2. Discover Configs via existence check
for (id, candidates) in &discovery.configs {
for candidate in candidates {
let path = PathBuf::from(candidate);
let path = if candidate.starts_with('/') {
base_path.join(&candidate[1..])
} else {
base_path.join(candidate)
};
if path.exists() {
debug!("Discovered config: {} -> {:?}", id, path);
registry.configs.insert(id.clone(), path);
@@ -96,24 +105,24 @@ fn discover_paths(discovery: &Discovery) -> PathRegistry {
}
/// 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))
fn read_dmi_info(base_path: &Path) -> (String, String) {
let vendor = read_sysfs_with_timeout(&base_path.join("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))
let model = read_sysfs_with_timeout(&base_path.join("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>) {
fn discover_hwmon(base_path: &Path, 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) {
let hwmon_base = base_path.join("sys/class/hwmon");
let entries = match fs::read_dir(&hwmon_base) {
Ok(e) => e,
Err(e) => {
warn!("Could not read /sys/class/hwmon: {}", e);
warn!("Could not read {:?}: {}", hwmon_base, e);
return (None, Vec::new());
}
};
@@ -170,11 +179,11 @@ fn discover_hwmon(cfg: &SensorDiscovery) -> (Option<PathBuf>, Vec<PathBuf>) {
}
/// Discovers RAPL powercap paths.
fn discover_rapl(cfg: &ActuatorDiscovery) -> Vec<PathBuf> {
fn discover_rapl(base_path: &Path, cfg: &ActuatorDiscovery) -> Vec<PathBuf> {
let mut paths = Vec::new();
let powercap_base = Path::new("/sys/class/powercap");
let powercap_base = base_path.join("sys/class/powercap");
let entries = match fs::read_dir(powercap_base) {
let entries = match fs::read_dir(&powercap_base) {
Ok(e) => e,
Err(_) => return Vec::new(),
};

View File

@@ -3,7 +3,7 @@ use std::fs;
use regex::Regex;
use tracing::{info, debug};
use crate::sal::traits::PlatformSal;
use crate::sal::traits::{PlatformSal, EnvironmentCtx};
use crate::sal::dell_xps_9380::DellXps9380Sal;
use crate::sal::generic_linux::GenericLinuxSal;
use crate::sal::heuristic::schema::HardwareDb;
@@ -13,7 +13,7 @@ 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>, SystemFactSheet)> {
pub fn detect_and_build(ctx: EnvironmentCtx) -> Result<(Box<dyn PlatformSal>, SystemFactSheet)> {
// 1. Load Hardware DB
let db_path = "assets/hardware_db.toml";
let db_content = fs::read_to_string(db_path)
@@ -24,7 +24,7 @@ impl HeuristicEngine {
.context("Failed to parse hardware_db.toml")?;
// 2. Discover Facts
let facts = discover_facts(&db.discovery, &db.conflicts);
let facts = discover_facts(&ctx.sysfs_base, &db.discovery, &db.conflicts, db.benchmarking.clone());
info!("System Identity: {} {}", facts.vendor, facts.model);
// 3. Routing Logic
@@ -32,7 +32,7 @@ impl HeuristicEngine {
// --- 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(facts.clone()).map_err(|e| miette::miette!(e))?;
let sal = DellXps9380Sal::init(ctx, facts.clone()).map_err(|e| miette::miette!(e))?;
return Ok((Box::new(sal), facts));
}
@@ -47,7 +47,7 @@ impl HeuristicEngine {
return Err(miette::miette!("No RAPL power interface discovered. Generic fallback impossible."));
}
Ok((Box::new(GenericLinuxSal::new(facts.clone(), db)), facts))
Ok((Box::new(GenericLinuxSal::new(ctx, facts.clone(), db)), facts))
}
}

View File

@@ -8,6 +8,7 @@ pub struct HardwareDb {
pub ecosystems: HashMap<String, Ecosystem>,
pub quirks: Vec<Quirk>,
pub discovery: Discovery,
pub benchmarking: Benchmarking,
pub preflight_checks: Vec<PreflightCheck>,
}
@@ -72,6 +73,15 @@ pub struct Discovery {
pub tools: HashMap<String, String>,
}
#[derive(Debug, Deserialize, Clone)]
pub struct Benchmarking {
pub idle_duration_s: u64,
pub stress_duration_min_s: u64,
pub stress_duration_max_s: u64,
pub cool_down_s: u64,
pub power_steps_watts: Vec<f32>,
}
#[derive(Debug, Deserialize, Clone)]
pub struct SensorDiscovery {
pub temp_labels: Vec<String>,