summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMahmoud Al-Qudsi <mqudsi@neosmart.net>2020-03-19 20:03:52 -0500
committerGitHub <noreply@github.com>2020-03-19 21:03:52 -0400
commit2b418fb506ee59c356bc4f5336752d7b93f3309e (patch)
treee0436ec1db3b4da55aed3a306f2fe0e5bb777d79
parentcd6187ec5f73105d74e0e8a9a485bb2a897e6933 (diff)
Fix division by zero when memory data is not available (#85)
The total memory values may be zero when bottom is run on an unsupported (or not-fully-supported) platform. The previous behavior resulted in a NaN value for the memory datapoints, which was passed through to tui-rs which ultimately panicked when attempting to graph the memory widget.
-rw-r--r--src/app/data_farmer.rs14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/app/data_farmer.rs b/src/app/data_farmer.rs
index 43fe9cba..b8cd06d5 100644
--- a/src/app/data_farmer.rs
+++ b/src/app/data_farmer.rs
@@ -144,9 +144,10 @@ impl DataCollection {
&mut self, harvested_data: &Data, harvested_time: Instant, new_entry: &mut TimedData,
) {
// Memory
- let mem_percent = harvested_data.memory.mem_used_in_mb as f64
- / harvested_data.memory.mem_total_in_mb as f64
- * 100.0;
+ let mem_percent = match harvested_data.memory.mem_total_in_mb {
+ 0 => 0f64,
+ total => (harvested_data.memory.mem_used_in_mb as f64) / (total as f64) * 100.0,
+ };
let mem_joining_pts = if let Some((time, last_pt)) = self.timed_data_vec.last() {
generate_joining_points(*time, last_pt.mem_data.0, harvested_time, mem_percent)
} else {
@@ -157,9 +158,10 @@ impl DataCollection {
// Swap
if harvested_data.swap.mem_total_in_mb > 0 {
- let swap_percent = harvested_data.swap.mem_used_in_mb as f64
- / harvested_data.swap.mem_total_in_mb as f64
- * 100.0;
+ let swap_percent = match harvested_data.swap.mem_total_in_mb {
+ 0 => 0f64,
+ total => (harvested_data.swap.mem_used_in_mb as f64) / (total as f64) * 100.0,
+ };
let swap_joining_pt = if let Some((time, last_pt)) = self.timed_data_vec.last() {
generate_joining_points(*time, last_pt.swap_data.0, harvested_time, swap_percent)
} else {