Skip to main content

mnemosyne_core/
options.rs

1//! Global thread-safe runtime configuration options.
2//!
3//! These options are populated once at startup from environment variables
4//! and accessed via relaxed atomic reads throughout the allocator.
5
6use core::sync::atomic::{AtomicBool, AtomicUsize};
7
8/// The maximum number of segments retained in the global segment pool.
9pub static MAX_RETAINED_SEGMENTS: AtomicUsize = AtomicUsize::new(default_max_retained_segments());
10
11const fn default_max_retained_segments() -> usize {
12    // Miri reports intentionally retained process-global cache mappings as
13    // leaks. Zero retention keeps Miri's ownership/leak evidence focused on
14    // live allocations while leaving production's bounded cache unchanged.
15    if cfg!(miri) {
16        0
17    } else {
18        crate::constants::MAX_RETAINED_SEGMENTS_LIMIT
19    }
20}
21
22/// Whether the advisory huge page hint (`MADV_HUGEPAGE`) is enabled on Linux.
23pub static ENABLE_HUGEPAGE_HINT: AtomicBool = AtomicBool::new(true);
24
25/// The cadence in milliseconds at which retained segments are purged in the background.
26pub static PURGE_CADENCE_MS: AtomicUsize = AtomicUsize::new(0);
27
28/// Runtime configuration options for the Mnemosyne allocator.
29#[derive(Clone, Copy, Debug, Eq, PartialEq)]
30pub struct MnemosyneOptions {
31    pub max_retained_segments: usize,
32    pub purge_cadence_ms: usize,
33    pub enable_hugepage_hint: bool,
34}
35
36impl Default for MnemosyneOptions {
37    #[inline]
38    fn default() -> Self {
39        Self {
40            max_retained_segments: default_max_retained_segments(),
41            purge_cadence_ms: 0,
42            enable_hugepage_hint: true,
43        }
44    }
45}
46
47/// Returns the current runtime configuration options snapshot.
48#[inline]
49pub fn get_options() -> MnemosyneOptions {
50    use core::sync::atomic::Ordering;
51    MnemosyneOptions {
52        max_retained_segments: MAX_RETAINED_SEGMENTS.load(Ordering::Acquire),
53        purge_cadence_ms: PURGE_CADENCE_MS.load(Ordering::Acquire),
54        enable_hugepage_hint: ENABLE_HUGEPAGE_HINT.load(Ordering::Acquire),
55    }
56}
57
58/// Overwrites the runtime configuration options.
59#[inline]
60pub fn set_options(options: MnemosyneOptions) {
61    use core::sync::atomic::Ordering;
62    let clamped_retained = core::cmp::min(
63        options.max_retained_segments,
64        crate::constants::MAX_RETAINED_SEGMENTS_LIMIT,
65    );
66    MAX_RETAINED_SEGMENTS.store(clamped_retained, Ordering::Release);
67    PURGE_CADENCE_MS.store(options.purge_cadence_ms, Ordering::Release);
68    ENABLE_HUGEPAGE_HINT.store(options.enable_hugepage_hint, Ordering::Release);
69}