From 0f8d75c7e053922171cea64ef1c51467f1ee70aa Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 16 Apr 2024 16:45:26 +0200 Subject: [PATCH] Add `System::refresh_cpu_list` method --- src/common.rs | 21 +++++++++++++++++++++ src/unix/apple/system.rs | 5 +++++ src/unix/freebsd/system.rs | 5 +++++ src/unix/linux/system.rs | 5 +++++ src/unknown/system.rs | 2 ++ src/windows/system.rs | 5 +++++ 6 files changed, 43 insertions(+) diff --git a/src/common.rs b/src/common.rs index ba987610b..e4ddd20f1 100644 --- a/src/common.rs +++ b/src/common.rs @@ -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. diff --git a/src/unix/apple/system.rs b/src/unix/apple/system.rs index c58f86a91..f9f118dcc 100644 --- a/src/unix/apple/system.rs +++ b/src/unix/apple/system.rs @@ -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, diff --git a/src/unix/freebsd/system.rs b/src/unix/freebsd/system.rs index 05eb9987e..ecbf6227a 100644 --- a/src/unix/freebsd/system.rs +++ b/src/unix/freebsd/system.rs @@ -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]>, diff --git a/src/unix/linux/system.rs b/src/unix/linux/system.rs index 4742eca67..c4aa9be80 100644 --- a/src/unix/linux/system.rs +++ b/src/unix/linux/system.rs @@ -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 { diff --git a/src/unknown/system.rs b/src/unknown/system.rs index 19aada516..1ada65327 100644 --- a/src/unknown/system.rs +++ b/src/unknown/system.rs @@ -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]>, diff --git a/src/windows/system.rs b/src/windows/system.rs index 267a978b9..77a90a279 100644 --- a/src/windows/system.rs +++ b/src/windows/system.rs @@ -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() {