feat(alloc): Implement trim, memory_usage, and memory_stats for mimalloc

This commit is contained in:
Jade Ellis committed 2026-11-21 02:54:49 +00:00
1 parent b5785348c6
commit cffa6d3e51
5 files changed
+104 -3

No files matched your search

Generated
+8
View File
@@ -998,6 +998,7 @@ dependencies = [
"lettre",
"libc",
"libloading 0.9.1",
"libmimalloc-sys",
"lock_api 0.5.0",
"maplit",
"mimalloc",
@@ -1552,6 +1553,12 @@ dependencies = [
"subtle",
]
[[package]]
name = "cty"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
[[package]]
name = "curve25519-dalek"
version = "5.0.0"
@@ -3257,6 +3264,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a45a52f43e1c16f667ccfe4dd8c85b7f7c204fd5e3bf46c5b0db9a5c3c0b8e9"
dependencies = [
"cc",
"cty",
]
[[package]]
+5
View File
@@ -603,6 +603,11 @@ version = "0.7.1"
[workspace.dependencies.mimalloc]
version = "0.1.52"
features = ["extended"]
[workspace.dependencies.libmimalloc-sys]
version = "0.1.49"
features = ["extended"]
#
# Patches
+3
View File
@@ -32,6 +32,7 @@ jemalloc = [
"dep:tikv-jemallocator",
]
mimalloc = [
"dep:libmimalloc-sys",
"dep:mimalloc",
]
jemalloc_conf = []
@@ -118,6 +119,8 @@ lock_api.workspace = true
hyper-util.workspace = true
resolvematrix.workspace = true
openidconnect.workspace = true
libmimalloc-sys.optional = true
libmimalloc-sys.workspace = true
mimalloc.optional = true
mimalloc.workspace = true
+84 -1
View File
@@ -1,4 +1,87 @@
//! mimalloc allocator
use std::ptr;
use libmimalloc_sys as ffi;
use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
static MIMALLOC: MiMalloc = MiMalloc;
pub fn trim<I: Into<Option<usize>>>(_: I) -> crate::Result {
// SAFETY: `mi_collect` is thread-safe and takes a single boolean flag.
unsafe {
ffi::mi_collect(true);
}
Ok(())
}
#[must_use]
pub fn memory_usage() -> Option<String> {
let mut current_rss = 0;
let mut peak_rss = 0;
let mut current_commit = 0;
let mut peak_commit = 0;
// SAFETY: All non-null out-pointers are valid writable `usize` stack slots;
// null pointers are explicitly permitted by `mi_process_info`.
unsafe {
ffi::mi_process_info(
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
&raw mut current_rss,
&raw mut peak_rss,
&raw mut current_commit,
&raw mut peak_commit,
ptr::null_mut(),
);
}
let mibs = |input: usize| {
let whole = u32::try_from(input / (1024 * 1024)).unwrap_or(u32::MAX);
let rem_kibs = u32::try_from((input / 1024) % 1024).unwrap_or_default();
f64::from(whole) + f64::from(rem_kibs) / 1024.0
};
Some(format!(
"current_rss: {:.2} MiB\npeak_rss: {:.2} MiB\ncurrent_commit: {:.2} MiB\npeak_commit: \
{:.2} MiB\n",
mibs(current_rss),
mibs(peak_rss),
mibs(current_commit),
mibs(peak_commit),
))
}
#[must_use]
pub fn memory_usage_stats() -> Option<[(&'static str, usize); 6]> { None }
#[must_use]
pub fn memory_stats(_opts: &str) -> Option<String> {
MiMalloc::stats_json()
.ok()
.and_then(|stats| stats.to_str().ok().map(str::to_owned))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn trim_succeeds() { trim(None).expect("trim(None)"); }
#[test]
fn memory_usage_returns_rss_and_commit() {
let usage = memory_usage().expect("memory_usage");
assert!(usage.contains("current_rss:"), "unexpected usage: {usage}");
assert!(usage.contains("current_commit:"), "unexpected usage: {usage}");
}
#[test]
fn memory_stats_returns_json() {
let stats = memory_stats("").expect("memory_stats");
assert!(stats.starts_with('{'), "expected JSON object, got: {stats}");
}
}
+4 -2
View File
@@ -9,9 +9,11 @@ pub use je::{memory_stats, memory_usage, memory_usage_stats, trim};
// mimalloc
#[cfg(all(feature = "mimalloc", any(target_env = "msvc", not(feature = "jemalloc"))))]
pub mod mimalloc;
#[cfg(all(feature = "mimalloc", any(target_env = "msvc", not(feature = "jemalloc"))))]
pub use mimalloc::{memory_stats, memory_usage, memory_usage_stats, trim};
// default
#[cfg(any(target_env = "msvc", not(feature = "jemalloc")))]
#[cfg(all(not(feature = "mimalloc"), any(target_env = "msvc", not(feature = "jemalloc"))))]
pub mod default;
#[cfg(any(target_env = "msvc", not(feature = "jemalloc")))]
#[cfg(all(not(feature = "mimalloc"), any(target_env = "msvc", not(feature = "jemalloc"))))]
pub use default::{memory_stats, memory_usage, memory_usage_stats, trim};