#endif
#define SYS_eventfd 323
-#define MAX_AIO_EV 32768
+#define MAX_AIO_EV 1024
struct io_cbdata {
gint fd;
/* Linux specific mappings and utilities to avoid using of libaio */
-typedef unsigned int aio_context_t;
+typedef unsigned long aio_context_t;
typedef enum io_iocb_cmd {
IO_CMD_PREAD = 0,
guint64 aio_buf;
guint64 aio_nbytes;
- guint64 aio_offset;
+ gint64 aio_offset;
/* extra parameters */
guint64 aio_reserved2; /* TODO: use this for a (struct sigevent *) */
# ifndef HAVE_SYS_EVENTFD_H
static int
-eventfd(guint initval, guint flags)
+eventfd (guint initval, guint flags)
{
return syscall (SYS_eventfd, initval);
}
gint event_fd;
struct event eventfd_ev;
aio_context_t io_ctx;
+ gpointer buf;
+ gsize buflen;
#elif defined(HAVE_AIO_H)
/* POSIX aio */
struct event rtsigs[SIGRTMAX - SIGRTMIN];
struct aio_context *ctx = ud;
guint64 ready;
gint done, i;
- struct io_event event[64];
+ struct io_event event[32];
struct timespec ts;
struct io_cbdata *ev_data;
while (ready) {
/* Get events ready */
- done = io_getevents (ctx->io_ctx, 1, 64, event, &ts);
+ done = io_getevents (ctx->io_ctx, 1, 32, event, &ts);
if (done > 0) {
ready -= done;
}
#ifdef LINUX
- fd = open (path, flags | O_DIRECT | O_NONBLOCK);
+ fd = open (path, flags | O_DIRECT);
return fd;
#elif defined(HAVE_AIO_H)
- fd = open (path, flags | O_NONBLOCK);
+ fd = open (path, flags);
#endif
return fd;
#ifdef LINUX
struct iocb *iocb[1];
+ /* We need to align pointer on boundary of 512 bytes here */
+ if (ctx->buflen < len) {
+ if (ctx->buf) {
+ free (ctx->buf);
+ }
+ if (posix_memalign (&ctx->buf, 512, len) != 0) {
+ return -1;
+ }
+ else {
+ ctx->buflen = len;
+ memcpy (ctx->buf, buf, len);
+ }
+ }
cbdata = g_slice_alloc (sizeof (struct io_cbdata));
cbdata->cb = cb;
cbdata->buf = buf;
iocb[0]->aio_fildes = fd;
iocb[0]->aio_lio_opcode = IO_CMD_PWRITE;
iocb[0]->aio_reqprio = 0;
- iocb[0]->aio_buf = (guint64)((uintptr_t)buf);
+ iocb[0]->aio_buf = (guint64)((uintptr_t)ctx->buf);
iocb[0]->aio_nbytes = len;
iocb[0]->aio_offset = offset;
iocb[0]->aio_flags |= (1 << 0) /* IOCB_FLAG_RESFD */;
/* Iocb is copied to kernel internally, so it is safe to put it on stack */
r = io_cancel (ctx->io_ctx, &iocb, &ev);
+ if (ctx->buf) {
+ free (ctx->buf);
+ }
close (fd);
return r;
#include "tests.h"
#include "main.h"
#include "aio_event.h"
+#include "mem_pool.h"
extern struct event_base *base;
guchar *p = data;
guint i;
- g_assert (res != -1);
+ g_assert (res > 0);
g_assert (len == BUFSIZ);
for (i = 0; i < len; i ++) {
aio_write_cb (gint fd, gint res, gsize len, gpointer data, gpointer ud)
{
struct aio_context *aio_ctx = ud;
- static gchar testbuf[BUFSIZ];
+ gchar *testbuf;
+
+ g_assert (res > 0);
- g_assert (res != -1);
+ g_assert (posix_memalign ((void **)&testbuf, 512, BUFSIZ) == 0);
- g_assert (rspamd_aio_read (fd, testbuf, sizeof (testbuf), aio_ctx, aio_read_cb, aio_ctx) != -1);
+ g_assert (rspamd_aio_read (fd, testbuf, BUFSIZ, 0, aio_ctx, aio_read_cb, aio_ctx) != -1);
}
void
/* Write some data */
memset (testbuf, 0xef, sizeof (testbuf));
- ret = rspamd_aio_write (afd, testbuf, sizeof (testbuf), aio_ctx, aio_write_cb, aio_ctx);
+ ret = rspamd_aio_write (afd, testbuf, sizeof (testbuf), 0, aio_ctx, aio_write_cb, aio_ctx);
g_assert (ret != -1);
event_base_loop (base, 0);