From 0f483fad2b2072f0872ec564ca157b8ec140e5ff Mon Sep 17 00:00:00 2001 From: David Knaack Date: Tue, 24 Nov 2020 03:17:15 +0100 Subject: [PATCH] perf(memory_usage): replace sysinfo with sys-info (#1886) * perf(memory_usage): replace sysinfo with sys-info * format * simplify error handling --- Cargo.lock | 30 +++++------------------------- Cargo.toml | 2 +- src/modules/memory_usage.rs | 23 +++++++++++++++++------ 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e024878..50c80766 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -389,12 +389,6 @@ dependencies = [ "rand", ] -[[package]] -name = "doc-comment" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" - [[package]] name = "dtoa" version = "0.4.6" @@ -706,15 +700,6 @@ dependencies = [ "winrt-notification", ] -[[package]] -name = "ntapi" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a31937dea023539c72ddae0e3571deadc1414b300483fa7aaec176168cfa9d2" -dependencies = [ - "winapi", -] - [[package]] name = "num-integer" version = "0.1.43" @@ -1229,7 +1214,7 @@ dependencies = [ "serde_json", "shell-words", "starship_module_config_derive", - "sysinfo", + "sys-info", "tempfile", "term_size", "toml", @@ -1303,18 +1288,13 @@ dependencies = [ ] [[package]] -name = "sysinfo" -version = "0.15.3" +name = "sys-info" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67330cbee3b2a819e3365a773f05e884a136603687f812bf24db5b6c3d76b696" +checksum = "e5cfbd84f86389198ade41b439f72a5b1b3a8ba728e61cd589e1720d0df44c39" dependencies = [ - "cfg-if 0.1.10", - "doc-comment", + "cc", "libc", - "ntapi", - "once_cell", - "rayon", - "winapi", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 09ea1657..fe1a43da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,7 @@ unicode-segmentation = "1.7.0" gethostname = "0.2.1" once_cell = "1.5.2" chrono = "0.4" -sysinfo = "0.15.3" +sys-info = "0.7.0" byte-unit = "4.0.9" starship_module_config_derive = { version = "0.1.2", path = "starship_module_config_derive" } yaml-rust = "0.4" diff --git a/src/modules/memory_usage.rs b/src/modules/memory_usage.rs index f3041c0e..12185567 100644 --- a/src/modules/memory_usage.rs +++ b/src/modules/memory_usage.rs @@ -1,5 +1,4 @@ use byte_unit::{Byte, ByteUnit}; -use sysinfo::{RefreshKind, SystemExt}; use super::{Context, Module, RootModuleConfig, Shell}; @@ -39,9 +38,21 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_memory()); - let used_memory_kib = system.get_used_memory(); - let total_memory_kib = system.get_total_memory(); + let system = match sys_info::mem_info() { + Ok(info) => info, + Err(err) => { + log::warn!("Unable to access memory usage information:\n{}", err); + return None; + } + }; + + // avail includes reclaimable memory, but isn't supported on all platforms + let avail_memory_kib = match system.avail { + 0 => system.free, + _ => system.avail, + }; + let used_memory_kib = system.total - avail_memory_kib; + let total_memory_kib = system.total; let ram_used = (used_memory_kib as f64 / total_memory_kib as f64) * 100.; let ram_pct = format_pct(ram_used, pct_sign); @@ -51,8 +62,8 @@ pub fn module<'a>(context: &'a Context) -> Option> { } let ram = format_usage_total(used_memory_kib, total_memory_kib); - let total_swap_kib = system.get_total_swap(); - let used_swap_kib = system.get_used_swap(); + let total_swap_kib = system.swap_total; + let used_swap_kib = system.swap_total - system.swap_free; let percent_swap_used = (used_swap_kib as f64 / total_swap_kib as f64) * 100.; let swap_pct = format_pct(percent_swap_used, pct_sign); let swap = format_usage_total(used_swap_kib, total_swap_kib);