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