windowsaio: fix completion thread affinitization
[fio.git] / engines / windowsaio.c
index 3a24fa717da7d3751a616e7a17b338cc9bd5a38f..f5cb04838a31f70dc6cf801239e33df16800f54c 100644 (file)
@@ -9,7 +9,6 @@
 #include <unistd.h>
 #include <signal.h>
 #include <errno.h>
-#include <windows.h>
 
 #include "../fio.h"
 
@@ -28,7 +27,6 @@ struct windowsaio_data {
        HANDLE iocp;
        HANDLE iothread;
        HANDLE iocomplete_event;
-       CANCELIOEX pCancelIoEx;
        BOOL iothread_running;
 };
 
@@ -37,11 +35,9 @@ struct thread_ctx {
        struct windowsaio_data *wd;
 };
 
-static int fio_windowsaio_cancel(struct thread_data *td,
-                                  struct io_u *io_u);
 static BOOL timeout_expired(DWORD start_count, DWORD end_count);
 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
-                                       unsigned int max, struct timespec *t);
+                               unsigned int max, const struct timespec *t);
 static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
 static int fio_windowsaio_queue(struct thread_data *td,
                                  struct io_u *io_u);
@@ -54,7 +50,6 @@ static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct f
 static int fio_windowsaio_init(struct thread_data *td)
 {
        struct windowsaio_data *wd;
-       HANDLE hKernel32Dll;
        int rc = 0;
 
        wd = calloc(1, sizeof(struct windowsaio_data));
@@ -89,9 +84,7 @@ static int fio_windowsaio_init(struct thread_data *td)
                }
        }
 
-       hKernel32Dll = GetModuleHandle("kernel32.dll");
-       wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
-       td->io_ops->data = wd;
+       td->io_ops_data = wd;
 
        if (!rc) {
                struct thread_ctx *ctx;
@@ -104,7 +97,7 @@ static int fio_windowsaio_init(struct thread_data *td)
                        rc = 1;
                }
 
-               wd = td->io_ops->data;
+               wd = td->io_ops_data;
                wd->iothread_running = TRUE;
                wd->iocp = hFile;
 
@@ -120,10 +113,15 @@ static int fio_windowsaio_init(struct thread_data *td)
 
                if (!rc)
                {
+                       DWORD threadid;
+
                        ctx->iocp = hFile;
                        ctx->wd = wd;
-                       wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
-                       if (wd->iothread == NULL)
+                       wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, &threadid);
+
+                       if (wd->iothread != NULL)
+                               fio_setaffinity(threadid, td->o.cpumask);
+                       else
                                log_err("windowsaio: failed to create io completion thread\n");
                }
 
@@ -138,7 +136,7 @@ static void fio_windowsaio_cleanup(struct thread_data *td)
 {
        struct windowsaio_data *wd;
 
-       wd = td->io_ops->data;
+       wd = td->io_ops_data;
 
        if (wd != NULL) {
                wd->iothread_running = FALSE;
@@ -150,7 +148,7 @@ static void fio_windowsaio_cleanup(struct thread_data *td)
                free(wd->aio_events);
                free(wd);
 
-               td->io_ops->data = NULL;
+               td->io_ops_data = NULL;
        }
 }
 
@@ -210,10 +208,10 @@ static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
 
        /* Only set up the completion port and thread if we're not just
         * querying the device size */
-       if (!rc && td->io_ops->data != NULL) {
+       if (!rc && td->io_ops_data != NULL) {
                struct windowsaio_data *wd;
 
-               wd = td->io_ops->data;
+               wd = td->io_ops_data;
 
                if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
                        log_err("windowsaio: failed to create io completion port\n");
@@ -258,17 +256,18 @@ static BOOL timeout_expired(DWORD start_count, DWORD end_count)
 
 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
 {
-       struct windowsaio_data *wd = td->io_ops->data;
+       struct windowsaio_data *wd = td->io_ops_data;
        return wd->aio_events[event];
 }
 
 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
-                                       unsigned int max, struct timespec *t)
+                                   unsigned int max,
+                                   const struct timespec *t)
 {
-       struct windowsaio_data *wd = td->io_ops->data;
-       struct flist_head *entry;
+       struct windowsaio_data *wd = td->io_ops_data;
        unsigned int dequeued = 0;
        struct io_u *io_u;
+       int i;
        struct fio_overlapped *fov;
        DWORD start_count = 0;
        DWORD end_count = 0;
@@ -282,20 +281,21 @@ static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
        }
 
        do {
-               flist_for_each(entry, &td->io_u_busylist) {
-                       io_u = flist_entry(entry, struct io_u, list);
+               io_u_qiter(&td->io_u_all, io_u, i) {
+                       if (!(io_u->flags & IO_U_F_FLIGHT))
+                               continue;
+
                        fov = (struct fio_overlapped*)io_u->engine_data;
 
                        if (fov->io_complete) {
                                fov->io_complete = FALSE;
-                               ResetEvent(fov->o.hEvent);
                                wd->aio_events[dequeued] = io_u;
                                dequeued++;
                        }
 
-                       if (dequeued >= min)
-                               break;
                }
+               if (dequeued >= min)
+                       break;
 
                if (dequeued < min) {
                        status = WaitForSingleObject(wd->iocomplete_event, mswait);
@@ -314,23 +314,22 @@ static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
 {
        struct fio_overlapped *o = io_u->engine_data;
        LPOVERLAPPED lpOvl = &o->o;
-       DWORD iobytes;
        BOOL success = FALSE;
        int rc = FIO_Q_COMPLETED;
 
        fio_ro_check(td, io_u);
 
-       lpOvl->Internal = STATUS_PENDING;
+       lpOvl->Internal = 0;
        lpOvl->InternalHigh = 0;
        lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
        lpOvl->OffsetHigh = io_u->offset >> 32;
 
        switch (io_u->ddir) {
        case DDIR_WRITE:
-               success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
+               success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
                break;
        case DDIR_READ:
-               success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
+               success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
                break;
        case DDIR_SYNC:
        case DDIR_DATASYNC:
@@ -402,33 +401,11 @@ static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
        return 0;
 }
 
-static int fio_windowsaio_cancel(struct thread_data *td,
-                                  struct io_u *io_u)
-{
-       int rc = 0;
-
-       struct windowsaio_data *wd = td->io_ops->data;
-
-       /* If we're running on Vista or newer, we can cancel individual IO requests */
-       if (wd->pCancelIoEx != NULL) {
-               struct fio_overlapped *ovl = io_u->engine_data;
-
-               if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o)) {
-                       log_err("windowsaio: failed to cancel io\n");
-                       rc = 1;
-               }
-       } else
-               rc = 1;
-
-       return rc;
-}
-
 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
 {
        struct fio_overlapped *o = io_u->engine_data;
 
        if (o) {
-               CloseHandle(o->o.hEvent);
                io_u->engine_data = NULL;
                free(o);
        }
@@ -441,13 +418,7 @@ static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
        o = malloc(sizeof(*o));
        o->io_complete = FALSE;
        o->io_u = io_u;
-       o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
-       if (o->o.hEvent == NULL) {
-               log_err("windowsaio: failed to create event handle\n");
-               free(o);
-               return 1;
-       }
-
+       o->o.hEvent = NULL;
        io_u->engine_data = o;
        return 0;
 }
@@ -457,7 +428,6 @@ static struct ioengine_ops ioengine = {
        .version        = FIO_IOOPS_VERSION,
        .init           = fio_windowsaio_init,
        .queue          = fio_windowsaio_queue,
-       .cancel         = fio_windowsaio_cancel,
        .getevents      = fio_windowsaio_getevents,
        .event          = fio_windowsaio_event,
        .cleanup        = fio_windowsaio_cleanup,