diff --git a/eos/src/unix.rs b/eos/src/unix.rs index 178b0bfc1..a09a3b3a0 100644 --- a/eos/src/unix.rs +++ b/eos/src/unix.rs @@ -180,10 +180,155 @@ pub fn raise_open_file_limit(desired: u64) -> Option { Some(current) } +/// Select the high-capacity CPU subset from `(cpu_id, cpu_capacity)` pairs +/// when the allowed set spans asymmetric core tiers (such as ARM big.LITTLE). +/// +/// Returns `None` when the allowed set is homogeneous, contains fewer than two +/// CPUs, has invalid zero capacities, or would collapse below two CPUs. +pub fn select_high_capacity_cpus(allowed_with_capacity: &[(usize, u32)]) -> Option> { + if allowed_with_capacity.len() < 2 { + return None; + } + let mut min_cap = u32::MAX; + let mut max_cap = 0u32; + for &(_, cap) in allowed_with_capacity { + if cap == 0 { + return None; + } + min_cap = min_cap.min(cap); + max_cap = max_cap.max(cap); + } + if min_cap == max_cap { + return None; + } + // Performance cores on asymmetric SoCs can differ by a few percent across + // clusters (for example, ACPI CPPC capacities 997..1024 vs efficiency 718..731). + let mut selected: Vec = allowed_with_capacity + .iter() + .filter(|&&(_, cap)| u64::from(cap) * 10 >= u64::from(max_cap) * 9) + .map(|&(cpu, _)| cpu) + .collect(); + if selected.len() < 2 { + // Avoid collapsing multiple busy threads onto a single prime core on + // three-tier topologies: fall back to all cores above the capacity midpoint. + let sum = u64::from(min_cap) + u64::from(max_cap); + selected = allowed_with_capacity + .iter() + .filter(|&&(_, cap)| u64::from(cap) * 2 > sum) + .map(|&(cpu, _)| cpu) + .collect(); + } + if selected.len() >= 2 && selected.len() < allowed_with_capacity.len() { + Some(selected) + } else { + None + } +} + +/// Restrict the calling thread to high-capacity CPUs within its current +/// affinity mask on asymmetric Linux systems. No-op on homogeneous CPUs, +/// restricted cpusets that already cover one tier, or non-Linux targets. +#[cfg(target_os = "linux")] +pub fn prefer_high_capacity_cpus() { + let mut allowed: libc::cpu_set_t = unsafe { std::mem::zeroed() }; + if unsafe { libc::sched_getaffinity(0, std::mem::size_of::(), &mut allowed) } + != 0 + { + return; + } + let max_cpus = std::mem::size_of::() * 8; + let mut allowed_with_capacity = Vec::new(); + for cpu in 0..max_cpus { + if unsafe { libc::CPU_ISSET(cpu, &allowed) } { + let path = format!("/sys/devices/system/cpu/cpu{cpu}/cpu_capacity"); + let Ok(text) = std::fs::read_to_string(&path) else { + return; + }; + let Ok(cap) = text.trim().parse::() else { + return; + }; + allowed_with_capacity.push((cpu, cap)); + } + } + let Some(selected) = select_high_capacity_cpus(&allowed_with_capacity) else { + return; + }; + let mut next: libc::cpu_set_t = unsafe { std::mem::zeroed() }; + for cpu in selected { + unsafe { libc::CPU_SET(cpu, &mut next) }; + } + let _ = unsafe { libc::sched_setaffinity(0, std::mem::size_of::(), &next) }; +} + +#[cfg(not(target_os = "linux"))] +pub fn prefer_high_capacity_cpus() {} + #[cfg(test)] mod tests { use super::*; + #[test] + fn select_high_capacity_cpus_splits_asymmetric_tiers() { + let mut gb10 = Vec::new(); + for cpu in 0..5 { + gb10.push((cpu, 718)); + } + for cpu in 5..10 { + gb10.push((cpu, 997)); + } + for cpu in 10..15 { + gb10.push((cpu, 731)); + } + for cpu in 15..19 { + gb10.push((cpu, 1017)); + } + gb10.push((19, 1024)); + assert_eq!( + select_high_capacity_cpus(&gb10), + Some(vec![5, 6, 7, 8, 9, 15, 16, 17, 18, 19]) + ); + } + + #[test] + fn select_high_capacity_cpus_is_noop_on_homogeneous_or_single_tier_cpuset() { + let homogeneous: Vec<_> = (0..16).map(|cpu| (cpu, 1024)).collect(); + assert_eq!(select_high_capacity_cpus(&homogeneous), None); + + let efficiency_only: Vec<_> = (0..5) + .map(|cpu| (cpu, 718)) + .chain((10..15).map(|cpu| (cpu, 731))) + .collect(); + assert_eq!(select_high_capacity_cpus(&efficiency_only), None); + + let performance_only: Vec<_> = (5..10) + .map(|cpu| (cpu, 997)) + .chain((15..19).map(|cpu| (cpu, 1017))) + .chain(std::iter::once((19, 1024))) + .collect(); + assert_eq!(select_high_capacity_cpus(&performance_only), None); + + assert_eq!(select_high_capacity_cpus(&[(19, 1024)]), None); + assert_eq!(select_high_capacity_cpus(&[(0, 0), (1, 1024)]), None); + } + + #[test] + fn select_high_capacity_cpus_avoids_single_core_collapse_on_three_tier_soc() { + let three_tier = vec![ + (0, 400), + (1, 400), + (2, 400), + (3, 400), + (4, 850), + (5, 850), + (6, 850), + (7, 1024), + ]; + assert_eq!( + select_high_capacity_cpus(&three_tier), + Some(vec![4, 5, 6, 7]) + ); + } + #[test] fn raise_open_file_limit_never_lowers_and_respects_hard_limit() { let mut before: libc::rlimit = unsafe { std::mem::zeroed() }; diff --git a/eos/src/windows.rs b/eos/src/windows.rs index 1e63ff6a3..5231752fd 100644 --- a/eos/src/windows.rs +++ b/eos/src/windows.rs @@ -216,6 +216,9 @@ pub fn raise_open_file_limit(_desired: u64) -> Option { None } +/// CPU capacity affinity narrowing is implemented for asymmetric Linux topologies. +pub fn prefer_high_capacity_cpus() {} + #[cfg(test)] mod tests { use super::*; diff --git a/helios/src/execution/parallel.rs b/helios/src/execution/parallel.rs index f139d6964..059445153 100644 --- a/helios/src/execution/parallel.rs +++ b/helios/src/execution/parallel.rs @@ -575,6 +575,7 @@ pub unsafe fn serve_rank( } }; executor.copy_logprobs = false; + flare_eos::prefer_high_capacity_cpus(); let result = (|| -> Result<()> { if world < 2 || !(1..world).contains(&rank) { return Err(invalid("invalid tensor-parallel peer identity")); diff --git a/helios/src/execution/threaded.rs b/helios/src/execution/threaded.rs index 61dbbe96b..8336976be 100644 --- a/helios/src/execution/threaded.rs +++ b/helios/src/execution/threaded.rs @@ -680,6 +680,7 @@ impl ThreadedExecutor { } }; executor.copy_logprobs = false; + flare_eos::prefer_high_capacity_cpus(); let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { rank_loop(index, &mut executor, commands, &replies, &peer_ring) })) diff --git a/helios/src/execution/worker.rs b/helios/src/execution/worker.rs index f2776a23f..1fc859a5a 100644 --- a/helios/src/execution/worker.rs +++ b/helios/src/execution/worker.rs @@ -83,6 +83,7 @@ impl ExecutionWorker { }; } }; + flare_eos::prefer_high_capacity_cpus(); let _ = ready_sender.send(Ok(())); let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { while let Ok(command) = receiver.recv() { diff --git a/helios/src/replay.rs b/helios/src/replay.rs index 7c1f52897..896f65ca0 100644 --- a/helios/src/replay.rs +++ b/helios/src/replay.rs @@ -170,7 +170,10 @@ impl ReplayWorker { let (ready_sender, ready_receiver) = mpsc::sync_channel(1); let thread = thread::Builder::new() .name("flare-cuda".into()) - .spawn(move || run(receiver, completion_sender, ready_sender)) + .spawn(move || { + flare_eos::prefer_high_capacity_cpus(); + run(receiver, completion_sender, ready_sender) + }) .map_err(|e| { ExecutionError::Worker(Failure::caused_by( format!("cannot start native replay worker: {e}"), diff --git a/helios/src/server/runtime.rs b/helios/src/server/runtime.rs index 6e61dc23f..e6ea17f15 100644 --- a/helios/src/server/runtime.rs +++ b/helios/src/server/runtime.rs @@ -778,6 +778,10 @@ impl ModelRuntime { if worker.config.contract.vision.enabled() { worker.image_preparation = Some(ImagePreparation::new(worker_input_store)); } + // Narrow affinity only after RankExecutor, host offload/codec/disk, and image + // preparation child threads have been spawned so background workers stay free + // to run on efficiency cores and observe the full process cpuset. + flare_eos::prefer_high_capacity_cpus(); worker_metrics.scheduler(worker.scheduler.stats()); worker_health.store(true, Ordering::Release); let _ = ready_tx.send(Ok(()));