Reduce CPU usage with conditional rendering
All checks were successful
Build and Release / build-and-release (push) Successful in 1m12s
All checks were successful
Build and Release / build-and-release (push) Successful in 1m12s
Implement event-driven rendering to dramatically reduce CPU usage. Only render when something actually changes instead of constantly rendering at 20 FPS. Changes: - Increase poll timeout from 50ms to 200ms (5 FPS) - Add needs_render flag to track when rendering is required - Trigger rendering only on: user input, new metrics, heartbeat checks, or terminal resize events - Reset render flag after each render cycle Based on cm-player optimization approach.
This commit is contained in:
parent
3c278351c9
commit
407bc9dbc2
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -279,7 +279,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard"
|
name = "cm-dashboard"
|
||||||
version = "0.1.264"
|
version = "0.1.265"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
@ -301,7 +301,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard-agent"
|
name = "cm-dashboard-agent"
|
||||||
version = "0.1.264"
|
version = "0.1.265"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
@ -325,7 +325,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard-shared"
|
name = "cm-dashboard-shared"
|
||||||
version = "0.1.264"
|
version = "0.1.265"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard-agent"
|
name = "cm-dashboard-agent"
|
||||||
version = "0.1.265"
|
version = "0.1.266"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard"
|
name = "cm-dashboard"
|
||||||
version = "0.1.265"
|
version = "0.1.266"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@ -138,11 +138,12 @@ impl Dashboard {
|
|||||||
let metrics_check_interval = Duration::from_millis(100); // Check for metrics every 100ms
|
let metrics_check_interval = Duration::from_millis(100); // Check for metrics every 100ms
|
||||||
let mut last_heartbeat_check = Instant::now();
|
let mut last_heartbeat_check = Instant::now();
|
||||||
let heartbeat_check_interval = Duration::from_secs(1); // Check for host connectivity every 1 second
|
let heartbeat_check_interval = Duration::from_secs(1); // Check for host connectivity every 1 second
|
||||||
|
let mut needs_render = true; // Track if we need to render
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Handle terminal events (keyboard and mouse input) only if not headless
|
// Handle terminal events (keyboard and mouse input) only if not headless
|
||||||
if !self.headless {
|
if !self.headless {
|
||||||
match event::poll(Duration::from_millis(50)) {
|
match event::poll(Duration::from_millis(200)) {
|
||||||
Ok(true) => {
|
Ok(true) => {
|
||||||
match event::read() {
|
match event::read() {
|
||||||
Ok(event) => {
|
Ok(event) => {
|
||||||
@ -152,6 +153,7 @@ impl Dashboard {
|
|||||||
// Handle keyboard input
|
// Handle keyboard input
|
||||||
match tui_app.handle_input(event) {
|
match tui_app.handle_input(event) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
|
needs_render = true;
|
||||||
// Check if we should quit
|
// Check if we should quit
|
||||||
if tui_app.should_quit() {
|
if tui_app.should_quit() {
|
||||||
info!("Quit requested, exiting dashboard");
|
info!("Quit requested, exiting dashboard");
|
||||||
@ -168,10 +170,11 @@ impl Dashboard {
|
|||||||
if let Err(e) = self.handle_mouse_event(mouse_event) {
|
if let Err(e) = self.handle_mouse_event(mouse_event) {
|
||||||
error!("Error handling mouse event: {}", e);
|
error!("Error handling mouse event: {}", e);
|
||||||
}
|
}
|
||||||
|
needs_render = true;
|
||||||
}
|
}
|
||||||
Event::Resize(_width, _height) => {
|
Event::Resize(_width, _height) => {
|
||||||
// Terminal was resized - just continue and re-render
|
// Terminal was resized - mark for re-render
|
||||||
// The next render will automatically use the new size
|
needs_render = true;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@ -189,38 +192,6 @@ impl Dashboard {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render UI immediately after handling input for responsive feedback
|
|
||||||
if let Some(ref mut terminal) = self.terminal {
|
|
||||||
if let Some(ref mut tui_app) = self.tui_app {
|
|
||||||
// Clear and autoresize terminal to handle any resize events
|
|
||||||
if let Err(e) = terminal.autoresize() {
|
|
||||||
warn!("Error autoresizing terminal: {}", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check minimum terminal size to prevent panics
|
|
||||||
let size = terminal.size().unwrap_or_default();
|
|
||||||
if size.width < 90 || size.height < 15 {
|
|
||||||
// Terminal too small, show error message
|
|
||||||
let msg_text = format!("Terminal too small\n\nMinimum: 90x15\nCurrent: {}x{}", size.width, size.height);
|
|
||||||
let _ = terminal.draw(|frame| {
|
|
||||||
use ratatui::widgets::{Paragraph, Block, Borders};
|
|
||||||
use ratatui::layout::Alignment;
|
|
||||||
let msg = Paragraph::new(msg_text.clone())
|
|
||||||
.alignment(Alignment::Center)
|
|
||||||
.block(Block::default().borders(Borders::ALL));
|
|
||||||
frame.render_widget(msg, frame.size());
|
|
||||||
});
|
|
||||||
} else if let Err(e) = terminal.draw(|frame| {
|
|
||||||
let (title_area, system_area, services_area) = tui_app.render(frame, &self.metric_store);
|
|
||||||
self.title_area = title_area;
|
|
||||||
self.system_area = system_area;
|
|
||||||
self.services_area = services_area;
|
|
||||||
}) {
|
|
||||||
error!("Error rendering TUI after input: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for new metrics
|
// Check for new metrics
|
||||||
@ -259,8 +230,10 @@ impl Dashboard {
|
|||||||
if let Some(ref mut tui_app) = self.tui_app {
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
tui_app.update_metrics(&mut self.metric_store);
|
tui_app.update_metrics(&mut self.metric_store);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
needs_render = true; // New metrics received, need to render
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also check for command output messages
|
// Also check for command output messages
|
||||||
if let Ok(Some(cmd_output)) = self.zmq_consumer.receive_command_output().await {
|
if let Ok(Some(cmd_output)) = self.zmq_consumer.receive_command_output().await {
|
||||||
debug!(
|
debug!(
|
||||||
@ -271,26 +244,27 @@ impl Dashboard {
|
|||||||
|
|
||||||
// Command output (terminal popup removed - output not displayed)
|
// Command output (terminal popup removed - output not displayed)
|
||||||
}
|
}
|
||||||
|
|
||||||
last_metrics_check = Instant::now();
|
last_metrics_check = Instant::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for host connectivity changes (heartbeat timeouts) periodically
|
// Check for host connectivity changes (heartbeat timeouts) periodically
|
||||||
if last_heartbeat_check.elapsed() >= heartbeat_check_interval {
|
if last_heartbeat_check.elapsed() >= heartbeat_check_interval {
|
||||||
let timeout = Duration::from_secs(self.config.zmq.heartbeat_timeout_seconds);
|
let timeout = Duration::from_secs(self.config.zmq.heartbeat_timeout_seconds);
|
||||||
|
|
||||||
// Clean up metrics for offline hosts
|
// Clean up metrics for offline hosts
|
||||||
self.metric_store.cleanup_offline_hosts(timeout);
|
self.metric_store.cleanup_offline_hosts(timeout);
|
||||||
|
|
||||||
if let Some(ref mut tui_app) = self.tui_app {
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
let connected_hosts = self.metric_store.get_connected_hosts(timeout);
|
let connected_hosts = self.metric_store.get_connected_hosts(timeout);
|
||||||
tui_app.update_hosts(connected_hosts);
|
tui_app.update_hosts(connected_hosts);
|
||||||
}
|
}
|
||||||
last_heartbeat_check = Instant::now();
|
last_heartbeat_check = Instant::now();
|
||||||
|
needs_render = true; // Heartbeat check happened, may have changed hosts
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render TUI (only if not headless)
|
// Render TUI only when needed (not headless and something changed)
|
||||||
if !self.headless {
|
if !self.headless && needs_render {
|
||||||
if let Some(ref mut terminal) = self.terminal {
|
if let Some(ref mut terminal) = self.terminal {
|
||||||
if let Some(ref mut tui_app) = self.tui_app {
|
if let Some(ref mut tui_app) = self.tui_app {
|
||||||
// Clear and autoresize terminal to handle any resize events
|
// Clear and autoresize terminal to handle any resize events
|
||||||
@ -322,10 +296,8 @@ impl Dashboard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
needs_render = false; // Reset flag after rendering
|
||||||
}
|
}
|
||||||
|
|
||||||
// Small sleep to prevent excessive CPU usage
|
|
||||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Dashboard main loop ended");
|
info!("Dashboard main loop ended");
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard-shared"
|
name = "cm-dashboard-shared"
|
||||||
version = "0.1.265"
|
version = "0.1.266"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user