use std::sync::Mutex;
use sysinfo::System;
use crate::footprint::SwappableFootprint;
pub trait RssSource: Send + Sync {
fn read_rss_bytes(&self) -> u64;
fn total_ram_bytes(&self) -> u64 {
panic!("RssSource::total_ram_bytes must be implemented");
}
}
pub struct SystemRss {
inner: Mutex<SystemRssInner>,
}
struct SystemRssInner {
system: System,
footprint: SwappableFootprint,
}
impl SystemRss {
#[must_use]
pub fn new() -> Self {
let mut system = System::new();
system.refresh_memory();
Self {
inner: Mutex::new(SystemRssInner {
system,
footprint: SwappableFootprint::new(),
}),
}
}
}
impl Default for SystemRss {
fn default() -> Self {
Self::new()
}
}
impl RssSource for SystemRss {
fn read_rss_bytes(&self) -> u64 {
let mut guard = self.inner.lock().expect("system-rss lock");
let SystemRssInner { system, footprint } = &mut *guard;
footprint.read_bytes(system)
}
fn total_ram_bytes(&self) -> u64 {
let mut guard = self.inner.lock().expect("system-rss lock");
guard.system.refresh_memory();
guard.system.total_memory()
}
}