Skip to content

Commit

Permalink
Merge pull request #1055 from patrickelectric/add_more_refresh
Browse files Browse the repository at this point in the history
traits: Add `refresh_cpu_frequency` and `refresh_cpu_all`
  • Loading branch information
GuillaumeGomez authored Sep 9, 2023
2 parents c1d6849 + 0e1ef48 commit ffd95c1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/c_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ pub extern "C" fn sysinfo_refresh_memory(system: CSystem) {
}
}

/// Equivalent of [`System::refresh_cpu()`][crate::System#method.refresh_cpu].
/// Equivalent of [`System::refresh_cpu_usage()`][crate::System#method.refresh_cpu_usage].
#[no_mangle]
pub extern "C" fn sysinfo_refresh_cpu(system: CSystem) {
assert!(!system.is_null());
unsafe {
let mut system: Box<System> = Box::from_raw(system as *mut System);
{
let system: &mut System = system.borrow_mut();
system.refresh_cpu();
system.refresh_cpu_usage();
}
Box::into_raw(system);
}
Expand Down
2 changes: 1 addition & 1 deletion src/freebsd/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl SystemExt for System {

fn refresh_components_list(&mut self) {
if self.cpus.cpus.is_empty() {
self.refresh_cpu();
self.refresh_cpu_usage();
}
self.components = unsafe { super::component::get_components(self.cpus.cpus.len()) };
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ mod test {
}
let mut s = System::new();
for _ in 0..10 {
s.refresh_cpu();
s.refresh_cpu_usage();
// Wait a bit to update CPU usage values
std::thread::sleep(System::MINIMUM_CPU_UPDATE_INTERVAL);
if s.cpus().iter().any(|c| c.cpu_usage() > 0.0) {
Expand Down Expand Up @@ -444,7 +444,7 @@ mod test {
.physical_core_count()
.expect("failed to get number of physical cores");

s.refresh_cpu();
s.refresh_cpu_usage();
// The cpus shouldn't be empty anymore.
assert!(!s.cpus().is_empty());

Expand Down Expand Up @@ -487,7 +487,7 @@ mod test {
for proc_ in s.cpus() {
assert_eq!(proc_.frequency(), 0);
}
s.refresh_cpu();
s.refresh_cpu_usage();
for proc_ in s.cpus() {
assert_eq!(proc_.frequency(), 0);
}
Expand Down
41 changes: 38 additions & 3 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ pub trait SystemExt: Sized + Debug + Default + Send + Sync {
/// ```
fn refresh_system(&mut self) {
self.refresh_memory();
self.refresh_cpu();
self.refresh_cpu_usage();
self.refresh_components();
}

Expand All @@ -760,7 +760,7 @@ pub trait SystemExt: Sized + Debug + Default + Send + Sync {
/// ```
fn refresh_memory(&mut self);

/// Refreshes CPUs information.
/// Refreshes CPUs usage.
///
/// ⚠️ Please note that the result will very likely be inaccurate at the first call.
/// You need to call this method at least twice (with a bit of time between each call, like
Expand All @@ -774,10 +774,45 @@ pub trait SystemExt: Sized + Debug + Default + Send + Sync {
/// use sysinfo::{System, SystemExt};
///
/// let mut s = System::new_all();
/// s.refresh_cpu_usage();
/// ```
fn refresh_cpu_usage(&mut self) {
self.refresh_cpu_specifics(CpuRefreshKind::new().with_cpu_usage())
}

/// Refreshes CPUs frequency information.
///
/// Calling this method is the same as calling
/// `refresh_cpu_specifics(CpuRefreshKind::new().with_frequency())`.
///
/// ```no_run
/// use sysinfo::{System, SystemExt};
///
/// let mut s = System::new_all();
/// s.refresh_cpu_frequency();
/// ```
fn refresh_cpu_frequency(&mut self) {
self.refresh_cpu_specifics(CpuRefreshKind::new().with_frequency())
}

/// Refreshes all information related to CPUs information.
///
/// ⚠️ Please note that the result will very likely be inaccurate at the first call.
/// You need to call this method at least twice (with a bit of time between each call, like
/// 200 ms, take a look at [`SystemExt::MINIMUM_CPU_UPDATE_INTERVAL`] for more information)
/// to get accurate value as it uses previous results to compute the next value.
///
/// Calling this method is the same as calling
/// `refresh_cpu_specifics(CpuRefreshKind::everything())`.
///
/// ```no_run
/// use sysinfo::{System, SystemExt};
///
/// let mut s = System::new_all();
/// s.refresh_cpu();
/// ```
fn refresh_cpu(&mut self) {
self.refresh_cpu_specifics(CpuRefreshKind::new().with_cpu_usage())
self.refresh_cpu_specifics(CpuRefreshKind::everything())
}

/// Refreshes CPUs specific information.
Expand Down

0 comments on commit ffd95c1

Please sign in to comment.