wip(prefill): d2d/stream-sync/stall timing diagnostics

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-17 10:17:51 +02:00
co-authored by Claude Sonnet 5.5
parent 73e2fdc038
commit 39385de8e5
4 changed files with 34 additions and 17 deletions
+21 -4
View File
@@ -72,10 +72,10 @@ pub(crate) fn note_alloc(what: &str, bytes: usize) {
}
}
const KINDS: usize = 6;
const KINDS: usize = 8;
static BLOCK_NS: [AtomicUsize; KINDS] = [const { AtomicUsize::new(0) }; KINDS];
static BLOCK_N: [AtomicUsize; KINDS] = [const { AtomicUsize::new(0) }; KINDS];
/// Run a host-blocking driver call (0 alloc, 1 free, 2 H2D, 3 D2H, 4 memset, 5 scoped drain), timing it while a chunk
/// Run a host-blocking driver call (0 alloc, 1 free, 2 H2D, 3 D2H, 4 memset, 5 scoped drain, 6 D2D, 7 other stream/event sync), timing it while a chunk
/// stream is in flight.
pub(crate) fn timed<T>(kind: usize, f: impl FnOnce() -> T) -> T {
if background() == 0 {
@@ -83,13 +83,30 @@ pub(crate) fn timed<T>(kind: usize, f: impl FnOnce() -> T) -> T {
}
let started = std::time::Instant::now();
let out = f();
BLOCK_NS[kind].fetch_add(started.elapsed().as_nanos() as usize, Ordering::Relaxed);
let took = started.elapsed();
BLOCK_NS[kind].fetch_add(took.as_nanos() as usize, Ordering::Relaxed);
BLOCK_N[kind].fetch_add(1, Ordering::Relaxed);
// LELEKA_PREFILL_STALL_TRACE=1: every blocking call over 2 ms (first 80) with its callers
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
static SHOWN: AtomicUsize = AtomicUsize::new(0);
if took.as_micros() > 2000
&& *ON.get_or_init(|| std::env::var_os("LELEKA_PREFILL_STALL_TRACE").is_some_and(|v| v == "1"))
&& SHOWN.fetch_add(1, Ordering::Relaxed) < 80
{
let trace = std::backtrace::Backtrace::force_capture().to_string();
let frames: Vec<&str> = trace
.lines()
.filter(|l| l.contains("leleka_cuda_driver::") || l.contains("leleka_runtime::"))
.skip(1)
.take(6)
.collect();
eprintln!("PREFILL-STALL kind={kind} ms={:.1} at {}", took.as_secs_f64() * 1e3, frames.join(" <- "));
}
out
}
/// `alloc n/ms free n/ms h2d n/ms d2h n/ms memset n/ms drain n/ms` since the last call.
pub(crate) fn take_blocking_report() -> String {
let names = ["alloc", "free", "h2d", "d2h", "memset", "drain"];
let names = ["alloc", "free", "h2d", "d2h", "memset", "drain", "d2d", "streamsync"];
(0..KINDS)
.map(|k| {
let n = BLOCK_N[k].swap(0, Ordering::Relaxed);
+8 -8
View File
@@ -2025,9 +2025,9 @@ mod enabled {
// SAFETY: the closed MMQ facade validates actual same-context,
// disjoint whole raw source and padded destination allocations.
// Owners stay borrowed until synchronize/pop, with poison on uncertainty.
check("cuMemcpyDtoD_v2", unsafe {
crate::compute_drain::timed(6, || check("cuMemcpyDtoD_v2", unsafe {
(self.copy_moe_down)(destination.0, source.0, bytes)
})
}))
}
pub(crate) fn copy_moe_down(
&self,
@@ -2038,9 +2038,9 @@ mod enabled {
// a checked ten-slot destination address and exactly10240 bytes.
// Both allocations remain borrowed until sync and context restore;
// ambiguous completion poisons their context before any later free.
check("cuMemcpyDtoD_v2", unsafe {
crate::compute_drain::timed(6, || check("cuMemcpyDtoD_v2", unsafe {
(self.copy_moe_down)(destination.0, source.0, 10_240)
})
}))
}
pub(crate) fn copy_original_down_group(
&self,
@@ -2053,9 +2053,9 @@ mod enabled {
}
// SAFETY: actual affine grouped Down producer and prompt arena
// supply disjoint checked extents; both survive sync and pop.
check("cuMemcpyDtoD_v2", unsafe {
crate::compute_drain::timed(6, || check("cuMemcpyDtoD_v2", unsafe {
(self.copy_moe_down)(destination.0, source.0, bytes)
})
}))
}
pub(crate) fn copy_moe_group_column(
&self,
@@ -2066,9 +2066,9 @@ mod enabled {
// a checked 64-column destination address and exactly2560 bytes.
// Both allocations remain borrowed until sync and context restore;
// ambiguous completion poisons their context before any later free.
check("cuMemcpyDtoD_v2", unsafe {
crate::compute_drain::timed(6, || check("cuMemcpyDtoD_v2", unsafe {
(self.copy_moe_down)(destination.0, source.0, 2560)
})
}))
}
}
}
@@ -150,7 +150,7 @@ mod platform {
check("cuStreamDestroy_v2", unsafe { (self.destroy)(s as Handle) })
}
pub(crate) fn stream_sync(&self, s: usize) -> Result<()> {
check("cuStreamSynchronize", unsafe { (self.sync)(s as Handle) })
crate::compute_drain::timed(7, || check("cuStreamSynchronize", unsafe { (self.sync)(s as Handle) }))
}
pub(crate) fn stream_query(&self, s: usize) -> Result<bool> {
let q = unsafe { (self.query)(s as Handle) };
@@ -201,9 +201,9 @@ mod platform {
}
}
pub(crate) fn event_sync(&self, e: usize) -> Result<()> {
check("cuEventSynchronize", unsafe {
crate::compute_drain::timed(7, || check("cuEventSynchronize", unsafe {
(self.event_sync)(e as Handle)
})
}))
}
pub(crate) fn elapsed(&self, a: usize, b: usize) -> Result<f32> {
let mut ms = 0.;
+2 -2
View File
@@ -380,9 +380,9 @@ mod enabled {
}
pub(crate) fn synchronize(&self) -> Result<()> {
// SAFETY: actual private stream remains owned through this call.
check("cuStreamSynchronize", unsafe {
crate::compute_drain::timed(7, || check("cuStreamSynchronize", unsafe {
(self.sync)(self.stream() as *mut c_void)
})
}))
}
pub(crate) fn close(mut self) -> Result<()> {
self.close_inner()