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 System::refresh_cpu_list method #1255

Merged
merged 1 commit into from Apr 16, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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