Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Native Windows async IO engine |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 3 | * Copyright (C) 2011 Bruce Cran <bruce@cran.org.uk> |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <unistd.h> |
| 9 | #include <signal.h> |
| 10 | #include <errno.h> |
| 11 | #include <windows.h> |
| 12 | |
| 13 | #include "../fio.h" |
| 14 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 15 | typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped); |
| 16 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 17 | typedef struct { |
| 18 | OVERLAPPED o; |
| 19 | struct io_u *io_u; |
| 20 | } FIO_OVERLAPPED; |
| 21 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 22 | struct windowsaio_data { |
| 23 | HANDLE *io_handles; |
| 24 | unsigned int io_index; |
| 25 | FIO_OVERLAPPED *ovls; |
| 26 | |
| 27 | HANDLE iothread; |
| 28 | HANDLE iothread_stopped; |
| 29 | BOOL iothread_running; |
| 30 | |
| 31 | struct io_u **aio_events; |
| 32 | HANDLE iocomplete_event; |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 33 | CANCELIOEX pCancelIoEx; |
| 34 | BOOL useIOCP; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 37 | struct thread_ctx { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 38 | HANDLE iocp; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 39 | struct windowsaio_data *wd; |
| 40 | }; |
| 41 | |
| 42 | static void PrintError(LPCSTR lpszFunction); |
| 43 | static int fio_windowsaio_cancel(struct thread_data *td, |
| 44 | struct io_u *io_u); |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 45 | static BOOL TimedOut(DWORD start_count, DWORD end_count); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 46 | static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min, |
| 47 | unsigned int max, struct timespec *t); |
| 48 | static struct io_u *fio_windowsaio_event(struct thread_data *td, int event); |
| 49 | static int fio_windowsaio_queue(struct thread_data *td, |
| 50 | struct io_u *io_u); |
| 51 | static void fio_windowsaio_cleanup(struct thread_data *td); |
| 52 | static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter); |
| 53 | static int fio_windowsaio_init(struct thread_data *td); |
| 54 | static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f); |
| 55 | static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f); |
| 56 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 57 | int sync_file_range(int fd, off64_t offset, off64_t nbytes, |
| 58 | unsigned int flags) |
| 59 | { |
| 60 | errno = ENOSYS; |
| 61 | return -1; |
| 62 | } |
| 63 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 64 | static void PrintError(LPCSTR lpszFunction) |
| 65 | { |
| 66 | // Retrieve the system error message for the last-error code |
| 67 | |
| 68 | LPSTR lpMsgBuf; |
| 69 | DWORD dw = GetLastError(); |
| 70 | |
| 71 | FormatMessage( |
| 72 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 73 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 74 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 75 | NULL, |
| 76 | dw, |
| 77 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 78 | (LPTSTR)&lpMsgBuf, |
| 79 | 0, NULL ); |
| 80 | |
| 81 | log_err("%s - %s", lpszFunction, lpMsgBuf); |
| 82 | LocalFree(lpMsgBuf); |
| 83 | } |
| 84 | |
| 85 | static int fio_windowsaio_cancel(struct thread_data *td, |
| 86 | struct io_u *io_u) |
| 87 | { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 88 | int rc = 0; |
| 89 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 90 | struct windowsaio_data *wd = td->io_ops->data; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 91 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 92 | /* If we're running on Vista or newer, we can cancel individual IO requests */ |
| 93 | if (wd->pCancelIoEx != NULL) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 94 | FIO_OVERLAPPED *ovl = io_u->engine_data; |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 95 | if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o)) |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 96 | rc = 1; |
| 97 | } else |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 98 | rc = 1; |
| 99 | |
| 100 | return rc; |
| 101 | } |
| 102 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 103 | static BOOL TimedOut(DWORD start_count, DWORD end_count) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 104 | { |
| 105 | BOOL expired = FALSE; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 106 | DWORD current_time; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 107 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 108 | current_time = GetTickCount(); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 109 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 110 | if ((end_count > start_count) && current_time >= end_count) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 111 | expired = TRUE; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 112 | else if (current_time < start_count && current_time > end_count) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 113 | expired = TRUE; |
| 114 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 115 | return expired; |
| 116 | } |
| 117 | |
| 118 | static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min, |
| 119 | unsigned int max, struct timespec *t) |
| 120 | { |
| 121 | struct windowsaio_data *wd = td->io_ops->data; |
| 122 | struct flist_head *entry; |
| 123 | unsigned int dequeued = 0; |
| 124 | struct io_u *io_u; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 125 | DWORD start_count = 0, end_count = 0; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 126 | BOOL timedout = FALSE; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 127 | unsigned int mswait = 100; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 128 | |
| 129 | if (t != NULL) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 130 | mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000); |
| 131 | start_count = GetTickCount(); |
| 132 | end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | while (dequeued < min && !timedout) { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 136 | flist_for_each(entry, &td->io_u_busylist) { |
| 137 | io_u = flist_entry(entry, struct io_u, list); |
| 138 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 139 | if (io_u->seen == 0) { |
| 140 | io_u->seen = 1; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 141 | wd->aio_events[dequeued] = io_u; |
| 142 | dequeued++; |
| 143 | } |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 144 | |
| 145 | if (dequeued == max) |
| 146 | break; |
| 147 | } |
| 148 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 149 | if (dequeued < min) |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 150 | WaitForSingleObject(wd->iocomplete_event, mswait); |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 151 | |
| 152 | if (t != NULL && TimedOut(start_count, end_count)) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 153 | timedout = TRUE; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 156 | return dequeued; |
| 157 | } |
| 158 | |
| 159 | static struct io_u *fio_windowsaio_event(struct thread_data *td, int event) |
| 160 | { |
| 161 | struct windowsaio_data *wd = td->io_ops->data; |
| 162 | return wd->aio_events[event]; |
| 163 | } |
| 164 | |
| 165 | static int fio_windowsaio_queue(struct thread_data *td, |
| 166 | struct io_u *io_u) |
| 167 | { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 168 | struct windowsaio_data *wd; |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 169 | LPOVERLAPPED lpOvl; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 170 | DWORD iobytes; |
| 171 | BOOL success = TRUE; |
| 172 | int ind; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 173 | int rc; |
| 174 | |
| 175 | fio_ro_check(td, io_u); |
| 176 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 177 | wd = td->io_ops->data; |
| 178 | ind = wd->io_index; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 179 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 180 | ResetEvent(wd->io_handles[ind]); |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 181 | |
| 182 | if (wd->useIOCP) { |
| 183 | lpOvl = &wd->ovls[ind].o; |
| 184 | |
| 185 | lpOvl->Internal = STATUS_PENDING; |
| 186 | lpOvl->InternalHigh = 0; |
| 187 | lpOvl->Offset = io_u->offset & 0xFFFFFFFF; |
| 188 | lpOvl->OffsetHigh = io_u->offset >> 32; |
| 189 | lpOvl->hEvent = wd->io_handles[ind]; |
| 190 | lpOvl->Pointer = NULL; |
| 191 | wd->ovls[ind].io_u = io_u; |
| 192 | } else { |
| 193 | lpOvl = NULL; |
| 194 | } |
Bruce Cran | e4db9fe | 2011-01-04 14:44:47 +0100 | [diff] [blame] | 195 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 196 | io_u->engine_data = &wd->ovls[ind]; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 197 | io_u->seen = 0; |
| 198 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 199 | if (io_u->ddir == DDIR_WRITE) { |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 200 | success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl); |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 201 | } else if (io_u->ddir == DDIR_READ) { |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 202 | success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl); |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 203 | } else if (io_u->ddir == DDIR_SYNC || |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 204 | io_u->ddir == DDIR_DATASYNC || |
| 205 | io_u->ddir == DDIR_SYNC_FILE_RANGE) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 206 | { |
| 207 | FlushFileBuffers(io_u->file->hFile); |
| 208 | return FIO_Q_COMPLETED; |
| 209 | } else if (io_u->ddir == DDIR_TRIM) { |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 210 | log_info("Manual TRIM isn't supported on Windows"); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 211 | return FIO_Q_COMPLETED; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 212 | } else |
| 213 | assert(0); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 214 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 215 | if (wd->useIOCP && (success || GetLastError() == ERROR_IO_PENDING)) { |
| 216 | wd->io_index = (wd->io_index + 1) % td->o.iodepth; |
| 217 | rc = FIO_Q_QUEUED; |
| 218 | } else if (success && !wd->useIOCP) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 219 | io_u->resid = io_u->xfer_buflen - iobytes; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 220 | io_u->error = 0; |
| 221 | rc = FIO_Q_COMPLETED; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 222 | } else { |
| 223 | PrintError(__func__); |
| 224 | io_u->error = GetLastError(); |
| 225 | io_u->resid = io_u->xfer_buflen; |
| 226 | rc = FIO_Q_COMPLETED; |
| 227 | } |
| 228 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 229 | return rc; |
| 230 | } |
| 231 | |
| 232 | static void fio_windowsaio_cleanup(struct thread_data *td) |
| 233 | { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 234 | int i; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 235 | struct windowsaio_data *wd; |
| 236 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 237 | wd = td->io_ops->data; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 238 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 239 | WaitForSingleObject(wd->iothread_stopped, INFINITE); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 240 | |
| 241 | if (wd != NULL) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 242 | CloseHandle(wd->iothread); |
| 243 | CloseHandle(wd->iothread_stopped); |
| 244 | CloseHandle(wd->iocomplete_event); |
| 245 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 246 | for (i = 0; i < td->o.iodepth; i++) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 247 | CloseHandle(wd->io_handles[i]); |
| 248 | } |
Bruce Cran | e4db9fe | 2011-01-04 14:44:47 +0100 | [diff] [blame] | 249 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 250 | free(wd->aio_events); |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 251 | free(wd->io_handles); |
| 252 | free(wd->ovls); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 253 | free(wd); |
Bruce Cran | e4db9fe | 2011-01-04 14:44:47 +0100 | [diff] [blame] | 254 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 255 | td->io_ops->data = NULL; |
| 256 | } |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 259 | /* Runs as a thread and waits for queued IO to complete */ |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 260 | static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter) |
| 261 | { |
| 262 | OVERLAPPED *ovl; |
| 263 | FIO_OVERLAPPED *fov; |
| 264 | struct io_u *io_u; |
| 265 | struct windowsaio_data *wd; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 266 | struct thread_ctx *ctx; |
| 267 | ULONG_PTR ulKey = 0; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 268 | DWORD bytes; |
| 269 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 270 | ctx = (struct thread_ctx*)lpParameter; |
| 271 | wd = ctx->wd; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 272 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 273 | do { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 274 | if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250)) |
| 275 | continue; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 276 | |
| 277 | fov = CONTAINING_RECORD(ovl, FIO_OVERLAPPED, o); |
| 278 | io_u = fov->io_u; |
| 279 | |
Bruce Cran | 1a958f0 | 2011-05-05 08:13:37 -0600 | [diff] [blame^] | 280 | /* We sometimes get an IO request that hasn't completed yet. Ignore it. */ |
| 281 | if (ovl->Internal == STATUS_PENDING) |
| 282 | continue; |
| 283 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 284 | if (ovl->Internal == ERROR_SUCCESS) { |
| 285 | io_u->resid = io_u->xfer_buflen - ovl->InternalHigh; |
| 286 | io_u->error = 0; |
| 287 | } else { |
| 288 | io_u->resid = io_u->xfer_buflen; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 289 | io_u->error = ovl->Internal; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 292 | SetEvent(wd->iocomplete_event); |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 293 | } while (ctx->wd->iothread_running); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 294 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 295 | CloseHandle(ctx->iocp); |
| 296 | SetEvent(ctx->wd->iothread_stopped); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 297 | free(ctx); |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 298 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static int fio_windowsaio_init(struct thread_data *td) |
| 303 | { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 304 | struct windowsaio_data *wd; |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 305 | HANDLE hKernel32Dll; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 306 | int rc = 0; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 307 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 308 | wd = malloc(sizeof(struct windowsaio_data)); |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 309 | if (wd != NULL) |
| 310 | ZeroMemory(wd, sizeof(struct windowsaio_data)); |
| 311 | else |
| 312 | rc = 1; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 313 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 314 | if (!rc) { |
| 315 | wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*)); |
| 316 | if (wd->aio_events == NULL) |
| 317 | rc = 1; |
Bruce Cran | e4db9fe | 2011-01-04 14:44:47 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 320 | if (!rc) { |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 321 | wd->io_handles = malloc(td->o.iodepth * sizeof(HANDLE)); |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 322 | if (wd->io_handles == NULL) |
| 323 | rc = 1; |
Bruce Cran | e4db9fe | 2011-01-04 14:44:47 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 326 | if (!rc) { |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 327 | wd->ovls = malloc(td->o.iodepth * sizeof(FIO_OVERLAPPED)); |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 328 | if (wd->ovls == NULL) |
| 329 | rc = 1; |
| 330 | } |
Bruce Cran | e4db9fe | 2011-01-04 14:44:47 +0100 | [diff] [blame] | 331 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 332 | if (!rc) { |
| 333 | /* Create an auto-reset event */ |
| 334 | wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 335 | if (wd->iocomplete_event == NULL) |
| 336 | rc = 1; |
| 337 | } |
| 338 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 339 | if (rc) { |
| 340 | PrintError(__func__); |
| 341 | if (wd != NULL) { |
| 342 | if (wd->ovls != NULL) |
| 343 | free(wd->ovls); |
| 344 | if (wd->io_handles != NULL) |
| 345 | free(wd->io_handles); |
| 346 | if (wd->aio_events != NULL) |
| 347 | free(wd->aio_events); |
| 348 | |
| 349 | free(wd); |
| 350 | } |
| 351 | } |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 352 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 353 | hKernel32Dll = GetModuleHandle("kernel32.dll"); |
| 354 | wd->pCancelIoEx = GetProcAddress(hKernel32Dll, "CancelIoEx"); |
| 355 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 356 | td->io_ops->data = wd; |
Bruce Cran | e4db9fe | 2011-01-04 14:44:47 +0100 | [diff] [blame] | 357 | return 0; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f) |
| 361 | { |
| 362 | int rc = 0; |
| 363 | HANDLE hFile; |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 364 | DWORD flags = FILE_FLAG_POSIX_SEMANTICS; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 365 | DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 366 | DWORD openmode = OPEN_ALWAYS; |
| 367 | DWORD access; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 368 | int i; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 369 | |
| 370 | dprint(FD_FILE, "fd open %s\n", f->file_name); |
| 371 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 372 | if (f->filetype == FIO_TYPE_PIPE) { |
| 373 | log_err("fio: windowsaio doesn't support pipes\n"); |
| 374 | return 1; |
| 375 | } |
| 376 | |
| 377 | if (!strcmp(f->file_name, "-")) { |
| 378 | log_err("fio: can't read/write to stdin/out\n"); |
| 379 | return 1; |
| 380 | } |
| 381 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 382 | if (!td->o.odirect && !td->o.sync_io && td->io_ops->data != NULL) |
| 383 | flags |= FILE_FLAG_OVERLAPPED; |
| 384 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 385 | if (td->o.odirect) |
| 386 | flags |= FILE_FLAG_NO_BUFFERING; |
| 387 | if (td->o.sync_io) |
| 388 | flags |= FILE_FLAG_WRITE_THROUGH; |
| 389 | |
| 390 | |
| 391 | if (td->o.td_ddir == TD_DDIR_READ || |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 392 | td->o.td_ddir == TD_DDIR_WRITE) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 393 | flags |= FILE_FLAG_SEQUENTIAL_SCAN; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 394 | else |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 395 | flags |= FILE_FLAG_RANDOM_ACCESS; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 396 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 397 | if (!td_write(td) || read_only) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 398 | access = GENERIC_READ; |
| 399 | else |
| 400 | access = (GENERIC_READ | GENERIC_WRITE); |
| 401 | |
| 402 | if (td->o.create_on_open > 0) |
| 403 | openmode = OPEN_ALWAYS; |
| 404 | else |
| 405 | openmode = OPEN_EXISTING; |
| 406 | |
| 407 | f->hFile = CreateFile(f->file_name, access, sharemode, |
| 408 | NULL, openmode, flags, NULL); |
| 409 | |
| 410 | if (f->hFile == INVALID_HANDLE_VALUE) { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 411 | PrintError(__func__); |
| 412 | rc = 1; |
| 413 | } |
| 414 | |
| 415 | /* Only set up the competion port and thread if we're not just |
| 416 | * querying the device size */ |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 417 | if (!rc && td->io_ops->data != NULL && !td->o.odirect && !td->o.sync_io) { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 418 | struct thread_ctx *ctx; |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 419 | struct windowsaio_data *wd; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 420 | hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0); |
| 421 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 422 | |
| 423 | wd = td->io_ops->data; |
| 424 | |
| 425 | if (!td->o.odirect && !td->o.sync_io) |
| 426 | wd->useIOCP = 1; |
| 427 | else |
| 428 | wd->useIOCP = 0; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 429 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 430 | wd->io_index = 0; |
| 431 | wd->iothread_running = TRUE; |
| 432 | /* Create a manual-reset event */ |
| 433 | wd->iothread_stopped = CreateEvent(NULL, TRUE, FALSE, NULL); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 434 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 435 | if (wd->iothread_stopped == NULL) |
| 436 | rc = 1; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 437 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 438 | if (!rc) { |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 439 | for (i = 0; i < td->o.iodepth; i++) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 440 | /* Create a manual-reset event for putting in OVERLAPPED */ |
| 441 | wd->io_handles[i] = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 442 | if (wd->io_handles[i] == NULL) { |
| 443 | PrintError(__func__); |
| 444 | rc = 1; |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (!rc) { |
| 451 | ctx = malloc(sizeof(struct thread_ctx)); |
| 452 | ctx->iocp = hFile; |
| 453 | ctx->wd = wd; |
| 454 | |
| 455 | wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL); |
| 456 | } |
| 457 | |
| 458 | if (rc || wd->iothread == NULL) { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 459 | PrintError(__func__); |
| 460 | rc = 1; |
| 461 | } |
| 462 | } |
| 463 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 464 | return rc; |
| 465 | } |
| 466 | |
| 467 | static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
| 468 | { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 469 | struct windowsaio_data *wd; |
| 470 | |
| 471 | dprint(FD_FILE, "fd close %s\n", f->file_name); |
| 472 | |
| 473 | if (td->io_ops->data != NULL) { |
| 474 | wd = td->io_ops->data; |
| 475 | wd->iothread_running = FALSE; |
| 476 | WaitForSingleObject(wd->iothread_stopped, INFINITE); |
| 477 | } |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 478 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 479 | if (f->hFile != INVALID_HANDLE_VALUE) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 480 | if (!CloseHandle(f->hFile)) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 481 | PrintError(__func__); |
| 482 | } |
| 483 | |
| 484 | f->hFile = INVALID_HANDLE_VALUE; |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static struct ioengine_ops ioengine = { |
| 489 | .name = "windowsaio", |
| 490 | .version = FIO_IOOPS_VERSION, |
| 491 | .init = fio_windowsaio_init, |
| 492 | .queue = fio_windowsaio_queue, |
| 493 | .cancel = fio_windowsaio_cancel, |
| 494 | .getevents = fio_windowsaio_getevents, |
| 495 | .event = fio_windowsaio_event, |
| 496 | .cleanup = fio_windowsaio_cleanup, |
| 497 | .open_file = fio_windowsaio_open_file, |
| 498 | .close_file = fio_windowsaio_close_file, |
| 499 | .get_file_size = generic_get_file_size |
| 500 | }; |
| 501 | |
| 502 | static void fio_init fio_posixaio_register(void) |
| 503 | { |
| 504 | register_ioengine(&ioengine); |
| 505 | } |
| 506 | |
| 507 | static void fio_exit fio_posixaio_unregister(void) |
| 508 | { |
| 509 | unregister_ioengine(&ioengine); |
| 510 | } |