[PATCH] Fix busy-looping with aio engine and depth > 1
[fio.git] / engines / fio-engine-libaio.c
index 57daf1b6b49a5c8b2933eac060b025318594803e..da43f18ce1571b7c729ceb35fce02ec1aae00e12 100644 (file)
@@ -7,8 +7,11 @@
 #include <unistd.h>
 #include <errno.h>
 #include <assert.h>
-#include "fio.h"
-#include "os.h"
+
+#include "../fio.h"
+#include "../os.h"
+
+#ifdef FIO_HAVE_LIBAIO
 
 #define ev_to_iou(ev)  (struct io_u *) ((unsigned long) (ev)->obj)
 
@@ -17,20 +20,18 @@ struct libaio_data {
        struct io_event *aio_events;
 };
 
-static int fio_libaio_sync(struct thread_data fio_unused *td,
-                          struct fio_file *f)
-{
-       return fsync(f->fd);
-}
-
 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
 {
        struct fio_file *f = io_u->file;
 
        if (io_u->ddir == DDIR_READ)
                io_prep_pread(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset);
-       else
+       else if (io_u->ddir == DDIR_WRITE)
                io_prep_pwrite(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset);
+       else if (io_u->ddir == DDIR_SYNC)
+               io_prep_fsync(&io_u->iocb, f->fd);
+       else
+               return 1;
 
        return 0;
 }
@@ -50,7 +51,9 @@ static int fio_libaio_getevents(struct thread_data *td, int min, int max,
 
        do {
                r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
-               if (r == -EAGAIN) {
+               if (r >= min)
+                       break;
+               else if (r == -EAGAIN) {
                        usleep(100);
                        continue;
                } else if (r == -EINTR)
@@ -59,6 +62,9 @@ static int fio_libaio_getevents(struct thread_data *td, int min, int max,
                        break;
        } while (1);
 
+       if (r < 0)
+               r = -r;
+
        return (int) r;
 }
 
@@ -80,9 +86,13 @@ static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
                        break;
        } while (1);
 
-       assert(ret);
+       if (ret <= 0) {
+               io_u->resid = io_u->buflen;
+               io_u->error = -ret;
+               return 1;
+       }
 
-       return (int) ret;
+       return 0;
 }
 
 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
@@ -113,6 +123,7 @@ static int fio_libaio_init(struct thread_data *td)
        memset(ld, 0, sizeof(*ld));
        if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
                td_verror(td, errno);
+               free(ld);
                return 1;
        }
 
@@ -122,7 +133,7 @@ static int fio_libaio_init(struct thread_data *td)
        return 0;
 }
 
-struct ioengine_ops ioengine = {
+static struct ioengine_ops ioengine = {
        .name           = "libaio",
        .version        = FIO_IOOPS_VERSION,
        .init           = fio_libaio_init,
@@ -132,5 +143,35 @@ struct ioengine_ops ioengine = {
        .getevents      = fio_libaio_getevents,
        .event          = fio_libaio_event,
        .cleanup        = fio_libaio_cleanup,
-       .sync           = fio_libaio_sync,
 };
+
+#else /* FIO_HAVE_LIBAIO */
+
+/*
+ * When we have a proper configure system in place, we simply wont build
+ * and install this io engine. For now install a crippled version that
+ * just complains and fails to load.
+ */
+static int fio_libaio_init(struct thread_data fio_unused *td)
+{
+       fprintf(stderr, "fio: libaio not available\n");
+       return 1;
+}
+
+static struct ioengine_ops ioengine = {
+       .name           = "libaio",
+       .version        = FIO_IOOPS_VERSION,
+       .init           = fio_libaio_init,
+};
+
+#endif
+
+static void fio_init fio_libaio_register(void)
+{
+       register_ioengine(&ioengine);
+}
+
+static void fio_exit fio_libaio_unregister(void)
+{
+       unregister_ioengine(&ioengine);
+}