From db06f8201f9f0bdfe5b8dc5791a04e22cfd99381 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Tue, 17 Sep 2019 00:24:36 -0400 Subject: [PATCH] Potential fix for windows processes. --- Cargo.toml | 1 + README.md | 2 +- TODO.md | 2 -- src/app/data_collection.rs | 2 +- src/app/data_collection/processes.rs | 48 ++++++++-------------------- src/app/process_killer.rs | 2 ++ src/convert_data.rs | 4 +-- src/main.rs | 4 ++- 8 files changed, 24 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 851ceb3d..ee7efccd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ keywords = ["cli", "monitoring-tool", "process", "system", "top"] license = "MIT" categories = ["command-line-utilities"] description = "A graphical top clone." +readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 02905c80..f2f9d39c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # bottom -[![Build Status](https://travis-ci.com/ClementTsang/rustop.svg?token=1wvzVgp94E1TZyPNs8JF&branch=master)](https://travis-ci.com/ClementTsang/rustop) +[![Build Status](https://travis-ci.com/ClementTsang/rustop.svg?token=1wvzVgp94E1TZyPNs8JF&branch=master)](https://travis-ci.com/ClementTsang/rustop) [![crates.io link](https://img.shields.io/crates/v/bottom.svg)](https://crates.io/crates/bottom) A top clone, written in Rust. Inspired by both [gtop](https://github.com/aksakalli/gtop) and [gotop](https://github.com/cjbassi/gotop) diff --git a/TODO.md b/TODO.md index 4c030170..411acf1a 100644 --- a/TODO.md +++ b/TODO.md @@ -46,8 +46,6 @@ Note this will probably migrate to GitHub's native Issues; this was mostly for p * Truncate columns if needed for tables -* Refactor everything because it's a mess - * Test for Windows support, mac support, other. May be doable, depends on sysinfo and how much I know about other OSes probably. * Efficiency!!! 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 { diff --git a/src/convert_data.rs b/src/convert_data.rs index 14111a2e..93452dae 100644 --- a/src/convert_data.rs +++ b/src/convert_data.rs @@ -101,9 +101,9 @@ pub fn update_process_row(app_data : &data_collection::Data) -> Vec> if let Some(mem_usage) = process.mem_usage_percent { mem_usage } - else if let Some(mem_usage_in_mb) = process.mem_usage_mb { + else if let Some(mem_usage_kb) = process.mem_usage_kb { if let Some(mem_data) = app_data.memory.last() { - mem_usage_in_mb as f64 / mem_data.mem_total_in_mb as f64 * 100_f64 + (mem_usage_kb / 1024) as f64 / mem_data.mem_total_in_mb as f64 * 100_f64 } else { 0_f64 diff --git a/src/main.rs b/src/main.rs index 0747fbd2..caeb2c8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,7 +96,9 @@ fn main() -> error::Result<()> { let tx = tx.clone(); thread::spawn(move || { let input = input(); - input.enable_mouse_mode().unwrap(); + if cfg!(target_os = "linux") { + input.enable_mouse_mode().unwrap(); + } let reader = input.read_sync(); for event in reader { match event {