diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2020-12-17 14:57:38 -0800 |
---|---|---|
committer | ClementTsang <cjhtsang@uwaterloo.ca> | 2020-12-17 18:02:52 -0500 |
commit | b05efd0d55bb959b05410f827394a322f5037caa (patch) | |
tree | 5a247cd4402bfbed60f5d278f2de32e4727c1812 | |
parent | 8f681604c4bacc509000d62a741345f1af0ec5ee (diff) |
refactor: re-use heim for ARM targets if possible (#360)
Use heim on ARM targets again where appropriate.
(cherry picked from commit 682f6493d1b0cd7e138b734cf5e789d81530bda5)
-rw-r--r-- | CHANGELOG.md | 6 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/app/data_harvester.rs | 107 | ||||
-rw-r--r-- | src/app/data_harvester/disks.rs | 43 | ||||
-rw-r--r-- | src/app/data_harvester/mem.rs | 37 | ||||
-rw-r--r-- | src/app/data_harvester/network.rs | 42 | ||||
-rw-r--r-- | src/app/data_harvester/temperature.rs | 4 | ||||
-rw-r--r-- | src/utils/error.rs | 1 |
8 files changed, 28 insertions, 215 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 53969de0..e92a13ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Bug Fixes +## [0.5.6] - Unreleased + +## Bug Fixes + +- [#361](https://github.com/ClementTsang/bottom/pull/361): Fixed temperature sensors not working on non-Linux platforms. + ## [0.5.5] - 2020-12-14 ## Bug Fixes @@ -45,7 +45,7 @@ itertools = "0.9.0" libc = "~0.2" once_cell = "1.5.2" regex = "1.4.2" -serde = {version = "~1.0", features = ["derive"] } +serde = { version = "~1.0", features = ["derive"] } sysinfo = "0.15.3" thiserror = "1.0.22" toml = "0.5.7" @@ -58,7 +58,6 @@ unicode-width = "0.1" fern = { version = "0.6.0", optional=true } log = { version="0.4.11", optional=true } -[target.'cfg(not(any(target_arch = "arm", target_arch = "aarch64")))'.dependencies] heim = { version = "0.1.0-rc.1", features = ["disk", "memory", "net", "sensors"] } [target.'cfg(windows)'.dependencies] diff --git a/src/app/data_harvester.rs b/src/app/data_harvester.rs index 9915e6d0..3644654f 100644 --- a/src/app/data_harvester.rs +++ b/src/app/data_harvester.rs @@ -97,7 +97,7 @@ impl Default for DataCollector { // trace!("Creating default data collector..."); DataCollector { data: Data::default(), - sys: System::new_with_specifics(sysinfo::RefreshKind::new()), + sys: System::new_with_specifics(sysinfo::RefreshKind::new()), // FIXME: Make this run on only macOS and Windows. #[cfg(target_os = "linux")] pid_mapping: FnvHashMap::default(), #[cfg(target_os = "linux")] @@ -127,10 +127,13 @@ impl Default for DataCollector { impl DataCollector { pub fn init(&mut self) { - // trace!("Initializing data collector."); self.sys.refresh_memory(); self.mem_total_kb = self.sys.get_total_memory(); - // trace!("Total memory in KB: {}", self.mem_total_kb); + + // Refresh components list once... + if self.widgets_to_harvest.use_temp { + self.sys.refresh_components_list(); + } if self.widgets_to_harvest.use_battery { // trace!("First run battery vec creation."); @@ -145,11 +148,6 @@ impl DataCollector { } } - // Refresh components list once... - if self.widgets_to_harvest.use_temp { - self.sys.refresh_components_list(); - } - // trace!("Running first run."); futures::executor::block_on(self.update_data()); // trace!("First run done. Sleeping for 250ms..."); @@ -182,32 +180,13 @@ impl DataCollector { self.sys.refresh_cpu(); } - if cfg!(any(target_arch = "arm", target_arch = "aarch64")) { - // ARM stuff + if cfg!(not(target_os = "linux")) { if self.widgets_to_harvest.use_proc { self.sys.refresh_processes(); } if self.widgets_to_harvest.use_temp { self.sys.refresh_components(); } - if self.widgets_to_harvest.use_net { - self.sys.refresh_networks(); - } - if self.widgets_to_harvest.use_mem { - self.sys.refresh_memory(); - } - } else { - if cfg!(not(target_os = "linux")) { - if self.widgets_to_harvest.use_proc { - self.sys.refresh_processes(); - } - if self.widgets_to_harvest.use_temp { - self.sys.refresh_components(); - } - } - if cfg!(target_os = "windows") && self.widgets_to_harvest.use_net { - self.sys.refresh_networks(); - } } let current_instant = std::time::Instant::now(); @@ -256,63 +235,19 @@ impl DataCollector { // I am *well* aware that the sysinfo part w/ blocking code is... not great. let network_data_fut = { - #[cfg(any(target_os = "windows", target_arch = "aarch64", target_arch = "arm"))] - { - network::get_network_data( - &self.sys, - self.last_collection_time, - &mut self.total_rx, - &mut self.total_tx, - current_instant, - self.widgets_to_harvest.use_net, - ) - } - #[cfg(not(any(target_os = "windows", target_arch = "aarch64", target_arch = "arm")))] - { - network::get_network_data( - self.last_collection_time, - &mut self.total_rx, - &mut self.total_tx, - current_instant, - self.widgets_to_harvest.use_net, - ) - } - }; - let mem_data_fut = { - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] - { - mem::get_mem_data(&self.sys, self.widgets_to_harvest.use_mem) - } - - #[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] - { - mem::get_mem_data(self.widgets_to_harvest.use_mem) - } - }; - let disk_data_fut = { - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] - { - disks::get_disk_usage(&self.sys, self.widgets_to_harvest.use_disk) - } - - #[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] - { - disks::get_disk_usage(self.widgets_to_harvest.use_disk) - } - }; - let disk_io_usage_fut = { - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] - { - disks::get_io_usage(&self.sys, self.widgets_to_harvest.use_disk) - } - - #[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] - { - disks::get_io_usage(false, self.widgets_to_harvest.use_disk) - } + network::get_network_data( + self.last_collection_time, + &mut self.total_rx, + &mut self.total_tx, + current_instant, + self.widgets_to_harvest.use_net, + ) }; + let mem_data_fut = mem::get_mem_data(self.widgets_to_harvest.use_mem); + let disk_data_fut = disks::get_disk_usage(self.widgets_to_harvest.use_disk); + let disk_io_usage_fut = disks::get_io_usage(false, self.widgets_to_harvest.use_disk); let temp_data_fut = { - #[cfg(any(not(target_os = "linux"), target_arch = "aarch64", target_arch = "arm"))] + #[cfg(not(target_os = "linux"))] { temperature::get_temperature_data( &self.sys, @@ -321,11 +256,7 @@ impl DataCollector { ) } - #[cfg(not(any( - not(target_os = "linux"), - target_arch = "aarch64", - target_arch = "arm" - )))] + #[cfg(target_os = "linux")] { temperature::get_temperature_data( &self.temperature_type, diff --git a/src/app/data_harvester/disks.rs b/src/app/data_harvester/disks.rs index 294288a0..8c447a46 100644 --- a/src/app/data_harvester/disks.rs +++ b/src/app/data_harvester/disks.rs @@ -15,48 +15,6 @@ pub struct IOData { pub type IOHarvest = std::collections::HashMap<String, Option<IOData>>; -#[cfg(any(target_arch = "aarch64", target_arch = "arm"))] -pub async fn get_io_usage( - _sys: &sysinfo::System, _actually_get: bool, -) -> crate::utils::error::Result<Option<IOHarvest>> { - let io_hash: std::collections::HashMap<String, Option<IOData>> = - std::collections::HashMap::new(); - Ok(Some(io_hash)) - - // TODO: Sysinfo disk I/O usage. - // ...sadly, this cannot be done as of now (other than me writing my own), it requires further - // work. See https://github.com/GuillaumeGomez/sysinfo/issues/304. -} - -#[cfg(any(target_arch = "aarch64", target_arch = "arm"))] -pub async fn get_disk_usage( - sys: &sysinfo::System, actually_get: bool, -) -> crate::utils::error::Result<Option<Vec<DiskHarvest>>> { - use sysinfo::{DiskExt, SystemExt}; - - if !actually_get { - return Ok(None); - } - - let mut vec_disks = sys - .get_disks() - .iter() - .map(|disk| DiskHarvest { - name: disk.get_name().to_string_lossy().into(), - mount_point: disk.get_mount_point().to_string_lossy().into(), - free_space: disk.get_available_space(), - used_space: disk - .get_total_space() - .saturating_sub(disk.get_available_space()), - total_space: disk.get_total_space(), - }) - .collect::<Vec<DiskHarvest>>(); - vec_disks.sort_by(|a, b| a.name.cmp(&b.name)); - - Ok(Some(vec_disks)) -} - -#[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] pub async fn get_io_usage( get_physical: bool, actually_get: bool, ) -> crate::utils::error::Result<Option<IOHarvest>> { @@ -105,7 +63,6 @@ pub async fn get_io_usage( Ok(Some(io_hash)) } -#[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] pub async fn get_disk_usage( actually_get: bool, ) -> crate::utils::error::Result<Option<Vec<DiskHarvest>>> { diff --git a/src/app/data_harvester/mem.rs b/src/app/data_harvester/mem.rs index 7348cdea..7dd0f4ca 100644 --- a/src/app/data_harvester/mem.rs +++ b/src/app/data_harvester/mem.rs @@ -13,41 +13,6 @@ impl Default for MemHarvest { } } -#[cfg(any(target_arch = "aarch64", target_arch = "arm"))] -pub async fn get_mem_data( - sys: &sysinfo::System, actually_get: bool, -) -> ( - crate::utils::error::Result<Option<MemHarvest>>, - crate::utils::error::Result<Option<MemHarvest>>, -) { - if !actually_get { - (Ok(None), Ok(None)) - } else { - (get_ram_data(sys), get_swap_data(sys)) - } -} - -#[cfg(any(target_arch = "aarch64", target_arch = "arm"))] -pub fn get_ram_data(sys: &sysinfo::System) -> crate::utils::error::Result<Option<MemHarvest>> { - use sysinfo::SystemExt; - - Ok(Some(MemHarvest { - mem_total_in_mb: sys.get_total_memory() / 1024, - mem_used_in_mb: sys.get_used_memory() / 1024, - })) -} - -#[cfg(any(target_arch = "aarch64", target_arch = "arm"))] -pub fn get_swap_data(sys: &sysinfo::System) -> crate::utils::error::Result<Option<MemHarvest>> { - use sysinfo::SystemExt; - - Ok(Some(MemHarvest { - mem_total_in_mb: sys.get_total_swap() / 1024, - mem_used_in_mb: sys.get_used_swap() / 1024, - })) -} - -#[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] pub async fn get_mem_data( actually_get: bool, ) -> ( @@ -63,7 +28,6 @@ pub async fn get_mem_data( } } -#[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] pub async fn get_ram_data() -> crate::utils::error::Result<Option<MemHarvest>> { let memory = heim::memory::memory().await?; @@ -76,7 +40,6 @@ pub async fn get_ram_data() -> crate::utils::error::Result<Option<MemHarvest>> { })) } -#[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] pub async fn get_swap_data() -> crate::utils::error::Result<Option<MemHarvest>> { let memory = heim::memory::swap().await?; diff --git a/src/app/data_harvester/network.rs b/src/app/data_harvester/network.rs index 37c8b46c..e15409c5 100644 --- a/src/app/data_harvester/network.rs +++ b/src/app/data_harvester/network.rs @@ -15,48 +15,6 @@ impl NetworkHarvest { } } -#[cfg(any(target_os = "windows", target_arch = "aarch64", target_arch = "arm"))] -pub async fn get_network_data( - sys: &sysinfo::System, prev_net_access_time: Instant, prev_net_rx: &mut u64, - prev_net_tx: &mut u64, curr_time: Instant, actually_get: bool, -) -> crate::utils::error::Result<Option<NetworkHarvest>> { - use sysinfo::{NetworkExt, SystemExt}; - - if !actually_get { - return Ok(None); - } - - let mut total_rx: u64 = 0; - let mut total_tx: u64 = 0; - - let networks = sys.get_networks(); - for (_, network) in networks { - total_rx += network.get_total_received(); - total_tx += network.get_total_transmitted(); - } - - let elapsed_time = curr_time.duration_since(prev_net_access_time).as_secs_f64(); - - let (rx, tx) = if elapsed_time == 0.0 { - (0, 0) - } else { - ( - ((total_rx.saturating_sub(*prev_net_rx)) as f64 / elapsed_time) as u64, - ((total_tx.saturating_sub(*prev_net_tx)) as f64 / elapsed_time) as u64, - ) - }; - - *prev_net_rx = total_rx; - *prev_net_tx = total_tx; - Ok(Some(NetworkHarvest { - rx, - tx, - total_rx, - total_tx, - })) -} - -#[cfg(not(any(target_os = "windows", target_arch = "aarch64", target_arch = "arm")))] pub async fn get_network_data( prev_net_access_time: Instant, prev_net_rx: &mut u64, prev_net_tx: &mut u64, curr_time: Instant, actually_get: bool, diff --git a/src/app/data_harvester/temperature.rs b/src/app/data_harvester/temperature.rs index 470b1dbb..512850c9 100644 --- a/src/app/data_harvester/temperature.rs +++ b/src/app/data_harvester/temperature.rs @@ -20,7 +20,7 @@ impl Default for TemperatureType { } } -#[cfg(any(not(target_os = "linux"), target_arch = "aarch64", target_arch = "arm"))] +#[cfg(not(target_os = "linux"))] pub async fn get_temperature_data( sys: &sysinfo::System, temp_type: &TemperatureType, actually_get: bool, ) -> crate::utils::error::Result<Option<Vec<TempHarvest>>> { @@ -59,7 +59,7 @@ pub async fn get_temperature_data( Ok(Some(temperature_vec)) } -#[cfg(not(any(not(target_os = "linux"), target_arch = "aarch64", target_arch = "arm")))] +#[cfg(target_os = "linux")] pub async fn get_temperature_data( temp_type: &TemperatureType, actually_get: bool, ) -> crate::utils::error::Result<Option<Vec<TempHarvest>>> { diff --git a/src/utils/error.rs b/src/utils/error.rs index 848b9919..f0a965b0 100644 --- a/src/utils/error.rs +++ b/src/utils/error.rs @@ -43,7 +43,6 @@ impl From<std::io::Error> for BottomError { } } -#[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] impl From<heim::Error> for BottomError { fn from(err: heim::Error) -> Self { BottomError::InvalidHeim(err.to_string()) |