Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for total accumulated process CPU usage #1044

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add support for total accumulated process CPU usage
Most, if not all, CPU usage accounting for processes provides values that count
from the creation of the process. This total value is useful for a variety of
accounting tasks beyond the snapshot value that is currently available in
sysinfo. This change adds a `fn total_cpu_usage` to `trait ProcessExt` to
provide that value.
  • Loading branch information
bruceg committed Aug 14, 2023
commit a5a25ea7c39ba5430f2d0011d60385b61e34c46b
4 changes: 4 additions & 0 deletions src/apple/app_store/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl ProcessExt for Process {
0.0
}

fn total_cpu_usage(&self) -> f32 {
0.0
}

fn disk_usage(&self) -> DiskUsage {
DiskUsage::default()
}
Expand Down
4 changes: 4 additions & 0 deletions src/apple/macos/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ impl ProcessExt for Process {
self.cpu_usage
}

fn total_cpu_usage(&self) -> f32 {
(self.utime + self.stime) as f32
}

fn disk_usage(&self) -> DiskUsage {
DiskUsage {
read_bytes: self.read_bytes.saturating_sub(self.old_read_bytes),
Expand Down
4 changes: 4 additions & 0 deletions src/freebsd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ impl ProcessExt for Process {
self.cpu_usage
}

fn total_cpu_usage(&self) -> f32 {
FIXME
}

fn disk_usage(&self) -> DiskUsage {
DiskUsage {
written_bytes: self.written_bytes.saturating_sub(self.old_written_bytes),
Expand Down
8 changes: 7 additions & 1 deletion src/linux/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::sys::utils::{
use crate::utils::into_iter;
use crate::{DiskUsage, Gid, Pid, ProcessExt, ProcessRefreshKind, ProcessStatus, Signal, Uid};

const HZ: f32 = 100.;

#[doc(hidden)]
impl From<char> for ProcessStatus {
fn from(status: char) -> ProcessStatus {
Expand Down Expand Up @@ -194,6 +196,10 @@ impl ProcessExt for Process {
self.cpu_usage
}

fn total_cpu_usage(&self) -> f32 {
self.utime.saturating_add(self.stime) as f32 / HZ
}

fn disk_usage(&self) -> DiskUsage {
DiskUsage {
written_bytes: self.written_bytes.saturating_sub(self.old_written_bytes),
Expand Down Expand Up @@ -258,7 +264,7 @@ pub(crate) fn compute_cpu_usage(p: &mut Process, total_time: f32, max_value: f32
.saturating_sub(p.old_utime)
.saturating_add(p.stime.saturating_sub(p.old_stime)) as f32
/ total_time
* 100.)
* HZ)
.min(max_value);

for task in p.tasks.values_mut() {
Expand Down
14 changes: 14 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,20 @@ pub trait ProcessExt: Debug {
/// ```
fn cpu_usage(&self) -> f32;

/// Returns the total accumulated CPU usage (in
/// CPU-seconds). Notice that it might be bigger than the total
/// clock run time of a process if run on a multi-core machine.
///
/// ```no_run
/// use sysinfo::{Pid, ProcessExt, System, SystemExt};
///
/// let s = System::new_all();
/// if let Some(process) = s.process(Pid::from(1337)) {
/// println!("{}sec", process.total_cpu_usage());
/// }
/// ```
fn total_cpu_usage(&self) -> f32;

/// Returns number of bytes read and written to disk.
///
/// ⚠️ On Windows and FreeBSD, this method actually returns **ALL** I/O read and written bytes.
Expand Down
4 changes: 4 additions & 0 deletions src/unknown/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ impl ProcessExt for Process {
0.0
}

fn total_cpu_usage(&self) -> f32 {
0.0
}

fn disk_usage(&self) -> DiskUsage {
DiskUsage::default()
}
Expand Down
4 changes: 4 additions & 0 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,10 @@ impl ProcessExt for Process {
self.cpu_usage
}

fn total_cpu_usage(&self) -> f32 {
FIXME
}

fn disk_usage(&self) -> DiskUsage {
DiskUsage {
written_bytes: self.written_bytes.saturating_sub(self.old_written_bytes),
Expand Down