From 09a79bdb278cd1bb7e36c18f8fbfa5c0336880c1 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 10 Nov 2026 05:05:46 -0800 Subject: [PATCH] scheduler_wakes: wait for the scheduler's thread to sleep, not for 5 ms Three scheduler_wakes tests need the scheduler's thread asleep before a commit: each burst's commits, each commit_and_submit, and the commit under the held lock. Each slept 5 ms and assumed the thread was asleep by then. The thread can outlast that, because the last commit's waking and pass run on a thread that load beside the test can hold off the CPU. - A commit_and_submit that finds the thread making a pass leaves its batch to the thread, so batches submitted at commit fall short of the commits. - One that finds the thread awake between passes wakes nothing, because the notify is spared, so the commit wakings fall short. The PHOENIX sighting (bug 748, "scheduler_wakes 9/10") has the first shape. Scheduler::asleep, compiled only under the device-test cfgs, reads the thread's sleep state under the lock of its sleep. It is true when the thread sleeps on its condition variable or on the GPU and no waker has woken it since. The tests wait for it, bounded by BOUND, before each commit that needs a sleeping thread. No runtime behaviour changes. The runtime-sources pin moves because scheduler/mod.rs gains the test-only accessor. RUNTIME_VERSION is left to the pin cut. Verified on the Radeon 780M PHOENIX (RADV, Mesa 26.2.6), potluck job 13644, with this commit carried on the scratch branch at fbfb62949e. A scratch switch there holds the thread 10 ms after each pass before it sleeps, holding no lock. - With that delay and the 5 ms wait, a_commit_and_submit_wakes... failed 5 of 5 runs. Run 1: "0 wakings by a commit, 9 batches submitted at commit, 10 commits". Runs 2-5: 0 wakings, 10 at commit. - With the delay and this wait: 5 of 5 passed, each with 10 wakings and 10 at commit. - Without the delay: both waits passed 5 of 5. - The whole module with this wait, run alone: 10 of 10. - Before the change, full suites on the 780M (jobs 13609, 13631) passed all four tests, 2 of 2. (cherry picked from commit 346f32bad36fb752caee2a991efe1b670a59b112) --- .../metal2vulkan-runtime/src/scheduler/mod.rs | 8 +++++ .../src/tests/scheduler_wakes.rs | 36 ++++++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/scheduler/mod.rs b/crates/metal2vulkan-runtime/src/scheduler/mod.rs index 409cc568d..911fc7a1a 100644 --- a/crates/metal2vulkan-runtime/src/scheduler/mod.rs +++ b/crates/metal2vulkan-runtime/src/scheduler/mod.rs @@ -1134,6 +1134,14 @@ impl Scheduler { ) } + /// Whether the thread sleeps, on its condition variable or on the GPU, and no waker woke it + /// since ([`Sleep`]): a test that wakes it waits for this, rather than for a time. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) fn asleep(&self) -> bool { + let sleep = lock(&self.shared.sleep); + sleep.asleep || sleep.on_gpu.is_some() + } + /// The passes the thread made so far (`WorkCounts::scheduler_passes`). pub(crate) fn passes(&self) -> u64 { self.shared.passes.load(Ordering::Relaxed) diff --git a/crates/metal2vulkan-runtime/src/tests/scheduler_wakes.rs b/crates/metal2vulkan-runtime/src/tests/scheduler_wakes.rs index 39e49bcf9..7c39b8d20 100644 --- a/crates/metal2vulkan-runtime/src/tests/scheduler_wakes.rs +++ b/crates/metal2vulkan-runtime/src/tests/scheduler_wakes.rs @@ -98,6 +98,19 @@ fn settle(completed: &AtomicU64, count: u64, what: &str) { } } +/// Wait until the scheduler's thread sleeps (`Scheduler::asleep`), failing where it stays awake +/// for [`BOUND`]: what the last commit or completion set going is over, and no pass is under way. +fn asleep(device: &Device, what: &str) { + let since = Instant::now(); + while !device.shared().scheduler.asleep() { + assert!( + since.elapsed() < BOUND, + "{what}: the scheduler's thread stayed awake for {BOUND:?}" + ); + std::thread::sleep(Duration::from_micros(50)); + } +} + /// The wakings the thread's notifies and nudges count between `before` and `after`, and the /// sleeps they may end: those the thread began between the two, and the one it may have been in /// as `before` was read, counted as it began, before `before`. A waking of that one is counted in @@ -116,8 +129,8 @@ fn wakings_and_sleeps(before: &WorkCounts, after: &WorkCounts) -> (u64, u64) { ) } -/// Bursts of commits made faster than the scheduler's thread wakes, each after an idle gap: the -/// thread is notified at most once a sleep. +/// Bursts of commits made faster than the scheduler's thread wakes, each once the thread sleeps +/// ([`asleep`]): the thread is notified at most once a sleep. #[test] fn a_burst_of_commits_wakes_the_sleeping_thread_once() { let device = device(); @@ -126,8 +139,7 @@ fn a_burst_of_commits_wakes_the_sleeping_thread_once() { let completed = Arc::new(AtomicU64::new(0)); let before = device.work_counts(); for burst in 0..10u64 { - // The thread sleeps by now. - std::thread::sleep(Duration::from_millis(5)); + asleep(&device, &format!("burst {burst}")); let committed: Vec = (0..BURST) .map(|at| counted_fill(&queue, &target, at as u8, &completed)) .collect(); @@ -202,10 +214,11 @@ fn commits_with_idle_gaps_lose_no_wake() { ); } -/// Commits made with `commit_and_submit` alone, each after an idle gap and each submitted by its -/// committing thread: where the device's batches may wait in a group (`CompletionPolicy::Grouped` -/// and its check found the merge, `command_buffer/group.rs`), each wakes the sleeping thread once, -/// counted as a commit's waking; elsewhere none wakes it. +/// Commits made with `commit_and_submit` alone, each once the thread sleeps ([`asleep`], not after +/// a time, which the last commit's waking and pass may outlast under load) and each submitted by +/// its committing thread: where the device's batches may wait in a group +/// (`CompletionPolicy::Grouped` and its check found the merge, `command_buffer/group.rs`), each +/// wakes the sleeping thread once, counted as a commit's waking; elsewhere none wakes it. #[test] fn a_commit_and_submit_wakes_the_sleeping_thread_only_for_a_batch_that_may_group() { const COMMITS: u64 = 10; @@ -226,8 +239,7 @@ fn a_commit_and_submit_wakes_the_sleeping_thread_only_for_a_batch_that_may_group settle(&completed, 1, "the first"); let before = device.work_counts(); for at in 1..=COMMITS { - // The thread sleeps by now. - std::thread::sleep(Duration::from_millis(5)); + asleep(&device, &format!("commit {at}")); let command_buffer = counted_fill(&queue, &target, at as u8, &completed); command_buffer.commit_and_submit().unwrap(); settle(&completed, at + 1, &format!("commit {at}")); @@ -278,8 +290,8 @@ fn a_commit_returns_while_the_lock_a_submission_holds_is_held() { }) }; holding.recv_timeout(BOUND).unwrap(); - // The thread sleeps by now: no pass is under way. - std::thread::sleep(Duration::from_millis(5)); + // No pass is under way. + asleep(&device, "before the lock"); let (returned, committer) = device.shared().scheduler.under_lock(|| { let (done, returns) = mpsc::channel(); let committer = std::thread::spawn(move || {