#[cfg(any(test, all(target_os = "linux", target_env = "gnu")))]
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
#[cfg(any(test, all(target_os = "linux", target_env = "gnu")))]
const PER_ARENA: u64 = 512 << 20;
#[cfg(any(test, all(target_os = "linux", target_env = "gnu")))]
const MOST: u64 = 8;
#[cfg_attr(test, allow(dead_code))]
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pub(super) fn cap() {
let Ok((limit, _)) = nix::sys::resource::getrlimit(nix::sys::resource::Resource::RLIMIT_AS)
else {
return;
};
let Some(arenas) = arenas(limit) else {
return;
};
let tunables = std::env::var_os("GLIBC_TUNABLES");
let alias = std::env::var_os("MALLOC_ARENA_MAX");
if configured(tunables.as_deref(), alias.as_deref()) {
return;
}
#[allow(unsafe_code, reason = "existing GNU Linux allocator call, ADR 0020")]
unsafe {
libc::mallopt(libc::M_ARENA_MAX, arenas)
};
}
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pub(super) fn trim() {
#[allow(unsafe_code, reason = "existing GNU Linux allocator call, ADR 0020")]
unsafe {
libc::malloc_trim(0)
};
}
#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
pub(super) fn trim() {}
#[cfg(any(test, all(target_os = "linux", target_env = "gnu")))]
fn arenas(limit: u64) -> Option<libc::c_int> {
(limit != libc::RLIM_INFINITY).then(|| (limit / PER_ARENA).clamp(1, MOST) as libc::c_int)
}
#[cfg(any(test, all(target_os = "linux", target_env = "gnu")))]
fn configured(tunables: Option<&OsStr>, alias: Option<&OsStr>) -> bool {
let positive = |value: &[u8]| {
!value.is_empty()
&& value.iter().all(u8::is_ascii_digit)
&& value.iter().any(|&digit| digit != b'0')
};
alias.is_some_and(|value| positive(value.as_bytes()))
|| tunables.is_some_and(|items| {
items.as_bytes().split(|&byte| byte == b':').any(|item| {
item.strip_prefix(b"glibc.malloc.arena_max=")
.is_some_and(positive)
})
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_limit_gives_one_arena_for_each_512_mib_from_one_to_eight() {
assert_eq!(arenas(libc::RLIM_INFINITY), None);
for (limit, expected) in [
(0, 1),
(1, 1),
(200_000 << 10, 1),
(750_000 << 10, 1),
((1 << 30) - 1, 1),
(1 << 30, 2),
((3 << 30) + (1 << 29), 7),
(4 << 30, 8),
(1 << 40, 8),
(libc::RLIM_INFINITY - 1, 8),
] {
assert_eq!(arenas(limit), Some(expected), "{limit}");
}
}
#[test]
fn an_arena_count_that_the_environment_sets_is_kept() {
assert!(!configured(None, None));
for (alias, set) in [
("2", true),
("16", true),
("", false),
("0", false),
("x", false),
] {
assert_eq!(configured(None, Some(OsStr::new(alias))), set, "{alias}");
}
for (tunables, set) in [
("glibc.malloc.arena_max=2", true),
("glibc.malloc.tcache_count=0:glibc.malloc.arena_max=1", true),
("glibc.malloc.arena_max=4:glibc.malloc.tcache_count=0", true),
("", false),
("glibc.malloc.arena_test=2", false),
("glibc.malloc.arena_max", false),
("glibc.malloc.arena_max=", false),
("glibc.malloc.arena_max=0", false),
("glibc.malloc.arena_max=two", false),
("xglibc.malloc.arena_max=2", false),
] {
assert_eq!(
configured(Some(OsStr::new(tunables)), None),
set,
"{tunables}"
);
}
}
}