upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/submit_mutex_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/submit_mutex_test.cpp')
-rw-r--r--runtime/test/submit_mutex_test.cpp209
1 files changed, 209 insertions, 0 deletions
diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp
new file mode 100644
index 000000000..345f30887
--- /dev/null
+++ b/runtime/test/submit_mutex_test.cpp
@@ -0,0 +1,209 @@
1// leonida::SubmitMutex (src/vulkan_device_fault.h), on the CPU: the submit lock's owner and hold tracking, its waiter
2// count, let_submit_finish (the handoff a pipeline create makes to submits before entering the driver) and the
3// compile gate between pipeline creates and queue submissions. Windows only: the owner is a Win32 thread id. No GPU,
4// no game data.
5#include "../src/vulkan_device_fault.h"
6
7#include <windows.h>
8
9#include <atomic>
10#include <chrono>
11#include <cstdio>
12#include <mutex>
13#include <thread>
14
15#define EXPECT(condition) do { \
16 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
17} while (0)
18
19namespace {
20
21using leonida::SubmitMutex;
22using namespace std::chrono_literals;
23
24std::int64_t now_ms() { return leonida::diagnostic_ms(); }
25
26// Polls `done` for up to two seconds.
27template<class Done> bool eventually(Done&& done) {
28 for (int i = 0; i < 2000; ++i) {
29 if (done()) return true;
30 std::this_thread::sleep_for(1ms);
31 }
32 return done();
33}
34
35int owner_tracking() {
36 SubmitMutex m;
37 EXPECT(m.owner() == 0 && m.waiting() == 0);
38 const std::int64_t before = now_ms();
39 m.lock();
40 EXPECT(m.owner() == GetCurrentThreadId());
41 EXPECT(m.since_ms() >= before && m.since_ms() <= now_ms());
42 // The holder's own thread: nothing to let through, no wait (it would deadlock on the lock it holds).
43 EXPECT(m.let_submit_finish(true) == 0);
44 EXPECT(m.let_submit_finish(false) == 0);
45 m.unlock();
46 EXPECT(m.owner() == 0);
47 EXPECT(m.try_lock());
48 EXPECT(m.owner() == GetCurrentThreadId());
49 m.unlock();
50 // Free: no wait at all.
51 EXPECT(m.let_submit_finish(true) == 0);
52 std::printf("owner and hold tracking, holder's own thread returns at once\n");
53 return 0;
54}
55
56int waits_for_holder() {
57 SubmitMutex m;
58 std::atomic<bool> held{false}, released{false};
59 std::thread holder([&] {
60 m.lock();
61 held = true;
62 std::this_thread::sleep_for(200ms);
63 released = true;
64 m.unlock();
65 });
66 EXPECT(eventually([&] { return held.load(); }));
67 EXPECT(m.owner() != 0 && m.owner() != GetCurrentThreadId());
68 const std::int64_t waited = m.let_submit_finish(false);
69 const bool after_release = released.load();
70 holder.join();
71 EXPECT(after_release);
72 EXPECT(waited >= 100);
73 std::printf("let_submit_finish(false) waited %lld ms for another thread's hold\n", static_cast<long long>(waited));
74 return 0;
75}
76
77int waits_for_queued_submitter() {
78 // A holds the lock, B queues behind it in lock(); the worker's handoff returns only after B has had its turn.
79 SubmitMutex m;
80 std::atomic<bool> a_held{false}, b_done{false};
81 std::atomic<std::int64_t> b_released_at{0};
82 std::thread a([&] {
83 m.lock();
84 a_held = true;
85 std::this_thread::sleep_for(150ms);
86 m.unlock();
87 });
88 EXPECT(eventually([&] { return a_held.load(); }));
89 std::thread b([&] {
90 m.lock();
91 std::this_thread::sleep_for(100ms);
92 b_released_at = now_ms();
93 b_done = true;
94 m.unlock();
95 });
96 EXPECT(eventually([&] { return m.waiting() == 1; })); // B is counted while it waits
97 const std::int64_t waited = m.let_submit_finish(true);
98 const bool b_had_turn = b_done.load();
99 const std::int64_t returned_at = now_ms();
100 a.join();
101 b.join();
102 EXPECT(b_had_turn);
103 EXPECT(returned_at >= b_released_at.load());
104 EXPECT(m.waiting() == 0 && m.owner() == 0);
105 std::printf("let_submit_finish(true) let a queued submitter go first (%lld ms)\n", static_cast<long long>(waited));
106 return 0;
107}
108
109int handoff_is_capped() {
110 // Two threads keep the lock busy back to back (one always queued): the worker still gets its turn after the cap.
111 SubmitMutex m;
112 std::atomic<bool> stop{false};
113 auto churn = [&] {
114 while (!stop) {
115 m.lock();
116 std::this_thread::sleep_for(10ms);
117 m.unlock();
118 }
119 };
120 std::thread a(churn), b(churn);
121 EXPECT(eventually([&] { return m.owner() != 0 && m.waiting() != 0; }));
122 const std::int64_t waited = m.let_submit_finish(true, 100);
123 stop = true;
124 a.join();
125 b.join();
126 std::printf("capped handoff: %lld ms\n", static_cast<long long>(waited));
127 EXPECT(waited >= 90 && waited < 600);
128 std::printf("let_submit_finish(true, 100) under a steady stream of submits returned after %lld ms\n",
129 static_cast<long long>(waited));
130 return 0;
131}
132
133int compile_gate() {
134 if (!SubmitMutex::compile_gate_enabled()) {
135 std::printf("compile gate off (LEONIDA_COMPILE_GATE=0): not checked\n");
136 return 0;
137 }
138 SubmitMutex m;
139 // Nothing in the driver: a submission passes at once.
140 {
141 const std::int64_t start = now_ms();
142 const SubmitMutex::SubmitTurn turn(m);
143 EXPECT(now_ms() - start < 50);
144 }
145 // A submission waits for the create in the driver; a create arriving meanwhile waits behind the submission.
146 std::atomic<bool> creating{false};
147 std::atomic<std::int64_t> create_left_at{0}, submit_entered_at{0}, submit_left_at{0}, second_entered_at{0};
148 std::atomic<std::int64_t> second_waited{-1};
149 std::thread create([&] {
150 const SubmitMutex::CompileTurn turn(m);
151 creating = true;
152 std::this_thread::sleep_for(250ms);
153 create_left_at = now_ms();
154 });
155 EXPECT(eventually([&] { return creating.load(); }));
156 std::thread submit([&] {
157 m.lock(); // as a submitter does: the submit lock, then the gate around vkQueueSubmit
158 {
159 const SubmitMutex::SubmitTurn turn(m);
160 submit_entered_at = now_ms();
161 std::this_thread::sleep_for(100ms);
162 submit_left_at = now_ms();
163 }
164 m.unlock();
165 });
166 EXPECT(eventually([&] { return m.gate_waiter() != 0; })); // waiting at the gate, with the submit lock held
167 EXPECT(m.owner() == m.gate_waiter());
168 std::thread second([&] {
169 const SubmitMutex::CompileTurn turn(m);
170 second_entered_at = now_ms();
171 second_waited = turn.waited_ms();
172 });
173 create.join();
174 submit.join();
175 second.join();
176 EXPECT(submit_entered_at.load() >= create_left_at.load()); // never in the driver together with a create
177 EXPECT(second_entered_at.load() >= submit_left_at.load()); // the waiting submission went first
178 EXPECT(second_waited.load() >= 50);
179 EXPECT(m.gate_waiter() == 0);
180 const SubmitMutex::GateStats stats = m.gate_stats();
181 EXPECT(stats.submits == 1 && stats.submit_max_ms >= 100);
182 EXPECT(stats.creates == 1 && stats.create_max_ms >= 50);
183 std::printf("compile gate: submission waited %lld ms for a create, a later create waited %lld ms behind it\n",
184 static_cast<long long>(stats.submit_max_ms), static_cast<long long>(stats.create_max_ms));
185 // Creates share the gate.
186 {
187 const SubmitMutex::CompileTurn first(m);
188 std::atomic<std::int64_t> other_waited{-1};
189 std::thread other([&] {
190 const SubmitMutex::CompileTurn turn(m);
191 other_waited = turn.waited_ms();
192 });
193 other.join();
194 EXPECT(other_waited.load() == 0);
195 }
196 std::printf("compile gate: creates pass it together\n");
197 return 0;
198}
199
200} // namespace
201
202int main() {
203 std::setvbuf(stdout, nullptr, _IONBF, 0);
204 if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() ||
205 compile_gate())
206 return 1;
207 std::printf("submit_mutex_test: all passed\n");
208 return 0;
209}