Skip to content

Commit

Permalink
Rename Components::refresh_list into refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Dec 3, 2024
1 parent 3e2fdc3 commit 6307c35
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 157 deletions.
12 changes: 1 addition & 11 deletions benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,7 @@ fn bench_refresh_components(b: &mut test::Bencher) {
let mut c = sysinfo::Components::new_with_refreshed_list();

b.iter(move || {
c.refresh();
});
}

#[cfg(feature = "component")]
#[bench]
fn bench_refresh_components_list(b: &mut test::Bencher) {
let mut c = sysinfo::Components::new_with_refreshed_list();

b.iter(move || {
c.refresh_list();
c.refresh(false);
});
}

Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn interpret_input(
}
"refresh_components" => {
writeln!(&mut io::stdout(), "Refreshing component list...");
components.refresh_list();
components.refresh(true);
writeln!(&mut io::stdout(), "Done.");
}
"refresh_cpu" => {
Expand Down
59 changes: 20 additions & 39 deletions src/common/component.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::utils::into_iter_mut;
use crate::{ComponentInner, ComponentsInner};

/// Interacting with components.
Expand Down Expand Up @@ -79,7 +78,7 @@ impl Components {
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
/// components.refresh(false);
/// for component in &components {
/// println!("{component:?}");
/// }
Expand All @@ -90,9 +89,8 @@ impl Components {
}
}

/// Creates a new [`Components`][crate::Components] type with the user list
/// loaded. It is a combination of [`Components::new`] and
/// [`Components::refresh_list`].
/// Creates a new [`Components`][crate::Components] type with the components list
/// loaded.
///
/// ```no_run
/// use sysinfo::Components;
Expand All @@ -104,7 +102,7 @@ impl Components {
/// ```
pub fn new_with_refreshed_list() -> Self {
let mut components = Self::new();
components.refresh_list();
components.refresh(true);
components
}

Expand Down Expand Up @@ -137,44 +135,27 @@ impl Components {
self.inner.list_mut()
}

/// Refreshes the listed components' information.
///
/// ⚠️ If a component is added or removed, this method won't take it into account. Use
/// [`Components::refresh_list`] instead.
///
/// ⚠️ If you didn't call [`Components::refresh_list`] beforehand, this method will do
/// nothing as the component list will be empty.
/// Refreshes the components list.
///
/// ```no_run
/// use sysinfo::Components;
///
/// let mut components = Components::new_with_refreshed_list();
/// // We wait some time...?
/// components.refresh();
/// components.refresh(false);
/// ```
pub fn refresh(&mut self) {
#[cfg(all(
feature = "multithread",
not(feature = "unknown-ci"),
not(all(target_os = "macos", feature = "apple-sandbox")),
))]
use rayon::iter::ParallelIterator;
into_iter_mut(self.list_mut()).for_each(|component| component.refresh());
}

/// The component list will be emptied then completely recomputed.
///
/// ```no_run
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
/// ```
///
/// ⚠️ This function is not doing anything on macOS until
/// [this issue](https://github.com/GuillaumeGomez/sysinfo/issues/1279) is fixed.
pub fn refresh_list(&mut self) {
self.inner.refresh_list()
pub fn refresh(&mut self, remove_not_listed_components: bool) {
self.inner.refresh();
if remove_not_listed_components {
// Remove interfaces which are gone.
self.inner.components.retain_mut(|c| {
if !c.inner.updated {
return false;
}
c.inner.updated = false;
true
});
}
}
}

Expand Down Expand Up @@ -300,7 +281,7 @@ mod tests {
#[test]
fn test_components_mac_m1() {
let mut components = Components::new();
components.refresh_list();
components.refresh_list();
components.refresh(false);
components.refresh(false);
}
}
8 changes: 5 additions & 3 deletions src/unix/apple/app_store/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use crate::Component;

pub(crate) struct ComponentInner;
pub(crate) struct ComponentInner {
pub(crate) updated: bool,
}

impl ComponentInner {
pub(crate) fn temperature(&self) -> f32 {
Expand All @@ -25,7 +27,7 @@ impl ComponentInner {
}

pub(crate) struct ComponentsInner {
components: Vec<Component>,
pub(crate) components: Vec<Component>,
}

impl ComponentsInner {
Expand All @@ -51,7 +53,7 @@ impl ComponentsInner {
&mut self.components
}

pub(crate) fn refresh_list(&mut self) {
pub(crate) fn refresh(&mut self) {
// Doesn't do anything.
}
}
25 changes: 17 additions & 8 deletions src/unix/apple/macos/component/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::sys::utils::CFReleaser;
use crate::Component;

pub(crate) struct ComponentsInner {
components: Vec<Component>,
pub(crate) components: Vec<Component>,
client: Option<CFReleaser<__IOHIDEventSystemClient>>,
}

Expand Down Expand Up @@ -51,9 +51,7 @@ impl ComponentsInner {
}

#[allow(unreachable_code)]
pub(crate) fn refresh_list(&mut self) {
self.components.clear();

pub(crate) fn refresh(&mut self) {
unsafe {
let matches = match CFReleaser::new(matching(
kHIDPage_AppleVendor,
Expand Down Expand Up @@ -103,12 +101,11 @@ impl ComponentsInner {
continue;
}

let name = match CFReleaser::new(IOHIDServiceClientCopyProperty(
let Some(name) = CFReleaser::new(IOHIDServiceClientCopyProperty(
service as *const _,
key_ref.inner(),
)) {
Some(n) => n,
None => continue,
)) else {
continue;
};

let name_ptr =
Expand All @@ -119,6 +116,16 @@ impl ComponentsInner {

let name_str = CStr::from_ptr(name_ptr).to_string_lossy().to_string();

if let Some(c) = self
.components
.iter_mut()
.find(|c| c.inner.label == name_str)
{
c.refresh();
c.inner.updated = true;
continue;
}

let mut component = ComponentInner::new(name_str, None, None, service as *mut _);
component.refresh();

Expand All @@ -134,6 +141,7 @@ pub(crate) struct ComponentInner {
label: String,
max: f32,
critical: Option<f32>,
pub(crate) updated: bool,
}

unsafe impl Send for ComponentInner {}
Expand All @@ -152,6 +160,7 @@ impl ComponentInner {
max: max.unwrap_or(0.),
critical,
temperature: 0.,
updated: true,
}
}

Expand Down
36 changes: 21 additions & 15 deletions src/unix/apple/macos/component/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ComponentFFI {

// Used to get CPU information, not supported on iOS, or inside the default macOS sandbox.
pub(crate) struct ComponentsInner {
components: Vec<Component>,
pub(crate) components: Vec<Component>,
connection: Option<IoService>,
}

Expand Down Expand Up @@ -77,20 +77,24 @@ impl ComponentsInner {
&mut self.components
}

pub(crate) fn refresh_list(&mut self) {
if let Some(ref connection) = self.connection {
let connection = connection.inner();
self.components.clear();
// getting CPU critical temperature
let critical_temp =
get_temperature(connection, &['T' as i8, 'C' as i8, '0' as i8, 'D' as i8, 0]);

for (id, v) in COMPONENTS_TEMPERATURE_IDS.iter() {
if let Some(c) =
ComponentInner::new((*id).to_owned(), None, critical_temp, v, connection)
{
self.components.push(Component { inner: c });
}
pub(crate) fn refresh(&mut self) {
let Some(ref connection) = self.connection else {
sysinfo_debug!("No connection to IoService, skipping components refresh");
return;
};
let connection = connection.inner();
// getting CPU critical temperature
let critical_temp =
get_temperature(connection, &['T' as i8, 'C' as i8, '0' as i8, 'D' as i8, 0]);

for (id, v) in COMPONENTS_TEMPERATURE_IDS.iter() {
if let Some(c) = self.components.iter_mut().find(|c| c.inner.label == *id) {
c.refresh();
c.inner.updated = true;
} else if let Some(c) =
ComponentInner::new((*id).to_owned(), None, critical_temp, v, connection)
{
self.components.push(Component { inner: c });
}
}
}
Expand All @@ -102,6 +106,7 @@ pub(crate) struct ComponentInner {
critical: Option<f32>,
label: String,
ffi_part: ComponentFFI,
pub(crate) updated: bool,
}

impl ComponentInner {
Expand All @@ -120,6 +125,7 @@ impl ComponentInner {
max: max.unwrap_or(temperature),
critical,
ffi_part,
updated: true,
})
}

Expand Down
38 changes: 23 additions & 15 deletions src/unix/freebsd/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) struct ComponentInner {
label: String,
temperature: f32,
max: f32,
pub(crate) updated: bool,
}

impl ComponentInner {
Expand Down Expand Up @@ -51,7 +52,7 @@ unsafe fn refresh_component(id: &[u8]) -> Option<f32> {

pub(crate) struct ComponentsInner {
nb_cpus: usize,
components: Vec<Component>,
pub(crate) components: Vec<Component>,
}

impl ComponentsInner {
Expand Down Expand Up @@ -82,22 +83,29 @@ impl ComponentsInner {
&mut self.components
}

pub(crate) fn refresh_list(&mut self) {
self.components.clear();
for core in 0..self.nb_cpus {
unsafe {
let id = format!("dev.cpu.{core}.temperature\0").as_bytes().to_vec();
if let Some(temperature) = refresh_component(&id) {
self.components.push(Component {
inner: ComponentInner {
id,
label: format!("CPU {}", core + 1),
temperature,
max: temperature,
},
});
pub(crate) fn refresh(&mut self) {
if self.components.len() != self.nb_cpus {
for core in 0..self.nb_cpus {
unsafe {
let id = format!("dev.cpu.{core}.temperature\0").as_bytes().to_vec();
if let Some(temperature) = refresh_component(&id) {
self.components.push(Component {
inner: ComponentInner {
id,
label: format!("CPU {}", core + 1),
temperature,
max: temperature,
updated: true,
},
});
}
}
}
} else {
for c in self.components.iter_mut() {
c.refresh();
c.inner.updated = true;
}
}
}
}
Loading

0 comments on commit 6307c35

Please sign in to comment.