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.
This commit is contained in:
Reilly Wood 2023-03-01 00:31:05 -08:00 committed by GitHub
parent e22b70acff
commit 324d625324
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,9 @@ use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
}; };
use std::time::{Duration, UNIX_EPOCH}; 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)] #[derive(Clone)]
pub struct Sys; pub struct Sys;
@ -199,7 +201,10 @@ pub fn net(sys: &mut System, span: Span) -> Option<Value> {
} }
pub fn cpu(sys: &mut System, span: Span) -> Option<Value> { pub fn cpu(sys: &mut System, span: Span) -> Option<Value> {
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![]; let mut output = vec![];
for cpu in sys.cpus() { for cpu in sys.cpus() {
@ -246,12 +251,6 @@ pub fn cpu(sys: &mut System, span: Span) -> Option<Value> {
span, 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 }); output.push(Value::Record { cols, vals, span });
} }