From e7477ce517d1cf816bdb5cb479452bb5217c47e7 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Fri, 6 Dec 2019 00:57:04 -0500 Subject: Update tui version... legends aren't showing up yet, will have to fork again. --- src/app/data_collection.rs | 62 ++++++++++----------- src/app/data_collection/cpu.rs | 16 +++--- src/app/data_collection/mem.rs | 18 +++--- src/app/data_collection/processes.rs | 103 ++++++++++++++++------------------- src/app/process_killer.rs | 13 ++--- 5 files changed, 99 insertions(+), 113 deletions(-) (limited to 'src/app') diff --git a/src/app/data_collection.rs b/src/app/data_collection.rs index e89a7390..bb03deb1 100644 --- a/src/app/data_collection.rs +++ b/src/app/data_collection.rs @@ -11,13 +11,13 @@ pub mod network; pub mod processes; pub mod temperature; -fn set_if_valid(result : &Result, value_to_set : &mut T) { +fn set_if_valid(result: &Result, value_to_set: &mut T) { if let Ok(result) = result { *value_to_set = (*result).clone(); } } -fn push_if_valid(result : &Result, vector_to_push : &mut Vec) { +fn push_if_valid(result: &Result, vector_to_push: &mut Vec) { if let Ok(result) = result { vector_to_push.push(result.clone()); } @@ -25,47 +25,47 @@ fn push_if_valid(result : &Result, - pub list_of_io : Vec, - pub list_of_physical_io : Vec, - pub memory : Vec, - pub swap : Vec, - pub list_of_temperature_sensor : Vec, - pub network : Vec, - pub list_of_processes : Vec, // Only need to keep a list of processes... - pub list_of_disks : Vec, // Only need to keep a list of disks and their data + pub list_of_cpu_packages: Vec, + pub list_of_io: Vec, + pub list_of_physical_io: Vec, + pub memory: Vec, + pub swap: Vec, + pub list_of_temperature_sensor: Vec, + pub network: Vec, + pub list_of_processes: Vec, // Only need to keep a list of processes... + pub list_of_disks: Vec, // Only need to keep a list of disks and their data } pub struct DataState { - pub data : Data, - first_run : bool, - sys : System, - stale_max_seconds : u64, - prev_pid_stats : HashMap, - prev_idle : f64, - prev_non_idle : f64, - temperature_type : temperature::TemperatureType, - last_clean : Instant, // Last time stale data was cleared + pub data: Data, + first_run: bool, + sys: System, + stale_max_seconds: u64, + prev_pid_stats: HashMap, + prev_idle: f64, + prev_non_idle: f64, + temperature_type: temperature::TemperatureType, + last_clean: Instant, // Last time stale data was cleared } impl Default for DataState { fn default() -> Self { DataState { - data : Data::default(), - first_run : true, - sys : System::new(), - stale_max_seconds : constants::STALE_MAX_MILLISECONDS / 1000, - prev_pid_stats : HashMap::new(), - prev_idle : 0_f64, - prev_non_idle : 0_f64, - temperature_type : temperature::TemperatureType::Celsius, - last_clean : Instant::now(), + data: Data::default(), + first_run: true, + sys: System::new(), + stale_max_seconds: constants::STALE_MAX_MILLISECONDS / 1000, + prev_pid_stats: HashMap::new(), + prev_idle: 0_f64, + prev_non_idle: 0_f64, + temperature_type: temperature::TemperatureType::Celsius, + last_clean: Instant::now(), } } } impl DataState { - pub fn set_temperature_type(&mut self, temperature_type : temperature::TemperatureType) { + pub fn set_temperature_type(&mut self, temperature_type: temperature::TemperatureType) { self.temperature_type = temperature_type; } @@ -113,7 +113,7 @@ impl DataState { let current_instant = std::time::Instant::now(); if current_instant.duration_since(self.last_clean).as_secs() > self.stale_max_seconds { - let stale_list : Vec<_> = self + let stale_list: Vec<_> = self .prev_pid_stats .iter() .filter(|&(_, &v)| current_instant.duration_since(v.1).as_secs() > self.stale_max_seconds) diff --git a/src/app/data_collection/cpu.rs b/src/app/data_collection/cpu.rs index 0a44bac8..0fd5d233 100644 --- a/src/app/data_collection/cpu.rs +++ b/src/app/data_collection/cpu.rs @@ -3,29 +3,29 @@ use sysinfo::{ProcessorExt, System, SystemExt}; #[derive(Clone)] pub struct CPUData { - pub cpu_name : Box, - pub cpu_usage : f64, + pub cpu_name: Box, + pub cpu_usage: f64, } #[derive(Clone)] pub struct CPUPackage { - pub cpu_vec : Vec, - pub instant : Instant, + pub cpu_vec: Vec, + pub instant: Instant, } -pub fn get_cpu_data_list(sys : &System) -> crate::utils::error::Result { +pub fn get_cpu_data_list(sys: &System) -> crate::utils::error::Result { let cpu_data = sys.get_processor_list(); let mut cpu_vec = Vec::new(); for cpu in cpu_data { cpu_vec.push(CPUData { - cpu_name : Box::from(cpu.get_name()), - cpu_usage : f64::from(cpu.get_cpu_usage()) * 100_f64, + cpu_name: Box::from(cpu.get_name()), + cpu_usage: f64::from(cpu.get_cpu_usage()) * 100_f64, }) } Ok(CPUPackage { cpu_vec, - instant : Instant::now(), + instant: Instant::now(), }) } diff --git a/src/app/data_collection/mem.rs b/src/app/data_collection/mem.rs index 929ee326..f4a8be1a 100644 --- a/src/app/data_collection/mem.rs +++ b/src/app/data_collection/mem.rs @@ -3,18 +3,18 @@ use std::time::Instant; #[derive(Clone)] pub struct MemData { - pub mem_total_in_mb : u64, - pub mem_used_in_mb : u64, - pub instant : Instant, + pub mem_total_in_mb: u64, + pub mem_used_in_mb: u64, + pub instant: Instant, } pub async fn get_mem_data_list() -> crate::utils::error::Result { let memory = heim::memory::memory().await?; Ok(MemData { - mem_total_in_mb : memory.total().get::(), - mem_used_in_mb : memory.total().get::() - memory.available().get::(), - instant : Instant::now(), + mem_total_in_mb: memory.total().get::(), + mem_used_in_mb: memory.total().get::() - memory.available().get::(), + instant: Instant::now(), }) } @@ -22,8 +22,8 @@ pub async fn get_swap_data_list() -> crate::utils::error::Result { let memory = heim::memory::swap().await?; Ok(MemData { - mem_total_in_mb : memory.total().get::(), - mem_used_in_mb : memory.used().get::(), - instant : Instant::now(), + mem_total_in_mb: memory.total().get::(), + mem_used_in_mb: memory.used().get::(), + instant: Instant::now(), }) } diff --git a/src/app/data_collection/processes.rs b/src/app/data_collection/processes.rs index 7e020968..982ebfaa 100644 --- a/src/app/data_collection/processes.rs +++ b/src/app/data_collection/processes.rs @@ -18,14 +18,14 @@ impl Default for ProcessSorting { // Possible process info struct? #[derive(Clone, Default)] pub struct ProcessData { - pub pid : u32, - pub cpu_usage_percent : f64, - pub mem_usage_percent : Option, - pub mem_usage_kb : Option, - pub command : String, + pub pid: u32, + pub cpu_usage_percent: f64, + pub mem_usage_percent: Option, + pub mem_usage_kb: Option, + pub command: String, } -fn vangelis_cpu_usage_calculation(prev_idle : &mut f64, prev_non_idle : &mut f64) -> std::io::Result<(f64, f64)> { +fn vangelis_cpu_usage_calculation(prev_idle: &mut f64, prev_non_idle: &mut f64) -> std::io::Result<(f64, f64)> { // Named after this SO answer: https://stackoverflow.com/a/23376195 let mut path = std::path::PathBuf::new(); path.push("/proc"); @@ -43,15 +43,15 @@ fn vangelis_cpu_usage_calculation(prev_idle : &mut f64, prev_non_idle : &mut f64 return Ok((1.0, 0.0)); // TODO: This is not the greatest... } - let user : f64 = val[1].parse::<_>().unwrap_or(0_f64); - let nice : f64 = val[2].parse::<_>().unwrap_or(0_f64); - let system : f64 = val[3].parse::<_>().unwrap_or(0_f64); - let idle : f64 = val[4].parse::<_>().unwrap_or(0_f64); - let iowait : f64 = val[5].parse::<_>().unwrap_or(0_f64); - let irq : f64 = val[6].parse::<_>().unwrap_or(0_f64); - let softirq : f64 = val[7].parse::<_>().unwrap_or(0_f64); - let steal : f64 = val[8].parse::<_>().unwrap_or(0_f64); - let guest : f64 = val[9].parse::<_>().unwrap_or(0_f64); + let user: f64 = val[1].parse::<_>().unwrap_or(0_f64); + let nice: f64 = val[2].parse::<_>().unwrap_or(0_f64); + let system: f64 = val[3].parse::<_>().unwrap_or(0_f64); + let idle: f64 = val[4].parse::<_>().unwrap_or(0_f64); + let iowait: f64 = val[5].parse::<_>().unwrap_or(0_f64); + let irq: f64 = val[6].parse::<_>().unwrap_or(0_f64); + let softirq: f64 = val[7].parse::<_>().unwrap_or(0_f64); + let steal: f64 = val[8].parse::<_>().unwrap_or(0_f64); + let guest: f64 = val[9].parse::<_>().unwrap_or(0_f64); let idle = idle + iowait; let non_idle = user + nice + system + irq + softirq + steal + guest; @@ -59,8 +59,8 @@ fn vangelis_cpu_usage_calculation(prev_idle : &mut f64, prev_non_idle : &mut f64 let total = idle + non_idle; let prev_total = *prev_idle + *prev_non_idle; - let total_delta : f64 = total - prev_total; - let idle_delta : f64 = idle - *prev_idle; + let total_delta: f64 = total - prev_total; + let idle_delta: f64 = idle - *prev_idle; //debug!("Vangelis function: CPU PERCENT: {}", (total_delta - idle_delta) / total_delta * 100_f64); @@ -69,8 +69,7 @@ fn vangelis_cpu_usage_calculation(prev_idle : &mut f64, prev_non_idle : &mut f64 let result = if total_delta - idle_delta != 0_f64 { total_delta - idle_delta - } - else { + } else { 1_f64 }; @@ -79,29 +78,25 @@ fn vangelis_cpu_usage_calculation(prev_idle : &mut f64, prev_non_idle : &mut f64 Ok((result, cpu_percentage)) // This works, REALLY damn well. The percentage check is within like 2% of the sysinfo one. } -fn get_ordering(a_val : T, b_val : T, reverse_order : bool) -> std::cmp::Ordering { +fn get_ordering(a_val: T, b_val: T, reverse_order: bool) -> std::cmp::Ordering { if a_val > b_val { if reverse_order { std::cmp::Ordering::Less - } - else { + } else { std::cmp::Ordering::Greater } - } - else if a_val < b_val { + } else if a_val < b_val { if reverse_order { std::cmp::Ordering::Greater - } - else { + } else { std::cmp::Ordering::Less } - } - else { + } else { std::cmp::Ordering::Equal } } -fn get_process_cpu_stats(pid : u32) -> std::io::Result { +fn get_process_cpu_stats(pid: u32) -> std::io::Result { let mut path = std::path::PathBuf::new(); path.push("/proc"); path.push(&pid.to_string()); @@ -118,14 +113,11 @@ fn get_process_cpu_stats(pid : u32) -> std::io::Result { } /// Note that cpu_percentage should be represented WITHOUT the \times 100 factor! -fn linux_cpu_usage( - pid : u32, cpu_usage : f64, cpu_percentage : f64, previous_pid_stats : &mut HashMap, -) -> std::io::Result { +fn linux_cpu_usage(pid: u32, cpu_usage: f64, cpu_percentage: f64, previous_pid_stats: &mut HashMap) -> std::io::Result { // Based heavily on https://stackoverflow.com/a/23376195 and https://stackoverflow.com/a/1424556 - let before_proc_val : f64 = if previous_pid_stats.contains_key(&pid.to_string()) { + let before_proc_val: f64 = if previous_pid_stats.contains_key(&pid.to_string()) { previous_pid_stats.get(&pid.to_string()).unwrap_or(&(0_f64, Instant::now())).0 - } - else { + } else { 0_f64 }; let after_proc_val = get_process_cpu_stats(pid)?; @@ -145,15 +137,15 @@ fn linux_cpu_usage( } fn convert_ps( - process : &str, cpu_usage : f64, cpu_percentage : f64, prev_pid_stats : &mut HashMap, + process: &str, cpu_usage: f64, cpu_percentage: f64, prev_pid_stats: &mut HashMap, ) -> std::io::Result { if process.trim().to_string().is_empty() { return Ok(ProcessData { - pid : 0, - command : "".to_string(), - mem_usage_percent : None, - mem_usage_kb : None, - cpu_usage_percent : 0_f64, + pid: 0, + command: "".to_string(), + mem_usage_percent: None, + mem_usage_kb: None, + cpu_usage_percent: 0_f64, }); } @@ -165,15 +157,15 @@ fn convert_ps( pid, command, mem_usage_percent, - mem_usage_kb : None, - cpu_usage_percent : linux_cpu_usage(pid, cpu_usage, cpu_percentage, prev_pid_stats)?, + mem_usage_kb: None, + cpu_usage_percent: linux_cpu_usage(pid, cpu_usage, cpu_percentage, prev_pid_stats)?, }) } pub async fn get_sorted_processes_list( - sys : &System, prev_idle : &mut f64, prev_non_idle : &mut f64, prev_pid_stats : &mut std::collections::HashMap, + sys: &System, prev_idle: &mut f64, prev_non_idle: &mut f64, prev_pid_stats: &mut std::collections::HashMap, ) -> crate::utils::error::Result> { - let mut process_vector : Vec = Vec::new(); + let mut process_vector: Vec = Vec::new(); if cfg!(target_os = "linux") { // Linux specific - this is a massive pain... ugh. @@ -192,26 +184,23 @@ pub async fn get_sorted_processes_list( } } } - } - else if cfg!(target_os = "windows") { + } else if cfg!(target_os = "windows") { // Windows let process_hashmap = sys.get_process_list(); for process_val in process_hashmap.values() { process_vector.push(ProcessData { - pid : process_val.pid() as u32, - command : process_val.name().to_string(), - mem_usage_percent : None, - mem_usage_kb : Some(process_val.memory()), - cpu_usage_percent : f64::from(process_val.cpu_usage()), + pid: process_val.pid() as u32, + command: process_val.name().to_string(), + mem_usage_percent: None, + mem_usage_kb: Some(process_val.memory()), + cpu_usage_percent: f64::from(process_val.cpu_usage()), }); } - } - else if cfg!(target_os = "macos") { + } else if cfg!(target_os = "macos") { // TODO: macOS debug!("Mac"); - } - else { + } else { // TODO: Others? debug!("Else"); // Solaris: https://stackoverflow.com/a/4453581 @@ -220,7 +209,7 @@ pub async fn get_sorted_processes_list( Ok(process_vector) } -pub fn sort_processes(process_vector : &mut Vec, sorting_method : &ProcessSorting, reverse_order : bool) { +pub fn sort_processes(process_vector: &mut Vec, sorting_method: &ProcessSorting, reverse_order: bool) { match sorting_method { // Always sort alphabetically first! ProcessSorting::CPU => { diff --git a/src/app/process_killer.rs b/src/app/process_killer.rs index 06ebeb0e..46396a78 100644 --- a/src/app/process_killer.rs +++ b/src/app/process_killer.rs @@ -18,7 +18,7 @@ struct Process(HANDLE); #[cfg(target_os = "windows")] impl Process { - fn open(pid : DWORD) -> Result { + fn open(pid: DWORD) -> Result { let pc = unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, 0, pid) }; if pc == null_mut() { return Err("!OpenProcess".to_string()); @@ -33,23 +33,20 @@ impl Process { } /// Kills a process, given a PID. -pub fn kill_process_given_pid(pid : u64) -> crate::utils::error::Result<()> { +pub fn kill_process_given_pid(pid: u64) -> crate::utils::error::Result<()> { if cfg!(target_os = "linux") { // Linux Command::new("kill").arg(pid.to_string()).output()?; - } - else if cfg!(target_os = "windows") { + } else if cfg!(target_os = "windows") { #[cfg(target_os = "windows")] let process = Process::open(pid as DWORD)?; #[cfg(target_os = "windows")] process.kill()?; - } - else if cfg!(target_os = "macos") { + } else if cfg!(target_os = "macos") { // TODO: macOS // See how sysinfo does it... https://docs.rs/sysinfo/0.9.5/sysinfo/trait.ProcessExt.html debug!("Sorry, macOS support is not implemented yet!"); - } - else { + } else { // TODO: Others? debug!("Sorry, other support this is not implemented yet!"); } -- cgit v1.2.3