From ad3a6cc39b2f4e5fffb288ffd6161444560cd2fe Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 19 Nov 2026 22:54:39 -0800 Subject: [PATCH] :racehorse: Wake the pool's waiters once the zeroer lets go of its lock The zeroer woke the pool's waiters at a chunk's mark with the pool's lock still held, so the wake was part of a hold every take queues behind, and a zeroer preempted between its wake and its unlock would keep them all waiting. The wake now comes once the lock is let go of: the mark is in the state a woken waiter reads either way. The mark's measured part now runs through the choice of whether to wake, so the hold counts cover all the zeroer does under the lock after a write. The 17.4 ms hold a 3090 walk showed lay in the mark itself (State::zeroed), before the wake, with no take waiting over 0.35 ms in that walk. --- crates/reims-vgpu/src/pool.rs | 5 +++-- crates/reims-vgpu/src/pool/takes.rs | 14 +++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/reims-vgpu/src/pool.rs b/crates/reims-vgpu/src/pool.rs index 5aa858190..8af99bca5 100644 --- a/crates/reims-vgpu/src/pool.rs +++ b/crates/reims-vgpu/src/pool.rs @@ -123,8 +123,9 @@ pub const ZERO_HOLD_MAX_NS_CODE: &str = "pool-zero-hold-max-ns"; /// The codes of the longest each part of the zeroer's hold of the pool's lock for one chunk took /// so far (`takes`, "Zeros ahead"), in nanoseconds, their counts rising to it: the chunk's pick -/// and claim (`State::next_zero`), and its mark as reading zero (`State::zeroed`). Its write lies -/// between them, with the lock let go of. +/// and claim (`State::next_zero`), and its mark as reading zero (`State::zeroed`) with the choice +/// of whether to wake the pool's waiters. Its write lies between them, and the wake after the +/// mark, each with the lock let go of. pub const ZERO_HOLD_PART_MAX_NS_CODES: [&str; 2] = ["pool-zero-hold-pick-max-ns", "pool-zero-hold-mark-max-ns"]; diff --git a/crates/reims-vgpu/src/pool/takes.rs b/crates/reims-vgpu/src/pool/takes.rs index a866033d9..d80307a14 100644 --- a/crates/reims-vgpu/src/pool/takes.rs +++ b/crates/reims-vgpu/src/pool/takes.rs @@ -40,7 +40,8 @@ //! a [`super::PieceHold`] does ([`State::next_zero`]), lets the lock go while it writes the //! chunk's zeros through the piece's mapping, then, the lock held again, records the chunk in //! the piece as reading zero ([`Ranges`]) and lets the claim and the piece go -//! ([`State::zeroed`]). Counted: its bytes ([`super::ZEROED_AHEAD_BYTES_CODE`]), its writes' +//! ([`State::zeroed`]), and wakes the pool's waiters, where one waits for a mark, once the lock +//! is let go of again. Counted: its bytes ([`super::ZEROED_AHEAD_BYTES_CODE`]), its writes' //! nanoseconds in all and the longest ([`super::ZERO_WRITE_NS_CODE`]), the longest it held the //! lock for a chunk ([`super::ZERO_HOLD_MAX_NS_CODE`]) with the longest of each of its parts, the //! pick and the mark, and each hold longer than [`super::ZERO_HOLD_LONG`] with its parts' time @@ -1142,7 +1143,6 @@ impl Shared { }); let marking = (self.now)(); st.zeroed(piece, range); - let marked = (self.now)(); #[cfg(test)] { st.zeroed_by[1] = st.zeroed_by[1].saturating_add(len); @@ -1151,14 +1151,18 @@ impl Shared { // for the hold on a piece on its way out or at the release wait for a mark; a wake // of the rest at each chunk would cost the lock's other users for nothing. let gone = !(st.pieces.get(&piece)).is_some_and(|p| p.usable()); - if st.claim_waits > 0 || st.closed || gone { - self.changed.notify_all(); - } + let wake = st.claim_waits > 0 || st.closed || gone; + let marked = (self.now)(); let parts = [ picked.saturating_duration_since(picking), marked.saturating_duration_since(marking), ]; MutexGuard::unlocked(&mut st, || { + // The wake comes once the lock is let go of: a waiter it wakes finds the lock + // free, never the zeroer holding it, and the mark is in the state it reads. + if wake { + self.changed.notify_all(); + } count_kind_by(ZEROED_AHEAD_BYTES_CODE, len); zero_written(written); zero_held(parts);