With the mapped low-RAM source (load_mapped_expert_source, the Original's --mmap-experts) the direct CPU tier opens every CPU expert of the group first without scanning it, then advises all their pages (MADV_WILLNEED) and faults them in on every pool worker and the caller in one read-only phase (expert_source.cpp prefetch on its fetch threads), and only then admits first-use experts and computes. The resident source skips the phase. Trace slot 'fetch' times it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
48 lines
3.1 KiB
Rust
48 lines
3.1 KiB
Rust
//! Linux x86_64 Original topology, affinity and actual stack admission.
|
|
use super::{Error,Result};
|
|
#[repr(C)]struct Limit{current:u64,max:u64}
|
|
#[repr(C,align(8))]struct Attr{words:[usize;7]}
|
|
unsafe extern "C" {
|
|
fn getrlimit(resource:i32,limit:*mut Limit)->i32;
|
|
fn pthread_getattr_default_np(attr:*mut Attr)->i32;
|
|
fn pthread_attr_getstacksize(attr:*const Attr,size:*mut usize)->i32;
|
|
fn pthread_attr_destroy(attr:*mut Attr)->i32;
|
|
fn sched_getaffinity(pid:i32,bytes:usize,set:*mut usize)->i32;
|
|
fn pthread_self()->usize;
|
|
fn pthread_setaffinity_np(thread:usize,bytes:usize,set:*const usize)->i32;
|
|
fn sysconf(name:i32)->i64;
|
|
fn madvise(addr:*mut u8,len:usize,advice:i32)->i32;
|
|
}
|
|
pub(super) fn stack_size()->Result<usize> {
|
|
#[cfg(not(target_env="gnu"))] {return Err(Error::Unsupported);}
|
|
#[cfg(target_env="gnu")] unsafe {
|
|
let mut limit=Limit{current:0,max:0};if getrlimit(3,&mut limit)!=0||limit.current==0||limit.current==u64::MAX||limit.current>8*1024*1024{return Err(Error::Unsupported);}
|
|
let mut attr=Attr{words:[0;7]};if pthread_getattr_default_np(&mut attr)!=0{return Err(Error::Unsupported);}
|
|
let mut size=0;let query=pthread_attr_getstacksize(&attr,&mut size);let release=pthread_attr_destroy(&mut attr);
|
|
if query!=0||release!=0||size==0||size>8*1024*1024{Err(Error::Unsupported)}else{Ok(size)}
|
|
}
|
|
}
|
|
fn read_cpu(cpu:i32,key:&str)->i64 {std::fs::read_to_string(format!("/sys/devices/system/cpu/cpu{cpu}/topology/{key}")).ok().and_then(|s|s.split_whitespace().next()?.parse().ok()).unwrap_or(-1)}
|
|
pub(super) fn cores(skip_first:bool)->Vec<i32> {
|
|
let mut set=[0usize;16];let mut allowed=Vec::new();
|
|
// SAFETY: initialized Linux cpu_set_t-sized mask, no pointer retention.
|
|
if unsafe {sched_getaffinity(0,std::mem::size_of_val(&set),set.as_mut_ptr())}==0 {for i in 0..1024 {if set[i/64]&(1usize<<(i%64))!=0{allowed.push(i as i32);}}}
|
|
else {let n=unsafe {sysconf(84)};for i in 0..n.max(0){allowed.push(i as i32);}}
|
|
let mut seen=Vec::new();let mut physical=Vec::new();for cpu in allowed {let key=(read_cpu(cpu,"physical_package_id"),read_cpu(cpu,"core_id"));if key.0>=0&&key.1>=0 {if seen.contains(&key){continue;}seen.push(key);}physical.push(cpu);}
|
|
if skip_first&&!physical.is_empty(){physical.remove(0);}physical
|
|
}
|
|
/// `madvise(MADV_WILLNEED)` over the pages of `bytes`: the kernel starts reading a file-mapped range in the
|
|
/// background (expert_source.cpp `warm`). Advice only; a failure changes nothing.
|
|
pub(super) fn will_need(bytes:&[u8]) {
|
|
if bytes.is_empty(){return;}
|
|
let page=4096usize;let start=bytes.as_ptr() as usize&!(page-1);let end=bytes.as_ptr() as usize+bytes.len();
|
|
// SAFETY: advice over pages that contain the caller's live borrowed range; MADV_WILLNEED (3) never
|
|
// changes the contents or the mapping.
|
|
unsafe {let _=madvise(start as *mut u8,end-start,3);}
|
|
}
|
|
pub(super) fn pin(core:i32) {
|
|
if core<0{return;}let mut set=[0usize;16];if core as usize>=1024{return;}set[core as usize/64]|=1usize<<(core as usize%64);
|
|
// Original intentionally ignores OS affinity refusal; it does not change math.
|
|
unsafe {let _=pthread_setaffinity_np(pthread_self(),std::mem::size_of_val(&set),set.as_ptr());}
|
|
}
|