nu_command/system/sys/
cpu.rs

1use super::trim_cstyle_null;
2use nu_engine::command_prelude::*;
3use sysinfo::{CpuRefreshKind, MINIMUM_CPU_UPDATE_INTERVAL, System};
4
5#[derive(Clone)]
6pub struct SysCpu;
7
8impl Command for SysCpu {
9    fn name(&self) -> &str {
10        "sys cpu"
11    }
12
13    fn signature(&self) -> Signature {
14        Signature::build("sys cpu")
15            .filter()
16            .switch(
17                "long",
18                "Get all available columns (slower, needs to sample CPU over time)",
19                Some('l'),
20            )
21            .category(Category::System)
22            .input_output_types(vec![(Type::Nothing, Type::table())])
23    }
24
25    fn description(&self) -> &str {
26        "View information about the system CPUs."
27    }
28
29    fn run(
30        &self,
31        engine_state: &EngineState,
32        stack: &mut Stack,
33        call: &Call,
34        _input: PipelineData,
35    ) -> Result<PipelineData, ShellError> {
36        let long = call.has_flag(engine_state, stack, "long")?;
37        Ok(cpu(long, call.head).into_pipeline_data())
38    }
39
40    fn examples(&self) -> Vec<Example> {
41        vec![Example {
42            description: "Show info about the system CPUs",
43            example: "sys cpu",
44            result: None,
45        }]
46    }
47}
48
49fn cpu(long: bool, span: Span) -> Value {
50    let mut sys = System::new();
51    if long {
52        sys.refresh_cpu_specifics(CpuRefreshKind::everything());
53        // We must refresh the CPU twice a while apart to get valid usage data.
54        // In theory we could just sleep MINIMUM_CPU_UPDATE_INTERVAL, but I've noticed that
55        // that gives poor results (error of ~5%). Decided to wait 2x that long, somewhat arbitrarily
56        std::thread::sleep(MINIMUM_CPU_UPDATE_INTERVAL * 2);
57        sys.refresh_cpu_specifics(CpuRefreshKind::nothing().with_cpu_usage());
58    } else {
59        sys.refresh_cpu_specifics(CpuRefreshKind::nothing().with_frequency());
60    }
61
62    let cpus = sys
63        .cpus()
64        .iter()
65        .map(|cpu| {
66            let load_avg = System::load_average();
67            let load_avg = format!(
68                "{:.2}, {:.2}, {:.2}",
69                load_avg.one, load_avg.five, load_avg.fifteen
70            );
71
72            let mut record = record! {
73                "name" => Value::string(trim_cstyle_null(cpu.name()), span),
74                "brand" => Value::string(trim_cstyle_null(cpu.brand()), span),
75                "vendor_id" => Value::string(trim_cstyle_null(cpu.vendor_id()), span),
76                "freq" => Value::int(cpu.frequency() as i64, span),
77                "load_average" => Value::string(load_avg, span),
78            };
79
80            if long {
81                // sysinfo CPU usage numbers are not very precise unless you wait a long time between refreshes.
82                // Round to 1DP (chosen somewhat arbitrarily) so people aren't misled by high-precision floats.
83                let rounded_usage = (f64::from(cpu.cpu_usage()) * 10.0).round() / 10.0;
84                record.push("cpu_usage", rounded_usage.into_value(span));
85            }
86
87            Value::record(record, span)
88        })
89        .collect();
90
91    Value::list(cpus, span)
92}