//! Per-CPU plugin. use crate::cpu_percent::cpu_percent; use crate::fields; use glances_core::plugin::{Field, Plugin, PluginModel, UpdateContext}; use glances_core::{Args, Config}; use serde_json::Value; pub struct PerCpuPlugin { pub model: PluginModel, } impl PerCpuPlugin { pub fn new(config: &Config, args: &Args) -> Self { let fields = fields! { "cpu_number" => Field::new("CPU number"), "total" => Field::new("Sum of CPU percentages (except idle) for current CPU number.").unit("percent"), "system" => Field::new("Percent time spent in kernel space. System CPU time is the time spent running code in the Operating System kernel.").unit("percent"), "user" => Field::new("CPU percent time spent in user space. User CPU time is the time spent on the processor running your program's code (or code in libraries).").unit("percent"), "iowait" => Field::new("*(Linux)*: percent time spent by the CPU waiting for I/O operations to complete.").unit("percent"), "idle" => Field::new("percent of CPU used by any program. Every program or task that runs on a computer system occupies a certain amount of processing time on the CPU. If the CPU has completed all tasks it is idle.").unit("percent"), "irq" => Field::new("*(Linux and BSD)*: percent time spent servicing/handling hardware/software interrupts. Time servicing interrupts (hardware + software).").unit("percent"), "nice" => Field::new("*(Unix)*: percent time occupied by user level processes with a positive nice value. The time the CPU has spent running users' processes that are *niced*.").unit("percent"), "steal" => Field::new("*(Linux)*: percentage of time a virtual CPU waits for a real CPU while the hypervisor is servicing another virtual processor.").unit("percent"), "guest" => Field::new("*(Linux)*: percent of time spent running a virtual CPU for guest operating systems under the control of the Linux kernel.").unit("percent"), "guest_nice" => Field::new("*(Linux)*: percent of time spent running a niced guest (virtual CPU).").unit("percent"), "softirq" => Field::new("*(Linux)*: percent of time spent handling software interrupts.").unit("percent"), "dpc" => Field::new("*(Windows)*: percent of time spent handling deferred procedure calls.").unit("percent"), "interrupt" => Field::new("*(Windows)*: percent of time spent handling software interrupts.").unit("percent"), }; let mut model = PluginModel::new( "percpu", fields, Vec::new(), Value::Array(Vec::new()), config, args.time.unwrap_or(2.0), args.disable_history, args.disable_config_exec, ); model.set_item_key("cpu_number"); Self { model } } } impl Plugin for PerCpuPlugin { fn name(&self) -> &str { "percpu" } fn model(&self) -> &PluginModel { &self.model } fn model_mut(&mut self) -> &mut PluginModel { &mut self.model } fn get_key(&self) -> Option<&'static str> { Some("cpu_number") } fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result { let mut state = cpu_percent().lock().unwrap(); Ok(Value::Array(state.get_percpu().to_vec())) } }