From db06f8201f9f0bdfe5b8dc5791a04e22cfd99381 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Tue, 17 Sep 2019 00:24:36 -0400 Subject: Potential fix for windows processes. --- src/app/data_collection.rs | 2 +- src/app/data_collection/processes.rs | 48 +++++++++++------------------------- src/app/process_killer.rs | 2 ++ 3 files changed, 17 insertions(+), 35 deletions(-) (limited to 'src/app') diff --git a/src/app/data_collection.rs b/src/app/data_collection.rs index ed0676a8..793d9e1b 100644 --- a/src/app/data_collection.rs +++ b/src/app/data_collection.rs @@ -93,7 +93,7 @@ impl DataState { push_if_valid(&mem::get_mem_data_list().await, &mut self.data.memory); push_if_valid(&mem::get_swap_data_list().await, &mut self.data.swap); set_if_valid( - &processes::get_sorted_processes_list(&mut self.prev_idle, &mut self.prev_non_idle, &mut self.prev_pid_stats).await, + &processes::get_sorted_processes_list(&self.sys, &mut self.prev_idle, &mut self.prev_non_idle, &mut self.prev_pid_stats).await, &mut self.data.list_of_processes, ); diff --git a/src/app/data_collection/processes.rs b/src/app/data_collection/processes.rs index 65b47d12..02eb3bb7 100644 --- a/src/app/data_collection/processes.rs +++ b/src/app/data_collection/processes.rs @@ -1,8 +1,5 @@ -use heim_common::{ - prelude::{StreamExt, TryStreamExt}, - units, -}; use std::{collections::HashMap, process::Command}; +use sysinfo::{ProcessExt, System, SystemExt}; #[derive(Clone)] pub enum ProcessSorting { @@ -24,7 +21,7 @@ pub struct ProcessData { pub pid : u32, pub cpu_usage_percent : f64, pub mem_usage_percent : Option, - pub mem_usage_mb : Option, + pub mem_usage_kb : Option, pub command : String, } @@ -97,14 +94,6 @@ fn get_ordering(a_val : T, b_val : T, reverse_order : } } -async fn non_linux_cpu_usage(process : heim::process::Process) -> heim::process::ProcessResult<(heim::process::Process, heim_common::units::Ratio)> { - let usage_1 = process.cpu_usage().await?; - futures_timer::Delay::new(std::time::Duration::from_millis(100)).await?; // TODO: For windows, make it like the linux check - let usage_2 = process.cpu_usage().await?; - - Ok((process, usage_2 - usage_1)) -} - fn get_process_cpu_stats(pid : u32) -> std::io::Result { let mut path = std::path::PathBuf::new(); path.push("/proc"); @@ -151,7 +140,7 @@ fn convert_ps(process : &str, cpu_usage_percentage : f64, prev_pid_stats : &mut pid : 0, command : "".to_string(), mem_usage_percent : None, - mem_usage_mb : None, + mem_usage_kb : None, cpu_usage_percent : 0_f64, }); } @@ -164,13 +153,13 @@ fn convert_ps(process : &str, cpu_usage_percentage : f64, prev_pid_stats : &mut pid, command, mem_usage_percent, - mem_usage_mb : None, + mem_usage_kb : None, cpu_usage_percent : linux_cpu_usage(pid, cpu_usage_percentage, prev_pid_stats)?, }) } pub async fn get_sorted_processes_list( - 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(); @@ -194,24 +183,15 @@ pub async fn get_sorted_processes_list( else if cfg!(target_os = "windows") { // Windows - // TODO: DO NOT USE HEIM! - let mut process_stream = heim::process::processes().map_ok(non_linux_cpu_usage).try_buffer_unordered(std::usize::MAX); - - let mut process_vector : Vec = Vec::new(); - while let Some(process) = process_stream.next().await { - if let Ok(process) = process { - let (process, cpu_usage) = process; - let mem_measurement = process.memory().await; - if let Ok(mem_measurement) = mem_measurement { - process_vector.push(ProcessData { - command : process.name().await.unwrap_or_else(|_| "".to_string()), - pid : process.pid() as u32, - cpu_usage_percent : f64::from(cpu_usage.get::()), - mem_usage_percent : None, - mem_usage_mb : Some(mem_measurement.rss().get::()), - }); - } - } + 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()), + }); } } else if cfg!(target_os = "macos") { diff --git a/src/app/process_killer.rs b/src/app/process_killer.rs index e91034c4..2e74de16 100644 --- a/src/app/process_killer.rs +++ b/src/app/process_killer.rs @@ -9,10 +9,12 @@ pub fn kill_process_given_pid(pid : u64) -> crate::utils::error::Result<()> { } else if cfg!(target_os = "windows") { // Windows + // See how sysinfo does it... https://docs.rs/sysinfo/0.9.5/sysinfo/trait.ProcessExt.html debug!("Sorry, Windows support is not implemented yet!"); } 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 { -- cgit v1.2.3