sysinfo/common/component.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::{ComponentInner, ComponentsInner, Error};
4
5/// Interacting with components.
6///
7/// ⚠️ Not implemented in NetBSD.
8///
9/// ```no_run
10/// use sysinfo::Components;
11///
12/// if let Ok(components) = Components::new_with_refreshed_list() {
13/// for component in &components {
14/// println!("{component:?}");
15/// }
16/// }
17/// ```
18pub struct Components {
19 pub(crate) inner: ComponentsInner,
20}
21
22impl std::ops::Deref for Components {
23 type Target = [Component];
24
25 fn deref(&self) -> &Self::Target {
26 self.inner.list()
27 }
28}
29
30impl std::ops::DerefMut for Components {
31 fn deref_mut(&mut self) -> &mut Self::Target {
32 self.inner.list_mut()
33 }
34}
35
36impl<'a> IntoIterator for &'a Components {
37 type Item = &'a Component;
38 type IntoIter = std::slice::Iter<'a, Component>;
39
40 fn into_iter(self) -> Self::IntoIter {
41 self.inner.list().iter()
42 }
43}
44
45impl<'a> IntoIterator for &'a mut Components {
46 type Item = &'a mut Component;
47 type IntoIter = std::slice::IterMut<'a, Component>;
48
49 fn into_iter(self) -> Self::IntoIter {
50 self.inner.list_mut().iter_mut()
51 }
52}
53
54impl Components {
55 /// Creates a new empty [`Components`][crate::Components] type.
56 ///
57 /// If you want it to be filled directly, take a look at
58 /// [`Components::new_with_refreshed_list`].
59 ///
60 /// ```no_run
61 /// use sysinfo::Components;
62 ///
63 /// if let Ok(mut components) = Components::new() {
64 /// components.refresh(false);
65 /// for component in &components {
66 /// println!("{component:?}");
67 /// }
68 /// }
69 /// ```
70 pub fn new() -> Result<Self, Error> {
71 Ok(Self {
72 inner: ComponentsInner::new()?,
73 })
74 }
75
76 /// Creates a new [`Components`][crate::Components] type with the components list
77 /// loaded.
78 ///
79 /// ```no_run
80 /// use sysinfo::Components;
81 ///
82 /// if let Ok(mut components) = Components::new_with_refreshed_list() {
83 /// for component in &components {
84 /// println!("{component:?}");
85 /// }
86 /// }
87 /// ```
88 pub fn new_with_refreshed_list() -> Result<Self, Error> {
89 let mut components = Self::new()?;
90 components.refresh(true);
91 Ok(components)
92 }
93
94 /// Refreshes the components list.
95 ///
96 /// ```no_run
97 /// use sysinfo::Components;
98 ///
99 /// if let Ok(mut components) = Components::new_with_refreshed_list() {
100 /// // We wait some time...?
101 /// components.refresh(false);
102 /// }
103 /// ```
104 pub fn refresh(&mut self, remove_not_listed_components: bool) {
105 self.inner.refresh();
106 if remove_not_listed_components {
107 // Remove interfaces which are gone.
108 self.inner.components.retain_mut(|c| {
109 if !c.inner.updated {
110 return false;
111 }
112 c.inner.updated = false;
113 true
114 });
115 } else {
116 for c in self.inner.components.iter_mut() {
117 c.inner.updated = false;
118 }
119 }
120 }
121}
122
123/// Getting a component temperature information.
124///
125/// ```no_run
126/// use sysinfo::Components;
127///
128/// if let Ok(components) = Components::new_with_refreshed_list() {
129/// for component in &components {
130/// if let Some(temperature) = component.temperature() {
131/// println!("{} {temperature}°C", component.label());
132/// } else {
133/// println!("{} (unknown temperature)", component.label());
134/// }
135/// }
136/// }
137/// ```
138pub struct Component {
139 pub(crate) inner: ComponentInner,
140}
141
142impl Component {
143 /// Returns the temperature of the component (in celsius degree).
144 ///
145 /// ## Linux
146 ///
147 /// Returns `f32::NAN` if it failed to retrieve it.
148 ///
149 /// ```no_run
150 /// use sysinfo::Components;
151 ///
152 /// if let Ok(components) = Components::new_with_refreshed_list() {
153 /// for component in &components {
154 /// if let Some(temperature) = component.temperature() {
155 /// println!("{temperature}°C");
156 /// }
157 /// }
158 /// }
159 /// ```
160 pub fn temperature(&self) -> Option<f32> {
161 self.inner.temperature()
162 }
163
164 /// Returns the maximum temperature of the component (in celsius degree).
165 ///
166 /// Note: if `temperature` is higher than the current `max`,
167 /// `max` value will be updated on refresh.
168 ///
169 /// ## Linux
170 ///
171 /// May be computed by `sysinfo` from kernel.
172 /// Returns `f32::NAN` if it failed to retrieve it.
173 ///
174 /// ```no_run
175 /// use sysinfo::Components;
176 ///
177 /// if let Ok(components) = Components::new_with_refreshed_list() {
178 /// for component in &components {
179 /// if let Some(max) = component.max() {
180 /// println!("{max}°C");
181 /// }
182 /// }
183 /// }
184 /// ```
185 pub fn max(&self) -> Option<f32> {
186 self.inner.max()
187 }
188
189 /// Returns the highest temperature before the component halts (in celsius degree).
190 ///
191 /// ## Linux
192 ///
193 /// Critical threshold defined by chip or kernel.
194 ///
195 /// ```no_run
196 /// use sysinfo::Components;
197 ///
198 /// if let Ok(components) = Components::new_with_refreshed_list() {
199 /// for component in &components {
200 /// if let Some(critical) = component.critical() {
201 /// println!("{critical}°C");
202 /// }
203 /// }
204 /// }
205 /// ```
206 pub fn critical(&self) -> Option<f32> {
207 self.inner.critical()
208 }
209
210 /// Returns the label of the component.
211 ///
212 /// ## Linux
213 ///
214 /// Since components information is retrieved thanks to `hwmon`,
215 /// the labels are generated as follows.
216 /// Note: it may change and it was inspired by `sensors` own formatting.
217 ///
218 /// | name | label | device_model | id_sensor | Computed label by `sysinfo` |
219 /// |---------|--------|------------|----------|----------------------|
220 /// | ✓ | ✓ | ✓ | ✓ | `"{name} {label} {device_model}"` |
221 /// | ✓ | ✓ | ✗ | ✓ | `"{name} {label}"` |
222 /// | ✓ | ✗ | ✓ | ✓ | `"{name} {device_model}"` |
223 /// | ✓ | ✗ | ✗ | ✓ | `"{name} temp{id}"` |
224 ///
225 /// ```no_run
226 /// use sysinfo::Components;
227 ///
228 /// if let Ok(components) = Components::new_with_refreshed_list() {
229 /// for component in &components {
230 /// println!("{}", component.label());
231 /// }
232 /// }
233 /// ```
234 pub fn label(&self) -> &str {
235 self.inner.label()
236 }
237
238 /// Returns the identifier of the component.
239 ///
240 /// Note: The identifier should be reasonably unique but is provided by the kernel.
241 /// It could change if the hardware changes or after a reboot.
242 ///
243 /// | OS | Computed ID by `sysinfo` | Example |
244 /// |----|--------------------------|----------|
245 /// | Linux/hwmon | hwmon file concatenated with the temp index. | ` hwmon0_1` if the temperature data comes from the `hwmon0/temp1_input` file. |
246 /// | Linux/thermal | thermal file name | `thermal_zone0` |
247 /// | FreeBSD | `cpu_` concatenated with the core index. | `cpu_1` for the first core. |
248 /// | macOS/arm | Serial ID reported by the HID driver. | |
249 /// | macOS/x86 | Technical ID sent to the OS (see below) | `TXCX` |
250 /// | Windows | `Computer` (same as the label) | `Computer` |
251 /// | appstore | Components are not available | None |
252 /// | unknown | Components are not available | None |
253 ///
254 /// For macOS on X86 the following identifiers are possible:
255 /// - `TXCX` or `TXCx` for PECI CPU (depending on if run on iMac or MacBook)
256 /// - `TC0P` for CPU Proximity
257 /// - `TG0P` for GPU
258 /// - `TB0T` for Battery
259 ///
260 /// ```no_run
261 /// use sysinfo::Components;
262 ///
263 /// if let Ok(components) = Components::new_with_refreshed_list() {
264 /// for component in &components {
265 /// if let Some(id) = component.id() {
266 /// println!("{id}");
267 /// }
268 /// }
269 /// }
270 /// ```
271 pub fn id(&self) -> Option<&str> {
272 self.inner.id()
273 }
274
275 /// Refreshes component.
276 ///
277 /// ```no_run
278 /// use sysinfo::Components;
279 ///
280 /// if let Ok(mut components) = Components::new_with_refreshed_list() {
281 /// for component in components.iter_mut() {
282 /// component.refresh();
283 /// }
284 /// }
285 /// ```
286 pub fn refresh(&mut self) {
287 self.inner.refresh()
288 }
289}
290
291#[cfg(test)]
292mod tests {
293 use crate::*;
294
295 #[test]
296 fn test_components_mac_m1() {
297 if let Ok(mut components) = Components::new() {
298 components.refresh(false);
299 components.refresh(false);
300 }
301 }
302}