cleanup of gpu and config gen
This commit is contained in:
+12
-24
@@ -397,39 +397,27 @@ impl Config {
|
|||||||
/// Check if a module is enabled in the configuration.
|
/// Check if a module is enabled in the configuration.
|
||||||
/// Returns false if the module is explicitly disabled; true if enabled or unknown.
|
/// Returns false if the module is explicitly disabled; true if enabled or unknown.
|
||||||
pub fn is_module_enabled(&self, module_name: &str) -> bool {
|
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 {
|
match module_name {
|
||||||
#[cfg(feature = "mod-network")]
|
$(
|
||||||
"net" | "network" => self.network.enabled,
|
#[cfg(feature = $feature)]
|
||||||
#[cfg(feature = "mod-hardware")]
|
$($name)|+ => self.$config.enabled,
|
||||||
"cpu" => self.cpu.enabled,
|
)*
|
||||||
#[cfg(feature = "mod-hardware")]
|
// Dispatch-only modules (no watch channel)
|
||||||
"mem" | "memory" => self.memory.enabled,
|
#[cfg(feature = "mod-audio")]
|
||||||
#[cfg(feature = "mod-hardware")]
|
"mic" => self.audio.enabled,
|
||||||
"gpu" => self.gpu.enabled,
|
|
||||||
#[cfg(feature = "mod-hardware")]
|
|
||||||
"sys" => self.sys.enabled,
|
|
||||||
#[cfg(feature = "mod-hardware")]
|
|
||||||
"disk" => self.disk.enabled,
|
|
||||||
#[cfg(feature = "mod-hardware")]
|
#[cfg(feature = "mod-hardware")]
|
||||||
"pool" | "btrfs" => self.pool.enabled,
|
"pool" | "btrfs" => self.pool.enabled,
|
||||||
#[cfg(feature = "mod-hardware")]
|
#[cfg(feature = "mod-hardware")]
|
||||||
"power" => self.power.enabled,
|
"power" => self.power.enabled,
|
||||||
#[cfg(feature = "mod-hardware")]
|
#[cfg(feature = "mod-hardware")]
|
||||||
"game" => self.game.enabled,
|
"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,
|
_ => true,
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
for_each_watched_module!(gen_enabled_match)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate(&self) {
|
pub fn validate(&self) {
|
||||||
|
|||||||
+55
-38
@@ -143,16 +143,50 @@ impl HardwareDaemon {
|
|||||||
async fn poll_gpu(&mut self, gpu: &mut crate::state::GpuState) {
|
async fn poll_gpu(&mut self, gpu: &mut crate::state::GpuState) {
|
||||||
gpu.active = false;
|
gpu.active = false;
|
||||||
|
|
||||||
if (self.gpu_vendor.as_deref() == Some("NVIDIA") || self.gpu_vendor.is_none())
|
match self.gpu_vendor.as_deref() {
|
||||||
&& let Ok(output) = tokio::process::Command::new("nvidia-smi")
|
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([
|
.args([
|
||||||
"--query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu,name",
|
"--query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu,name",
|
||||||
"--format=csv,noheader,nounits",
|
"--format=csv,noheader,nounits",
|
||||||
])
|
])
|
||||||
.output()
|
.output()
|
||||||
.await
|
.await
|
||||||
&& output.status.success()
|
else {
|
||||||
{
|
return;
|
||||||
|
};
|
||||||
|
if !output.status.success() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
if let Some(line) = stdout.lines().next() {
|
if let Some(line) = stdout.lines().next() {
|
||||||
let parts: Vec<&str> = line.split(',').collect();
|
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.vram_total = parts[2].trim().parse::<f64>().unwrap_or(0.0) / 1024.0;
|
||||||
gpu.temp = parts[3].trim().parse().unwrap_or(0.0);
|
gpu.temp = parts[3].trim().parse().unwrap_or(0.0);
|
||||||
gpu.model = parts[4].trim().to_string();
|
gpu.model = parts[4].trim().to_string();
|
||||||
self.gpu_vendor = Some("NVIDIA".to_string());
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.gpu_vendor.as_deref() == Some("AMD")
|
fn poll_amd(gpu: &mut crate::state::GpuState) {
|
||||||
|| self.gpu_vendor.as_deref() == Some("Intel")
|
|
||||||
|| self.gpu_vendor.is_none()
|
|
||||||
{
|
|
||||||
for i in 0..=3 {
|
for i in 0..=3 {
|
||||||
let base = format!("/sys/class/drm/card{}/device", i);
|
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.active = true;
|
||||||
gpu.vendor = "AMD".to_string();
|
gpu.vendor = "AMD".to_string();
|
||||||
gpu.usage = usage_str.trim().parse().unwrap_or(0.0);
|
gpu.usage = usage_str.trim().parse().unwrap_or(0.0);
|
||||||
|
|
||||||
if let Ok(mem_used) =
|
if let Ok(mem_used) = std::fs::read_to_string(format!("{}/mem_info_vram_used", base)) {
|
||||||
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;
|
||||||
gpu.vram_used = mem_used.trim().parse::<f64>().unwrap_or(0.0)
|
|
||||||
/ 1024.0
|
|
||||||
/ 1024.0
|
|
||||||
/ 1024.0;
|
|
||||||
}
|
}
|
||||||
if let Ok(mem_total) =
|
if let Ok(mem_total) = std::fs::read_to_string(format!("{}/mem_info_vram_total", base))
|
||||||
std::fs::read_to_string(format!("{}/mem_info_vram_total", base))
|
|
||||||
{
|
{
|
||||||
gpu.vram_total = mem_total.trim().parse::<f64>().unwrap_or(0.0)
|
gpu.vram_total =
|
||||||
/ 1024.0
|
mem_total.trim().parse::<f64>().unwrap_or(0.0) / 1024.0 / 1024.0 / 1024.0;
|
||||||
/ 1024.0
|
|
||||||
/ 1024.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(entries) = std::fs::read_dir(format!("{}/hwmon", base)) {
|
if let Ok(entries) = std::fs::read_dir(format!("{}/hwmon", base)) {
|
||||||
@@ -212,18 +234,16 @@ impl HardwareDaemon {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
gpu.model = "AMD GPU".to_string();
|
gpu.model = "AMD GPU".to_string();
|
||||||
self.gpu_vendor = Some("AMD".to_string());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if self.gpu_vendor.as_deref() == Some("Intel") || self.gpu_vendor.is_none() {
|
fn poll_intel(gpu: &mut crate::state::GpuState) {
|
||||||
let freq_path =
|
for i in 0..=3 {
|
||||||
if std::path::Path::new(&format!("{}/gt_cur_freq_mhz", base)).exists() {
|
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))
|
Some(format!("{}/gt_cur_freq_mhz", base))
|
||||||
} else if std::path::Path::new(&format!(
|
} else if std::path::Path::new(&format!("/sys/class/drm/card{}/gt_cur_freq_mhz", i))
|
||||||
"/sys/class/drm/card{}/gt_cur_freq_mhz",
|
|
||||||
i
|
|
||||||
))
|
|
||||||
.exists()
|
.exists()
|
||||||
{
|
{
|
||||||
Some(format!("/sys/class/drm/card{}/gt_cur_freq_mhz", i))
|
Some(format!("/sys/class/drm/card{}/gt_cur_freq_mhz", i))
|
||||||
@@ -254,11 +274,8 @@ impl HardwareDaemon {
|
|||||||
gpu.vram_used = 0.0;
|
gpu.vram_used = 0.0;
|
||||||
gpu.vram_total = 0.0;
|
gpu.vram_total = 0.0;
|
||||||
gpu.model = format!("Intel iGPU ({}MHz)", cur_freq);
|
gpu.model = format!("Intel iGPU ({}MHz)", cur_freq);
|
||||||
self.gpu_vendor = Some("Intel".to_string());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user