Move Windows port to MinGW
[fio.git] / engines / windowsaio.c
index 299acc47a5f3651d6c772d5d02fbef0569b4b5d3..766cc5d4fe6f0b684cfb6f0921a3d4b190aa0c85 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Native Windows async IO engine
- * Copyright (C) 2011 Bruce Cran <bruce@cran.org.uk>
+ * Copyright (C) 2012 Bruce Cran <bruce@cran.org.uk>
  */
 
 #include <stdio.h>
@@ -18,7 +18,7 @@ struct fio_overlapped {
        OVERLAPPED o;
        struct io_u *io_u;
        BOOL io_complete;
-    BOOL io_free;
+       BOOL io_free;
 };
 
 struct windowsaio_data {
@@ -28,7 +28,6 @@ struct windowsaio_data {
        HANDLE iocomplete_event;
        CANCELIOEX pCancelIoEx;
        BOOL iothread_running;
-       BOOL use_iocp;
 };
 
 struct thread_ctx {
@@ -36,7 +35,6 @@ struct thread_ctx {
        struct windowsaio_data *wd;
 };
 
-static void PrintError(LPCSTR lpszFunction);
 static int fio_windowsaio_cancel(struct thread_data *td,
                               struct io_u *io_u);
 static BOOL timeout_expired(DWORD start_count, DWORD end_count);
@@ -58,27 +56,6 @@ int sync_file_range(int fd, off64_t offset, off64_t nbytes,
        return -1;
 }
 
-static void PrintError(LPCSTR lpszFunction)
-{
-       // Retrieve the system error message for the last-error code
-
-       LPSTR lpMsgBuf;
-       DWORD dw = GetLastError();
-
-       FormatMessage(
-               FORMAT_MESSAGE_ALLOCATE_BUFFER |
-               FORMAT_MESSAGE_FROM_SYSTEM |
-               FORMAT_MESSAGE_IGNORE_INSERTS,
-               NULL,
-               dw,
-               MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-               (LPTSTR)&lpMsgBuf,
-               0, NULL );
-
-       log_err("%s - %s", lpszFunction, lpMsgBuf);
-       LocalFree(lpMsgBuf);
-}
-
 static int fio_windowsaio_init(struct thread_data *td)
 {
        struct windowsaio_data *wd;
@@ -125,7 +102,6 @@ static int fio_windowsaio_init(struct thread_data *td)
        }
 
        if (rc) {
-               PrintError(__func__);
                if (wd != NULL) {
                        if (wd->ovls != NULL)
                                free(wd->ovls);
@@ -137,10 +113,10 @@ static int fio_windowsaio_init(struct thread_data *td)
        }
 
        hKernel32Dll = GetModuleHandle("kernel32.dll");
-       wd->pCancelIoEx = GetProcAddress(hKernel32Dll, "CancelIoEx");
-
+       wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
        td->io_ops->data = wd;
-       return 0;
+
+       return rc;
 }
 
 static void fio_windowsaio_cleanup(struct thread_data *td)
@@ -151,8 +127,8 @@ static void fio_windowsaio_cleanup(struct thread_data *td)
        wd = td->io_ops->data;
 
        if (wd != NULL) {
-           wd->iothread_running = FALSE;
-       WaitForSingleObject(wd->iothread, INFINITE);
+               wd->iothread_running = FALSE;
+               WaitForSingleObject(wd->iothread, INFINITE);
 
                CloseHandle(wd->iothread);
                CloseHandle(wd->iocomplete_event);
@@ -174,7 +150,7 @@ static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
 {
        int rc = 0;
        HANDLE hFile;
-       DWORD flags = FILE_FLAG_POSIX_SEMANTICS;
+       DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
        DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
        DWORD openmode = OPEN_ALWAYS;
        DWORD access;
@@ -191,15 +167,15 @@ static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
                return 1;
        }
 
-    if (!td->o.odirect && !td->o.sync_io && td->io_ops->data != NULL)
-           flags |= FILE_FLAG_OVERLAPPED;
-
        if (td->o.odirect)
                flags |= FILE_FLAG_NO_BUFFERING;
        if (td->o.sync_io)
                flags |= FILE_FLAG_WRITE_THROUGH;
 
-
+       /*
+        * Inform Windows whether we're going to be doing sequential or
+        * random io so it can tune the Cache Manager
+        */
        if (td->o.td_ddir == TD_DDIR_READ  ||
                td->o.td_ddir == TD_DDIR_WRITE)
                flags |= FILE_FLAG_SEQUENTIAL_SCAN;
@@ -211,7 +187,7 @@ static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
        else
                access = (GENERIC_READ | GENERIC_WRITE);
 
-       if (td->o.create_on_open > 0)
+       if (td->o.create_on_open)
                openmode = OPEN_ALWAYS;
        else
                openmode = OPEN_EXISTING;
@@ -219,25 +195,18 @@ static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
        f->hFile = CreateFile(f->file_name, access, sharemode,
                NULL, openmode, flags, NULL);
 
-       if (f->hFile == INVALID_HANDLE_VALUE) {
-               PrintError(__func__);
+       if (f->hFile == INVALID_HANDLE_VALUE)
                rc = 1;
-       }
 
-       /* Only set up the competion port and thread if we're not just
+       /* Only set up the completion port and thread if we're not just
         * querying the device size */
-    if (!rc && td->io_ops->data != NULL && !td->o.odirect && !td->o.sync_io) {
+       if (!rc && td->io_ops->data != NULL) {
                struct thread_ctx *ctx;
-        struct windowsaio_data *wd;
-               hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
-
-        wd = td->io_ops->data;
+               struct windowsaio_data *wd;
 
-        if (!td->o.odirect && !td->o.sync_io)
-            wd->use_iocp = 1;
-        else
-            wd->use_iocp = 0;
+               hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
 
+               wd = td->io_ops->data;
                wd->iothread_running = TRUE;
 
                if (!rc) {
@@ -248,10 +217,8 @@ static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
                        wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
                }
 
-               if (rc || wd->iothread == NULL) {
-                       PrintError(__func__);
+               if (rc || wd->iothread == NULL)
                        rc = 1;
-               }
        }
 
        return rc;
@@ -259,15 +226,17 @@ static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
 
 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
 {
+       int rc = 0;
+
        dprint(FD_FILE, "fd close %s\n", f->file_name);
 
        if (f->hFile != INVALID_HANDLE_VALUE) {
                if (!CloseHandle(f->hFile))
-                       PrintError(__func__);
+                       rc = 1;
        }
 
        f->hFile = INVALID_HANDLE_VALUE;
-       return 0;
+       return rc;
 }
 
 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
@@ -316,7 +285,7 @@ static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
                        fov = (struct fio_overlapped*)io_u->engine_data;
 
                        if (fov->io_complete) {
-                fov->io_complete = FALSE;
+                               fov->io_complete = FALSE;
                                fov->io_free  = TRUE;
                                wd->aio_events[dequeued] = io_u;
                                dequeued++;
@@ -326,7 +295,7 @@ static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
                                break;
                }
 
-        if (dequeued < min) {
+               if (dequeued < min) {
                        status = WaitForSingleObject(wd->iocomplete_event, mswait);
                        if (status != WAIT_OBJECT_0 && dequeued > 0)
                            break;
@@ -339,13 +308,12 @@ static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
        return dequeued;
 }
 
-static int fio_windowsaio_queue(struct thread_data *td,
-                             struct io_u *io_u)
+static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
 {
-    LPOVERLAPPED lpOvl = NULL;
+       LPOVERLAPPED lpOvl = NULL;
        struct windowsaio_data *wd;
        DWORD iobytes;
-       BOOL success;
+       BOOL success = FALSE;
        int index;
        int rc = FIO_Q_COMPLETED;
 
@@ -353,29 +321,26 @@ static int fio_windowsaio_queue(struct thread_data *td,
 
        wd = td->io_ops->data;
 
-       if (wd->use_iocp) {
-           for (index = 0; index < td->o.iodepth; index++) {
-               if (wd->ovls[index].io_free) {
-                wd->ovls[index].io_free = FALSE;
-                   ResetEvent(wd->ovls[index].o.hEvent);
-                   break;
-               }
-        }
-
-        assert(index < td->o.iodepth);
-
-        lpOvl = &wd->ovls[index].o;
-           wd->ovls[index].io_u = io_u;
-       lpOvl->Internal = STATUS_PENDING;
-       lpOvl->InternalHigh = 0;
-       lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
-       lpOvl->OffsetHigh = io_u->offset >> 32;
-       lpOvl->Pointer = NULL;
-        io_u->engine_data = &wd->ovls[index];
+       for (index = 0; index < td->o.iodepth; index++) {
+               if (wd->ovls[index].io_free) {
+                       wd->ovls[index].io_free = FALSE;
+                       ResetEvent(wd->ovls[index].o.hEvent);
+                       break;
+               }
        }
 
+       assert(index < td->o.iodepth);
+
+       lpOvl = &wd->ovls[index].o;
+       wd->ovls[index].io_u = io_u;
+       lpOvl->Internal = STATUS_PENDING;
+       lpOvl->InternalHigh = 0;
+       lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
+       lpOvl->OffsetHigh = io_u->offset >> 32;
+       io_u->engine_data = &wd->ovls[index];
+
        switch (io_u->ddir) {
-    case DDIR_WRITE:
+       case DDIR_WRITE:
                success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
                break;
        case DDIR_READ:
@@ -398,15 +363,12 @@ static int fio_windowsaio_queue(struct thread_data *td,
                break;
        default:
                assert(0);
+               break;
        }
 
-    if (wd->use_iocp && (success || GetLastError() == ERROR_IO_PENDING)) {
+       if (success || GetLastError() == ERROR_IO_PENDING)
                rc = FIO_Q_QUEUED;
-       } else if (success && !wd->use_iocp) {
-               io_u->resid = io_u->xfer_buflen - iobytes;
-               io_u->error = 0;
-       } else {
-               PrintError(__func__);
+       else {
                io_u->error = GetLastError();
                io_u->resid = io_u->xfer_buflen;
        }
@@ -443,7 +405,7 @@ static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
                        io_u->error = ovl->Internal;
                }
 
-        fov->io_complete = TRUE;
+               fov->io_complete = TRUE;
                SetEvent(wd->iocomplete_event);
        } while (ctx->wd->iothread_running);
 
@@ -462,6 +424,7 @@ static int fio_windowsaio_cancel(struct thread_data *td,
        /* 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))
                        rc = 1;
        } else