forked from alibaba/PhotonLibOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aio-wrapper.cpp
423 lines (391 loc) · 13.3 KB
/
aio-wrapper.cpp
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
Copyright 2022 The Photon Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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 "aio-wrapper.h"
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/eventfd.h>
#include <sys/uio.h>
#include <signal.h>
#include <fcntl.h>
#include <aio.h>
#include <libaio.h>
#include <memory>
#include "../thread/thread.h"
#include "fd-events.h"
#include "../common/utility.h"
#include "../common/alog.h"
#include "reset_handle.h"
namespace photon
{
constexpr static int IODEPTH_MAX = 2048;
struct libaio_ctx_t
{
int evfd = -1, running = 0, iodepth = 32;
io_context_t aio_ctx = {0};
thread* polling_thread = nullptr;
condition_variable cond;
};
static __thread libaio_ctx_t* libaio_ctx = nullptr;
template<typename F>
ssize_t have_n_try(const F& f, const char* name, ssize_t error_level = 0)
{
int ntry = 0;
while(true)
{
ssize_t ret = f();
auto e = errno;
if (ret >= error_level || e == ECANCELED)
return ret;
thread_usleep(1000*10); // sleep 10ms whenever error occurs
if (e == EINTR) continue;
if (ntry == 7) return ret;
LOG_WARN("failed to do `() for the `-th time ` ", name, ntry+1, VALUE(ret), ERRNO(e));
thread_usleep(1000*10 * (1 << ntry++));
}
}
#define HAVE_N_TRY_(func, args, error_level) have_n_try( \
[&]() __INLINE__ { return func args; }, #func, error_level)
#define HAVE_N_TRY(func, args) HAVE_N_TRY_(func, args, 0)
struct libaiocb : public iocb
{
ssize_t ioret;
void cancel()
{
struct io_event cancel_ret;
HAVE_N_TRY(io_cancel, (libaio_ctx->aio_ctx, this, &cancel_ret));
}
ssize_t submit_and_wait(uint64_t timedout = -1)
{
auto ctx = libaio_ctx;
io_set_eventfd(this, ctx->evfd);
this->data = CURRENT;
auto piocb = (iocb*)this;
while(true)
{
int ret = io_submit(ctx->aio_ctx, 1, &piocb);
if (ret == 1) break;
if (ret < 0)
{
auto e = -ret;
switch(e)
{
case EAGAIN:
ctx->cond.wait_no_lock();
case EINTR:
continue;
case EBADF:
case EFAULT:
case EINVAL:
default:
thread_usleep(1000*10); // sleep 10ms whenever error occurs
LOG_ERRNO_RETURN(e, ret, "failed to io_submit()");
}
}
}
int ret = thread_usleep(timedout);
if (ret == 0) // timedout
{
cancel();
LOG_WARN("libaio timedout fd=`, offset=`, nbytes=`", aio_fildes, u.c.offset, u.c.nbytes);
errno = ETIMEDOUT;
return -1;
}
auto e = errno;
if (e != EOK) // interrupted by a user thread
{
cancel();
LOG_ERROR_RETURN(e, -1, "libaio interrupted");
}
if (this->ioret < 0) {
e = -this->ioret;
LOG_ERROR_RETURN(e, -1, "libaio result error");
}
return this->ioret;
}
template<typename F, typename... ARGS>
ssize_t asyncio(F io_prep, ARGS... args)
{
io_prep(this, args...);
return submit_and_wait();
}
};
static int my_io_getevents(long min_nr, long nr, struct io_event *events)
{
int ret = ::io_getevents(libaio_ctx->aio_ctx, min_nr, nr, events, NULL);
if (ret < 0)
errno = -ret;
return ret;
}
static void resume_libaio_requesters()
{
retry:
struct io_event events[IODEPTH_MAX];
int n = HAVE_N_TRY(my_io_getevents, (0, libaio_ctx->iodepth, events));
for (int i=0; i<n; ++i)
{
auto piocb = (libaiocb*)events[i].obj;
piocb->ioret = events[i].res;
if (events[i].res2 < 0)
LOG_WARN("libaio delievers error, ", VALUE(events[i].res),
VALUE(events[i].res2), VALUE(events[i].obj),
VALUE(piocb->aio_lio_opcode), VALUE(piocb->aio_fildes),
VALUE(piocb->u.c.offset), VALUE(piocb->u.c.nbytes),
VALUE(piocb->u.c.buf), VALUE(piocb->u.c.resfd));
thread_interrupt((thread *)events[i].data, EOK);
}
if (n == libaio_ctx->iodepth)
{
thread_yield();
goto retry;
}
}
static uint64_t wait_for_events()
{
auto ctx = libaio_ctx;
auto ret = HAVE_N_TRY(wait_for_fd_readable, (ctx->evfd));
if (ret < 0)
return 0;
uint64_t nevents = 0;
HAVE_N_TRY_(::read, (ctx->evfd, &nevents, sizeof(nevents)), sizeof(nevents));
return nevents;
}
static void* libaio_polling(void*)
{
DEFER(libaio_ctx->running = 0);
while (libaio_ctx->running == 1)
{
libaio_ctx->running = 2;
wait_for_events();
if (libaio_ctx->running == -1) break;
libaio_ctx->running = 1;
resume_libaio_requesters();
libaio_ctx->cond.notify_all();
}
return nullptr;
}
struct posix_aiocb : public aiocb
{
thread* th;
ssize_t ioret;
posix_aiocb(int fd)
{
memset(this, 0, sizeof(aiocb));
th = CURRENT;
aio_fildes = fd;
aio_sigevent.sigev_notify = SIGEV_THREAD;
aio_sigevent.sigev_notify_function = &aio_completion_handler;
aio_sigevent.sigev_value.sival_ptr = this;
}
static void aio_completion_handler( sigval_t sigval )
{
auto req = (struct posix_aiocb*)sigval.sival_ptr;
req->ioret = aio_return(req);
// interrupt current or next sleep in th. note that
// interrupt may call even before th comming into sleep
while (photon::thread_stat(req->th) != SLEEPING) {
::sched_yield();
}
thread_interrupt(req->th, EOK);
}
template<typename F, typename...ARGS>
ssize_t async_perform(F iofunc, ARGS...args)
{
int ret = iofunc(args..., this);
if (ret < 0) return ret;
again:
thread_usleep(-1);
ERRNO e;
if (e.no != EOK)
{
LOG_ERROR("unexpected wakeup!", e);
goto again;
}
return this->ioret;
}
void prep_io(void *buf, size_t count, off_t offset)
{
aio_buf = buf;
aio_nbytes = count;
aio_offset = offset;
}
ssize_t pread(void *buf, size_t count, off_t offset)
{
prep_io(buf, count, offset);
return async_perform(&::aio_read);
}
ssize_t pwrite(void *buf, size_t count, off_t offset)
{
prep_io(buf, count, offset);
return async_perform(&::aio_write);
}
int fsync() { return (int)async_perform(&::aio_fsync, O_SYNC); }
int fdatasync() { return (int)async_perform(&::aio_fsync, O_DSYNC); }
};
class Counter
{
public:
int& c;
Counter(int& c) : c(c) { c++; }
~Counter() { c--; }
};
ssize_t libaio_pread(int fd, void *buf, size_t count, off_t offset)
{
static int n; Counter c(n);
return libaiocb().asyncio(&io_prep_pread, fd, buf, count, offset);
}
ssize_t libaio_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
static int n; Counter c(n);
return libaiocb().asyncio(&io_prep_preadv, fd, iov, iovcnt, offset);
}
ssize_t libaio_pwrite(int fd, const void *buf, size_t count, off_t offset)
{
static int n; Counter c(n);
return libaiocb().asyncio(&io_prep_pwrite, fd, (void*)buf, count, offset);
}
ssize_t libaio_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
static int n; Counter c(n);
return libaiocb().asyncio(&io_prep_pwritev, fd, iov, iovcnt, offset);
}
/*
int libaio_fsync(int fd)
{
Counter<__LINE__> c;
return (int)libaiocb().asyncio(&io_prep_fsync, fd);
}
*/
ssize_t posixaio_pread(int fd, void *buf, size_t count, off_t offset)
{
static int n; Counter c(n);
return posix_aiocb(fd).pread(buf, count, offset);
}
ssize_t posixaio_pwrite(int fd, const void *buf, size_t count, off_t offset)
{
static int n; Counter c(n);
return posix_aiocb(fd).pwrite((void *)buf, count, offset);
}
int posixaio_fsync(int fd)
{
static int n; Counter c(n);
return posix_aiocb(fd).fsync();
}
int posixaio_fdatasync(int fd)
{
static int n; Counter c(n);
return posix_aiocb(fd).fdatasync();
}
ssize_t posixaio::preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
ssize_t rst = 0;
for (auto& x: ptr_array(iov, iovcnt))
{
ssize_t ret = posixaio_pread(fd, x.iov_base, x.iov_len, offset + rst);
if (ret < 0)
LOG_ERRNO_RETURN(-1, 0, "failed to posixaio_preadv");
if (ret < (ssize_t)x.iov_len)
return rst + ret;
rst += ret;
}
return rst;
}
ssize_t posixaio::pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
ssize_t rst = 0;
for (auto& x: ptr_array(iov, iovcnt))
{
ssize_t ret = posixaio_pwrite(fd, x.iov_base, x.iov_len, offset + rst);
if (ret < 0)
LOG_ERRNO_RETURN(-1, 0, "failed to posixaio_pwrite()");
if (ret < (ssize_t)x.iov_len)
return rst + ret;
rst += ret;
}
return rst;
}
class AioResetHandle : public ResetHandle {
int reset() override {
if (!libaio_ctx)
return 0;
LOG_INFO("reset libaio by reset handle");
close(libaio_ctx->evfd);
libaio_ctx->evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
if (libaio_ctx->evfd < 0) {
LOG_ERROR("failed to create eventfd ", ERRNO());
exit(-1);
}
io_destroy(libaio_ctx->aio_ctx);
libaio_ctx->aio_ctx = {0};
int ret = io_setup(libaio_ctx->iodepth, &libaio_ctx->aio_ctx);
if (ret < 0) {
LOG_ERROR("failed to create aio context by io_setup() ", ERRNO(), VALUE(ret));
exit(-1);
}
thread_interrupt(libaio_ctx->polling_thread, ECANCELED);
return 0;
}
};
static thread_local AioResetHandle *reset_handler = nullptr;
int libaio_wrapper_init(int iodepth)
{
if (libaio_ctx)
return 0;
if (iodepth <= 0)
LOG_ERROR_RETURN(EINVAL, -1, "iodepth should be greater than 0");
std::unique_ptr<libaio_ctx_t> ctx(new libaio_ctx_t);
ctx->evfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
ctx->iodepth = iodepth > IODEPTH_MAX ? IODEPTH_MAX : iodepth;
if (ctx->evfd < 0)
LOG_ERRNO_RETURN(0, -1, "failed to create eventfd");
int ret = io_setup(ctx->iodepth, &ctx->aio_ctx);
if (ret < 0)
{
LOG_ERROR("failed to create aio context by io_setup() ", ERRNO(), VALUE(ret));
close(ctx->evfd);
return ret;
}
ctx->polling_thread = thread_create(&libaio_polling, nullptr);
assert(ctx->polling_thread);
libaio_ctx = ctx.release();
libaio_ctx->running = 1;
if (reset_handler == nullptr) {
reset_handler = new AioResetHandle();
}
LOG_DEBUG("libaio initialized");
return 0;
}
int libaio_wrapper_fini()
{
if (!libaio_ctx || !libaio_ctx->running ||
!libaio_ctx->polling_thread || libaio_ctx->evfd < 0)
return 0;
if (libaio_ctx->running == 2) // if waiting for fd readable
thread_interrupt(libaio_ctx->polling_thread, ECANCELED);
libaio_ctx->running = -1;
while (libaio_ctx->running != 0)
thread_usleep(1000*10);
io_destroy(libaio_ctx->aio_ctx);
close(libaio_ctx->evfd);
libaio_ctx->evfd = -1;
safe_delete(libaio_ctx);
safe_delete(reset_handler);
LOG_DEBUG("libaio finished");
return 0;
}
}