diff --git a/src/app/data_harvester/memory/sysinfo.rs b/src/app/data_harvester/memory/sysinfo.rs index d03f7822..63e16111 100644 --- a/src/app/data_harvester/memory/sysinfo.rs +++ b/src/app/data_harvester/memory/sysinfo.rs @@ -41,16 +41,16 @@ pub(crate) fn get_swap_usage(sys: &System) -> Option { /// between the available and free memory. On windows, this will always be 0. #[cfg(not(target_os = "windows"))] pub(crate) fn get_cache_usage(sys: &System) -> Option { - let mem_used_in_kib = (sys.available_memory() - sys.free_memory()) / 1024; - let mem_total_in_kib = sys.total_memory() / 1024; + let mem_used = sys.available_memory() - sys.free_memory(); + let mem_total = sys.total_memory(); Some(MemHarvest { - total_kib: mem_total_in_kib, - used_kib: mem_used_in_kib, - use_percent: if mem_total_in_kib == 0 { + total_bytes: mem_total, + used_bytes: mem_used, + use_percent: if mem_total == 0 { None } else { - Some(mem_used_in_kib as f64 / mem_total_in_kib as f64 * 100.0) + Some(mem_used as f64 / mem_total as f64 * 100.0) }, }) } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 05ff1c10..f3341360 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -280,33 +280,17 @@ fn get_mem_binary_unit_and_denominator(bytes: u64) -> (&'static str, f64) { } } +/// Returns the unit type and denominator for given total amount of memory in kibibytes. pub fn convert_mem_label(harvest: &MemHarvest) -> Option<(String, String)> { - /// Returns the unit type and denominator for given total amount of memory in kibibytes. - fn return_unit_and_denominator_for_mem_kib(mem_total_kib: u64) -> (&'static str, f64) { - if mem_total_kib < 1024 { - // Stay with KiB - ("KiB", 1.0) - } else if mem_total_kib < MEBI_LIMIT { - // Use MiB - ("MiB", KIBI_LIMIT_F64) - } else if mem_total_kib < GIBI_LIMIT { - // Use GiB - ("GiB", MEBI_LIMIT_F64) - } else { - // Use TiB - ("TiB", GIBI_LIMIT_F64) - } - } - - if harvest.total_kib > 0 { + if harvest.total_bytes > 0 { Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { - let (unit, denominator) = return_unit_and_denominator_for_mem_kib(harvest.total_kib); + let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes); format!( " {:.1}{}/{:.1}{}", - harvest.used_kib as f64 / denominator, + harvest.used_bytes as f64 / denominator, unit, - (harvest.total_kib as f64 / denominator), + (harvest.total_bytes as f64 / denominator), unit ) }))