From 324d6253246f481d90cd26ba7597cbdd96cc835b Mon Sep 17 00:00:00 2001 From: Reilly Wood <26268125+rgwood@users.noreply.github.com> Date: Wed, 1 Mar 2023 00:31:05 -0800 Subject: [PATCH] Fix CPU frequency in `sys` output (#8275) The `sys | get cpu.freq` column (supposed to contain the frequency for each CPU core in megahertz) was incorrect for 2 reasons: 1. We weren't telling the `sysinfo` crate to refresh CPU frequency info 2. We were overwriting the values in the column with the systemwide physical core count. Whoops! ### Before ![image](https://user-images.githubusercontent.com/26268125/222045977-2c021c92-794f-4498-b12c-e3a1bbaa7483.png) ### After ![image](https://user-images.githubusercontent.com/26268125/222046066-ff8ccd21-3c47-4d7d-8f14-e0744822cd2d.png) ## Future work This PR does not fix https://github.com/nushell/nushell/issues/8264 ; the `cpu_usage` column is still incorrect. --- crates/nu-command/src/system/sys.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/crates/nu-command/src/system/sys.rs b/crates/nu-command/src/system/sys.rs index 2ad738b1a..26a7e23c6 100644 --- a/crates/nu-command/src/system/sys.rs +++ b/crates/nu-command/src/system/sys.rs @@ -6,7 +6,9 @@ use nu_protocol::{ Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value, }; use std::time::{Duration, UNIX_EPOCH}; -use sysinfo::{ComponentExt, CpuExt, DiskExt, NetworkExt, System, SystemExt, UserExt}; +use sysinfo::{ + ComponentExt, CpuExt, CpuRefreshKind, DiskExt, NetworkExt, System, SystemExt, UserExt, +}; #[derive(Clone)] pub struct Sys; @@ -199,7 +201,10 @@ pub fn net(sys: &mut System, span: Span) -> Option { } pub fn cpu(sys: &mut System, span: Span) -> Option { - sys.refresh_cpu(); + // FIXME: we must refresh the CPU *twice* System::MINIMUM_CPU_UPDATE_INTERVAL apart to + // get valid usage data, we're currently only doing it once. Consider using a LazyRecord + // to avoid slowing down all calls to `sys` + sys.refresh_cpu_specifics(CpuRefreshKind::everything()); let mut output = vec![]; for cpu in sys.cpus() { @@ -246,12 +251,6 @@ pub fn cpu(sys: &mut System, span: Span) -> Option { span, }); - cols.push("freq".into()); - vals.push(Value::Int { - val: sys.physical_core_count().unwrap_or(0) as i64, - span, - }); - output.push(Value::Record { cols, vals, span }); }