#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
const ALLOC_BYTES: usize = 96 * 1024 * 1024;
fn main() {
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.expect("build runtime");
rt.block_on(async {
use otel_bootstrap::profiling::SamplingActivation;
match otel_bootstrap::profiling::activate_jemalloc_sampling() {
SamplingActivation::Panicked => {
println!("HEAP_PROBE_PANIC activation panicked");
std::process::exit(2);
}
SamplingActivation::Unavailable(e) => {
println!("HEAP_PROBE_INACTIVE {e}");
std::process::exit(3);
}
SamplingActivation::Activated => {}
}
let mut ballast: Vec<Vec<u8>> = Vec::new();
for _ in 0..96 {
ballast.push(vec![7u8; ALLOC_BYTES / 96]);
}
std::hint::black_box(&ballast);
let pprof = match dump_pprof() {
Ok(bytes) => bytes,
Err(e) => {
println!("HEAP_PROBE_DUMP_FAILED {e}");
std::process::exit(4);
}
};
if pprof.is_empty() {
println!("HEAP_PROBE_EMPTY_PROFILE");
std::process::exit(5);
}
std::hint::black_box(&ballast);
println!("HEAP_PROBE_OK pprof_bytes={}", pprof.len());
});
}
fn dump_pprof() -> Result<Vec<u8>, String> {
let ctl = jemalloc_pprof::PROF_CTL
.as_ref()
.ok_or_else(|| "jemalloc profiling not compiled into this binary".to_owned())?;
let mut guard = ctl
.try_lock()
.map_err(|_| "prof ctl held elsewhere".to_owned())?;
guard.dump_pprof().map_err(|e| e.to_string())
}