cleanup of gpu and config gen

This commit is contained in:
2026-04-05 11:42:08 +02:00
parent d1178ff8ba
commit 198986efac
2 changed files with 145 additions and 140 deletions
+12 -24
View File
@@ -397,39 +397,27 @@ impl Config {
/// Check if a module is enabled in the configuration.
/// Returns false if the module is explicitly disabled; true if enabled or unknown.
pub fn is_module_enabled(&self, module_name: &str) -> bool {
macro_rules! gen_enabled_match {
($( { $feature:literal, $field:ident, $state:ty, [$($name:literal),+], [$($sig_name:literal),+], $module:path, $signal:ident, [$($default_arg:literal),*], $config:ident } )*) => {
match module_name {
#[cfg(feature = "mod-network")]
"net" | "network" => self.network.enabled,
#[cfg(feature = "mod-hardware")]
"cpu" => self.cpu.enabled,
#[cfg(feature = "mod-hardware")]
"mem" | "memory" => self.memory.enabled,
#[cfg(feature = "mod-hardware")]
"gpu" => self.gpu.enabled,
#[cfg(feature = "mod-hardware")]
"sys" => self.sys.enabled,
#[cfg(feature = "mod-hardware")]
"disk" => self.disk.enabled,
$(
#[cfg(feature = $feature)]
$($name)|+ => self.$config.enabled,
)*
// Dispatch-only modules (no watch channel)
#[cfg(feature = "mod-audio")]
"mic" => self.audio.enabled,
#[cfg(feature = "mod-hardware")]
"pool" | "btrfs" => self.pool.enabled,
#[cfg(feature = "mod-hardware")]
"power" => self.power.enabled,
#[cfg(feature = "mod-hardware")]
"game" => self.game.enabled,
#[cfg(feature = "mod-audio")]
"vol" | "audio" | "mic" => self.audio.enabled,
#[cfg(feature = "mod-bt")]
"bt" | "bluetooth" => self.bt.enabled,
#[cfg(feature = "mod-dbus")]
"mpris" => self.mpris.enabled,
#[cfg(feature = "mod-dbus")]
"backlight" => self.backlight.enabled,
#[cfg(feature = "mod-dbus")]
"kbd" | "keyboard" => self.keyboard.enabled,
#[cfg(feature = "mod-dbus")]
"dnd" => self.dnd.enabled,
_ => true,
}
};
}
for_each_watched_module!(gen_enabled_match)
}
pub fn validate(&self) {
+55 -38
View File
@@ -143,16 +143,50 @@ impl HardwareDaemon {
async fn poll_gpu(&mut self, gpu: &mut crate::state::GpuState) {
gpu.active = false;
if (self.gpu_vendor.as_deref() == Some("NVIDIA") || self.gpu_vendor.is_none())
&& let Ok(output) = tokio::process::Command::new("nvidia-smi")
match self.gpu_vendor.as_deref() {
Some("NVIDIA") => {
Self::poll_nvidia(gpu).await;
}
Some("AMD") => {
Self::poll_amd(gpu);
}
Some("Intel") => {
Self::poll_intel(gpu);
}
_ => {
// Detection pass: try each vendor, cache the first that responds.
Self::poll_nvidia(gpu).await;
if gpu.active {
self.gpu_vendor = Some("NVIDIA".to_string());
return;
}
Self::poll_amd(gpu);
if gpu.active {
self.gpu_vendor = Some("AMD".to_string());
return;
}
Self::poll_intel(gpu);
if gpu.active {
self.gpu_vendor = Some("Intel".to_string());
}
}
}
}
async fn poll_nvidia(gpu: &mut crate::state::GpuState) {
let Ok(output) = tokio::process::Command::new("nvidia-smi")
.args([
"--query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu,name",
"--format=csv,noheader,nounits",
])
.output()
.await
&& output.status.success()
{
else {
return;
};
if !output.status.success() {
return;
}
let stdout = String::from_utf8_lossy(&output.stdout);
if let Some(line) = stdout.lines().next() {
let parts: Vec<&str> = line.split(',').collect();
@@ -164,42 +198,30 @@ impl HardwareDaemon {
gpu.vram_total = parts[2].trim().parse::<f64>().unwrap_or(0.0) / 1024.0;
gpu.temp = parts[3].trim().parse().unwrap_or(0.0);
gpu.model = parts[4].trim().to_string();
self.gpu_vendor = Some("NVIDIA".to_string());
return;
}
}
}
if self.gpu_vendor.as_deref() == Some("AMD")
|| self.gpu_vendor.as_deref() == Some("Intel")
|| self.gpu_vendor.is_none()
{
fn poll_amd(gpu: &mut crate::state::GpuState) {
for i in 0..=3 {
let base = format!("/sys/class/drm/card{}/device", i);
let Ok(usage_str) = std::fs::read_to_string(format!("{}/gpu_busy_percent", base))
else {
continue;
};
if (self.gpu_vendor.as_deref() == Some("AMD") || self.gpu_vendor.is_none())
&& let Ok(usage_str) =
std::fs::read_to_string(format!("{}/gpu_busy_percent", base))
{
gpu.active = true;
gpu.vendor = "AMD".to_string();
gpu.usage = usage_str.trim().parse().unwrap_or(0.0);
if let Ok(mem_used) =
std::fs::read_to_string(format!("{}/mem_info_vram_used", base))
{
gpu.vram_used = mem_used.trim().parse::<f64>().unwrap_or(0.0)
/ 1024.0
/ 1024.0
/ 1024.0;
if let Ok(mem_used) = std::fs::read_to_string(format!("{}/mem_info_vram_used", base)) {
gpu.vram_used =
mem_used.trim().parse::<f64>().unwrap_or(0.0) / 1024.0 / 1024.0 / 1024.0;
}
if let Ok(mem_total) =
std::fs::read_to_string(format!("{}/mem_info_vram_total", base))
if let Ok(mem_total) = std::fs::read_to_string(format!("{}/mem_info_vram_total", base))
{
gpu.vram_total = mem_total.trim().parse::<f64>().unwrap_or(0.0)
/ 1024.0
/ 1024.0
/ 1024.0;
gpu.vram_total =
mem_total.trim().parse::<f64>().unwrap_or(0.0) / 1024.0 / 1024.0 / 1024.0;
}
if let Ok(entries) = std::fs::read_dir(format!("{}/hwmon", base)) {
@@ -212,18 +234,16 @@ impl HardwareDaemon {
}
}
gpu.model = "AMD GPU".to_string();
self.gpu_vendor = Some("AMD".to_string());
return;
}
}
if self.gpu_vendor.as_deref() == Some("Intel") || self.gpu_vendor.is_none() {
let freq_path =
if std::path::Path::new(&format!("{}/gt_cur_freq_mhz", base)).exists() {
fn poll_intel(gpu: &mut crate::state::GpuState) {
for i in 0..=3 {
let base = format!("/sys/class/drm/card{}/device", i);
let freq_path = if std::path::Path::new(&format!("{}/gt_cur_freq_mhz", base)).exists() {
Some(format!("{}/gt_cur_freq_mhz", base))
} else if std::path::Path::new(&format!(
"/sys/class/drm/card{}/gt_cur_freq_mhz",
i
))
} else if std::path::Path::new(&format!("/sys/class/drm/card{}/gt_cur_freq_mhz", i))
.exists()
{
Some(format!("/sys/class/drm/card{}/gt_cur_freq_mhz", i))
@@ -254,11 +274,8 @@ impl HardwareDaemon {
gpu.vram_used = 0.0;
gpu.vram_total = 0.0;
gpu.model = format!("Intel iGPU ({}MHz)", cur_freq);
self.gpu_vendor = Some("Intel".to_string());
return;
}
}
}
}
}
}