diff options
Diffstat (limited to 'src/app/data_harvester.rs')
-rw-r--r-- | src/app/data_harvester.rs | 91 |
1 files changed, 21 insertions, 70 deletions
diff --git a/src/app/data_harvester.rs b/src/app/data_harvester.rs index 2f4633bf..0ea6e3e9 100644 --- a/src/app/data_harvester.rs +++ b/src/app/data_harvester.rs @@ -27,7 +27,7 @@ pub mod temperature; #[derive(Clone, Debug)] pub struct Data { - pub last_collection_time: Instant, + pub collection_time: Instant, pub cpu: Option<cpu::CpuHarvest>, pub load_avg: Option<cpu::LoadAvgHarvest>, pub memory: Option<memory::MemHarvest>, @@ -50,7 +50,7 @@ pub struct Data { impl Default for Data { fn default() -> Self { Data { - last_collection_time: Instant::now(), + collection_time: Instant::now(), cpu: None, load_avg: None, memory: None, @@ -284,24 +284,20 @@ impl DataCollector { pub fn update_data(&mut self) { self.refresh_sysinfo_data(); - let current_instant = Instant::now(); + self.data.collection_time = Instant::now(); self.update_cpu_usage(); self.update_memory_usage(); - self.update_processes( - #[cfg(target_os = "linux")] - current_instant, - ); + self.update_processes(); self.update_temps(); - self.update_network_usage(current_instant); + self.update_network_usage(); self.update_disks(); #[cfg(feature = "battery")] self.update_batteries(); // Update times for future reference. - self.last_collection_time = current_instant; - self.data.last_collection_time = current_instant; + self.last_collection_time = self.data.collection_time; } #[inline] @@ -317,66 +313,9 @@ impl DataCollector { } #[inline] - fn update_processes(&mut self, #[cfg(target_os = "linux")] current_instant: Instant) { + fn update_processes(&mut self) { if self.widgets_to_harvest.use_proc { - if let Ok(mut process_list) = { - let total_memory = if let Some(memory) = &self.data.memory { - memory.total_bytes - } else { - self.sys.total_memory() - }; - - #[cfg(target_os = "linux")] - { - use self::processes::{PrevProc, ProcHarvestOptions}; - - let prev_proc = PrevProc { - prev_idle: &mut self.prev_idle, - prev_non_idle: &mut self.prev_non_idle, - }; - - let proc_harvest_options = ProcHarvestOptions { - use_current_cpu_total: self.use_current_cpu_total, - unnormalized_cpu: self.unnormalized_cpu, - }; - - let time_diff = current_instant - .duration_since(self.last_collection_time) - .as_secs(); - - processes::get_process_data( - &self.sys, - prev_proc, - &mut self.pid_mapping, - proc_harvest_options, - time_diff, - total_memory, - &mut self.user_table, - ) - } - #[cfg(not(target_os = "linux"))] - { - #[cfg(target_family = "unix")] - { - processes::get_process_data( - &self.sys, - self.use_current_cpu_total, - self.unnormalized_cpu, - total_memory, - &mut self.user_table, - ) - } - #[cfg(not(target_family = "unix"))] - { - processes::get_process_data( - &self.sys, - self.use_current_cpu_total, - self.unnormalized_cpu, - total_memory, - ) - } - } - } { + if let Ok(mut process_list) = self.get_processes() { // NB: To avoid duplicate sorts on rerenders/events, we sort the processes by PID here. // We also want to avoid re-sorting *again* later on if we're sorting by PID, since we already // did it here! @@ -435,7 +374,9 @@ impl DataCollector { } #[inline] - fn update_network_usage(&mut self, current_instant: Instant) { + fn update_network_usage(&mut self) { + let current_instant = self.data.collection_time; + if self.widgets_to_harvest.use_net { let net_data = network::get_network_data( &self.sys, @@ -485,6 +426,16 @@ impl DataCollector { self.data.io = disks::get_io_usage().ok(); } } + + /// Returns the total memory of the system. + #[inline] + fn total_memory(&self) -> u64 { + if let Some(memory) = &self.data.memory { + memory.total_bytes + } else { + self.sys.total_memory() + } + } } /// We set a sleep duration between 10ms and 250ms, ideally sysinfo's [`System::MINIMUM_CPU_UPDATE_INTERVAL`] + 1. |