implemented generic linux sal with heuristics
This commit is contained in:
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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user