237 lines
8.9 KiB
Rust
237 lines
8.9 KiB
Rust
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
use std::time::{Duration};
|
|
use std::thread;
|
|
use std::sync::mpsc;
|
|
use std::collections::HashMap;
|
|
use crate::sal::heuristic::schema::{SensorDiscovery, ActuatorDiscovery, Conflict, Discovery, Benchmarking};
|
|
use crate::sys::SyscallRunner;
|
|
use tracing::{debug, warn, info};
|
|
|
|
/// 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 {
|
|
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>,
|
|
pub conflict_services: 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,
|
|
runner: &dyn SyscallRunner,
|
|
discovery: &Discovery,
|
|
conflicts: &[Conflict],
|
|
bench_config: Benchmarking,
|
|
) -> SystemFactSheet {
|
|
let (vendor, model) = read_dmi_info(base_path);
|
|
|
|
debug!("DMI Identity: Vendor='{}', Model='{}'", vendor, model);
|
|
|
|
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();
|
|
let mut conflict_services = Vec::new();
|
|
for conflict in conflicts {
|
|
let mut found_active = false;
|
|
for service in &conflict.services {
|
|
if is_service_active(runner, service) {
|
|
if !found_active {
|
|
debug!("Detected active conflict: {} (Service: {})", conflict.id, service);
|
|
active_conflicts.push(conflict.id.clone());
|
|
found_active = true;
|
|
}
|
|
conflict_services.push(service.clone());
|
|
}
|
|
}
|
|
}
|
|
|
|
let paths = discover_paths(base_path, discovery);
|
|
|
|
SystemFactSheet {
|
|
vendor, model, temp_path, fan_paths, rapl_paths, active_conflicts, conflict_services, paths,
|
|
bench_config: Some(bench_config),
|
|
}
|
|
}
|
|
|
|
fn discover_paths(base_path: &Path, discovery: &Discovery) -> PathRegistry {
|
|
let mut registry = PathRegistry::default();
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
for (id, candidates) in &discovery.configs {
|
|
for candidate in candidates {
|
|
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);
|
|
break;
|
|
}
|
|
}
|
|
if !registry.configs.contains_key(id) {
|
|
if let Some(first) = candidates.first() {
|
|
registry.configs.insert(id.clone(), PathBuf::from(first));
|
|
}
|
|
}
|
|
}
|
|
|
|
registry
|
|
}
|
|
|
|
fn read_dmi_info(base_path: &Path) -> (String, String) {
|
|
let vendor = fs::read_to_string(base_path.join("sys/class/dmi/id/sys_vendor"))
|
|
.map(|s| s.trim().to_string()).unwrap_or_else(|_| "Unknown".to_string());
|
|
let model = fs::read_to_string(base_path.join("sys/class/dmi/id/product_name"))
|
|
.map(|s| s.trim().to_string()).unwrap_or_else(|_| "Unknown".to_string());
|
|
(vendor, model)
|
|
}
|
|
|
|
/// Discovers hwmon sensors by matching labels and prioritizing drivers.
|
|
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 = base_path.join("sys/class/hwmon");
|
|
let entries = fs::read_dir(&hwmon_base).map_err(|e| {
|
|
warn!("Could not read {:?}: {}", hwmon_base, e);
|
|
e
|
|
}).ok();
|
|
|
|
if let Some(entries) = entries {
|
|
for entry in entries.flatten() {
|
|
let hwmon_path = entry.path();
|
|
|
|
// # SAFETY: Read driver name directly. This file is virtual and never blocks.
|
|
// Using a timeout wrapper here was causing discovery to fail if the thread-pool lagged.
|
|
let driver_name = fs::read_to_string(hwmon_path.join("name"))
|
|
.map(|s| s.trim().to_string()).unwrap_or_default();
|
|
|
|
let priority = cfg.hwmon_priority
|
|
.iter()
|
|
.position(|p| driver_name.contains(p))
|
|
.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();
|
|
|
|
// 1. Temperatures
|
|
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(500)) {
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 2. Fans (Label Match)
|
|
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(500)) {
|
|
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() {
|
|
debug!("Discovered fan by label: {:?} (priority {})", input_path, priority);
|
|
fan_candidates.push((priority, input_path));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Fans (Priority Fallback - CRITICAL FOR DELL 9380)
|
|
// If we found a priority driver (e.g., dell_smm), we take every fan*_input we find.
|
|
if priority < usize::MAX && file_name.starts_with("fan") && file_name.ends_with("_input") {
|
|
if !fan_candidates.iter().any(|(_, p)| p == &hw_entry.path()) {
|
|
info!("Heuristic Discovery: Force-adding unlabeled fan sensor from priority driver '{}': {:?}", driver_name, hw_entry.path());
|
|
fan_candidates.push((priority, hw_entry.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: Vec<PathBuf> = fan_candidates.into_iter().map(|(_, p)| p).collect();
|
|
|
|
if best_fans.is_empty() {
|
|
warn!("Heuristic Discovery: No fan RPM sensors found.");
|
|
} else {
|
|
info!("Heuristic Discovery: Final registry contains {} fan sensors.", best_fans.len());
|
|
}
|
|
|
|
(best_temp, best_fans)
|
|
}
|
|
|
|
fn discover_rapl(base_path: &Path, cfg: &ActuatorDiscovery) -> Vec<PathBuf> {
|
|
let mut paths = Vec::new();
|
|
let powercap_base = base_path.join("sys/class/powercap");
|
|
|
|
if let Ok(entries) = fs::read_dir(&powercap_base) {
|
|
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 Ok(name) = fs::read_to_string(path.join("name")) {
|
|
if cfg.rapl_paths.iter().any(|p| p == name.trim()) {
|
|
paths.push(path);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
paths
|
|
}
|
|
|
|
pub fn is_service_active(runner: &dyn SyscallRunner, service: &str) -> bool {
|
|
runner.run("systemctl", &["is-active", "--quiet", service]).is_ok()
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|