mirror of
https://github.com/ClementTsang/bottom
synced 2024-11-25 21:50:20 +00:00
Change how we call time in data_collection step
This commit is contained in:
parent
13180c72d4
commit
514c39cc56
6 changed files with 33 additions and 19 deletions
|
@ -95,6 +95,9 @@ impl DataState {
|
|||
self.sys.refresh_network();
|
||||
}
|
||||
|
||||
// Filter out stale timed entries
|
||||
let current_instant = std::time::Instant::now();
|
||||
|
||||
// What we want to do: For timed data, if there is an error, just do not add. For other data, just don't update!
|
||||
push_if_valid(
|
||||
&network::get_network_data(
|
||||
|
@ -102,17 +105,24 @@ impl DataState {
|
|||
&mut self.prev_net_rx_bytes,
|
||||
&mut self.prev_net_tx_bytes,
|
||||
&mut self.prev_net_access_time,
|
||||
¤t_instant,
|
||||
)
|
||||
.await,
|
||||
&mut self.data.network,
|
||||
);
|
||||
push_if_valid(
|
||||
&cpu::get_cpu_data_list(&self.sys),
|
||||
&cpu::get_cpu_data_list(&self.sys, ¤t_instant),
|
||||
&mut self.data.list_of_cpu_packages,
|
||||
);
|
||||
|
||||
push_if_valid(&mem::get_mem_data_list().await, &mut self.data.memory);
|
||||
push_if_valid(&mem::get_swap_data_list().await, &mut self.data.swap);
|
||||
push_if_valid(
|
||||
&mem::get_mem_data_list(¤t_instant).await,
|
||||
&mut self.data.memory,
|
||||
);
|
||||
push_if_valid(
|
||||
&mem::get_swap_data_list(¤t_instant).await,
|
||||
&mut self.data.swap,
|
||||
);
|
||||
set_if_valid(
|
||||
&processes::get_sorted_processes_list(
|
||||
&self.sys,
|
||||
|
@ -120,6 +130,7 @@ impl DataState {
|
|||
&mut self.prev_non_idle,
|
||||
&mut self.prev_pid_stats,
|
||||
self.use_current_cpu_total,
|
||||
¤t_instant,
|
||||
),
|
||||
&mut self.data.list_of_processes,
|
||||
);
|
||||
|
@ -142,9 +153,6 @@ impl DataState {
|
|||
self.first_run = false;
|
||||
}
|
||||
|
||||
// Filter out stale timed entries
|
||||
let current_instant = std::time::Instant::now();
|
||||
|
||||
if current_instant.duration_since(self.last_clean).as_secs() > self.stale_max_seconds {
|
||||
let stale_list: Vec<_> = self
|
||||
.prev_pid_stats
|
||||
|
|
|
@ -13,7 +13,9 @@ pub struct CPUPackage {
|
|||
pub instant: Instant,
|
||||
}
|
||||
|
||||
pub fn get_cpu_data_list(sys: &System) -> crate::utils::error::Result<CPUPackage> {
|
||||
pub fn get_cpu_data_list(
|
||||
sys: &System, curr_time: &Instant,
|
||||
) -> crate::utils::error::Result<CPUPackage> {
|
||||
let cpu_data = sys.get_processor_list();
|
||||
let mut cpu_vec = Vec::new();
|
||||
|
||||
|
@ -26,6 +28,6 @@ pub fn get_cpu_data_list(sys: &System) -> crate::utils::error::Result<CPUPackage
|
|||
|
||||
Ok(CPUPackage {
|
||||
cpu_vec,
|
||||
instant: Instant::now(),
|
||||
instant: *curr_time,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -8,23 +8,23 @@ pub struct MemData {
|
|||
pub instant: Instant,
|
||||
}
|
||||
|
||||
pub async fn get_mem_data_list() -> crate::utils::error::Result<MemData> {
|
||||
pub async fn get_mem_data_list(curr_time: &Instant) -> crate::utils::error::Result<MemData> {
|
||||
let memory = heim::memory::memory().await?;
|
||||
|
||||
Ok(MemData {
|
||||
mem_total_in_mb: memory.total().get::<information::megabyte>(),
|
||||
mem_used_in_mb: memory.total().get::<information::megabyte>()
|
||||
- memory.available().get::<information::megabyte>(),
|
||||
instant: Instant::now(),
|
||||
instant: *curr_time,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_swap_data_list() -> crate::utils::error::Result<MemData> {
|
||||
pub async fn get_swap_data_list(curr_time: &Instant) -> crate::utils::error::Result<MemData> {
|
||||
let memory = heim::memory::swap().await?;
|
||||
|
||||
Ok(MemData {
|
||||
mem_total_in_mb: memory.total().get::<information::megabyte>(),
|
||||
mem_used_in_mb: memory.used().get::<information::megabyte>(),
|
||||
instant: Instant::now(),
|
||||
instant: *curr_time,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -16,12 +16,12 @@ pub struct NetworkData {
|
|||
|
||||
pub async fn get_network_data(
|
||||
sys: &System, prev_net_rx_bytes: &mut u64, prev_net_tx_bytes: &mut u64,
|
||||
prev_net_access_time: &mut std::time::Instant,
|
||||
prev_net_access_time: &mut Instant, curr_time: &Instant,
|
||||
) -> crate::utils::error::Result<NetworkData> {
|
||||
if cfg!(target_os = "windows") {
|
||||
let network_data = sys.get_network();
|
||||
|
||||
*prev_net_access_time = Instant::now();
|
||||
*prev_net_access_time = *curr_time;
|
||||
Ok(NetworkData {
|
||||
rx: network_data.get_income(),
|
||||
tx: network_data.get_outcome(),
|
||||
|
|
|
@ -147,12 +147,13 @@ fn get_process_cpu_stats(pid: u32) -> std::io::Result<f64> {
|
|||
fn linux_cpu_usage(
|
||||
pid: u32, cpu_usage: f64, cpu_percentage: f64,
|
||||
previous_pid_stats: &mut HashMap<String, (f64, Instant)>, use_current_cpu_total: bool,
|
||||
curr_time: &Instant,
|
||||
) -> std::io::Result<f64> {
|
||||
// Based heavily on https://stackoverflow.com/a/23376195 and https://stackoverflow.com/a/1424556
|
||||
let before_proc_val: f64 = if previous_pid_stats.contains_key(&pid.to_string()) {
|
||||
previous_pid_stats
|
||||
.get(&pid.to_string())
|
||||
.unwrap_or(&(0_f64, Instant::now()))
|
||||
.unwrap_or(&(0_f64, *curr_time))
|
||||
.0
|
||||
} else {
|
||||
0_f64
|
||||
|
@ -170,8 +171,8 @@ fn linux_cpu_usage(
|
|||
|
||||
let entry = previous_pid_stats
|
||||
.entry(pid.to_string())
|
||||
.or_insert((after_proc_val, Instant::now()));
|
||||
*entry = (after_proc_val, Instant::now());
|
||||
.or_insert((after_proc_val, *curr_time));
|
||||
*entry = (after_proc_val, *curr_time);
|
||||
if use_current_cpu_total {
|
||||
Ok((after_proc_val - before_proc_val) / cpu_usage * 100_f64)
|
||||
} else {
|
||||
|
@ -182,6 +183,7 @@ fn linux_cpu_usage(
|
|||
fn convert_ps(
|
||||
process: &str, cpu_usage: f64, cpu_percentage: f64,
|
||||
prev_pid_stats: &mut HashMap<String, (f64, Instant)>, use_current_cpu_total: bool,
|
||||
curr_time: &Instant,
|
||||
) -> std::io::Result<ProcessData> {
|
||||
if process.trim().to_string().is_empty() {
|
||||
return Ok(ProcessData {
|
||||
|
@ -219,6 +221,7 @@ fn convert_ps(
|
|||
cpu_percentage,
|
||||
prev_pid_stats,
|
||||
use_current_cpu_total,
|
||||
curr_time,
|
||||
)?,
|
||||
pid_vec: None,
|
||||
})
|
||||
|
@ -227,7 +230,7 @@ fn convert_ps(
|
|||
pub fn get_sorted_processes_list(
|
||||
sys: &System, prev_idle: &mut f64, prev_non_idle: &mut f64,
|
||||
prev_pid_stats: &mut std::collections::HashMap<String, (f64, Instant)>,
|
||||
use_current_cpu_total: bool,
|
||||
use_current_cpu_total: bool, curr_time: &Instant,
|
||||
) -> crate::utils::error::Result<Vec<ProcessData>> {
|
||||
let mut process_vector: Vec<ProcessData> = Vec::new();
|
||||
|
||||
|
@ -251,6 +254,7 @@ pub fn get_sorted_processes_list(
|
|||
cpu_percentage,
|
||||
prev_pid_stats,
|
||||
use_current_cpu_total,
|
||||
curr_time,
|
||||
) {
|
||||
if !process_object.name.is_empty() {
|
||||
process_vector.push(process_object);
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
pub mod error;
|
||||
pub mod general_utility;
|
||||
pub mod gen_util;
|
||||
pub mod logging;
|
||||
|
|
Loading…
Reference in a new issue