use std::path::{Path, PathBuf};
const POOL_FD_BUDGET_FRACTION: f64 = 0.5;
const POOL_FD_RESERVE: usize = 64;
pub(crate) const MAX_CONNECTIONS_PER_HOST: usize = 4096;
const MIN_CONNECTIONS_PER_HOST: usize = 10;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct DescriptorLimit {
pub(crate) soft: usize,
pub(crate) hard: Option<usize>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ConnectionCap {
pub(crate) max_per_host: usize,
pub(crate) descriptors: Option<DescriptorLimit>,
}
impl ConnectionCap {
pub(crate) fn detect() -> Self {
Self::from_descriptor_limit(descriptor_limit())
}
pub(crate) fn from_descriptor_limit(descriptors: Option<DescriptorLimit>) -> Self {
let max_per_host = descriptors
.map_or(MAX_CONNECTIONS_PER_HOST, |limit| {
descriptor_budget(limit.soft)
})
.clamp(MIN_CONNECTIONS_PER_HOST, MAX_CONNECTIONS_PER_HOST);
Self {
max_per_host,
descriptors,
}
}
pub(crate) fn limited_by_descriptors(&self) -> bool {
self.max_per_host < MAX_CONNECTIONS_PER_HOST
}
}
fn descriptor_budget(soft: usize) -> usize {
let fraction = (soft as f64 * POOL_FD_BUDGET_FRACTION) as usize;
fraction.min(soft.saturating_sub(POOL_FD_RESERVE))
}
#[cfg(unix)]
fn descriptor_limit() -> Option<DescriptorLimit> {
use nix::sys::resource::{getrlimit, Resource};
let (soft, hard) = getrlimit(Resource::RLIMIT_NOFILE).ok()?;
parse_descriptor_limit(soft, hard, nix::sys::resource::RLIM_INFINITY)
}
#[cfg(not(unix))]
fn descriptor_limit() -> Option<DescriptorLimit> {
None
}
#[cfg_attr(not(unix), allow(dead_code))]
fn parse_descriptor_limit<T>(soft: T, hard: T, infinity: T) -> Option<DescriptorLimit>
where
T: Copy + PartialEq + TryInto<usize>,
{
let finite = |limit: T| (limit != infinity).then(|| limit.try_into().unwrap_or(usize::MAX));
let soft = finite(soft).filter(|&soft| soft > 0)?;
Some(DescriptorLimit {
soft,
hard: finite(hard),
})
}
#[cfg(any(target_os = "android", target_os = "linux"))]
pub(crate) fn available_ram() -> Option<usize> {
match (meminfo_total(), cgroup_mem_limit()) {
(Some(total), Some(cgroup)) => Some(total.min(cgroup)),
(total, None) => total,
(None, cgroup) => cgroup,
}
}
#[cfg(not(any(
target_os = "android",
target_os = "freebsd",
target_os = "linux",
target_os = "macos",
target_os = "windows"
)))]
pub(crate) fn available_ram() -> Option<usize> {
None
}
#[cfg(target_os = "freebsd")]
pub(crate) fn available_ram() -> Option<usize> {
let mut bytes: u64 = 0;
let mut len = std::mem::size_of::<u64>();
let result = unsafe {
libc::sysctlbyname(
c"hw.physmem".as_ptr(),
(&mut bytes as *mut u64).cast(),
&mut len,
std::ptr::null_mut(),
0,
)
};
if result != 0 || len != std::mem::size_of::<u64>() || bytes == 0 {
return None;
}
usize::try_from(bytes).ok()
}
#[cfg(target_os = "macos")]
pub(crate) fn available_ram() -> Option<usize> {
let mut bytes: u64 = 0;
let mut len = std::mem::size_of::<u64>();
let rc = unsafe {
libc::sysctlbyname(
c"hw.memsize".as_ptr(),
(&mut bytes as *mut u64).cast(),
&mut len,
std::ptr::null_mut(),
0,
)
};
if rc == 0 && bytes > 0 {
usize::try_from(bytes).ok()
} else {
None
}
}
#[cfg(target_os = "windows")]
pub(crate) fn available_ram() -> Option<usize> {
use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX};
let mut status: MEMORYSTATUSEX = unsafe { std::mem::zeroed() };
status.dwLength = std::mem::size_of::<MEMORYSTATUSEX>() as u32;
let ok = unsafe { GlobalMemoryStatusEx(&mut status) };
if ok != 0 && status.ullTotalPhys > 0 {
usize::try_from(status.ullTotalPhys).ok()
} else {
None
}
}
#[cfg(any(target_os = "android", target_os = "linux"))]
fn meminfo_total() -> Option<usize> {
parse_meminfo_total(&std::fs::read_to_string("/proc/meminfo").ok()?)
}
#[cfg(any(target_os = "android", target_os = "linux"))]
fn cgroup_mem_limit() -> Option<usize> {
let proc_cgroup = std::fs::read_to_string("/proc/self/cgroup").ok()?;
let mountinfo = std::fs::read_to_string("/proc/self/mountinfo").unwrap_or_default();
min_limit(&cgroup_v2_files(&proc_cgroup, &mountinfo))
.or_else(|| min_limit(&cgroup_v1_files(&proc_cgroup, &mountinfo)))
}
#[cfg(any(target_os = "android", target_os = "linux"))]
fn min_limit(files: &[PathBuf]) -> Option<usize> {
files
.iter()
.filter_map(|f| std::fs::read_to_string(f).ok())
.filter_map(|s| parse_cgroup_limit(&s))
.min()
}
#[cfg_attr(not(any(target_os = "android", target_os = "linux")), allow(dead_code))]
fn parse_meminfo_total(meminfo: &str) -> Option<usize> {
let line = meminfo.lines().find(|l| l.starts_with("MemTotal:"))?;
line.split_whitespace()
.nth(1)?
.parse::<usize>()
.ok()?
.checked_mul(1024)
}
#[cfg_attr(not(any(target_os = "android", target_os = "linux")), allow(dead_code))]
fn parse_cgroup_limit(raw: &str) -> Option<usize> {
let raw = raw.trim();
if raw == "max" {
return None;
}
raw.parse().ok()
}
#[cfg_attr(not(any(target_os = "android", target_os = "linux")), allow(dead_code))]
fn cgroup_v2_files(proc_cgroup: &str, mountinfo: &str) -> Vec<PathBuf> {
let Some(path) = cgroup_v2_path(proc_cgroup) else {
return Vec::new();
};
let mount =
mountinfo_mount(mountinfo, "cgroup2", None).unwrap_or_else(|| "/sys/fs/cgroup".to_string());
walk_paths(&mount, &path, "memory.max")
}
#[cfg_attr(not(any(target_os = "android", target_os = "linux")), allow(dead_code))]
fn cgroup_v1_files(proc_cgroup: &str, mountinfo: &str) -> Vec<PathBuf> {
let Some(path) = cgroup_v1_memory_path(proc_cgroup) else {
return Vec::new();
};
let mount = mountinfo_mount(mountinfo, "cgroup", Some("memory"))
.unwrap_or_else(|| "/sys/fs/cgroup/memory".to_string());
walk_paths(&mount, &path, "memory.limit_in_bytes")
}
#[cfg_attr(not(any(target_os = "android", target_os = "linux")), allow(dead_code))]
fn cgroup_v2_path(proc_cgroup: &str) -> Option<String> {
proc_cgroup.lines().find_map(|line| {
let mut fields = line.splitn(3, ':');
match (fields.next(), fields.next(), fields.next()) {
(Some("0"), Some(""), Some(path)) => Some(path.to_string()),
_ => None,
}
})
}
#[cfg_attr(not(any(target_os = "android", target_os = "linux")), allow(dead_code))]
fn cgroup_v1_memory_path(proc_cgroup: &str) -> Option<String> {
proc_cgroup.lines().find_map(|line| {
let mut fields = line.splitn(3, ':');
let (_id, controllers, path) = (fields.next()?, fields.next()?, fields.next()?);
controllers
.split(',')
.any(|c| c == "memory")
.then(|| path.to_string())
})
}
#[cfg_attr(not(any(target_os = "android", target_os = "linux")), allow(dead_code))]
fn mountinfo_mount(mountinfo: &str, fstype: &str, super_opt: Option<&str>) -> Option<String> {
mountinfo.lines().find_map(|line| {
let (left, right) = line.split_once(" - ")?;
let mut right_fields = right.split_whitespace();
if right_fields.next()? != fstype {
return None;
}
if let Some(opt) = super_opt {
let superopts = right_fields.nth(1).unwrap_or("");
if !superopts.split(',').any(|o| o == opt) {
return None;
}
}
left.split_whitespace().nth(4).map(str::to_string)
})
}
#[cfg_attr(not(any(target_os = "android", target_os = "linux")), allow(dead_code))]
fn walk_paths(mount: &str, cgroup_path: &str, filename: &str) -> Vec<PathBuf> {
let mount = Path::new(mount);
let rel = cgroup_path.trim_start_matches('/');
let mut dir = if rel.is_empty() {
mount.to_path_buf()
} else {
mount.join(rel)
};
let mut out = Vec::new();
loop {
out.push(dir.join(filename));
if dir == mount {
break;
}
match dir.parent() {
Some(parent) if parent.starts_with(mount) => dir = parent.to_path_buf(),
_ => break,
}
}
out
}
const MIN_CONCURRENCY_SEED: usize = 10;
const MAX_CONCURRENCY_SEED: usize = 10_000;
const GBPS_PER_CONN: f64 = 0.4;
const FALLBACK_INFLIGHT_PER_VCPU: usize = 5;
const FALLBACK_MIN: usize = 32;
const FAMILY_NIC_GBPS: &[(&str, f64, u32)] = &[
("p6-b300", 350.0, 192),
("p5", 400.0, 192),
("p5en", 100.0, 192),
("p5e", 100.0, 192),
("p6-b200", 200.0, 192),
("trn1n", 800.0, 128),
("p6e-gb200", 1700.0, 144),
("g7e", 1600.0, 192),
("trn1", 600.0, 128),
("dl1", 400.0, 96),
("p4d", 400.0, 96),
("p4de", 400.0, 96),
("g7", 700.0, 192),
("c7gn", 200.0, 64),
("c8gn", 600.0, 192),
("m8gn", 600.0, 192),
("r8gn", 600.0, 192),
("hpc7g", 200.0, 64),
("c8gb", 400.0, 192),
("m8gb", 400.0, 192),
("r8gb", 400.0, 192),
("m8azn", 200.0, 96),
("c6in", 200.0, 128),
("c8in", 600.0, 384),
("m6idn", 200.0, 128),
("m6in", 200.0, 128),
("m8idn", 600.0, 384),
("m8in", 600.0, 384),
("r6idn", 200.0, 128),
("r6in", 200.0, 128),
("r8idn", 600.0, 384),
("r8in", 600.0, 384),
("c6gn", 100.0, 64),
("im4gn", 100.0, 64),
("i8ge", 300.0, 192),
("c5n", 100.0, 72),
("i3en", 100.0, 96),
("inf1", 100.0, 96),
("p3dn", 100.0, 96),
("c8ib", 400.0, 384),
("m8ib", 400.0, 384),
("m8idb", 400.0, 384),
("r8ib", 400.0, 384),
("r8idb", 400.0, 384),
("x2idn", 100.0, 128),
("x2iedn", 100.0, 128),
("i4i", 75.0, 128),
("f2", 100.0, 192),
("i7ie", 100.0, 192),
("inf2", 100.0, 192),
("m5n", 100.0, 96),
("m5dn", 100.0, 96),
("r5n", 100.0, 96),
("r5dn", 100.0, 96),
("m5zn", 100.0, 48),
("x2iezn", 100.0, 48),
("d3en", 75.0, 48),
("is4gen", 50.0, 32),
("d3", 25.0, 32),
("c8ine", 75.0, 48),
("g4dn", 100.0, 96),
("g6e", 400.0, 192),
];
#[derive(Debug, Clone)]
pub(crate) struct MachineProfile {
pub(crate) instance_type: Option<String>,
pub(crate) vcpus: usize,
pub(crate) ram_bytes: Option<usize>,
}
impl MachineProfile {
pub(crate) fn detect_local() -> Self {
let instance_type = match detect_instance_type_dmi() {
DmiDetection::Instance(ty) => Some(ty),
DmiDetection::NotEc2 | DmiDetection::Unknown => None,
};
Self {
instance_type,
vcpus: local_vcpus(),
ram_bytes: available_ram(),
}
}
}
pub(crate) fn local_vcpus() -> usize {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
}
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum DmiDetection {
Instance(String),
NotEc2,
Unknown,
}
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn classify_dmi(vendor: &str, product: Option<&str>) -> DmiDetection {
if !vendor.trim().eq_ignore_ascii_case("Amazon EC2") {
return DmiDetection::NotEc2;
}
match product.map(str::trim) {
Some(ty) if !ty.is_empty() => DmiDetection::Instance(ty.to_string()),
_ => DmiDetection::Unknown,
}
}
#[cfg(target_os = "linux")]
pub(crate) fn detect_instance_type_dmi() -> DmiDetection {
let Ok(vendor) = std::fs::read_to_string("/sys/devices/virtual/dmi/id/sys_vendor") else {
return DmiDetection::Unknown;
};
let product = std::fs::read_to_string("/sys/devices/virtual/dmi/id/product_name").ok();
classify_dmi(&vendor, product.as_deref())
}
#[cfg(not(target_os = "linux"))]
pub(crate) fn detect_instance_type_dmi() -> DmiDetection {
DmiDetection::Unknown
}
fn family_gbps_per_vcpu(instance_type: &str) -> Option<f64> {
let family = instance_type.split('.').next()?;
FAMILY_NIC_GBPS
.iter()
.find(|(name, _, _)| name.eq_ignore_ascii_case(family))
.map(|(_, max_gbps, vcpus_at_max)| max_gbps / *vcpus_at_max as f64)
}
pub(crate) fn seed_from_gbps(gbps: f64) -> usize {
let n = (gbps / GBPS_PER_CONN).ceil();
(n as usize).clamp(MIN_CONCURRENCY_SEED, MAX_CONCURRENCY_SEED)
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum AutoConcurrencySource {
InstanceFamily,
VcpuFallback,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct ResolvedAutoConcurrency {
pub(crate) target: usize,
pub(crate) estimated_gbps: Option<f64>,
pub(crate) source: AutoConcurrencySource,
}
pub(crate) fn resolve_auto_concurrency(
instance_type: Option<&str>,
vcpus: usize,
) -> ResolvedAutoConcurrency {
match instance_type.and_then(family_gbps_per_vcpu) {
Some(per_vcpu) => {
let estimated_gbps = per_vcpu * vcpus as f64;
ResolvedAutoConcurrency {
target: seed_from_gbps(estimated_gbps),
estimated_gbps: Some(estimated_gbps),
source: AutoConcurrencySource::InstanceFamily,
}
}
None => ResolvedAutoConcurrency {
target: (FALLBACK_INFLIGHT_PER_VCPU * vcpus).clamp(FALLBACK_MIN, MAX_CONCURRENCY_SEED),
estimated_gbps: None,
source: AutoConcurrencySource::VcpuFallback,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
fn soft(soft: usize) -> Option<DescriptorLimit> {
Some(DescriptorLimit { soft, hard: None })
}
#[test]
fn test_cap_without_descriptor_limit_is_ceiling() {
let cap = ConnectionCap::from_descriptor_limit(None);
assert_eq!(cap.max_per_host, MAX_CONNECTIONS_PER_HOST);
assert!(!cap.limited_by_descriptors());
}
#[test]
fn test_cap_low_descriptor_limit_is_floored() {
let cap = ConnectionCap::from_descriptor_limit(soft(8));
assert_eq!(cap.max_per_host, MIN_CONNECTIONS_PER_HOST);
assert!(cap.limited_by_descriptors());
}
#[test]
fn test_cap_reserve_binds_below_twice_the_reserve() {
assert_eq!(
ConnectionCap::from_descriptor_limit(soft(100)).max_per_host,
36
);
}
#[test]
fn test_cap_reserve_does_not_bind_above_twice_the_reserve() {
assert_eq!(
ConnectionCap::from_descriptor_limit(soft(160)).max_per_host,
80
);
}
#[test]
fn test_cap_macos_default_limit() {
assert_eq!(
ConnectionCap::from_descriptor_limit(soft(256)).max_per_host,
128
);
}
#[test]
fn test_cap_mid_descriptor_limit_is_fraction() {
let cap = ConnectionCap::from_descriptor_limit(soft(1024));
assert_eq!(cap.max_per_host, 512);
assert!(cap.limited_by_descriptors());
}
#[test]
fn test_cap_high_descriptor_limit_is_ceiling() {
let cap = ConnectionCap::from_descriptor_limit(soft(524_288));
assert_eq!(cap.max_per_host, MAX_CONNECTIONS_PER_HOST);
assert!(!cap.limited_by_descriptors());
}
#[test]
fn test_cap_at_ceiling_boundary() {
let cap = ConnectionCap::from_descriptor_limit(soft(2 * MAX_CONNECTIONS_PER_HOST));
assert_eq!(cap.max_per_host, MAX_CONNECTIONS_PER_HOST);
assert!(!cap.limited_by_descriptors());
}
#[test]
fn test_parse_descriptor_limit() {
const INF: u64 = u64::MAX;
assert_eq!(
parse_descriptor_limit(1024, 524_288, INF),
Some(DescriptorLimit {
soft: 1024,
hard: Some(524_288)
})
);
assert_eq!(
parse_descriptor_limit(1024, INF, INF),
Some(DescriptorLimit {
soft: 1024,
hard: None
})
);
assert_eq!(parse_descriptor_limit(INF, INF, INF), None);
assert_eq!(parse_descriptor_limit(0, 1024, INF), None);
}
#[test]
fn test_parse_signed_descriptor_limit() {
const INF: i64 = i64::MAX;
assert_eq!(
parse_descriptor_limit(1024, INF, INF),
Some(DescriptorLimit {
soft: 1024,
hard: None
})
);
assert_eq!(parse_descriptor_limit(INF, INF, INF), None);
}
#[test]
#[cfg_attr(
miri,
ignore = "descriptor detection calls getrlimit, a syscall miri cannot emulate"
)]
fn test_detected_cap_within_bounds() {
let cap = ConnectionCap::detect();
assert!((MIN_CONNECTIONS_PER_HOST..=MAX_CONNECTIONS_PER_HOST).contains(&cap.max_per_host));
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_parse_meminfo_total() {
let s = "MemFree: 100 kB\nMemTotal: 65536000 kB\nBuffers: 5 kB\n";
assert_eq!(parse_meminfo_total(s), Some(65536000 * 1024));
}
#[test]
fn test_parse_meminfo_missing() {
assert_eq!(parse_meminfo_total("Foo: 1 kB\n"), None);
}
#[test]
fn test_parse_cgroup_max_is_none() {
assert_eq!(parse_cgroup_limit("max\n"), None);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_parse_cgroup_value() {
assert_eq!(parse_cgroup_limit("4294967296\n"), Some(4_294_967_296));
}
#[test]
fn test_cgroup_v2_path() {
assert_eq!(
cgroup_v2_path("0::/foo/bar\n"),
Some("/foo/bar".to_string())
);
assert_eq!(cgroup_v2_path("0::/\n"), Some("/".to_string()));
assert_eq!(cgroup_v2_path("11:memory:/x\n"), None);
}
#[test]
fn test_cgroup_v1_memory_path() {
let f = "12:pids:/a\n4:cpu,cpuacct:/b\n3:memory:/foo/bar\n";
assert_eq!(cgroup_v1_memory_path(f), Some("/foo/bar".to_string()));
assert_eq!(cgroup_v1_memory_path("0::/unified\n"), None);
}
#[test]
fn test_mountinfo_mount_v2() {
let mi = "35 24 0:30 / /sys/fs/cgroup rw,nosuid - cgroup2 cgroup2 rw,nsdelegate\n";
assert_eq!(
mountinfo_mount(mi, "cgroup2", None),
Some("/sys/fs/cgroup".to_string())
);
}
#[test]
fn test_mountinfo_mount_v1_memory() {
let mi = "30 24 0:26 / /sys/fs/cgroup/cpu rw - cgroup cgroup rw,cpu\n\
31 24 0:27 / /sys/fs/cgroup/memory rw - cgroup cgroup rw,memory\n";
assert_eq!(
mountinfo_mount(mi, "cgroup", Some("memory")),
Some("/sys/fs/cgroup/memory".to_string())
);
assert_eq!(mountinfo_mount(mi, "cgroup", Some("hugetlb")), None);
}
#[test]
fn test_walk_paths_subcgroup() {
assert_eq!(
walk_paths("/sys/fs/cgroup", "/foo/bar", "memory.max"),
vec![
PathBuf::from("/sys/fs/cgroup/foo/bar/memory.max"),
PathBuf::from("/sys/fs/cgroup/foo/memory.max"),
PathBuf::from("/sys/fs/cgroup/memory.max"),
]
);
}
#[test]
fn test_walk_paths_root() {
assert_eq!(
walk_paths("/sys/fs/cgroup", "/", "memory.max"),
vec![PathBuf::from("/sys/fs/cgroup/memory.max")]
);
}
#[cfg(any(
target_os = "freebsd",
target_os = "linux",
target_os = "macos",
target_os = "windows"
))]
#[test]
#[cfg_attr(
miri,
ignore = "available_ram reads /proc and calls platform syscalls miri cannot emulate"
)]
fn test_available_ram_detected_on_supported_platforms() {
const GIB: usize = 1024 * 1024 * 1024;
match available_ram() {
Some(bytes) => assert!(bytes >= GIB),
None if cfg!(target_pointer_width = "32") => {}
None => panic!("available_ram must detect RAM on a supported 64-bit platform"),
}
}
fn auto_concurrency_seed(instance_type: Option<&str>, vcpus: usize) -> usize {
resolve_auto_concurrency(instance_type, vcpus).target
}
#[test]
fn test_family_lookup_parses_family_from_type() {
assert_eq!(family_gbps_per_vcpu("m6idn.16xlarge"), Some(1.5625));
assert_eq!(family_gbps_per_vcpu("m6idn.32xlarge"), Some(1.5625));
assert_eq!(family_gbps_per_vcpu("m6idn.metal"), Some(1.5625));
assert_eq!(family_gbps_per_vcpu("C8GN.48XLARGE"), Some(3.125));
assert_eq!(family_gbps_per_vcpu("t3.large"), None);
assert_eq!(family_gbps_per_vcpu("nonsense"), None);
}
#[test]
fn test_seed_recognized_family_scales_with_vcpu() {
assert_eq!(auto_concurrency_seed(Some("m6idn.16xlarge"), 64), 250);
assert_eq!(auto_concurrency_seed(Some("m6idn.32xlarge"), 128), 500);
assert_eq!(auto_concurrency_seed(Some("c8gn.16xlarge"), 64), 500);
}
#[test]
fn test_auto_resolution_reports_family_estimate() {
let resolved = resolve_auto_concurrency(Some("m6idn.16xlarge"), 64);
assert_eq!(
resolved,
ResolvedAutoConcurrency {
target: 250,
estimated_gbps: Some(100.0),
source: AutoConcurrencySource::InstanceFamily,
}
);
}
#[test]
fn test_auto_resolution_reports_vcpu_fallback() {
let resolved = resolve_auto_concurrency(Some("unknown.16xlarge"), 64);
assert_eq!(
resolved,
ResolvedAutoConcurrency {
target: 320,
estimated_gbps: None,
source: AutoConcurrencySource::VcpuFallback,
}
);
}
#[test]
fn test_seed_uses_realized_vcpu_not_instance_size() {
assert_eq!(auto_concurrency_seed(Some("m6idn.32xlarge"), 4), 16);
}
#[test]
fn test_seed_recognizes_hyphenated_family() {
assert_eq!(auto_concurrency_seed(Some("p6-b300.6xlarge"), 24), 110);
assert_ne!(
auto_concurrency_seed(Some("p6-b300.6xlarge"), 24),
FALLBACK_INFLIGHT_PER_VCPU * 24
);
}
#[test]
fn test_seed_fallback_scales_with_vcpu() {
assert_eq!(auto_concurrency_seed(Some("t3.2xlarge"), 8), 40);
assert_eq!(auto_concurrency_seed(Some("gcp-n2-standard-64"), 64), 320);
assert_eq!(auto_concurrency_seed(None, 16), 80);
}
#[test]
fn test_seed_fallback_floored_for_small_box() {
assert_eq!(auto_concurrency_seed(None, 4), FALLBACK_MIN);
assert_eq!(
auto_concurrency_seed(Some("gcp-n2-standard-4"), 4),
FALLBACK_MIN
);
assert_eq!(auto_concurrency_seed(None, 6), FALLBACK_MIN);
assert_eq!(auto_concurrency_seed(None, 7), 35);
}
#[test]
fn test_seed_clamps_to_bounds() {
assert_eq!(auto_concurrency_seed(None, 1), FALLBACK_MIN);
assert_eq!(auto_concurrency_seed(Some("t3.micro"), 2), FALLBACK_MIN);
assert_eq!(
auto_concurrency_seed(Some("i4i.large"), 2),
MIN_CONCURRENCY_SEED
);
assert_eq!(
auto_concurrency_seed(Some("p6-b300.48xlarge"), 100_000),
MAX_CONCURRENCY_SEED
);
}
#[test]
fn test_seed_from_gbps_edges() {
assert_eq!(seed_from_gbps(100.0), 250);
assert_eq!(seed_from_gbps(0.0), MIN_CONCURRENCY_SEED); assert_eq!(seed_from_gbps(f64::NAN), MIN_CONCURRENCY_SEED); assert_eq!(seed_from_gbps(1.0e9), MAX_CONCURRENCY_SEED); }
#[test]
fn test_family_rate_is_flagship_gbps_over_vcpu() {
assert_eq!(auto_concurrency_seed(Some("m6idn.metal"), 128), 500);
assert_eq!(auto_concurrency_seed(Some("c8gn.48xlarge"), 192), 1500);
assert_eq!(auto_concurrency_seed(Some("i4i.32xlarge"), 128), 188);
}
#[test]
fn test_family_nic_table_is_well_formed() {
use std::collections::HashSet;
let mut seen = HashSet::new();
for (family, gbps, vcpus) in FAMILY_NIC_GBPS {
assert!(seen.insert(*family), "duplicate family entry: {family:?}");
assert!(!family.is_empty(), "empty family name");
assert!(
!family.contains('.'),
"family {family:?} must be a bare family, not an instance type"
);
assert_eq!(
family.to_ascii_lowercase(),
*family,
"family {family:?} must be lowercase for case-insensitive lookup"
);
assert!(*gbps > 0.0 && *gbps <= 6400.0, "{family}: gbps {gbps}");
assert!(*vcpus > 0 && *vcpus <= 1024, "{family}: vcpus {vcpus}");
}
}
#[test]
fn test_local_vcpus_is_at_least_one() {
assert!(local_vcpus() >= 1);
}
#[cfg_attr(
miri,
ignore = "detect_local reads /sys and /proc and calls platform syscalls miri cannot emulate"
)]
#[test]
fn test_detect_local_fills_profile() {
let p = MachineProfile::detect_local();
assert!(p.vcpus >= 1);
}
#[cfg(not(target_os = "linux"))]
#[test]
fn test_dmi_detection_unknown_off_linux() {
assert_eq!(detect_instance_type_dmi(), DmiDetection::Unknown);
}
#[test]
fn test_classify_dmi_three_states() {
assert_eq!(
classify_dmi("Amazon EC2\n", Some("m6idn.16xlarge\n")),
DmiDetection::Instance("m6idn.16xlarge".to_string())
);
assert_eq!(
classify_dmi("amazon ec2", Some("c7gn.16xlarge")),
DmiDetection::Instance("c7gn.16xlarge".to_string())
);
assert_eq!(
classify_dmi("QEMU", Some("Standard PC")),
DmiDetection::NotEc2
);
assert_eq!(classify_dmi("", Some("whatever")), DmiDetection::NotEc2);
assert_eq!(classify_dmi("Amazon EC2", None), DmiDetection::Unknown);
assert_eq!(
classify_dmi("Amazon EC2", Some(" ")),
DmiDetection::Unknown
);
}
}