added dynamic discovery of configurations

This commit is contained in:
2026-02-26 16:04:34 +01:00
parent 073414a25e
commit 07ccf7ccc7
13 changed files with 188 additions and 130 deletions

View File

@@ -1,12 +1,20 @@
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Duration;
use std::time::{Duration};
use std::thread;
use std::sync::mpsc;
use crate::sal::heuristic::schema::{SensorDiscovery, ActuatorDiscovery, Conflict};
use std::collections::HashMap;
use crate::sal::heuristic::schema::{SensorDiscovery, ActuatorDiscovery, Conflict, Discovery};
use tracing::{debug, warn};
/// Registry of dynamically discovered paths for configs and tools.
#[derive(Debug, Clone, Default)]
pub struct PathRegistry {
pub configs: HashMap<String, PathBuf>,
pub tools: HashMap<String, PathBuf>,
}
/// Strongly-typed findings about the current system.
#[derive(Debug, Clone, Default)]
pub struct SystemFactSheet {
@@ -15,21 +23,21 @@ pub struct SystemFactSheet {
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
pub active_conflicts: Vec<String>,
pub paths: PathRegistry,
}
/// Probes the system for hardware sensors, actuators, and service conflicts.
/// Probes the system for hardware sensors, actuators, service conflicts, and paths.
pub fn discover_facts(
sensors: &SensorDiscovery,
actuators: &ActuatorDiscovery,
discovery: &Discovery,
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 (temp_path, fan_paths) = discover_hwmon(&discovery.sensors);
let rapl_paths = discover_rapl(&discovery.actuators);
let mut active_conflicts = Vec::new();
for conflict in conflicts {
@@ -37,11 +45,13 @@ pub fn discover_facts(
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
break;
}
}
}
let paths = discover_paths(discovery);
SystemFactSheet {
vendor,
model,
@@ -49,9 +59,42 @@ pub fn discover_facts(
fan_paths,
rapl_paths,
active_conflicts,
paths,
}
}
fn discover_paths(discovery: &Discovery) -> PathRegistry {
let mut registry = PathRegistry::default();
// 1. Discover Tools via PATH
for (id, binary_name) in &discovery.tools {
if let Ok(path) = which::which(binary_name) {
debug!("Discovered tool: {} -> {:?}", id, path);
registry.tools.insert(id.clone(), path);
}
}
// 2. Discover Configs via existence check
for (id, candidates) in &discovery.configs {
for candidate in candidates {
let path = PathBuf::from(candidate);
if path.exists() {
debug!("Discovered config: {} -> {:?}", id, path);
registry.configs.insert(id.clone(), path);
break;
}
}
// If not found, use the first one as default if any exist
if !registry.configs.contains_key(id) {
if let Some(first) = candidates.first() {
registry.configs.insert(id.clone(), PathBuf::from(first));
}
}
}
registry
}
/// 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))