Skip to content

Commit

Permalink
Merge pull request #1255 from GuillaumeGomez/add-refresh-cpu-list
Browse files Browse the repository at this point in the history
Add `System::refresh_cpu_list` method
  • Loading branch information
GuillaumeGomez committed Apr 16, 2024
2 parents 67d4517 + 0f8d75c commit 6e330cb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/common.rs
Expand Up @@ -199,6 +199,27 @@ impl System {
self.refresh_cpu_specifics(CpuRefreshKind::new().with_frequency())
}

/// Refreshes the list of CPU.
///
/// Normally, this should almost never be needed as it's pretty rare for a computer
/// to add a CPU while running, but it's possible on some computers which shutdown
/// CPU if the load is low enough.
///
/// The `refresh_kind` argument tells what information you want to be retrieved
/// for each CPU.
///
/// ```no_run
/// use sysinfo::{CpuRefreshKind, System};
///
/// let mut s = System::new_all();
/// // We already have the list of CPU filled, but we want to recompute it
/// // in case new CPUs were added.
/// s.refresh_cpu_list(CpuRefreshKind::everything());
/// ```
pub fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind) {
self.inner.refresh_cpu_list(refresh_kind);
}

/// Refreshes all information related to CPUs information.
///
/// If you only want the CPU usage, use [`System::refresh_cpu_usage`] instead.
Expand Down
5 changes: 5 additions & 0 deletions src/unix/apple/system.rs
Expand Up @@ -169,6 +169,11 @@ impl SystemInner {
self.cpus.refresh(refresh_kind, self.port);
}

pub(crate) fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind) {
self.cpus = CpusWrapper::new();
self.cpus.refresh(refresh_kind, self.port);
}

#[cfg(any(target_os = "ios", feature = "apple-sandbox"))]
pub(crate) fn refresh_processes_specifics(
&mut self,
Expand Down
5 changes: 5 additions & 0 deletions src/unix/freebsd/system.rs
Expand Up @@ -68,6 +68,11 @@ impl SystemInner {
self.cpus.refresh(refresh_kind)
}

pub(crate) fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind) {
self.cpus = CpusWrapper::new();
self.cpus.refresh(refresh_kind);
}

pub(crate) fn refresh_processes_specifics(
&mut self,
filter: Option<&[Pid]>,
Expand Down
5 changes: 5 additions & 0 deletions src/unix/linux/system.rs
Expand Up @@ -505,6 +505,11 @@ impl SystemInner {
})
}
}

pub(crate) fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind) {
self.cpus = CpusWrapper::new();
self.refresh_cpu_specifics(refresh_kind);
}
}

fn read_u64(filename: &str) -> Option<u64> {
Expand Down
2 changes: 2 additions & 0 deletions src/unknown/system.rs
Expand Up @@ -29,6 +29,8 @@ impl SystemInner {

pub(crate) fn refresh_cpu_specifics(&mut self, _refresh_kind: CpuRefreshKind) {}

pub(crate) fn refresh_cpu_list(&mut self, _refresh_kind: CpuRefreshKind) {}

pub(crate) fn refresh_processes_specifics(
&mut self,
_filter: Option<&[Pid]>,
Expand Down
5 changes: 5 additions & 0 deletions src/windows/system.rs
Expand Up @@ -138,6 +138,11 @@ impl SystemInner {
}
}

pub(crate) fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind) {
self.cpus = CpusWrapper::new();
self.refresh_cpu_specifics(refresh_kind);
}

pub(crate) fn refresh_memory_specifics(&mut self, refresh_kind: MemoryRefreshKind) {
unsafe {
if refresh_kind.ram() {
Expand Down

0 comments on commit 6e330cb

Please sign in to comment.