redup-bootstrap 0.3.4

Ядро платформы: инстанс службы, модули, жизненный цикл, здоровье и телеметрия.
Documentation
//! Память процесса: настройка системного аллокатора.

use redup_config::Config;

/// Управление памятью процесса.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Config)]
pub struct MemoryConfig {
    /// Порог, с которого системный аллокатор выделяет память напрямую у операционной системы,
    /// в байтах. Крупные буферы выше порога возвращаются системе сразу после освобождения.
    /// `~` или ноль — не трогать настройку аллокатора.
    mmap_threshold: Option<u64>,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            mmap_threshold: Some(2 * 1024 * 1024),
        }
    }
}

impl MemoryConfig {
    /// Действующий порог mmap в байтах; `None` — настройка выключена (`~` или ноль).
    pub fn mmap_threshold(&self) -> Option<u64> {
        self.mmap_threshold.filter(|threshold| *threshold > 0)
    }
}

#[cfg(test)]
mod tests {
    use redup_config::{Source, load};

    use super::MemoryConfig;

    /// Читает секцию `memory` из временного файла.
    fn read(section: &str) -> MemoryConfig {
        let directory = tempfile::tempdir().expect("a temporary directory");
        std::fs::write(
            directory.path().join("config.yaml"),
            format!("memory:\n{section}"),
        )
        .expect("the configuration file");
        load::<MemoryConfig>(&Source::directory(directory.path()))
            .expect("the configuration is accepted")
            .value
    }

    #[test]
    fn threshold_is_two_mebibytes_by_default() {
        assert_eq!(
            MemoryConfig::default().mmap_threshold(),
            Some(2 * 1024 * 1024)
        );
        assert_eq!(
            read("\x20 mmapThreshold: 4Mi\n").mmap_threshold(),
            Some(4 * 1024 * 1024)
        );
    }

    #[test]
    fn null_disables_the_threshold() {
        assert_eq!(read("\x20 mmapThreshold: ~\n").mmap_threshold(), None);
    }

    #[test]
    fn zero_disables_the_threshold() {
        assert_eq!(read("\x20 mmapThreshold: 0\n").mmap_threshold(), None);
    }
}