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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/apple/app_store/process.rs
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
Expand Up @@ -181,6 +181,10 @@
self.cpu_usage
}

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

Check failure on line 185 in src/apple/macos/process.rs

View workflow job for this annotation

GitHub Actions / Check 1.63.0 / x86_64-apple-darwin

no field `utime` on type `&apple::macos::process::Process`

Check failure on line 185 in src/apple/macos/process.rs

View workflow job for this annotation

GitHub Actions / Check 1.63.0 / x86_64-apple-darwin

no field `stime` on type `&apple::macos::process::Process`

Check failure on line 185 in src/apple/macos/process.rs

View workflow job for this annotation

GitHub Actions / Check stable / x86_64-apple-darwin

no field `utime` on type `&apple::macos::process::Process`

Check failure on line 185 in src/apple/macos/process.rs

View workflow job for this annotation

GitHub Actions / Check stable / x86_64-apple-darwin

no field `stime` on type `&apple::macos::process::Process`

Check failure on line 185 in src/apple/macos/process.rs

View workflow job for this annotation

GitHub Actions / Check nightly / x86_64-apple-darwin

no field `utime` on type `&apple::macos::process::Process`

Check failure on line 185 in src/apple/macos/process.rs

View workflow job for this annotation

GitHub Actions / Check nightly / x86_64-apple-darwin

no field `stime` on type `&apple::macos::process::Process`
}

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
Expand Up @@ -129,6 +129,10 @@ impl ProcessExt for Process {
self.cpu_usage
}

fn total_cpu_usage(&self) -> f32 {
FIXME
bruceg marked this conversation as resolved.
Show resolved Hide resolved
}

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
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.;
bruceg marked this conversation as resolved.
Show resolved Hide resolved

#[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
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;
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved

/// 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
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
Expand Up @@ -599,6 +599,10 @@
self.cpu_usage
}

fn total_cpu_usage(&self) -> f32 {
bruceg marked this conversation as resolved.
Show resolved Hide resolved
FIXME

Check failure on line 603 in src/windows/process.rs

View workflow job for this annotation

GitHub Actions / Check nightly / x86_64-pc-windows-msvc

cannot find value `FIXME` in this scope

Check failure on line 603 in src/windows/process.rs

View workflow job for this annotation

GitHub Actions / Check 1.63.0 / x86_64-pc-windows-msvc

cannot find value `FIXME` in this scope

Check failure on line 603 in src/windows/process.rs

View workflow job for this annotation

GitHub Actions / Check stable / x86_64-pc-windows-msvc

cannot find value `FIXME` in this scope
}

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