use core::time::Duration;
use std::collections::BTreeSet;
use std::sync::{Arc, Mutex, PoisonError};
use sysinfo::{
CpuRefreshKind, DiskRefreshKind, Disks, IpNetwork, MemoryRefreshKind, NetworkData, Networks,
RefreshKind, System,
};
use tokio::task::JoinHandle;
use tokio::time::Instant;
use shep_core::protocol::HostUsage;
pub(crate) const HOST_POLL_INTERVAL: Duration = Duration::from_secs(1);
pub(crate) trait HostSource: Send + 'static {
fn sample(&mut self) -> HostUsage;
}
#[derive(Debug)]
pub(crate) struct HostState {
latest: Arc<Mutex<Option<HostUsage>>>,
task: Option<JoinHandle<()>>,
}
impl HostState {
#[must_use]
pub(crate) fn real() -> Arc<Self> {
Self::start(
HostWatch::install().map(|watch| Box::new(watch) as Box<dyn HostSource>),
HOST_POLL_INTERVAL,
)
}
#[must_use]
pub(crate) fn latest(&self) -> Option<HostUsage> {
*self.latest.lock().unwrap_or_else(PoisonError::into_inner)
}
#[cfg(test)]
pub(crate) fn fixed(reading: Option<HostUsage>) -> Arc<Self> {
Arc::new(Self {
latest: Arc::new(Mutex::new(reading)),
task: None,
})
}
fn start(source: Option<Box<dyn HostSource>>, interval: Duration) -> Arc<Self> {
let latest = Arc::new(Mutex::new(None));
let task = source.map(|mut source| {
let writes_to = Arc::clone(&latest);
tokio::spawn(async move {
*writes_to.lock().unwrap_or_else(PoisonError::into_inner) = Some(source.sample());
let mut ticker = tokio::time::interval(interval);
ticker.tick().await;
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
ticker.tick().await;
let reading = source.sample();
*writes_to.lock().unwrap_or_else(PoisonError::into_inner) = Some(reading);
}
})
});
Arc::new(Self { latest, task })
}
}
impl Drop for HostState {
fn drop(&mut self) {
if let Some(task) = &self.task {
task.abort();
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct DiskIo {
lifetime: (u64, u64),
window: (u64, u64),
}
fn distinct_disk_io(entries: impl IntoIterator<Item = DiskIo>) -> (u64, u64) {
let mut seen = BTreeSet::new();
let mut read = 0u64;
let mut written = 0u64;
for entry in entries {
if seen.insert(entry.lifetime) {
read = read.saturating_add(entry.window.0);
written = written.saturating_add(entry.window.1);
}
}
(read, written)
}
fn is_loopback(interface: &NetworkData) -> bool {
addresses_are_loopback(interface.ip_networks())
}
fn addresses_are_loopback(addresses: &[IpNetwork]) -> bool {
!addresses.is_empty() && addresses.iter().all(|network| network.addr.is_loopback())
}
fn is_measurable(window: Duration) -> bool {
window >= sysinfo::MINIMUM_CPU_UPDATE_INTERVAL
}
fn per_second(bytes: u64, window: Duration) -> u64 {
let millis = window.as_millis().max(1);
u64::try_from(u128::from(bytes) * 1000 / millis).unwrap_or(u64::MAX)
}
#[derive(Debug)]
struct HostWatch {
system: System,
disks: Disks,
networks: Networks,
sampled_at: Instant,
}
impl HostWatch {
fn install() -> Option<Self> {
if !sysinfo::IS_SUPPORTED_SYSTEM {
return None;
}
Some(Self {
system: System::new_with_specifics(Self::refresh()),
disks: Disks::new_with_refreshed_list_specifics(Self::disk_refresh()),
networks: Networks::new_with_refreshed_list(),
sampled_at: Instant::now(),
})
}
fn refresh() -> RefreshKind {
RefreshKind::nothing()
.with_cpu(CpuRefreshKind::nothing().with_cpu_usage())
.with_memory(MemoryRefreshKind::nothing().with_ram())
}
fn disk_refresh() -> DiskRefreshKind {
DiskRefreshKind::nothing().with_io_usage()
}
}
impl HostSource for HostWatch {
fn sample(&mut self) -> HostUsage {
let now = Instant::now();
let window = now.saturating_duration_since(self.sampled_at);
self.sampled_at = now;
self.system.refresh_specifics(Self::refresh());
self.disks.refresh_specifics(true, Self::disk_refresh());
self.networks.refresh(true);
let measured = is_measurable(window);
let disk = measured.then(|| {
let (read, written) = distinct_disk_io(self.disks.list().iter().map(|disk| {
let usage = disk.usage();
DiskIo {
lifetime: (usage.total_read_bytes, usage.total_written_bytes),
window: (usage.read_bytes, usage.written_bytes),
}
}));
(per_second(read, window), per_second(written, window))
});
let network = measured.then(|| {
let (received, transmitted) = self
.networks
.list()
.values()
.filter(|interface| !is_loopback(interface))
.fold((0u64, 0u64), |(received, transmitted), interface| {
(
received.saturating_add(interface.received()),
transmitted.saturating_add(interface.transmitted()),
)
});
(
per_second(received, window),
per_second(transmitted, window),
)
});
HostUsage {
cpu_percent: measured.then(|| self.system.global_cpu_usage()),
memory_used_bytes: self.system.used_memory(),
memory_total_bytes: self.system.total_memory(),
disk_bytes_per_second: disk,
network_bytes_per_second: network,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn io(lifetime: (u64, u64), window: (u64, u64)) -> DiskIo {
DiskIo { lifetime, window }
}
#[test]
fn two_mount_points_on_one_device_are_counted_once() {
let volumes = [
io((1_089_321_177_088, 1_750_606_974_976), (2_641_920, 4_096)),
io((1_089_321_177_088, 1_750_606_974_976), (2_625_536, 4_096)),
];
assert_eq!(distinct_disk_io(volumes), (2_641_920, 4_096));
}
#[test]
fn two_devices_are_counted_twice() {
let devices = [io((100, 200), (10, 20)), io((300, 400), (30, 40))];
assert_eq!(distinct_disk_io(devices), (40, 60));
}
#[test]
fn no_devices_move_no_bytes() {
assert_eq!(distinct_disk_io([]), (0, 0));
}
#[test]
fn a_rate_scales_a_window_up_to_a_second() {
assert_eq!(
per_second(100 * 1024 * 1024, Duration::from_millis(100)),
1000 * 1024 * 1024
);
assert_eq!(per_second(u64::MAX, Duration::from_millis(1)), u64::MAX);
assert_eq!(per_second(512, Duration::from_secs(2)), 256);
}
#[test]
fn a_window_of_no_time_does_not_divide_by_zero() {
assert_eq!(per_second(4, Duration::ZERO), 4000);
}
#[test]
fn a_window_under_sysinfos_own_floor_carries_no_rate() {
assert!(!is_measurable(Duration::ZERO));
assert!(!is_measurable(Duration::from_millis(5)));
assert!(!is_measurable(
sysinfo::MINIMUM_CPU_UPDATE_INTERVAL - Duration::from_nanos(1)
));
assert!(is_measurable(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL));
assert!(
is_measurable(HOST_POLL_INTERVAL),
"the tick this daemon actually runs on"
);
}
fn net(addr: &str) -> IpNetwork {
IpNetwork {
addr: addr.parse().expect("a literal address"),
prefix: 8,
}
}
#[test]
fn an_interface_is_loopback_only_when_every_address_is() {
assert!(addresses_are_loopback(&[net("127.0.0.1")]));
assert!(addresses_are_loopback(&[net("127.0.0.1"), net("::1")]));
assert!(!addresses_are_loopback(&[net("192.168.1.4")]));
assert!(
!addresses_are_loopback(&[net("127.0.0.1"), net("192.168.1.4")]),
"one routable address is enough to make an interface count"
);
}
#[test]
fn an_interface_with_no_addresses_is_not_loopback() {
assert!(!addresses_are_loopback(&[]));
}
#[test]
fn a_real_reading_reads_this_machines_memory() {
let Some(mut watch) = HostWatch::install() else {
return;
};
let reading = watch.sample();
assert!(reading.memory_total_bytes > 0, "a machine has memory");
assert!(reading.memory_used_bytes > 0);
}
struct ScriptedSource {
readings: std::sync::mpsc::Receiver<HostUsage>,
calls: Arc<Mutex<u32>>,
}
impl HostSource for ScriptedSource {
fn sample(&mut self) -> HostUsage {
*self.calls.lock().unwrap_or_else(PoisonError::into_inner) += 1;
self.readings
.try_recv()
.expect("the script ran out of readings")
}
}
fn reading(used: u64, cpu: Option<f32>) -> HostUsage {
HostUsage {
cpu_percent: cpu,
memory_used_bytes: used,
memory_total_bytes: 51_539_607_552,
disk_bytes_per_second: cpu.map(|_| (1_258_291, 491_520)),
network_bytes_per_second: cpu.map(|_| (24_594, 9_260)),
}
}
async fn scripted(script: Vec<HostUsage>) -> (Arc<HostState>, Arc<Mutex<u32>>) {
let (tx, readings) = std::sync::mpsc::channel();
for entry in script {
tx.send(entry).expect("the receiver is alive");
}
let calls = Arc::new(Mutex::new(0));
let source = ScriptedSource {
readings,
calls: Arc::clone(&calls),
};
let state = HostState::start(Some(Box::new(source)), HOST_POLL_INTERVAL);
tokio::task::yield_now().await;
(state, calls)
}
#[tokio::test(start_paused = true)]
async fn the_seeding_reading_lands_before_the_first_tick() {
let (state, calls) = scripted(vec![reading(1, None)]).await;
let seeded = state.latest().expect("a seeding reading");
assert_eq!(seeded.memory_used_bytes, 1);
assert_eq!(seeded.cpu_percent, None, "no window has passed yet");
assert_eq!(*calls.lock().unwrap(), 1);
}
#[tokio::test(start_paused = true)]
async fn each_tick_replaces_what_a_listing_reads() {
let (state, calls) = scripted(vec![
reading(1, None),
reading(2, Some(11.5)),
reading(3, Some(22.5)),
])
.await;
tokio::time::sleep(HOST_POLL_INTERVAL + Duration::from_millis(1)).await;
assert_eq!(state.latest().expect("tick 1").memory_used_bytes, 2);
tokio::time::sleep(HOST_POLL_INTERVAL).await;
assert_eq!(state.latest().expect("tick 2").memory_used_bytes, 3);
assert_eq!(
*calls.lock().unwrap(),
3,
"the seeding read plus one per tick, and nothing else"
);
}
#[tokio::test(start_paused = true)]
async fn a_listing_never_samples() {
let (state, calls) = scripted(vec![reading(1, None), reading(2, Some(11.5))]).await;
tokio::time::sleep(HOST_POLL_INTERVAL + Duration::from_millis(1)).await;
let after_one_tick = *calls.lock().unwrap();
for _ in 0..100 {
assert_eq!(state.latest().expect("a reading").memory_used_bytes, 2);
}
assert_eq!(
*calls.lock().unwrap(),
after_one_tick,
"a hundred listings must cost the machine nothing"
);
}
#[tokio::test(start_paused = true)]
async fn a_platform_that_cannot_be_read_polls_nothing() {
let state = HostState::start(None, HOST_POLL_INTERVAL);
assert!(state.task.is_none());
tokio::time::sleep(HOST_POLL_INTERVAL * 5).await;
assert_eq!(state.latest(), None);
}
#[tokio::test(start_paused = true)]
async fn dropping_the_last_handle_stops_the_task() {
let (state, calls) = scripted(vec![reading(1, None), reading(2, Some(11.5))]).await;
tokio::time::sleep(HOST_POLL_INTERVAL + Duration::from_millis(1)).await;
let before = *calls.lock().unwrap();
drop(state);
tokio::time::sleep(HOST_POLL_INTERVAL * 5).await;
assert_eq!(*calls.lock().unwrap(), before);
}
}