mirror of
https://github.com/ClementTsang/bottom
synced 2025-02-16 21:28:26 +00:00
refactor: clean up data init sleep duration code (#1101)
* refactor: clean up data init sleep duration code * const
This commit is contained in:
parent
20902e87b9
commit
513024aefd
1 changed files with 25 additions and 13 deletions
|
@ -194,20 +194,10 @@ impl DataCollector {
|
|||
|
||||
self.update_data();
|
||||
|
||||
// Sleep a few seconds to avoid potentially weird data...
|
||||
let sleep_duration = {
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_os = "freebsd")] {
|
||||
// FreeBSD's min duration value is 1s, which is a bit too long for me so I'll accept the one-time
|
||||
// inaccuracy.
|
||||
std::time::Duration::from_millis(250)
|
||||
} else {
|
||||
sysinfo::System::MINIMUM_CPU_UPDATE_INTERVAL + Duration::from_millis(1)
|
||||
}
|
||||
}
|
||||
};
|
||||
// Sleep a few seconds to avoid potentially weird data.
|
||||
const SLEEP: Duration = get_sleep_duration();
|
||||
|
||||
std::thread::sleep(sleep_duration);
|
||||
std::thread::sleep(SLEEP);
|
||||
self.data.cleanup();
|
||||
}
|
||||
|
||||
|
@ -497,6 +487,28 @@ impl DataCollector {
|
|||
}
|
||||
}
|
||||
|
||||
/// We set a sleep duration between 10ms and 250ms, ideally sysinfo's [`System::MINIMUM_CPU_UPDATE_INTERVAL`] + 1.
|
||||
///
|
||||
/// We bound the upper end to avoid waiting too long (e.g. FreeBSD is 1s, which I'm fine with losing
|
||||
/// accuracy on for the first refresh), and we bound the lower end just to avoid the off-chance that
|
||||
/// refreshing too quickly causes problems. This second case should only happen on unsupported
|
||||
/// systems via sysinfo, in which case [`System::MINIMUM_CPU_UPDATE_INTERVAL`] is defined as 0.
|
||||
///
|
||||
/// We also do `INTERVAL + 1` for some wiggle room, just in case.
|
||||
const fn get_sleep_duration() -> Duration {
|
||||
const MIN_SLEEP: u64 = 10;
|
||||
const MAX_SLEEP: u64 = 250;
|
||||
const INTERVAL: u64 = System::MINIMUM_CPU_UPDATE_INTERVAL.as_millis() as u64;
|
||||
|
||||
if INTERVAL < MIN_SLEEP {
|
||||
Duration::from_millis(MIN_SLEEP)
|
||||
} else if INTERVAL > MAX_SLEEP {
|
||||
Duration::from_millis(MAX_SLEEP)
|
||||
} else {
|
||||
Duration::from_millis(INTERVAL + 1)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "freebsd")]
|
||||
/// Deserialize [libxo](https://www.freebsd.org/cgi/man.cgi?query=libxo&apropos=0&sektion=0&manpath=FreeBSD+13.1-RELEASE+and+Ports&arch=default&format=html) JSON data
|
||||
fn deserialize_xo<T>(key: &str, data: &[u8]) -> Result<T, std::io::Error>
|
||||
|
|
Loading…
Add table
Reference in a new issue