refactor: complete rewrite for high performance hierarchical monitoring
This commit is contained in:
@@ -5,11 +5,9 @@
|
||||
|
||||
mod models;
|
||||
mod monitor;
|
||||
mod commands; // We'll keep this but gut it
|
||||
mod cli;
|
||||
|
||||
use tauri::{State, Manager};
|
||||
use std::sync::Mutex;
|
||||
use tauri::State;
|
||||
use crate::monitor::Monitor;
|
||||
use crate::models::GlobalStats;
|
||||
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::collections::HashMap;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone, Serialize, Debug)]
|
||||
pub struct ProcessNode {
|
||||
pub pid: u32,
|
||||
pub name: String,
|
||||
pub cpu_self: f32, // CPU usage of this specific process
|
||||
pub cpu_children: f32, // Sum of children's CPU (recursive)
|
||||
pub mem_rss: u64, // Resident Set Size (Self)
|
||||
pub mem_children: u64, // Sum of children's Mem
|
||||
pub cpu_self: f32,
|
||||
pub cpu_children: f32,
|
||||
pub mem_rss: u64,
|
||||
pub mem_children: u64,
|
||||
pub children: Vec<ProcessNode>,
|
||||
}
|
||||
|
||||
@@ -27,12 +26,6 @@ pub struct GlobalStats {
|
||||
pub cpu_total: f32,
|
||||
pub mem_used: u64,
|
||||
pub mem_total: u64,
|
||||
pub process_tree: Vec<ProcessNode>, // Top-level roots (e.g. systemd/init or orphans)
|
||||
pub process_tree: Vec<ProcessNode>,
|
||||
pub process_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub enum MemoryMode {
|
||||
Rss, // Fast, default for monitoring
|
||||
Pss, // Slow, accurate for profiling
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use sysinfo::System;
|
||||
use sysinfo::{System, SystemExt, ProcessExt, CpuExt, PidExt};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::collections::HashMap;
|
||||
use crate::models::*;
|
||||
|
||||
pub struct Monitor {
|
||||
@@ -39,8 +39,6 @@ impl Monitor {
|
||||
|
||||
thread::spawn(move || {
|
||||
let mut sys = System::new_all();
|
||||
|
||||
// Initial wait to let sysinfo calculate CPU diff
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
sys.refresh_all();
|
||||
|
||||
@@ -49,21 +47,17 @@ impl Monitor {
|
||||
break;
|
||||
}
|
||||
|
||||
// 1. Refresh System State (Efficiently)
|
||||
sys.refresh_cpu_all();
|
||||
sys.refresh_memory();
|
||||
sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
|
||||
|
||||
// 2. Calculate Global Stats
|
||||
let cpu_total = sys.global_cpu_usage();
|
||||
let mem_used = sys.used_memory();
|
||||
let mem_total = sys.total_memory();
|
||||
let process_count = sys.processes().len();
|
||||
|
||||
// 3. Build Process Tree
|
||||
let tree = build_process_tree(&sys);
|
||||
|
||||
// 4. Update Shared State
|
||||
{
|
||||
let mut lock = data_ref.lock().unwrap();
|
||||
lock.cpu_total = cpu_total;
|
||||
@@ -73,19 +67,13 @@ impl Monitor {
|
||||
lock.process_count = process_count;
|
||||
}
|
||||
|
||||
// 5. Sleep (Aim for 1s interval)
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn stop(&self) {
|
||||
*self.running.lock().unwrap() = false;
|
||||
}
|
||||
}
|
||||
|
||||
fn build_process_tree(sys: &System) -> Vec<ProcessNode> {
|
||||
// 1. Collect all raw nodes
|
||||
let mut raw_nodes: HashMap<u32, ProcessNode> = HashMap::new();
|
||||
let mut ppid_map: HashMap<u32, u32> = HashMap::new();
|
||||
|
||||
@@ -107,19 +95,16 @@ fn build_process_tree(sys: &System) -> Vec<ProcessNode> {
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Build Hierarchy (Bottom-Up or Map-Based)
|
||||
let mut children_map: HashMap<u32, Vec<u32>> = HashMap::new();
|
||||
for (child, parent) in &ppid_map {
|
||||
children_map.entry(*parent).or_default().push(*child);
|
||||
}
|
||||
|
||||
// Identify roots
|
||||
let roots: Vec<u32> = raw_nodes.keys()
|
||||
.filter(|pid| !ppid_map.contains_key(pid)) // Has no parent in the list
|
||||
.filter(|pid| !ppid_map.contains_key(pid))
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
// 3. Recursive Tree Builder
|
||||
fn build_recursive(
|
||||
pid: u32,
|
||||
nodes_map: &mut HashMap<u32, ProcessNode>,
|
||||
@@ -136,7 +121,6 @@ fn build_process_tree(sys: &System) -> Vec<ProcessNode> {
|
||||
}
|
||||
}
|
||||
|
||||
// Sort children by Mem impact by default
|
||||
node.children.sort_by(|a, b| b.total_mem().cmp(&a.total_mem()));
|
||||
|
||||
Some(node)
|
||||
@@ -152,7 +136,6 @@ fn build_process_tree(sys: &System) -> Vec<ProcessNode> {
|
||||
}
|
||||
}
|
||||
|
||||
// Sort roots by total CPU
|
||||
tree.sort_by(|a, b| b.total_cpu().partial_cmp(&a.total_cpu()).unwrap_or(std::cmp::Ordering::Equal));
|
||||
|
||||
tree
|
||||
|
||||
Reference in New Issue
Block a user