use std::time::Duration;
pub mod io {
use super::Duration;
pub const TAIL_FLUSH_INTERVAL: Duration = Duration::from_millis(250);
pub const FLUSH_LINE_COUNT: u16 = 40;
pub const LINE_CAPACITY: usize = 512;
pub const READ_BUFFER_SIZE: usize = 8192;
pub const OUTPUT_BUFFER_CAPACITY: usize = 1024;
}
pub mod processing {
pub const LARGE_OFFSET_THRESHOLD: u64 = 10_000;
pub const CHUNKED_WITH_OFFSET_FILE_SIZE: u64 = 100 * 1024 * 1024;
pub const ALWAYS_CHUNKED_FILE_SIZE: u64 = 1024 * 1024 * 1024; }
pub mod memory {
pub const MEMORY_LIMIT_BYTES: usize = 10 * 1024 * 1024; }
pub struct SystemDefaults;
impl SystemDefaults {
pub const MIN_MEMORY_BUDGET: usize = 5 * 1024 * 1024;
pub const MAX_MEMORY_BUDGET: usize = 500 * 1024 * 1024;
pub const DEFAULT_CHUNK_SIZE: usize = 32 * 1024;
pub const MIN_CHUNK_SIZE: usize = 4 * 1024;
pub fn optimal_chunk_for_file(file_size: u64) -> usize {
const BLOCK_SIZE: usize = 4096;
let base_size = match file_size {
0..=102_400 => Self::MIN_CHUNK_SIZE,
102_401..=1_048_576 => 8 * 1024,
1_048_577..=10_485_760 => Self::DEFAULT_CHUNK_SIZE,
10_485_761..=104_857_600 => 128 * 1024,
104_857_601..=1_073_741_824 => 512 * 1024,
_ => 1024 * 1024,
};
base_size.div_ceil(BLOCK_SIZE) * BLOCK_SIZE
}
pub const MEMORY_PRESSURE_LOW_THRESHOLD: f64 = 0.60; pub const MEMORY_PRESSURE_MODERATE_THRESHOLD: f64 = 0.85; pub const MEMORY_PRESSURE_HIGH_THRESHOLD: f64 = 0.95; }
pub enum ConfigPreset {
LowMemory,
Balanced,
HighPerformance,
Conservative,
}
pub struct PresetSettings {
pub memory_percentage: f64,
pub max_memory_mb: usize,
pub force_chunked: bool,
}
impl ConfigPreset {
pub fn settings(&self) -> PresetSettings {
match self {
ConfigPreset::LowMemory => PresetSettings {
memory_percentage: 5.0,
max_memory_mb: 50,
force_chunked: true,
},
ConfigPreset::Balanced => PresetSettings {
memory_percentage: 10.0,
max_memory_mb: 200,
force_chunked: false,
},
ConfigPreset::HighPerformance => PresetSettings {
memory_percentage: 20.0,
max_memory_mb: 500,
force_chunked: false,
},
ConfigPreset::Conservative => PresetSettings {
memory_percentage: 5.0,
max_memory_mb: 100,
force_chunked: true,
},
}
}
pub fn auto_detect() -> Self {
if let Some(memory_stats) = memory_stats::memory_stats() {
let total_memory_mb = memory_stats.physical_mem / (1024 * 1024);
match total_memory_mb {
0..=2048 => ConfigPreset::LowMemory,
2049..=8192 => ConfigPreset::Conservative,
8193..=16384 => ConfigPreset::Balanced,
_ => ConfigPreset::HighPerformance,
}
} else {
ConfigPreset::Balanced
}
}
}
pub fn get_system_config() -> PresetSettings {
if let Ok(preset_name) = std::env::var("TALE_PRESET") {
let preset = match preset_name.to_lowercase().as_str() {
"low" | "lowmemory" | "low_memory" => ConfigPreset::LowMemory,
"balanced" | "balance" => ConfigPreset::Balanced,
"high" | "highperformance" | "high_performance" => ConfigPreset::HighPerformance,
"conservative" | "conserve" => ConfigPreset::Conservative,
_ => {
eprintln!("Warning: Unknown TALE_PRESET '{}', using auto-detection", preset_name);
ConfigPreset::auto_detect()
}
};
return preset.settings();
}
ConfigPreset::auto_detect().settings()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chunk_sizes_are_block_aligned() {
const BLOCK_SIZE: usize = 4096;
let tiny = SystemDefaults::optimal_chunk_for_file(50_000);
assert_eq!(tiny % BLOCK_SIZE, 0);
assert_eq!(tiny, SystemDefaults::MIN_CHUNK_SIZE);
let small = SystemDefaults::optimal_chunk_for_file(500_000);
assert_eq!(small % BLOCK_SIZE, 0);
assert_eq!(small, 8 * 1024);
let medium = SystemDefaults::optimal_chunk_for_file(5_000_000);
assert_eq!(medium % BLOCK_SIZE, 0);
assert_eq!(medium, SystemDefaults::DEFAULT_CHUNK_SIZE);
let large = SystemDefaults::optimal_chunk_for_file(50_000_000);
assert_eq!(large % BLOCK_SIZE, 0);
assert_eq!(large, 128 * 1024);
let huge = SystemDefaults::optimal_chunk_for_file(2_000_000_000);
assert_eq!(huge % BLOCK_SIZE, 0);
assert_eq!(huge, 1024 * 1024);
}
#[test]
fn presets_are_as_expected() {
let low_mem = ConfigPreset::LowMemory.settings();
assert_eq!(low_mem.memory_percentage, 5.0);
assert!(low_mem.force_chunked);
let balanced = ConfigPreset::Balanced.settings();
assert_eq!(balanced.memory_percentage, 10.0);
assert!(!balanced.force_chunked);
let high_perf = ConfigPreset::HighPerformance.settings();
assert_eq!(high_perf.memory_percentage, 20.0);
assert!(!high_perf.force_chunked);
}
#[test]
fn we_have_processing_constants() {
use super::processing::*;
assert_eq!(LARGE_OFFSET_THRESHOLD, 10_000);
assert_eq!(CHUNKED_WITH_OFFSET_FILE_SIZE, 100 * 1024 * 1024);
assert_eq!(ALWAYS_CHUNKED_FILE_SIZE, 1024 * 1024 * 1024);
}
#[test]
fn we_have_io_constants() {
use super::io::*;
assert_eq!(TAIL_FLUSH_INTERVAL.as_millis(), 250);
assert_eq!(FLUSH_LINE_COUNT, 40);
assert_eq!(READ_BUFFER_SIZE, 8192);
}
}