2024-06-22 19:28:26 +00:00
|
|
|
//! jemalloc allocator
|
|
|
|
|
2024-04-27 04:50:20 -07:00
|
|
|
use std::ffi::{c_char, c_void};
|
|
|
|
|
|
|
|
use tikv_jemalloc_ctl as mallctl;
|
|
|
|
use tikv_jemalloc_sys as ffi;
|
|
|
|
use tikv_jemallocator as jemalloc;
|
|
|
|
|
|
|
|
#[global_allocator]
|
|
|
|
static JEMALLOC: jemalloc::Jemalloc = jemalloc::Jemalloc;
|
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
#[must_use]
|
|
|
|
pub fn memory_usage() -> String {
|
2024-04-27 04:50:20 -07:00
|
|
|
use mallctl::stats;
|
|
|
|
let allocated = stats::allocated::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
|
|
|
|
let active = stats::active::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
|
|
|
|
let mapped = stats::mapped::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
|
|
|
|
let metadata = stats::metadata::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
|
|
|
|
let resident = stats::resident::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
|
|
|
|
let retained = stats::retained::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;
|
|
|
|
format!(
|
|
|
|
" allocated: {allocated:.2} MiB\n active: {active:.2} MiB\n mapped: {mapped:.2} MiB\n metadata: {metadata:.2} \
|
|
|
|
MiB\n resident: {resident:.2} MiB\n retained: {retained:.2} MiB\n "
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
#[must_use]
|
|
|
|
pub fn memory_stats() -> String {
|
2024-04-27 04:50:20 -07:00
|
|
|
const MAX_LENGTH: usize = 65536 - 4096;
|
|
|
|
|
|
|
|
let opts_s = "d";
|
2024-04-27 22:03:30 -07:00
|
|
|
let mut str = String::new();
|
2024-04-27 04:50:20 -07:00
|
|
|
|
2024-04-28 11:48:06 -04:00
|
|
|
let opaque = std::ptr::from_mut(&mut str).cast::<c_void>();
|
2024-06-02 00:30:04 +00:00
|
|
|
let opts_p: *const c_char = std::ffi::CString::new(opts_s)
|
|
|
|
.expect("cstring")
|
|
|
|
.into_raw()
|
|
|
|
.cast_const();
|
2024-04-27 22:03:30 -07:00
|
|
|
|
2024-04-28 01:31:24 -04:00
|
|
|
// SAFETY: calls malloc_stats_print() with our string instance which must remain
|
|
|
|
// in this frame. https://docs.rs/tikv-jemalloc-sys/latest/tikv_jemalloc_sys/fn.malloc_stats_print.html
|
2024-04-27 04:50:20 -07:00
|
|
|
unsafe { ffi::malloc_stats_print(Some(malloc_stats_cb), opaque, opts_p) };
|
|
|
|
|
|
|
|
str.truncate(MAX_LENGTH);
|
2024-04-28 01:31:24 -04:00
|
|
|
format!("<pre><code>{str}</code></pre>")
|
2024-04-27 04:50:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn malloc_stats_cb(opaque: *mut c_void, msg: *const c_char) {
|
2024-04-27 22:03:30 -07:00
|
|
|
// SAFETY: we have to trust the opaque points to our String
|
2024-04-28 11:48:06 -04:00
|
|
|
let res: &mut String = unsafe { opaque.cast::<String>().as_mut().unwrap() };
|
2024-04-27 22:03:30 -07:00
|
|
|
|
|
|
|
// SAFETY: we have to trust the string is null terminated.
|
2024-04-27 04:50:20 -07:00
|
|
|
let msg = unsafe { std::ffi::CStr::from_ptr(msg) };
|
2024-04-27 22:03:30 -07:00
|
|
|
|
2024-04-27 04:50:20 -07:00
|
|
|
let msg = String::from_utf8_lossy(msg.to_bytes());
|
|
|
|
res.push_str(msg.as_ref());
|
|
|
|
}
|