From e9ea77b8a5d4f4cf8d0e8198c32f7d9cc5f31a3d Mon Sep 17 00:00:00 2001 From: bktech2021 Date: Mon, 9 Oct 2023 15:40:35 +0300 Subject: [PATCH 1/4] solve ratio problem solves the ratio problem for laptops and fanless cards. --- src/app.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/app.rs b/src/app.rs index e177334..9f1defa 100644 --- a/src/app.rs +++ b/src/app.rs @@ -154,7 +154,9 @@ pub fn run( selected_gpu = usize::from(n - 1) } KeyCode::Char('p') => { - // re-scan pci tree to let driver discover new devices (only works as sudo) + //commented out because discover_gpus not implemented + + /* // re-scan pci tree to let driver discover new devices (only works as sudo) match nvml.discover_gpus(PciInfo { bus: 0, bus_id: "".into(), @@ -174,7 +176,7 @@ pub fn run( gpu_list = crate::gpu::try_init_gpus(&nvml, lh)?; if selected_gpu >= gpu_list.len() { selected_gpu = 0; - } + } */ } _ => {} } @@ -198,9 +200,14 @@ fn draw_fan_speed<'d>(gpu: &GpuInfo<'d>) -> Gauge<'d> { .map(|u| u as f64) .sum::() / temps as f64; - - let percentage = (avg / 100.).clamp(0., 1.0); - let label = format!("{:.1}%", avg); + let percentage = match avg.is_nan() { + true => 0.0, + false => avg + }; + let label = match avg.is_nan() { + true => "No fan".to_string(), + false => format!("{:.1}%", avg) + }; let spanned_label = Span::styled(label, Style::new().white().bold().bg(Color::Black)); Gauge::default() From b53271f8676b33ed3428618a9c1514a22b3c9ca6 Mon Sep 17 00:00:00 2001 From: bktech2021 Date: Mon, 9 Oct 2023 15:51:42 +0300 Subject: [PATCH 2/4] no fan -> no fans changed the message when there is no fans, no fan to no fans --- src/app.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app.rs b/src/app.rs index 9f1defa..8970cd1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -205,7 +205,7 @@ fn draw_fan_speed<'d>(gpu: &GpuInfo<'d>) -> Gauge<'d> { false => avg }; let label = match avg.is_nan() { - true => "No fan".to_string(), + true => "No fans".to_string(), false => format!("{:.1}%", avg) }; let spanned_label = Span::styled(label, Style::new().white().bold().bg(Color::Black)); From acfa8292c34d5acda614f55e4c8686849a3da14f Mon Sep 17 00:00:00 2001 From: bktech2021 Date: Tue, 10 Oct 2023 08:58:45 +0300 Subject: [PATCH 3/4] discover_gpus cross-platform issue --- src/app.rs | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/app.rs b/src/app.rs index 8970cd1..bd8b3ba 100644 --- a/src/app.rs +++ b/src/app.rs @@ -32,6 +32,7 @@ pub fn run( let mut gpu_list = crate::gpu::try_init_gpus(&nvml, lh)?; let mut selected_gpu: usize = 0; + let mut have_fans: bool = true; loop { _ = terminal.draw(|f| { @@ -44,8 +45,12 @@ pub fn run( .constraints([Constraint::Min(0), Constraint::Length(1)]) .split(f.size()); + #[cfg(target_os = "linux")] f.render_widget(Paragraph::new("q to quit, p to rescan devices"), layout[1]); + #[cfg(target_os = "windows")] + f.render_widget(Paragraph::new("q to quit"), layout[1]); + layout[0] } else { let layout = Layout::default() @@ -138,9 +143,15 @@ pub fn run( let temp_gauge = draw_gpu_die_temp(gpu); f.render_widget(temp_gauge, chunks[1]); - // Fanspeed: - let fan_gauge = draw_fan_speed(gpu); - f.render_widget(fan_gauge, chunks[2]); + // Fan speed: + if have_fans { + match draw_fan_speed(gpu) { + Ok(gauge) => { + f.render_widget(gauge, chunks[2]); + } + Err(_) => have_fans = false, + } + } } })?; @@ -154,9 +165,8 @@ pub fn run( selected_gpu = usize::from(n - 1) } KeyCode::Char('p') => { - //commented out because discover_gpus not implemented - - /* // re-scan pci tree to let driver discover new devices (only works as sudo) + // re-scan pci tree to let driver discover new devices (only works as sudo) + #[cfg(target_os = "linux")] match nvml.discover_gpus(PciInfo { bus: 0, bus_id: "".into(), @@ -176,7 +186,7 @@ pub fn run( gpu_list = crate::gpu::try_init_gpus(&nvml, lh)?; if selected_gpu >= gpu_list.len() { selected_gpu = 0; - } */ + } } _ => {} } @@ -193,29 +203,28 @@ pub fn run( Ok(()) } -fn draw_fan_speed<'d>(gpu: &GpuInfo<'d>) -> Gauge<'d> { +fn draw_fan_speed<'d>(gpu: &GpuInfo<'d>) -> Result, ()> { let temps = gpu.inner.num_fans().map_or(0, |fc| fc); let avg = (0..temps as usize) .flat_map(|v| gpu.inner.fan_speed(v as u32)) .map(|u| u as f64) .sum::() / temps as f64; - let percentage = match avg.is_nan() { - true => 0.0, - false => avg - }; - let label = match avg.is_nan() { - true => "No fans".to_string(), - false => format!("{:.1}%", avg) - }; + + if avg.is_nan() { + return Err(()); + } + + let percentage = (avg / 100.).clamp(0., 1.0); + let label = format!("{:.1}%", avg); let spanned_label = Span::styled(label, Style::new().white().bold().bg(Color::Black)); - Gauge::default() + Ok(Gauge::default() .block(Block::default().borders(Borders::ALL).title("Fan Speed")) .gauge_style(calculate_severity(percentage).style_for()) .label(spanned_label) .set_style(Style::default()) - .ratio(percentage) + .ratio(percentage)) } fn draw_gpu_die_temp<'d>(gpu: &GpuInfo<'d>) -> Gauge<'d> { @@ -226,7 +235,7 @@ fn draw_gpu_die_temp<'d>(gpu: &GpuInfo<'d>) -> Gauge<'d> { let label = format!("{:.2}°C", gpu_die_temperature); let spanned_label = Span::styled(label, Style::new().white().bold().bg(Color::Black)); - let temp_ratio = (gpu_die_temperature as f64 / 100.).clamp(0., 1.0); + let temp_ratio = (gpu_die_temperature as f64 / 100.).clamp(0.0, 1.0); Gauge::default() .block(Block::default().borders(Borders::ALL).title("Temp")) From 4d83f6c5f642d1d9eb15ab52d6a09046db2b5122 Mon Sep 17 00:00:00 2001 From: bktech2021 Date: Tue, 10 Oct 2023 09:16:18 +0300 Subject: [PATCH 4/4] small fixes --- src/app.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index bd8b3ba..c17e7c7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -32,7 +32,9 @@ pub fn run( let mut gpu_list = crate::gpu::try_init_gpus(&nvml, lh)?; let mut selected_gpu: usize = 0; - let mut have_fans: bool = true; + let mut have_fans: bool = gpu_list + .iter() + .any(|gpu| gpu.inner.num_fans().map_or(0, |fc| fc) != 0); loop { _ = terminal.draw(|f| { @@ -145,12 +147,8 @@ pub fn run( // Fan speed: if have_fans { - match draw_fan_speed(gpu) { - Ok(gauge) => { - f.render_widget(gauge, chunks[2]); - } - Err(_) => have_fans = false, - } + let gauge = draw_fan_speed(gpu); + f.render_widget(gauge, chunks[2]); } } })?; @@ -164,9 +162,10 @@ pub fn run( KeyCode::F(n) if (1..=gpu_list.len()).contains(&n.into()) => { selected_gpu = usize::from(n - 1) } + + #[cfg(target_os = "linux")] KeyCode::Char('p') => { // re-scan pci tree to let driver discover new devices (only works as sudo) - #[cfg(target_os = "linux")] match nvml.discover_gpus(PciInfo { bus: 0, bus_id: "".into(), @@ -175,13 +174,15 @@ pub fn run( pci_device_id: 0, pci_sub_system_id: Some(0), }) { - Ok(()) => lh.debug("Re-scanned PCI tree"), + Ok(()) => { + lh.debug("Re-scanned PCI tree"); + have_fans = true; + } Err(e @ (NvmlError::OperatingSystem | NvmlError::NoPermission)) => { lh.debug(&format!("Failed to re-scan PCI tree: {e}")); } Err(e) => return Err(e.into()), } - // re-scan for devices gpu_list = crate::gpu::try_init_gpus(&nvml, lh)?; if selected_gpu >= gpu_list.len() { @@ -203,7 +204,7 @@ pub fn run( Ok(()) } -fn draw_fan_speed<'d>(gpu: &GpuInfo<'d>) -> Result, ()> { +fn draw_fan_speed<'d>(gpu: &GpuInfo<'d>) -> Gauge<'d> { let temps = gpu.inner.num_fans().map_or(0, |fc| fc); let avg = (0..temps as usize) .flat_map(|v| gpu.inner.fan_speed(v as u32)) @@ -211,20 +212,16 @@ fn draw_fan_speed<'d>(gpu: &GpuInfo<'d>) -> Result, ()> { .sum::() / temps as f64; - if avg.is_nan() { - return Err(()); - } - let percentage = (avg / 100.).clamp(0., 1.0); let label = format!("{:.1}%", avg); let spanned_label = Span::styled(label, Style::new().white().bold().bg(Color::Black)); - Ok(Gauge::default() + Gauge::default() .block(Block::default().borders(Borders::ALL).title("Fan Speed")) .gauge_style(calculate_severity(percentage).style_for()) .label(spanned_label) .set_style(Style::default()) - .ratio(percentage)) + .ratio(percentage) } fn draw_gpu_die_temp<'d>(gpu: &GpuInfo<'d>) -> Gauge<'d> {