From 7e26b4fcc2c29041d3115dca8f05fd75fd7878df Mon Sep 17 00:00:00 2001 From: nibon7 Date: Fri, 5 Jan 2024 19:31:29 +0800 Subject: [PATCH] Bump sysinfo from 0.29 to 0.30 (#11484) # Description Bumps `sysinfo` to 0.30. * Changelog https://github.com/GuillaumeGomez/sysinfo/blob/master/CHANGELOG.md#0304 # User-Facing Changes N/A --- Cargo.lock | 24 +++++++-- crates/nu-cli/Cargo.toml | 2 +- crates/nu-cli/src/repl.rs | 15 +----- crates/nu-command/Cargo.toml | 2 +- crates/nu-command/src/debug/info.rs | 82 +++++++++++++++++------------ crates/nu-command/src/system/sys.rs | 58 ++++++++++---------- crates/nu-system/Cargo.toml | 2 +- crates/nu-system/src/os_info.rs | 5 +- 8 files changed, 100 insertions(+), 90 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43732e9b4..794549e63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2697,7 +2697,7 @@ dependencies = [ "percent-encoding", "reedline", "rstest", - "sysinfo", + "sysinfo 0.30.4", "unicode-segmentation", "uuid", "which 5.0.0", @@ -2876,7 +2876,7 @@ dependencies = [ "serde_urlencoded", "serde_yaml", "sha2", - "sysinfo", + "sysinfo 0.30.4", "tabled", "terminal_size 0.3.0", "titlecase", @@ -3060,7 +3060,7 @@ dependencies = [ "ntapi", "once_cell", "procfs", - "sysinfo", + "sysinfo 0.30.4", "windows 0.52.0", ] @@ -4089,7 +4089,7 @@ dependencies = [ "polars-error", "rayon", "smartstring", - "sysinfo", + "sysinfo 0.29.11", "version_check", ] @@ -5280,10 +5280,24 @@ dependencies = [ "libc", "ntapi", "once_cell", - "rayon", "winapi", ] +[[package]] +name = "sysinfo" +version = "0.30.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "717570a2533606f81f8cfac02a1915a620e725ffb78f6fc5e259769a4d747407" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "libc", + "ntapi", + "once_cell", + "rayon", + "windows 0.52.0", +] + [[package]] name = "tabled" version = "0.14.0" diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index 6f6cf7821..32ffabdb0 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -37,7 +37,7 @@ miette = { version = "5.10", features = ["fancy-no-backtrace"] } once_cell = "1.18" percent-encoding = "2" pathdiff = "0.2" -sysinfo = "0.29" +sysinfo = "0.30" unicode-segmentation = "1.10" uuid = { version = "1.6.0", features = ["v4"] } which = "5.0.0" diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index e59f1e267..e1c814a48 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -32,7 +32,7 @@ use std::{ sync::atomic::Ordering, time::Instant, }; -use sysinfo::SystemExt; +use sysinfo::System; // According to Daniel Imms @Tyriar, we need to do these this way: // <133 A><133 B><133 C> @@ -135,17 +135,6 @@ pub fn evaluate_repl( use_color, ); - start_time = std::time::Instant::now(); - let sys = sysinfo::System::new(); - perf( - "get sysinfo", - start_time, - file!(), - line!(), - column!(), - use_color, - ); - if let Some(s) = prerun_command { eval_source( engine_state, @@ -428,7 +417,7 @@ pub fn evaluate_repl( match input { Ok(Signal::Success(s)) => { - let hostname = sys.host_name(); + let hostname = System::host_name(); let history_supports_meta = matches!(config.history_file_format, HistoryFileFormat::Sqlite); if history_supports_meta && !s.is_empty() && line_editor.has_last_command_context() diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index eaf1ba3b9..94047fa0d 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -80,7 +80,7 @@ serde_json = "1.0" serde_urlencoded = "0.7" serde_yaml = "0.9" sha2 = "0.10" -sysinfo = "0.29" +sysinfo = "0.30" tabled = { version = "0.14.0", features = ["color"], default-features = false } terminal_size = "0.3" titlecase = "2.0" diff --git a/crates/nu-command/src/debug/info.rs b/crates/nu-command/src/debug/info.rs index 31480071f..c095af33f 100644 --- a/crates/nu-command/src/debug/info.rs +++ b/crates/nu-command/src/debug/info.rs @@ -4,7 +4,7 @@ use nu_protocol::{ record, Category, Example, IntoPipelineData, LazyRecord, PipelineData, Record, ShellError, Signature, Span, Type, Value, }; -use sysinfo::{Pid, PidExt, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt}; +use sysinfo::{MemoryRefreshKind, Pid, ProcessRefreshKind, RefreshKind, System}; const ENV_PATH_SEPARATOR_CHAR: char = { #[cfg(target_family = "windows")] { @@ -98,8 +98,9 @@ impl LazySystemInfoRecord { } "system" => { // only get information requested - let system_opt = - SystemOpt::from((system_option, || RefreshKind::new().with_memory())); + let system_opt = SystemOpt::from((system_option, || { + RefreshKind::new().with_memory(MemoryRefreshKind::everything()) + })); let system = system_opt.get_system(); @@ -135,53 +136,66 @@ impl LazySystemInfoRecord { "virtual_memory" => Value::filesize(p.virtual_memory() as i64, self.span), "status" => Value::string(p.status().to_string(), self.span), "root" => { - if let Some(filename) = p.exe().parent() { - Value::string(filename.to_string_lossy().to_string(), self.span) + if let Some(path) = p.exe().and_then(|p| p.parent()) { + Value::string(path.to_string_lossy().to_string(), self.span) } else { Value::nothing(self.span) } }, - "cwd" => Value::string(p.cwd().to_string_lossy().to_string(), self.span), - "exe_path" => Value::string(p.exe().to_string_lossy().to_string(), self.span), + "cwd" => { + if let Some(path) = p.cwd() { + Value::string(path.to_string_lossy().to_string(), self.span) + }else{ + Value::nothing(self.span) + } + }, + "exe_path" => { + if let Some(path)= p.exe() { + Value::string(path.to_string_lossy().to_string(), self.span) + }else{ + Value::nothing(self.span) + } + }, "command" => Value::string(p.cmd().join(" "), self.span), "name" => Value::string(p.name().to_string(), self.span), - "environment" => { - let mut env_rec = Record::new(); - for val in p.environ() { - if let Some((key, value)) = val.split_once('=') { - let is_env_var_a_list = { + "environment" => { + let mut env_rec = Record::new(); + for val in p.environ() { + if let Some((key, value)) = val.split_once('=') { + let is_env_var_a_list = { + { + #[cfg(target_family = "windows")] { - #[cfg(target_family = "windows")] - { - key == "Path" || key == "PATHEXT" || key == "PSMODULEPATH" || key == "PSModulePath" - } - #[cfg(not(target_family = "windows"))] - { - key == "PATH" || key == "DYLD_FALLBACK_LIBRARY_PATH" - } + key == "Path" || key == "PATHEXT" || key == "PSMODULEPATH" || key == "PSModulePath" + } + #[cfg(not(target_family = "windows"))] + { + key == "PATH" || key == "DYLD_FALLBACK_LIBRARY_PATH" } - }; - if is_env_var_a_list { - let items = value.split(ENV_PATH_SEPARATOR_CHAR).map(|r| Value::string(r.to_string(), self.span)).collect::>(); - env_rec.push(key.to_string(), Value::list(items, self.span)); - } else if key == "LS_COLORS" { // LS_COLORS is a special case, it's a colon separated list of key=value pairs - let items = value.split(':').map(|r| Value::string(r.to_string(), self.span)).collect::>(); - env_rec.push(key.to_string(), Value::list(items, self.span)); - } else { - env_rec.push(key.to_string(), Value::string(value.to_string(), self.span)); } + }; + if is_env_var_a_list { + let items = value.split(ENV_PATH_SEPARATOR_CHAR).map(|r| Value::string(r.to_string(), self.span)).collect::>(); + env_rec.push(key.to_string(), Value::list(items, self.span)); + } else if key == "LS_COLORS" { // LS_COLORS is a special case, it's a colon separated list of key=value pairs + let items = value.split(':').map(|r| Value::string(r.to_string(), self.span)).collect::>(); + env_rec.push(key.to_string(), Value::list(items, self.span)); + } else { + env_rec.push(key.to_string(), Value::string(value.to_string(), self.span)); } } - Value::record(env_rec, self.span) - }, + } + Value::record(env_rec, self.span) + }, }, self.span, )) } else { // If we can't get the process information, just return the system information // only get information requested - let system_opt = - SystemOpt::from((system_option, || RefreshKind::new().with_memory())); + let system_opt = SystemOpt::from((system_option, || { + RefreshKind::new().with_memory(MemoryRefreshKind::everything()) + })); let system = system_opt.get_system(); Ok(Value::record( @@ -228,7 +242,7 @@ impl<'a> LazyRecord<'a> for LazySystemInfoRecord { .without_disk_usage() .without_user(), ) - .with_memory(); + .with_memory(MemoryRefreshKind::everything()); // only get information requested let system = System::new_with_specifics(rk); diff --git a/crates/nu-command/src/system/sys.rs b/crates/nu-command/src/system/sys.rs index 2f92298a6..8b3b30c77 100644 --- a/crates/nu-command/src/system/sys.rs +++ b/crates/nu-command/src/system/sys.rs @@ -8,7 +8,7 @@ use nu_protocol::{ }; use std::time::{Duration, UNIX_EPOCH}; use sysinfo::{ - ComponentExt, CpuExt, CpuRefreshKind, DiskExt, NetworkExt, System, SystemExt, UserExt, + Components, CpuRefreshKind, Disks, Networks, System, Users, MINIMUM_CPU_UPDATE_INTERVAL, }; #[derive(Clone)] @@ -106,14 +106,12 @@ pub fn trim_cstyle_null(s: String) -> String { } pub fn disks(span: Span) -> Value { - let mut sys = System::new(); - sys.refresh_disks(); - sys.refresh_disks_list(); + let disks = Disks::new_with_refreshed_list(); let mut output = vec![]; - for disk in sys.disks() { + for disk in disks.list() { let device = trim_cstyle_null(disk.name().to_string_lossy().to_string()); - let typ = trim_cstyle_null(String::from_utf8_lossy(disk.file_system()).to_string()); + let typ = trim_cstyle_null(disk.file_system().to_string_lossy().to_string()); let record = record! { "device" => Value::string(device, span), @@ -131,12 +129,10 @@ pub fn disks(span: Span) -> Value { } pub fn net(span: Span) -> Value { - let mut sys = System::new(); - sys.refresh_networks(); - sys.refresh_networks_list(); + let networks = Networks::new_with_refreshed_list(); let mut output = vec![]; - for (iface, data) in sys.networks() { + for (iface, data) in networks.list() { let record = record! { "name" => Value::string(trim_cstyle_null(iface.to_string()), span), "sent" => Value::filesize(data.total_transmitted() as i64, span), @@ -154,7 +150,7 @@ pub fn cpu(span: Span) -> Value { // We must refresh the CPU twice a while apart to get valid usage data. // In theory we could just sleep MINIMUM_CPU_UPDATE_INTERVAL, but I've noticed that // that gives poor results (error of ~5%). Decided to wait 2x that long, somewhat arbitrarily - std::thread::sleep(System::MINIMUM_CPU_UPDATE_INTERVAL * 2); + std::thread::sleep(MINIMUM_CPU_UPDATE_INTERVAL * 2); sys.refresh_cpu_specifics(CpuRefreshKind::new().with_cpu_usage()); let mut output = vec![]; @@ -163,7 +159,7 @@ pub fn cpu(span: Span) -> Value { // Round to 1DP (chosen somewhat arbitrarily) so people aren't misled by high-precision floats. let rounded_usage = (cpu.cpu_usage() * 10.0).round() / 10.0; - let load_avg = sys.load_average(); + let load_avg = System::load_average(); let load_avg = trim_cstyle_null(format!( "{:.2}, {:.2}, {:.2}", load_avg.one, load_avg.five, load_avg.fifteen @@ -211,42 +207,39 @@ pub fn mem(span: Span) -> Value { } pub fn host(span: Span) -> Value { - let mut sys = System::new(); - sys.refresh_users_list(); - let mut record = Record::new(); - if let Some(name) = sys.name() { + if let Some(name) = System::name() { record.push("name", Value::string(trim_cstyle_null(name), span)); } - if let Some(version) = sys.os_version() { + if let Some(version) = System::os_version() { record.push("os_version", Value::string(trim_cstyle_null(version), span)); } - if let Some(long_version) = sys.long_os_version() { + if let Some(long_version) = System::long_os_version() { record.push( "long_os_version", Value::string(trim_cstyle_null(long_version), span), ); } - if let Some(version) = sys.kernel_version() { + if let Some(version) = System::kernel_version() { record.push( "kernel_version", Value::string(trim_cstyle_null(version), span), ); } - if let Some(hostname) = sys.host_name() { + if let Some(hostname) = System::host_name() { record.push("hostname", Value::string(trim_cstyle_null(hostname), span)); } record.push( "uptime", - Value::duration(1000000000 * sys.uptime() as i64, span), + Value::duration(1000000000 * System::uptime() as i64, span), ); // Creates a new SystemTime from the specified number of whole seconds - let d = UNIX_EPOCH + Duration::from_secs(sys.boot_time()); + let d = UNIX_EPOCH + Duration::from_secs(System::boot_time()); // Create DateTime from SystemTime let datetime = DateTime::::from(d); // Convert to local time and then rfc3339 @@ -254,11 +247,16 @@ pub fn host(span: Span) -> Value { record.push("boot_time", Value::string(timestamp_str, span)); - let mut users = vec![]; - for user in sys.users() { + let users = Users::new_with_refreshed_list(); + + let mut users_list = vec![]; + for user in users.list() { let mut groups = vec![]; for group in user.groups() { - groups.push(Value::string(trim_cstyle_null(group.to_string()), span)); + groups.push(Value::string( + trim_cstyle_null(group.name().to_string()), + span, + )); } let record = record! { @@ -266,24 +264,22 @@ pub fn host(span: Span) -> Value { "groups" => Value::list(groups, span), }; - users.push(Value::record(record, span)); + users_list.push(Value::record(record, span)); } if !users.is_empty() { - record.push("sessions", Value::list(users, span)); + record.push("sessions", Value::list(users_list, span)); } Value::record(record, span) } pub fn temp(span: Span) -> Value { - let mut sys = System::new(); - sys.refresh_components(); - sys.refresh_components_list(); + let components = Components::new_with_refreshed_list(); let mut output = vec![]; - for component in sys.components() { + for component in components.list() { let mut record = record! { "unit" => Value::string(component.label(), span), "temp" => Value::float(component.temperature() as f64, span), diff --git a/crates/nu-system/Cargo.toml b/crates/nu-system/Cargo.toml index 75f124019..bf36c7394 100644 --- a/crates/nu-system/Cargo.toml +++ b/crates/nu-system/Cargo.toml @@ -15,7 +15,7 @@ bench = false [dependencies] libc = "0.2" log = "0.4" -sysinfo = "0.29" +sysinfo = "0.30" [target.'cfg(target_family = "unix")'.dependencies] nix = { version = "0.27", default-features = false, features = ["fs", "term", "process", "signal"] } diff --git a/crates/nu-system/src/os_info.rs b/crates/nu-system/src/os_info.rs index d0611284a..e80edce77 100644 --- a/crates/nu-system/src/os_info.rs +++ b/crates/nu-system/src/os_info.rs @@ -1,5 +1,3 @@ -use sysinfo::SystemExt; - pub fn get_os_name() -> &'static str { std::env::consts::OS } @@ -13,8 +11,7 @@ pub fn get_os_family() -> &'static str { } pub fn get_kernel_version() -> String { - let sys = sysinfo::System::new(); - match sys.kernel_version() { + match sysinfo::System::kernel_version() { Some(v) => v, None => "unknown".to_string(), }