Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 120 additions & 44 deletions helios/src/runtime_core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ pub struct NativeResources {
integrated: bool,
events: HashMap<String, Handle>,
cache: Option<flare_selene::CacheGeometry>,
#[cfg(windows)]
host_mappings: Vec<(u64, usize)>,
green_partition: Option<persistent_worker::GreenContextPartition>,
persistent_worker: Option<Arc<persistent_worker::PersistentWorkerRuntime>>,
Expand Down Expand Up @@ -421,7 +420,6 @@ impl NativeResources {
integrated: integrated != 0,
events: HashMap::new(),
cache: None,
#[cfg(windows)]
host_mappings: Vec::new(),
green_partition: None,
persistent_worker: None,
Expand Down Expand Up @@ -547,6 +545,118 @@ impl NativeResources {
// Every CUDA resource is immediately retained by self for error cleanup.
unsafe {
driver.check("cuCtxSetCurrent", (driver.context_set)(self.context))?;
// Coalesce contiguous fixed MappedHost allocations into shared VMAs before pinning so
// transparent huge pages can span layer boundaries without losing alignment edges.
#[cfg(unix)]
{
let mut spans: Vec<(u64, usize, u32)> = core
.allocations
.iter()
.filter(|a| {
a.sizing.is_none()
&& matches!(a.memory, Memory::MappedHost)
&& a.nbytes > 0
&& !self.allocations.get(&a.name).is_some_and(|s| s.nbytes > 0)
})
.filter_map(|a| {
let addr = a.address?;
let flags = if self.peer_readers.contains_key(&addr) { 3 } else { 2 };
Some((addr, a.nbytes, flags))
})
.collect();
spans.sort_unstable_by_key(|&(addr, _, _)| addr);
let mut merged: Vec<(u64, usize, u32)> = Vec::with_capacity(spans.len());
for (addr, nbytes, flags) in spans {
if let Some(last) = merged.last_mut()
&& last.2 == flags
&& last.0.checked_add(last.1 as u64) == Some(addr)
{
last.1 = last
.1
.checked_add(nbytes)
.ok_or_else(|| invalid("coalesced mapped host size overflow"))?;
} else {
merged.push((addr, nbytes, flags));
}
}
let mut mapped_spans: Vec<(u64, usize, u32)> = Vec::with_capacity(merged.len());
let unmap_unregistered = |spans: &[(u64, usize, u32)]| {
for &(addr, bytes, _) in spans {
libc::munmap(addr as Handle, bytes);
}
};
for (span_addr, span_bytes, flags) in merged {
let mapped = libc::mmap(
span_addr as Handle,
span_bytes,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_FIXED_NOREPLACE,
-1,
0,
);
if mapped != span_addr as Handle {
let error = std::io::Error::last_os_error();
unmap_unregistered(&mapped_spans);
return Err(invalid(format!(
"host address collision at {span_addr:#x}: {error}"
)));
}
#[cfg(target_os = "linux")]
{
// Best-effort transparent huge page hint before pinning so unified-memory
// GPU replay walks fewer TLB entries.
let _ = libc::madvise(mapped, span_bytes, libc::MADV_HUGEPAGE);
}
mapped_spans.push((span_addr, span_bytes, flags));
}
#[cfg(target_os = "linux")]
{
// Pre-fault at 2 MiB stride in package allocation order before
// cuMemHostRegister_v2 pins the VMA: the first touch in a 512 MiB PMD faults
// the whole PMD when available, 2 MiB stride also pre-faults sub-PMD mTHP
// folios (2 MiB..256 MiB) when enabled, and preserving allocation order keeps
// scarce PMDs aligned with the exporter's layout priority.
const PREFAULT_STRIDE: usize = 2 * 1024 * 1024;
for a in &core.allocations {
if a.sizing.is_some()
|| !matches!(a.memory, Memory::MappedHost)
|| a.nbytes == 0
|| self.allocations.get(&a.name).is_some_and(|s| s.nbytes > 0)
{
continue;
}
let Some(addr) = a.address else { continue };
let start_addr = addr as usize;
let end_addr = start_addr + a.nbytes;
let mut cur = (start_addr + PREFAULT_STRIDE - 1) & !(PREFAULT_STRIDE - 1);
while cur < end_addr {
std::ptr::write_volatile(cur as *mut u8, 0);
cur = match cur.checked_add(PREFAULT_STRIDE) {
Some(next) => next,
None => break,
};
}
}
}
for (index, &(span_addr, span_bytes, flags)) in mapped_spans.iter().enumerate() {
let mapped = span_addr as Handle;
// Registration does not pin host memory for an integrated GPU, and a
// swapped-out page turns each GPU access during replay into a fault.
if self.integrated && libc::mlock(mapped, span_bytes) != 0 {
let error = std::io::Error::last_os_error();
unmap_unregistered(&mapped_spans[index..]);
return Err(invalid(format!(
"cannot lock {span_bytes} bytes of mapped host storage: {error}; raise RLIMIT_MEMLOCK"
)));
}
let status = (driver.host_register)(mapped, span_bytes, flags);
if status != 0 {
unmap_unregistered(&mapped_spans[index..]);
}
driver.check("cuMemHostRegister_v2", status)?;
self.host_mappings.push((span_addr, span_bytes));
}
}
let footprint = core
.allocations
.iter()
Expand Down Expand Up @@ -619,40 +729,7 @@ impl NativeResources {
driver.check("cuMemMap", mapped)?;
}
Memory::MappedHost => {
#[cfg(unix)]
{
let mapped = libc::mmap(
address as Handle,
allocation.nbytes,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_PRIVATE
| libc::MAP_ANONYMOUS
| libc::MAP_FIXED_NOREPLACE,
-1,
0,
);
if mapped != address as Handle {
return Err(invalid(format!(
"host address collision at {address:#x}: {}",
std::io::Error::last_os_error()
)));
}
// Registration does not pin host memory for an integrated GPU, and a
// swapped-out page turns each GPU access during replay into a fault.
if self.integrated && libc::mlock(mapped, allocation.nbytes) != 0 {
let error = std::io::Error::last_os_error();
libc::munmap(mapped, allocation.nbytes);
return Err(invalid(format!(
"cannot lock {} bytes of mapped host storage: {error}; raise RLIMIT_MEMLOCK",
allocation.nbytes
)));
}
let status = (driver.host_register)(mapped, allocation.nbytes, 2);
if status != 0 {
libc::munmap(mapped, allocation.nbytes);
}
driver.check("cuMemHostRegister_v2", status)?;
}
// Mapped, pre-faulted, and registered in coalesced spans above.
}
// The owning rank maps peer storage and grants this rank access.
Memory::Peer => {}
Expand Down Expand Up @@ -1583,19 +1660,18 @@ impl NativeResources {
(self.driver.unmap)(address, bytes);
}
}
(Memory::MappedHost, _) => {
#[cfg(unix)]
{
(self.driver.host_unregister)(allocation.address as Handle);
libc::munmap(allocation.address as Handle, allocation.nbytes);
}
}
(Memory::MappedHost, _) => {}
(Memory::Peer, _) => {}
}
}
#[cfg(windows)]
for (address, bytes) in self.host_mappings.drain(..).rev() {
#[cfg(windows)]
(self.driver.unmap)(address, bytes);
#[cfg(unix)]
{
(self.driver.host_unregister)(address as Handle);
libc::munmap(address as Handle, bytes);
}
}
self.persistent_worker = None;
if had_previous && previous != self.context {
Expand Down
Loading
Loading