diff --git a/src/main.rs b/src/main.rs index a016c14a..1efe1256 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,9 @@ async fn main() -> Result<(), io::Error> { let mut terminal = Terminal::new(backend)?; let tick_rate_in_milliseconds : u64 = 250; - let update_rate_in_milliseconds : u64 = 1000; + let update_rate_in_milliseconds : u64 = 1000; // TODO: Must set a check to prevent this from going into negatives! + + let mut app = widgets::App::new("rustop"); let log = init_logger(); @@ -57,12 +59,11 @@ async fn main() -> Result<(), io::Error> { loop { futures::executor::block_on(data_state.update_data()); // TODO: Fix tx.send(Event::Update(data_state.data.clone())).unwrap(); - thread::sleep(Duration::from_millis(update_rate_in_milliseconds)); + thread::sleep(Duration::from_millis(update_rate_in_milliseconds - 1000)); } }); } - let mut app = widgets::App::new("rustop"); terminal.clear()?; let mut app_data = widgets::Data::default(); diff --git a/src/widgets/processes.rs b/src/widgets/processes.rs index 6445c6c5..a3dee05d 100644 --- a/src/widgets/processes.rs +++ b/src/widgets/processes.rs @@ -74,10 +74,9 @@ fn get_cpu_use_val() -> std::io::Result { Ok(val[0].parse::().unwrap_or(0_f64) + val[1].parse::().unwrap_or(0_f64) + val[2].parse::().unwrap_or(0_f64) + val[3].parse::().unwrap_or(0_f64)) } -async fn linux_cpu_usage(pid : u32) -> std::io::Result { +async fn linux_cpu_usage(pid : u32, before_cpu_val : f64) -> std::io::Result { // Based heavily on https://stackoverflow.com/a/23376195 and https://stackoverflow.com/a/1424556 let before_proc_val = get_process_cpu_stats(pid)?; - let before_cpu_val = get_cpu_use_val()?; futures_timer::Delay::new(std::time::Duration::from_millis(1000)).await.unwrap(); let after_proc_val = get_process_cpu_stats(pid)?; let after_cpu_val = get_cpu_use_val()?; @@ -86,13 +85,27 @@ async fn linux_cpu_usage(pid : u32) -> std::io::Result { } async fn convert_ps(process : &str) -> std::io::Result { - let mut result = process.split_whitespace(); - let pid = result.next().unwrap_or("").parse::().unwrap_or(0); + let before_cpu_val = get_cpu_use_val()?; + + debug!("Process: |{}|", process); + if process.trim().to_string().is_empty() { + return Ok(ProcessData { + pid : 0, + command : "".to_string(), + mem_usage_percent : 0_f64, + cpu_usage_percent : 0_f64, + }); + } + + let pid = (&process[..11]).trim().to_string().parse::().unwrap_or(0); + let command = (&process[11..61]).trim().to_string(); + let mem_usage_percent = (&process[62..]).trim().to_string().parse::().unwrap_or(0_f64); + Ok(ProcessData { pid, - command : result.next().unwrap_or("").to_string(), - mem_usage_percent : result.next().unwrap_or("").parse::().unwrap_or(0_f64), - cpu_usage_percent : linux_cpu_usage(pid).await?, + command, + mem_usage_percent, + cpu_usage_percent : linux_cpu_usage(pid, before_cpu_val).await?, }) } @@ -101,14 +114,16 @@ pub async fn get_sorted_processes_list(total_mem : u64) -> Result(split_string.collect::>()).map(convert_ps).buffer_unordered(std::usize::MAX); while let Some(process) = process_stream.next().await { if let Ok(process) = process { - process_vector.push(process); + if !process.command.is_empty() { + process_vector.push(process); + } } } }