Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/common/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2334,7 +2334,7 @@ pub enum ProcessesToUpdate<'a> {
/// information from `/proc/<pid>/` as well as all the information from `/proc/<pid>/task/<tid>/`
/// 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.
///
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/unix/apple/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub(crate) struct CpuUsage {
data: Arc<CpuData>,
// Cannot be frequency for each CPU apparently so we store it in the CPU usage...
frequency: u64,
total_time: u64,
}

impl CpuUsage {
Expand All @@ -123,16 +124,25 @@ impl CpuUsage {
percent: 0.,
data: Arc::new(CpuData::new(std::ptr::null_mut(), 0)),
frequency: 0,
total_time: 0,
}
}

pub(crate) fn percent(&self) -> f32 {
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 {
Expand All @@ -156,6 +166,7 @@ impl CpuInner {
percent: 0.,
data: cpu_data,
frequency,
total_time: 0,
},
vendor_id,
brand,
Expand All @@ -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<CpuData>) {
self.usage.percent = cpu_usage;
self.usage.data = cpu_data;
Expand All @@ -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
}
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions src/unix/freebsd/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions src/unix/linux/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions src/unknown/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ impl CpuInner {
0.0
}

pub(crate) fn cpu_total_time(&self) -> u64 {
0
}

pub(crate) fn name(&self) -> &str {
""
}
Expand Down
7 changes: 7 additions & 0 deletions src/windows/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ impl CpusWrapper {
global: CpuUsage {
percent: 0f32,
key_used: None,
total_time: 0,
},
cpus: Vec::new(),
}
Expand Down Expand Up @@ -309,6 +310,7 @@ impl CpusWrapper {
pub(crate) struct CpuUsage {
percent: f32,
pub(crate) key_used: Option<KeyHandler>,
total_time: u64,
}

impl CpuUsage {
Expand All @@ -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
}
Expand Down Expand Up @@ -357,6 +363,7 @@ impl CpuInner {
usage: CpuUsage {
percent: 0f32,
key_used: None,
total_time: 0,
},
vendor_id,
brand,
Expand Down