Fix for divide by zero if swap/mem was 0

This commit is contained in:
ClementTsang 2019-09-25 02:13:10 -04:00
parent f5d1f624cc
commit 5ecc80e2ad
3 changed files with 26 additions and 13 deletions

View File

@ -1,6 +1,7 @@
/// This file is meant to house (OS specific) implementations on how to kill processes.
use std::process::Command;
// Copied from SO: https://stackoverflow.com/a/55231715
#[cfg(target_os = "windows")]
use std::ptr::null_mut;
#[cfg(target_os = "windows")]
@ -12,7 +13,6 @@ use winapi::{
},
};
// Copied from SO: https://stackoverflow.com/a/55231715
#[cfg(target_os = "windows")]
struct Process(HANDLE);

View File

@ -123,6 +123,25 @@ pub fn draw_data<B : backend::Backend>(terminal : &mut Terminal<B>, app_state :
{
let x_axis : Axis<String> = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 600_000.0]);
let y_axis = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([-0.5, 100.5]).labels(&["0%", "100%"]); // Offset as the zero value isn't drawn otherwise...
let mem_name = "RAM:".to_string() + &format!("{:3}%", (canvas_data.mem_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64));
let swap_name = "SWP:".to_string() + &format!("{:3}%", (canvas_data.swap_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64));
let mut mem_canvas_vec : Vec<Dataset> = vec![Dataset::default()
.name(&mem_name)
.marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille })
.style(Style::default().fg(Color::LightBlue))
.data(&canvas_data.mem_data)];
if !(&canvas_data.swap_data).is_empty() {
mem_canvas_vec.push(
Dataset::default()
.name(&swap_name)
.marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille })
.style(Style::default().fg(Color::LightYellow))
.data(&canvas_data.swap_data),
);
}
Chart::default()
.block(
Block::default()
@ -135,18 +154,7 @@ pub fn draw_data<B : backend::Backend>(terminal : &mut Terminal<B>, app_state :
)
.x_axis(x_axis)
.y_axis(y_axis)
.datasets(&[
Dataset::default()
.name(&("RAM:".to_string() + &format!("{:3}%", (canvas_data.mem_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64))))
.marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille })
.style(Style::default().fg(Color::LightBlue))
.data(&canvas_data.mem_data),
Dataset::default()
.name(&("SWP:".to_string() + &format!("{:3}%", (canvas_data.swap_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64))))
.marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille })
.style(Style::default().fg(Color::LightYellow))
.data(&canvas_data.swap_data),
])
.datasets(&mem_canvas_vec)
.render(&mut f, middle_chunks[0]);
}

View File

@ -188,6 +188,11 @@ pub fn convert_mem_data(mem_data : &[data_collection::mem::MemData]) -> Vec<(f64
let mut result : Vec<(f64, f64)> = Vec::new();
for data in mem_data {
if data.mem_total_in_mb == 0 {
// Assume none (usually in the case of swap)! Also catches div by zero.
return result;
}
let current_time = std::time::Instant::now();
let new_entry = (
((STALE_MAX_MILLISECONDS as f64 - current_time.duration_since(data.instant).as_millis() as f64) * 10_f64).floor(),