1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 09:13:54 +00:00

perf(memory_usage): replace sysinfo with sys-info (#1886)

* perf(memory_usage): replace sysinfo with sys-info

* format

* simplify error handling
This commit is contained in:
David Knaack 2020-11-24 03:17:15 +01:00 committed by GitHub
parent 688f1b3457
commit 0f483fad2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 32 deletions

30
Cargo.lock generated
View File

@ -389,12 +389,6 @@ dependencies = [
"rand", "rand",
] ]
[[package]]
name = "doc-comment"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]] [[package]]
name = "dtoa" name = "dtoa"
version = "0.4.6" version = "0.4.6"
@ -706,15 +700,6 @@ dependencies = [
"winrt-notification", "winrt-notification",
] ]
[[package]]
name = "ntapi"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a31937dea023539c72ddae0e3571deadc1414b300483fa7aaec176168cfa9d2"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.43" version = "0.1.43"
@ -1229,7 +1214,7 @@ dependencies = [
"serde_json", "serde_json",
"shell-words", "shell-words",
"starship_module_config_derive", "starship_module_config_derive",
"sysinfo", "sys-info",
"tempfile", "tempfile",
"term_size", "term_size",
"toml", "toml",
@ -1303,18 +1288,13 @@ dependencies = [
] ]
[[package]] [[package]]
name = "sysinfo" name = "sys-info"
version = "0.15.3" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67330cbee3b2a819e3365a773f05e884a136603687f812bf24db5b6c3d76b696" checksum = "e5cfbd84f86389198ade41b439f72a5b1b3a8ba728e61cd589e1720d0df44c39"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cc",
"doc-comment",
"libc", "libc",
"ntapi",
"once_cell",
"rayon",
"winapi",
] ]
[[package]] [[package]]

View File

@ -45,7 +45,7 @@ unicode-segmentation = "1.7.0"
gethostname = "0.2.1" gethostname = "0.2.1"
once_cell = "1.5.2" once_cell = "1.5.2"
chrono = "0.4" chrono = "0.4"
sysinfo = "0.15.3" sys-info = "0.7.0"
byte-unit = "4.0.9" byte-unit = "4.0.9"
starship_module_config_derive = { version = "0.1.2", path = "starship_module_config_derive" } starship_module_config_derive = { version = "0.1.2", path = "starship_module_config_derive" }
yaml-rust = "0.4" yaml-rust = "0.4"

View File

@ -1,5 +1,4 @@
use byte_unit::{Byte, ByteUnit}; use byte_unit::{Byte, ByteUnit};
use sysinfo::{RefreshKind, SystemExt};
use super::{Context, Module, RootModuleConfig, Shell}; use super::{Context, Module, RootModuleConfig, Shell};
@ -39,9 +38,21 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None; return None;
} }
let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_memory()); let system = match sys_info::mem_info() {
let used_memory_kib = system.get_used_memory(); Ok(info) => info,
let total_memory_kib = system.get_total_memory(); 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_used = (used_memory_kib as f64 / total_memory_kib as f64) * 100.;
let ram_pct = format_pct(ram_used, pct_sign); let ram_pct = format_pct(ram_used, pct_sign);
@ -51,8 +62,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
} }
let ram = format_usage_total(used_memory_kib, total_memory_kib); let ram = format_usage_total(used_memory_kib, total_memory_kib);
let total_swap_kib = system.get_total_swap(); let total_swap_kib = system.swap_total;
let used_swap_kib = system.get_used_swap(); 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 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_pct = format_pct(percent_swap_used, pct_sign);
let swap = format_usage_total(used_swap_kib, total_swap_kib); let swap = format_usage_total(used_swap_kib, total_swap_kib);