#[derive(Debug, Clone)]
pub struct SystemMemoryStats {
pub rss_bytes: usize,
pub allocated_bytes: usize,
pub active_bytes: usize,
pub mapped_bytes: usize,
pub retained_bytes: usize,
pub fragmentation_ratio: f64,
}
impl SystemMemoryStats {
pub fn query() -> Option<Self> {
let _ = tikv_jemalloc_ctl::epoch::advance();
let allocated = tikv_jemalloc_ctl::stats::allocated::read().ok()?;
let active = tikv_jemalloc_ctl::stats::active::read().ok()?;
let mapped = tikv_jemalloc_ctl::stats::mapped::read().ok()?;
let retained = tikv_jemalloc_ctl::stats::retained::read().ok()?;
let resident = tikv_jemalloc_ctl::stats::resident::read().ok()?;
let fragmentation_ratio = if active > 0 {
(active.saturating_sub(allocated)) as f64 / active as f64
} else {
0.0
};
Some(Self {
rss_bytes: resident,
allocated_bytes: allocated,
active_bytes: active,
mapped_bytes: mapped,
retained_bytes: retained,
fragmentation_ratio,
})
}
pub fn is_fragmentation_critical(&self) -> bool {
self.fragmentation_ratio > 0.25
}
}