-
Notifications
You must be signed in to change notification settings - Fork 1
/
io_queue.h
229 lines (200 loc) · 4.64 KB
/
io_queue.h
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
#ifndef IO_QUEUE_H_
#define IO_QUEUE_H_
#include <assert.h>
#include <iostream>
#include <mutex> // NOLINT
#include <queue> // NOLINT
#include <stdint.h>
#include <unistd.h>
#include "async_io.h"
enum class IoEngine {
IO_ENGINE_LIBAIO,
IO_ENGINE_URING,
IO_ENGINE_NONE,
};
class Submitter {
public:
Submitter(IoEngine ioEngine, unsigned ioDepth) {
switch (ioEngine) {
case IoEngine::IO_ENGINE_LIBAIO:
ioChannel_ = new Libaio(ioDepth);
break;
#ifdef ENABLE_URING
case IoEngine::IO_ENGINE_URING:
ioChannel_ = new Uring(ioDepth);
break;
#endif
default:
assert(0);
}
}
~Submitter() { delete ioChannel_; }
int Run() {
if (pthread_create(&tidp_, nullptr, IoSubmitter, (void *)this)) {
perror("failed to run submitter");
return -1;
}
return 0;
}
void Finish() {
pthread_cancel(tidp_);
pthread_join(tidp_, nullptr);
}
void Push(IoTask *task) {
mtx_.lock();
tasks_.push_back(task);
mtx_.unlock();
}
AsyncIo *getIoChannel() { return ioChannel_; }
private:
static void *IoSubmitter(void *arg) {
Submitter *submitter = (Submitter *)arg;
while (1) {
pthread_testcancel();
submitter->mtx_.lock();
IoTask *task = submitter->tasks_.front();
if (task == nullptr) {
submitter->mtx_.unlock();
usleep(100);
continue;
}
submitter->tasks_.pop_front();
submitter->mtx_.unlock();
int ret = 0;
if (ret = submitter->ioChannel_->SubmitIo(task)) {
perror("submit failed");
submitter->mtx_.lock();
submitter->tasks_.push_front(task);
submitter->mtx_.unlock();
// wait and retry
usleep(100);
continue;
}
}
}
private:
AsyncIo *ioChannel_;
pthread_t tidp_;
std::deque<IoTask *> tasks_;
std::mutex mtx_;
};
class Reaper {
public:
int Run(AsyncIo *ioChannel) {
if (pthread_create(&tidp_, nullptr, IoReaper, ioChannel)) {
perror("create reaper failed");
return -1;
}
return 0;
}
void Finish() {
pthread_cancel(tidp_);
pthread_join(tidp_, nullptr);
}
private:
static void *IoReaper(void *arg) {
AsyncIo *ioChannel = (AsyncIo *)arg;
while (1) {
pthread_testcancel();
IoTask *task = ioChannel->ReapIo();
if (task == nullptr) {
usleep(100);
continue;
}
if (task->cb != nullptr)
task->cb(task);
}
}
pthread_t tidp_;
};
class CallbackWorker {
public:
CallbackWorker(unsigned index, unsigned size) : index_(index), size_(size) {}
~CallbackWorker() {
pthread_cancel(tid_);
pthread_join(tid_, nullptr);
}
int Run() {
if (pthread_create(&tid_, nullptr, CbWoker, (void *)this)) {
perror("create callback worker failed");
return -1;
}
return 0;
}
int Push(IoTask *task) {
int ret = 0;
lock_.lock();
if (queue_.size() < size_) {
queue_.push(task);
} else {
std::cout << " callback queue_ " << index_ << " is full" << std::endl;
ret = -1;
}
lock_.unlock();
return ret;
}
private:
static void *CbWoker(void *arg) {
CallbackWorker *worker = (CallbackWorker *)arg;
while (1) {
pthread_testcancel();
worker->lock_.lock();
IoTask *task = worker->queue_.front();
if (task == nullptr) {
worker->lock_.unlock();
usleep(100);
continue;
}
worker->queue_.pop();
worker->lock_.unlock();
assert(task->cb != nullptr);
task->cb(task);
}
}
private:
unsigned index_;
pthread_t tid_;
std::queue<IoTask *> queue_;
unsigned size_;
std::mutex lock_;
};
enum class CallbackSchedule {
CALLBACK_SCHED_ROUNDROBIN,
CALLBACK_SCHED_HASH,
};
class CallbackPool {
public:
CallbackPool(unsigned pool_size, unsigned queue_size, CallbackSchedule cb_sch)
: poolSize_(pool_size), queueSize_(queue_size), cbSch_(cb_sch) {}
~CallbackPool() {
for (unsigned i = 0; i < poolSize_; i++)
delete workers_[i];
delete workers_;
}
int Run() {
workers_ = new CallbackWorker *[poolSize_];
for (unsigned i = 0; i < poolSize_; i++) {
workers_[i] = new CallbackWorker(i, queueSize_);
assert(!workers_[i]->Run());
}
}
void Push(IoTask *task) {
static unsigned index = 0;
bool done = false;
// round robin
for (; index < poolSize_; index++) {
if (workers_[index]->Push(task))
continue;
done = true;
std::cout << "task pushed to worker " << index << std::endl;
break;
}
assert(done);
}
private:
unsigned poolSize_;
unsigned queueSize_;
CallbackSchedule cbSch_;
CallbackWorker **workers_;
};
#endif // IO_QUEUE_H_