#![cfg(unix)]
mod common;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use stoolap_jemalloc::{Jemalloc, prof};
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc::new();
static STOP: AtomicBool = AtomicBool::new(false);
#[test]
fn dump_while_libraries_unload() {
let answer = "#[unsafe(no_mangle)] pub extern \"C\" fn answer() -> i32 { 42 }\n";
let Some(lib) = common::build_library("dltarget", answer) else {
eprintln!("rustc not found; skipped");
return;
};
prof::activate();
let path = std::ffi::CString::new(lib.to_str().unwrap()).unwrap();
let churn = thread::spawn(move || {
let mut cycles = 0u32;
while !STOP.load(Ordering::Relaxed) {
unsafe {
let h = libc::dlopen(path.as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL);
assert!(!h.is_null());
libc::dlclose(h);
}
cycles += 1;
}
cycles
});
for _ in 0..150 {
let profile = prof::dump_pprof().unwrap();
assert!(profile.len() > 16);
}
STOP.store(true, Ordering::Relaxed);
assert!(churn.join().unwrap() > 0);
let _ = std::fs::remove_dir_all(lib.parent().unwrap());
}