impemented mock testing
This commit is contained in:
@@ -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(),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user