diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-28 01:08:13 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-28 01:08:13 -0500 |
| commit | 6fd734ee84000ed7d08f186b9c5c6dfcbc43a7ce (patch) | |
| tree | 0cb72dd86d02448fcab477f8e3c7ba18ee5c8446 /runtime/test/submit_frame_test.cpp | |
| parent | 3007ae0d357da7c6ec015606788a5743a7abfed3 (diff) | |
| parent | 758b98756ca86dd23d756a39973ca836be6a342d (diff) | |
Merge branch 'r6/queue-contention' into test/round6
Diffstat (limited to 'runtime/test/submit_frame_test.cpp')
| -rw-r--r-- | runtime/test/submit_frame_test.cpp | 196 |
1 files changed, 195 insertions, 1 deletions
diff --git a/runtime/test/submit_frame_test.cpp b/runtime/test/submit_frame_test.cpp index 4e2f59be6..71417da31 100644 --- a/runtime/test/submit_frame_test.cpp +++ b/runtime/test/submit_frame_test.cpp | |||
| @@ -127,8 +127,30 @@ int scripted() { | |||
| 127 | EXPECT(load(f.phase_ns[sp::shadows]) >= 2 * ms); | 127 | EXPECT(load(f.phase_ns[sp::shadows]) >= 2 * ms); |
| 128 | EXPECT(load(f.part_ns[sp::part_lock_wait]) >= 2 * ms); | 128 | EXPECT(load(f.part_ns[sp::part_lock_wait]) >= 2 * ms); |
| 129 | EXPECT(load(f.part_ns[sp::part_lists]) < load(f.phase_ns[sp::shadows])); | 129 | EXPECT(load(f.part_ns[sp::part_lists]) < load(f.phase_ns[sp::shadows])); |
| 130 | // Contended lock_timed inside a WorkerScope bills worker_lock_wait_ns without polluting title-thread lock waits. | ||
| 131 | { | ||
| 132 | const std::uint64_t title_waits_before = load(f.part_ns[sp::part_lock_wait]); | ||
| 133 | std::mutex worker_m; | ||
| 134 | std::atomic<bool> held_flag{false}; | ||
| 135 | std::thread holder([&] { | ||
| 136 | const std::lock_guard held(worker_m); | ||
| 137 | held_flag.store(true, std::memory_order_release); | ||
| 138 | std::this_thread::sleep_for(std::chrono::milliseconds(4)); | ||
| 139 | }); | ||
| 140 | while (!held_flag.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 141 | std::thread worker([&] { | ||
| 142 | const sp::WorkerScope busy(sp::worker_submit); | ||
| 143 | std::unique_lock lock(worker_m, std::defer_lock); | ||
| 144 | sp::lock_timed(lock); | ||
| 145 | }); | ||
| 146 | worker.join(); | ||
| 147 | holder.join(); | ||
| 148 | EXPECT(load(f.worker_lock_wait_ns) >= 2 * ms); | ||
| 149 | EXPECT(load(f.part_ns[sp::part_lock_wait]) == title_waits_before); | ||
| 150 | } | ||
| 130 | sp::frame_lines(1.0); | 151 | sp::frame_lines(1.0); |
| 131 | std::printf("scripted: frame sums, title and own threads, worst frame and floor as scripted\n"); | 152 | EXPECT(load(f.worker_lock_wait_ns) == 0); |
| 153 | std::printf("scripted: frame sums, title and own threads, worker lock waits, worst frame and floor as scripted\n"); | ||
| 132 | return 0; | 154 | return 0; |
| 133 | } | 155 | } |
| 134 | 156 | ||
| @@ -1822,6 +1844,178 @@ int queue_sync_and_async() { | |||
| 1822 | std::printf("queue execute+signal coalescing: unwaited merge, 17-batch burst, cross-queue Wait split, Present split & error fallback ok\n"); | 1844 | std::printf("queue execute+signal coalescing: unwaited merge, 17-batch burst, cross-queue Wait split, Present split & error fallback ok\n"); |
| 1823 | } | 1845 | } |
| 1824 | 1846 | ||
| 1847 | // 12. Round 6 queue contention fixes: | ||
| 1848 | // (a) 24 Wait-separated batches within one frame on depth=4 enqueue with zero room_cv_ stalls; | ||
| 1849 | // (b) Distinct Present-separated frames still throttle cleanly at depth=4 (bounded backpressure); | ||
| 1850 | // (c) Held-only Compute queue executions via release_held() are counted in async_run_ns (worker busy %); | ||
| 1851 | // (d) Visibility epochs (current_before_execute_seq / signaled_visibility_seq) allow safe single-flight | ||
| 1852 | // before_execute_() coalescing across concurrent/back-to-back queues while NEVER skipping a refresh | ||
| 1853 | // when a Wait-before-Signal CPU Fence::Signal lands after the previous refresh started. | ||
| 1854 | { | ||
| 1855 | VulkanContext context(d.device_proc, d.device, true); | ||
| 1856 | EXPECT(context.loaded()); | ||
| 1857 | VulkanQueue direct(context, d.queue, d.family, VulkanQueue::QueueKind::direct); | ||
| 1858 | VulkanQueue compute(context, d.queue, d.family, VulkanQueue::QueueKind::compute); | ||
| 1859 | EXPECT(direct.ok() && compute.ok()); | ||
| 1860 | direct.set_async_depth(4); | ||
| 1861 | compute.set_async_depth(4); | ||
| 1862 | |||
| 1863 | auto empty = std::make_shared<CommandList>(); | ||
| 1864 | EXPECT(empty->close()); | ||
| 1865 | std::shared_ptr<const CommandList> snap = empty; | ||
| 1866 | |||
| 1867 | // (a) 24 Wait-separated batches within a single present_epoch on depth=4: zero room_cv_ stalls! | ||
| 1868 | std::atomic<bool> block_first{true}, in_first{false}; | ||
| 1869 | context.set_before_execute([&] { | ||
| 1870 | if (block_first.exchange(false, std::memory_order_acq_rel)) { | ||
| 1871 | in_first.store(true, std::memory_order_release); | ||
| 1872 | while (in_first.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 1873 | } | ||
| 1874 | }); | ||
| 1875 | VulkanFence intra_f(context, 0), done_a(context, 0); | ||
| 1876 | EXPECT(intra_f.semaphore() && done_a.semaphore()); | ||
| 1877 | EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); | ||
| 1878 | while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 1879 | |||
| 1880 | std::atomic<bool> burst24_done{false}; | ||
| 1881 | std::thread producer24([&] { | ||
| 1882 | bool ok = true; | ||
| 1883 | for (int i = 1; i <= 24 && ok; ++i) { | ||
| 1884 | direct.wait(intra_f.semaphore(), std::uint64_t(i - 1)); | ||
| 1885 | ok = (direct.execute({&snap, 1}) == ReplayResult::success) && | ||
| 1886 | direct.signal(intra_f.semaphore(), std::uint64_t(i)); | ||
| 1887 | } | ||
| 1888 | ok = ok && direct.signal(done_a.semaphore(), 1); | ||
| 1889 | if (ok) burst24_done.store(true, std::memory_order_release); | ||
| 1890 | }); | ||
| 1891 | const auto d24 = std::chrono::steady_clock::now() + std::chrono::milliseconds(500); | ||
| 1892 | while (!burst24_done.load(std::memory_order_acquire) && std::chrono::steady_clock::now() < d24) | ||
| 1893 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
| 1894 | const bool non_stalling_24 = burst24_done.load(std::memory_order_acquire); | ||
| 1895 | in_first.store(false, std::memory_order_release); | ||
| 1896 | producer24.join(); | ||
| 1897 | EXPECT(non_stalling_24); | ||
| 1898 | EXPECT(done_a.wait(1, 5'000'000'000ull)); | ||
| 1899 | EXPECT(direct.idle()); | ||
| 1900 | EXPECT(direct.async_room_waits_for_test() == 0); | ||
| 1901 | |||
| 1902 | // (b) Distinct Present frames on depth=4 still throttle when 4 frames are already queued/running. | ||
| 1903 | block_first.store(true, std::memory_order_release); | ||
| 1904 | in_first.store(false, std::memory_order_release); | ||
| 1905 | VulkanFence done_b(context, 0); | ||
| 1906 | EXPECT(done_b.semaphore()); | ||
| 1907 | EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); | ||
| 1908 | while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 1909 | |||
| 1910 | std::atomic<unsigned> frames_queued{0}; | ||
| 1911 | std::thread frame_producer([&] { | ||
| 1912 | for (unsigned f = 1; f <= 5; ++f) { | ||
| 1913 | { | ||
| 1914 | const sp::TitleCall pres(sp::part_present); | ||
| 1915 | } | ||
| 1916 | (void)direct.execute({&snap, 1}); | ||
| 1917 | frames_queued.store(f, std::memory_order_release); | ||
| 1918 | } | ||
| 1919 | (void)direct.signal(done_b.semaphore(), 1); | ||
| 1920 | }); | ||
| 1921 | std::this_thread::sleep_for(std::chrono::milliseconds(20)); | ||
| 1922 | // Worker busy (1) + 3 distinct present_epoch frames in inbox_ = 4 == depth: 4th queued frame must wait! | ||
| 1923 | EXPECT(frames_queued.load(std::memory_order_acquire) == 3); | ||
| 1924 | in_first.store(false, std::memory_order_release); | ||
| 1925 | frame_producer.join(); | ||
| 1926 | EXPECT(done_b.wait(1, 5'000'000'000ull)); | ||
| 1927 | EXPECT(direct.idle()); | ||
| 1928 | EXPECT(direct.async_room_waits_for_test() >= 1); | ||
| 1929 | |||
| 1930 | // (c) Held-only Compute queue execution via release_held() is measured in async_run_ns (worker busy %). | ||
| 1931 | context.set_before_execute([] { std::this_thread::sleep_for(std::chrono::milliseconds(10)); }); | ||
| 1932 | VulkanFence gate_c(context, 0), done_c(context, 0); | ||
| 1933 | EXPECT(gate_c.semaphore() && done_c.semaphore()); | ||
| 1934 | compute.wait(gate_c.semaphore(), 1); | ||
| 1935 | EXPECT(compute.execute({&snap, 1}) == ReplayResult::success); | ||
| 1936 | EXPECT(compute.signal(done_c.semaphore(), 1)); | ||
| 1937 | EXPECT(compute.idle()); // flushed into held_ while gate_c is unmet | ||
| 1938 | const std::uint64_t run_before = compute.async_run_ns_for_test(); | ||
| 1939 | EXPECT(gate_c.signal(1)); | ||
| 1940 | EXPECT(done_c.wait(1, 5'000'000'000ull)); | ||
| 1941 | EXPECT(compute.idle()); | ||
| 1942 | const std::uint64_t run_after = compute.async_run_ns_for_test(); | ||
| 1943 | EXPECT(run_after - run_before >= 5 * ms); | ||
| 1944 | |||
| 1945 | // (d) Visibility epoch contract for single-flight before_execute_() coalescing + Wait-before-Signal safety. | ||
| 1946 | std::mutex coal_m; | ||
| 1947 | std::uint64_t coal_completed = 0; | ||
| 1948 | std::atomic<unsigned> coal_ran{0}, coal_skipped{0}; | ||
| 1949 | std::atomic<bool> hold_lead{false}, in_lead{false}, coal_bad_req{false}; | ||
| 1950 | std::atomic<int> cpu_data{0}, observed_data{0}; | ||
| 1951 | context.set_before_execute([&] { | ||
| 1952 | const std::uint64_t req = VulkanContext::current_before_execute_seq(); | ||
| 1953 | if (req == 0) { | ||
| 1954 | coal_bad_req.store(true, std::memory_order_relaxed); | ||
| 1955 | return; | ||
| 1956 | } | ||
| 1957 | std::unique_lock lock(coal_m); | ||
| 1958 | if (coal_completed >= req) { | ||
| 1959 | coal_skipped.fetch_add(1, std::memory_order_relaxed); | ||
| 1960 | return; | ||
| 1961 | } | ||
| 1962 | const std::uint64_t pass = (std::max)(req, context.current_visibility_seq()); | ||
| 1963 | lock.unlock(); | ||
| 1964 | if (hold_lead.exchange(false, std::memory_order_acq_rel)) { | ||
| 1965 | in_lead.store(true, std::memory_order_release); | ||
| 1966 | while (in_lead.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 1967 | } | ||
| 1968 | observed_data.store(cpu_data.load(std::memory_order_acquire), std::memory_order_release); | ||
| 1969 | coal_ran.fetch_add(1, std::memory_order_relaxed); | ||
| 1970 | lock.lock(); | ||
| 1971 | if (pass > coal_completed) coal_completed = pass; | ||
| 1972 | }); | ||
| 1973 | |||
| 1974 | // Case d1: Back-to-back direct + compute enqueued before direct's pass starts -> compute coalesces cleanly! | ||
| 1975 | VulkanFence cross_d(context, 0), comp_done_d(context, 0); | ||
| 1976 | EXPECT(cross_d.semaphore() && comp_done_d.semaphore()); | ||
| 1977 | hold_lead.store(true, std::memory_order_release); | ||
| 1978 | EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); // batch 0 holds lead | ||
| 1979 | while (!in_lead.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 1980 | |||
| 1981 | cpu_data.store(42, std::memory_order_release); | ||
| 1982 | EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); | ||
| 1983 | EXPECT(direct.signal(cross_d.semaphore(), 1)); | ||
| 1984 | compute.wait(cross_d.semaphore(), 1); | ||
| 1985 | EXPECT(compute.execute({&snap, 1}) == ReplayResult::success); | ||
| 1986 | EXPECT(compute.signal(comp_done_d.semaphore(), 1)); | ||
| 1987 | EXPECT(compute.idle()); // compute batch now held waiting for cross_d | ||
| 1988 | |||
| 1989 | in_lead.store(false, std::memory_order_release); | ||
| 1990 | EXPECT(comp_done_d.wait(1, 5'000'000'000ull)); | ||
| 1991 | EXPECT(direct.idle() && compute.idle()); | ||
| 1992 | EXPECT(observed_data.load(std::memory_order_acquire) == 42); | ||
| 1993 | EXPECT(coal_skipped.load(std::memory_order_acquire) >= 1); | ||
| 1994 | |||
| 1995 | // Case d2: Wait-before-Signal with CPU write + CPU Fence::Signal AFTER prior pass completed MUST NOT skip! | ||
| 1996 | VulkanFence cpu_gate(context, 0), comp_done_d2(context, 0); | ||
| 1997 | EXPECT(cpu_gate.semaphore() && comp_done_d2.semaphore()); | ||
| 1998 | compute.wait(cpu_gate.semaphore(), 1); | ||
| 1999 | EXPECT(compute.execute({&snap, 1}) == ReplayResult::success); | ||
| 2000 | EXPECT(compute.signal(comp_done_d2.semaphore(), 1)); | ||
| 2001 | EXPECT(compute.idle()); // held in compute.held_ | ||
| 2002 | |||
| 2003 | // Run a direct pass now: coal_completed advances past compute's enqueue visibility_seq! | ||
| 2004 | EXPECT(direct.execute({&snap, 1}) == ReplayResult::success); | ||
| 2005 | EXPECT(direct.idle()); | ||
| 2006 | const unsigned ran_before_cpu_sig = coal_ran.load(std::memory_order_acquire); | ||
| 2007 | |||
| 2008 | // Title writes new CPU data and signals cpu_gate from CPU: compute MUST run a fresh refresh and see 99! | ||
| 2009 | cpu_data.store(99, std::memory_order_release); | ||
| 2010 | EXPECT(cpu_gate.signal(1)); | ||
| 2011 | EXPECT(comp_done_d2.wait(1, 5'000'000'000ull)); | ||
| 2012 | EXPECT(compute.idle()); | ||
| 2013 | EXPECT(!coal_bad_req.load(std::memory_order_acquire)); | ||
| 2014 | EXPECT(coal_ran.load(std::memory_order_acquire) == ran_before_cpu_sig + 1); | ||
| 2015 | EXPECT(observed_data.load(std::memory_order_acquire) == 99); | ||
| 2016 | std::printf("queue r6 contention: 24-wait frame 0 room stalls, frame-group bound, held run_ns & visibility coalescing ok\n"); | ||
| 2017 | } | ||
| 2018 | |||
| 1825 | return 0; | 2019 | return 0; |
| 1826 | } | 2020 | } |
| 1827 | 2021 | ||