use std::time::Duration;
use metrics::gauge;
use tikv_jemalloc_ctl::{epoch, stats};
use tracing::error;
const REPORT_INTERVAL: Duration = Duration::from_secs(60);
pub fn spawn_stats_reporter() -> tokio::task::JoinHandle<()> {
tokio::spawn(async {
let mut ticker = tokio::time::interval(REPORT_INTERVAL);
loop {
ticker.tick().await;
if let Err(err) = report_once() {
error!("jemalloc stats reporter stopping, read failed: {err}");
break;
}
}
})
}
fn report_once() -> Result<(), tikv_jemalloc_ctl::Error> {
epoch::advance()?;
gauge!("jemalloc_allocated_bytes").set(stats::allocated::read()? as f64);
gauge!("jemalloc_active_bytes").set(stats::active::read()? as f64);
gauge!("jemalloc_resident_bytes").set(stats::resident::read()? as f64);
gauge!("jemalloc_retained_bytes").set(stats::retained::read()? as f64);
gauge!("jemalloc_mapped_bytes").set(stats::mapped::read()? as f64);
Ok(())
}