-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathtest_io_uring_context.cpp
More file actions
373 lines (345 loc) · 14.1 KB
/
test_io_uring_context.cpp
File metadata and controls
373 lines (345 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright (c) 2023 Maikel Nadolski
* Copyright (c) 2023 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <linux/version.h>
// Some kernel versions have <linux/io_uring.h> but don't support or don't
// allow user access to some of the necessary system calls.
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0) && __has_include(<linux/io_uring.h>)
# include "exec/finally.hpp"
# include "exec/linux/io_uring_context.hpp"
# include "exec/scope.hpp"
# include "exec/single_thread_context.hpp"
# include "exec/start_detached.hpp"
# include "exec/when_any.hpp"
# include "catch2/catch.hpp"
using namespace STDEXEC;
using namespace exec;
using namespace std::chrono_literals;
namespace
{
// clang-12 does not know jthread yet
class jthread
{
std::thread thread_;
public:
template <typename Callable, typename... Args>
explicit jthread(Callable&& callable, Args&&... args)
: thread_(std::forward<Callable>(callable), std::forward<Args>(args)...)
{}
jthread(jthread&& other) noexcept
: thread_(std::move(other.thread_))
{}
auto operator=(jthread&& other) noexcept -> jthread&
{
thread_ = std::move(other.thread_);
return *this;
}
~jthread()
{
if (thread_.joinable())
{
thread_.join();
}
}
[[nodiscard]]
auto get_id() const noexcept -> std::thread::id
{
return thread_.get_id();
}
};
TEST_CASE("io_uring_context - unused context", "[types][io_uring][schedulers]")
{
io_uring_context context;
CHECK(context.is_running() == false);
}
TEST_CASE("io_uring_context Satisfy concepts", "[types][io_uring][schedulers]")
{
STATIC_REQUIRE(timed_scheduler<io_uring_scheduler>);
STATIC_REQUIRE_FALSE(std::is_move_assignable_v<io_uring_context>);
}
TEST_CASE("io_uring_context Schedule runs in io thread", "[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
jthread io_thread{[&] { context.run_until_stopped(); }};
{
scope_guard guard{[&]() noexcept { context.request_stop(); }};
bool is_called = false;
sync_wait(schedule(scheduler)
| then(
[&]
{
CHECK(io_thread.get_id() == std::this_thread::get_id());
is_called = true;
}));
CHECK(is_called);
is_called = false;
sync_wait(schedule_after(scheduler, 1ms)
| then(
[&]
{
CHECK(io_thread.get_id() == std::this_thread::get_id());
is_called = true;
}));
CHECK(is_called);
}
}
TEST_CASE("io_uring_context Call io_uring::run_until_empty with start_detached",
"[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
bool is_called = false;
exec::start_detached(schedule(scheduler)
| then(
[&]
{
CHECK(context.is_running());
is_called = true;
}));
context.run_until_empty();
CHECK(is_called);
CHECK(!context.is_running());
CHECK(!context.stop_requested());
}
TEST_CASE("io_uring_context Call now(io_uring) is running clock", "[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
auto start = now(scheduler);
std::this_thread::sleep_for(10ms);
CHECK(start + 10ms <= now(scheduler));
}
TEST_CASE("io_uring_context Call io_uring::run_until_empty with sync_wait",
"[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
auto just_run = just() | then([&] { context.run_until_empty(); });
bool is_called = false;
sync_wait(when_all(schedule_after(scheduler, 500us)
| then(
[&]
{
CHECK(context.is_running());
is_called = true;
}),
just_run));
CHECK(is_called);
CHECK(!context.is_running());
CHECK(!context.stop_requested());
}
TEST_CASE("io_uring_context Call io_uring::run with sync_wait and when_any",
"[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
bool is_called = false;
sync_wait(when_any(schedule_after(scheduler, 500us)
| then(
[&]
{
CHECK(context.is_running());
is_called = true;
}),
context.run()));
CHECK(is_called);
CHECK(!context.is_running());
CHECK(context.stop_requested());
}
TEST_CASE("io_uring_context Call io_uring::run with sync_wait and when_all",
"[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
bool is_called = false;
sync_wait(when_all(schedule_after(scheduler, 500us)
| then(
[&]
{
CHECK(context.is_running());
is_called = true;
}),
context.run(until::empty)));
CHECK(is_called);
CHECK(!context.is_running());
CHECK(!context.stop_requested());
}
TEST_CASE("io_uring_context Explicitly stop the io_uring_context",
"[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
{
bool is_called = false;
exec::start_detached(schedule(scheduler)
| then(
[&]
{
CHECK(context.is_running());
is_called = true;
}));
context.run_until_empty();
CHECK(is_called);
CHECK(!context.is_running());
CHECK(!context.stop_requested());
}
context.request_stop();
CHECK(context.stop_requested());
context.run_until_stopped();
CHECK(context.stop_requested());
bool is_stopped = false;
sync_wait(schedule(scheduler) | then([&] { CHECK(false); })
| STDEXEC::upon_stopped([&] { is_stopped = true; }));
CHECK(is_stopped);
}
TEST_CASE("io_uring_context Thread-safe to schedule from multiple threads",
"[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
jthread io_thread{[&] { context.run_until_stopped(); }};
auto fn = [&]
{
CHECK(io_thread.get_id() == std::this_thread::get_id());
};
{
scope_guard guard{[&]() noexcept { context.request_stop(); }};
jthread thread1{[&]
{
for (int i = 0; i < 10; ++i)
{
sync_wait(when_all(schedule(scheduler) | then(fn),
schedule(scheduler) | then(fn),
schedule(scheduler) | then(fn),
schedule(scheduler) | then(fn),
schedule(scheduler) | then(fn),
schedule(scheduler) | then(fn),
schedule(scheduler) | then(fn),
schedule(scheduler) | then(fn),
schedule(scheduler) | then(fn),
schedule(scheduler) | then(fn)));
}
}};
jthread thread2{[&]
{
for (int i = 0; i < 10; ++i)
{
auto tp = std::chrono::steady_clock::now() + 500us;
sync_wait(when_all(schedule_at(scheduler, tp) | then(fn),
schedule_at(scheduler, tp) | then(fn),
schedule_at(scheduler, tp) | then(fn),
schedule_at(scheduler, tp) | then(fn),
schedule_at(scheduler, tp) | then(fn),
schedule_at(scheduler, tp) | then(fn),
schedule_at(scheduler, tp) | then(fn),
schedule_at(scheduler, tp) | then(fn),
schedule_at(scheduler, tp) | then(fn),
schedule_at(scheduler, tp) | then(fn)));
}
}};
jthread thread3{[&]
{
for (int i = 0; i < 10; ++i)
{
sync_wait(when_all(schedule_after(scheduler, 500us) | then(fn),
schedule_after(scheduler, 500us) | then(fn),
schedule_after(scheduler, 500us) | then(fn),
schedule_after(scheduler, 500us) | then(fn),
schedule_after(scheduler, 500us) | then(fn),
schedule_after(scheduler, 500us) | then(fn),
schedule_after(scheduler, 500us) | then(fn),
schedule_after(scheduler, 500us) | then(fn),
schedule_after(scheduler, 500us) | then(fn),
schedule_after(scheduler, 500us) | then(fn)));
}
}};
}
}
TEST_CASE("io_uring_context Stop io_uring_context", "[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
jthread io_thread{[&] { context.run_until_stopped(); }};
{
single_thread_context ctx1{};
auto sch1 = ctx1.get_scheduler();
bool is_called = false;
sync_wait(finally(schedule(scheduler) | then([&]() noexcept { is_called = true; }),
schedule(sch1) | then([&]() noexcept { context.request_stop(); })));
CHECK(is_called);
}
bool is_called = false;
sync_wait(schedule(scheduler) | then([&] { is_called = true; }));
CHECK_FALSE(is_called);
}
TEST_CASE("io_uring_context schedule_after 0s", "[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
jthread io_thread{[&] { context.run_until_stopped(); }};
{
scope_guard guard{[&]() noexcept { context.request_stop(); }};
bool is_called = false;
sync_wait(when_any(schedule_after(scheduler, 0s)
| then(
[&]
{
CHECK(io_thread.get_id() == std::this_thread::get_id());
is_called = true;
}),
schedule_after(scheduler, 5ms)));
CHECK(is_called);
}
}
TEST_CASE("io_uring_context schedule_after -1s", "[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
jthread io_thread{[&] { context.run_until_stopped(); }};
{
scope_guard guard{[&]() noexcept { context.request_stop(); }};
bool is_called_1 = false;
bool is_called_2 = false;
auto start = std::chrono::steady_clock::now();
auto timeout = 100ms;
sync_wait(when_any(schedule_after(scheduler, -1s)
| then(
[&]
{
CHECK(io_thread.get_id() == std::this_thread::get_id());
is_called_1 = true;
}),
schedule_after(scheduler, timeout) | then([&] { is_called_2 = true; })));
auto end = std::chrono::steady_clock::now();
std::chrono::nanoseconds diff = end - start;
CHECK(diff.count() < std::chrono::duration_cast<std::chrono::nanoseconds>(timeout).count());
CHECK(is_called_1 == true);
CHECK(is_called_2 == false);
}
}
TEST_CASE("io_uring_context - reuse context after being used", "[types][io_uring][schedulers]")
{
io_uring_context context;
io_uring_scheduler scheduler = context.get_scheduler();
CHECK(sync_wait(exec::when_any(schedule(scheduler), context.run())));
CHECK(!sync_wait(exec::when_any(schedule(scheduler), context.run())));
context.reset();
CHECK(sync_wait(exec::when_any(schedule(scheduler), context.run())));
CHECK(!sync_wait(exec::when_any(schedule(scheduler), context.run())));
}
} // namespace
#endif