use tikv_jemalloc_ctl::raw;
pub use tikv_jemallocator;
pub async fn run() -> anyhow::Result<()> {
match unsafe { raw::read::<bool>(b"prof.active\0") } {
Ok(true) => tracing::info!("jemalloc heap profiling is active"),
Ok(false) => {
tracing::info!(
"jemalloc profiling compiled in but not active. Set MALLOC_CONF=prof:true,prof_active:true at startup to enable"
);
return Ok(());
}
Err(err) => {
tracing::debug!(%err, "jemalloc profiling not available");
return Ok(());
}
}
let mut sig = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1())?;
loop {
sig.recv().await;
match unsafe { raw::write(b"prof.dump\0", std::ptr::null::<u8>()) } {
Ok(()) => tracing::info!("heap profile dumped"),
Err(err) => tracing::error!(%err, "failed to dump heap profile"),
}
}
}