-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIOUring.cpp
More file actions
184 lines (153 loc) · 5 KB
/
AIOUring.cpp
File metadata and controls
184 lines (153 loc) · 5 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
//
// Created by sergei on 07.06.22.
//
#include "include/aiouring/AIOUring.h"
using namespace aioutils;
#define SQ_CQ_MIN_NUMBER 4096
AIOUring::AIOUring(std::optional<int> iouringBackend, bool useSQPoll) :
iouringBackend{iouringBackend}, useSQPoll{useSQPoll} {
instanceId = idGenerator.fetch_add(1);
}
void AIOUring::executeLongTask(AIOUringLongTask longTask) {
if(longTask.task.has_value())
{
executor.silent_async([=, this]() {
try {
(*longTask.task)(&executor);
} catch (std::exception &e) {
kklogging::ERROR(fmt::format("During long running task: {}", e.what()));
}
if(longTask.eventfd.has_value()) {
eventfd_write(*longTask.eventfd, 1L);
}
});
}
}
std::tuple<bool, int> AIOUring::processCQE(io_uring_cqe *cqe) {
auto *task = static_cast<AIOUringTask *>(
reinterpret_cast<void *>(cqe->user_data));
try {
AIOUringTask::TaskFuture taskFuture = AIOUringTask::futureEmpty();
if(!task->isTaskFinal()) {
taskFuture = task->poll(cqe->res);
} else {
taskFuture = task->finally(cqe->res);
}
std::optional<AIOUringOp> taskOp = std::get<0>(taskFuture);
if(!taskOp.has_value())
{
try {
std::optional<std::any> &result = std::get<1>(taskFuture);
if(result.has_value()) {
auto error = std::any_cast<std::runtime_error>(*result);
kklogging::ERROR(fmt::format("{}: {}", task->getClassName(), error.what()));
}
} catch(...) {};
if(!task->isTaskFinal()) {
task->setTaskFinal();
auto op = AIOUringOp::Nop();
(*op.submit)(&this->ring, cqe->user_data);
} else {
freeTask(task);
}
}
else
{
auto op = *taskOp;
if(op.shutdown)
{
return std::make_tuple(false, op.shutdownCode);
}
if(op.submit.has_value())
{
(*op.submit)(&this->ring, cqe->user_data);
}
else
{
kklogging::ERROR("No submit function for operation.");
}
}
}
catch(std::exception &e) {
kklogging::ERROR(fmt::format("Uncaught exception during poll/finally(), task {}: {}",
task->getClassName(), e.what()));
return std::make_tuple(false, 1);
}
return std::make_tuple(true, 0);
}
int AIOUring::run() {
kklogging::INFO("IO_URING has started.");
while(true)
{
auto result = io_uring_submit_and_wait(&ring, 1);
struct io_uring_cqe *cqe;
unsigned head;
unsigned count = 0;
if(result < 0)
{
kklogging::ERROR("io_uring_submit_and_wait failed: " + uexcept::errnoStr(-result));
std::this_thread::sleep_for(std::chrono::seconds(1));
continue;
}
io_uring_for_each_cqe(&ring, head, cqe) {
++count;
if(cqe->user_data == 0)
{
kklogging::ERROR("cqe->user_data == 0");
continue;
}
auto res = processCQE(cqe);
if(!std::get<0>(res)) {
kklogging::WARN("IO_URING shutdown.");
return std::get<1>(res);
}
}
io_uring_cq_advance(&ring, count);
}
}
void AIOUring::setup() {
if(useSQPoll)
{
useSQPoll = ulinux::linuxKernelNotLessThan(5, 11);
}
if(useSQPoll)
{
params.flags |= IORING_SETUP_SQPOLL;
params.sq_thread_idle = 2000;
}
if(!useSQPoll)
{
kklogging::INFO("SqlPoll is disabled");
}
if(iouringBackend.has_value())
{
params.flags |= IORING_SETUP_ATTACH_WQ;
params.wq_fd = *iouringBackend;
}
if (io_uring_queue_init_params(SQ_CQ_MIN_NUMBER, &ring, ¶ms) < 0) {
if(errno == EPERM)
{
throw AIOUringException("Operation not permitted. TRY ROOT or disable SQPOLL by setting "
"RTSP_PROXY_IOURING_USE_SQPOLL=false. If it is run from docker try --privileged.");
}
else
{
THROW_ERRNO(AIOUringException, "During io_uring_queue_init_params");
}
}
if (!(params.features & IORING_FEAT_FAST_POLL)) {
throw AIOUringException("IORING_FEAT_FAST_POLL not available in the kernel. "
"It is available starting from 5.7 kernel version.");
}
if(useSQPoll)
{
if (!(params.features & IORING_FEAT_SQPOLL_NONFIXED)) {
throw AIOUringException("IORING_FEAT_SQPOLL_NONFIXED not available in the kernel. "
"It is available starting from 5.11 kernel version.");
}
}
setupPassed = true;
}
int AIOUring::getInstanceId() const {
return instanceId;
}