diff --git a/src/common/system.rs b/src/common/system.rs index 545bb9a3..397108b2 100644 --- a/src/common/system.rs +++ b/src/common/system.rs @@ -2334,7 +2334,7 @@ pub enum ProcessesToUpdate<'a> { /// information from `/proc//` as well as all the information from `/proc//task//` /// folders. This makes the refresh mechanism a lot slower depending on the number of tasks /// each process has. -/// +/// /// If you don't care about tasks information, use `ProcessRefreshKind::everything().without_tasks()` /// as much as possible. /// @@ -2777,6 +2777,22 @@ impl Cpu { self.inner.cpu_usage() } + /// Returns this CPU's total_time. + /// + /// ```no_run + /// use sysinfo::{System, RefreshKind, CpuRefreshKind}; + /// + /// let s = System::new_with_specifics( + /// RefreshKind::nothing().with_cpu(CpuRefreshKind::everything()), + /// ); + /// for cpu in s.cpus() { + /// println!("{}", cpu.cpu_total_time()); + /// } + /// ``` + pub fn cpu_total_time(&self) -> u64 { + self.inner.cpu_total_time() + } + /// Returns this CPU's name. /// /// ```no_run diff --git a/src/unix/apple/cpu.rs b/src/unix/apple/cpu.rs index 92edd4f0..fb8c542e 100644 --- a/src/unix/apple/cpu.rs +++ b/src/unix/apple/cpu.rs @@ -115,6 +115,7 @@ pub(crate) struct CpuUsage { data: Arc, // Cannot be frequency for each CPU apparently so we store it in the CPU usage... frequency: u64, + total_time: u64, } impl CpuUsage { @@ -123,6 +124,7 @@ impl CpuUsage { percent: 0., data: Arc::new(CpuData::new(std::ptr::null_mut(), 0)), frequency: 0, + total_time: 0, } } @@ -130,9 +132,17 @@ impl CpuUsage { self.percent } + pub(crate) fn total_time(&self) -> u64 { + self.total_time + } + pub(crate) fn set_cpu_usage(&mut self, value: f32) { self.percent = value; } + + pub(crate) fn set_cpu_total_time(&mut self, value: u64) { + self.total_time = value; + } } pub(crate) struct CpuInner { @@ -156,6 +166,7 @@ impl CpuInner { percent: 0., data: cpu_data, frequency, + total_time: 0, }, vendor_id, brand, @@ -166,6 +177,10 @@ impl CpuInner { self.usage.set_cpu_usage(cpu_usage); } + pub(crate) fn set_cpu_total_time(&mut self, total_time: u64) { + self.usage.set_cpu_total_time(total_time) + } + pub(crate) fn update(&mut self, cpu_usage: f32, cpu_data: Arc) { self.usage.percent = cpu_usage; self.usage.data = cpu_data; @@ -183,6 +198,10 @@ impl CpuInner { self.usage.percent() } + pub(crate) fn cpu_total_time(&self) -> u64 { + self.usage.total_time() + } + pub(crate) fn name(&self) -> &str { &self.name } @@ -355,6 +374,7 @@ pub(crate) fn init_cpus( let cpu_usage = compute_usage_of_cpu(&cpu, cpu_info, offset); cpu.inner.set_cpu_usage(cpu_usage); percentage += cpu.cpu_usage(); + cpu.inner.set_cpu_total_time(cpu.cpu_total_time()); } cpus.push(cpu); diff --git a/src/unix/freebsd/cpu.rs b/src/unix/freebsd/cpu.rs index 346fb3aa..613360f6 100644 --- a/src/unix/freebsd/cpu.rs +++ b/src/unix/freebsd/cpu.rs @@ -116,6 +116,7 @@ impl CpusWrapper { pub(crate) struct CpuInner { pub(crate) cpu_usage: f32, + pub(crate) cpu_total_time: u64, name: String, pub(crate) vendor_id: String, pub(crate) frequency: u64, @@ -125,6 +126,7 @@ impl CpuInner { pub(crate) fn new(name: String, vendor_id: String, frequency: u64) -> Self { Self { cpu_usage: 0., + cpu_total_time: 0, name, vendor_id, frequency, @@ -135,6 +137,10 @@ impl CpuInner { self.cpu_usage } + pub(crate) fn cpu_total_time(&self) -> u64 { + self.cpu_total_time + } + pub(crate) fn name(&self) -> &str { &self.name } diff --git a/src/unix/linux/cpu.rs b/src/unix/linux/cpu.rs index cd722f76..d4825c42 100644 --- a/src/unix/linux/cpu.rs +++ b/src/unix/linux/cpu.rs @@ -395,6 +395,10 @@ impl CpuInner { self.usage.percent } + pub(crate) fn cpu_total_time(&self) -> u64 { + self.usage.total_time + } + pub(crate) fn name(&self) -> &str { &self.name } diff --git a/src/unknown/cpu.rs b/src/unknown/cpu.rs index 9ef70f6f..475da6ff 100644 --- a/src/unknown/cpu.rs +++ b/src/unknown/cpu.rs @@ -7,6 +7,10 @@ impl CpuInner { 0.0 } + pub(crate) fn cpu_total_time(&self) -> u64 { + 0 + } + pub(crate) fn name(&self) -> &str { "" } diff --git a/src/windows/cpu.rs b/src/windows/cpu.rs index f13b63a4..c1b8f4c0 100644 --- a/src/windows/cpu.rs +++ b/src/windows/cpu.rs @@ -268,6 +268,7 @@ impl CpusWrapper { global: CpuUsage { percent: 0f32, key_used: None, + total_time: 0, }, cpus: Vec::new(), } @@ -309,6 +310,7 @@ impl CpusWrapper { pub(crate) struct CpuUsage { percent: f32, pub(crate) key_used: Option, + total_time: u64, } impl CpuUsage { @@ -330,6 +332,10 @@ impl CpuInner { self.usage.percent } + pub(crate) fn cpu_total_time(&self) -> u64 { + self.usage.total_time + } + pub(crate) fn name(&self) -> &str { &self.name } @@ -357,6 +363,7 @@ impl CpuInner { usage: CpuUsage { percent: 0f32, key_used: None, + total_time: 0, }, vendor_id, brand,