| 1 | /* |
| 2 | * Native Windows async IO engine |
| 3 | * Copyright (C) 2012 Bruce Cran <bruce@cran.org.uk> |
| 4 | */ |
| 5 | |
| 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 | |
| 15 | typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped); |
| 16 | |
| 17 | struct fio_overlapped { |
| 18 | OVERLAPPED o; |
| 19 | struct io_u *io_u; |
| 20 | BOOL io_complete; |
| 21 | BOOL io_free; |
| 22 | }; |
| 23 | |
| 24 | struct windowsaio_data { |
| 25 | struct fio_overlapped *ovls; |
| 26 | struct io_u **aio_events; |
| 27 | HANDLE iothread; |
| 28 | HANDLE iocomplete_event; |
| 29 | CANCELIOEX pCancelIoEx; |
| 30 | BOOL iothread_running; |
| 31 | }; |
| 32 | |
| 33 | struct thread_ctx { |
| 34 | HANDLE iocp; |
| 35 | struct windowsaio_data *wd; |
| 36 | }; |
| 37 | |
| 38 | static int fio_windowsaio_cancel(struct thread_data *td, |
| 39 | struct io_u *io_u); |
| 40 | static BOOL timeout_expired(DWORD start_count, DWORD end_count); |
| 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 | |
| 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 | |
| 59 | static int fio_windowsaio_init(struct thread_data *td) |
| 60 | { |
| 61 | struct windowsaio_data *wd; |
| 62 | HANDLE hKernel32Dll; |
| 63 | int rc = 0; |
| 64 | int i; |
| 65 | |
| 66 | wd = malloc(sizeof(struct windowsaio_data)); |
| 67 | if (wd != NULL) |
| 68 | ZeroMemory(wd, sizeof(struct windowsaio_data)); |
| 69 | else |
| 70 | rc = 1; |
| 71 | |
| 72 | if (!rc) { |
| 73 | wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*)); |
| 74 | if (wd->aio_events == NULL) |
| 75 | rc = 1; |
| 76 | } |
| 77 | |
| 78 | if (!rc) { |
| 79 | wd->ovls = malloc(td->o.iodepth * sizeof(struct fio_overlapped)); |
| 80 | if (wd->ovls == NULL) |
| 81 | rc = 1; |
| 82 | } |
| 83 | |
| 84 | if (!rc) { |
| 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 | } |
| 95 | } |
| 96 | |
| 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 | |
| 104 | if (rc) { |
| 105 | if (wd != NULL) { |
| 106 | if (wd->ovls != NULL) |
| 107 | free(wd->ovls); |
| 108 | if (wd->aio_events != NULL) |
| 109 | free(wd->aio_events); |
| 110 | |
| 111 | free(wd); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | hKernel32Dll = GetModuleHandle("kernel32.dll"); |
| 116 | wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx"); |
| 117 | td->io_ops->data = wd; |
| 118 | |
| 119 | return rc; |
| 120 | } |
| 121 | |
| 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) { |
| 130 | wd->iothread_running = FALSE; |
| 131 | WaitForSingleObject(wd->iothread, INFINITE); |
| 132 | |
| 133 | CloseHandle(wd->iothread); |
| 134 | CloseHandle(wd->iocomplete_event); |
| 135 | |
| 136 | for (i = 0; i < td->o.iodepth; i++) { |
| 137 | CloseHandle(wd->ovls[i].o.hEvent); |
| 138 | } |
| 139 | |
| 140 | free(wd->aio_events); |
| 141 | free(wd->ovls); |
| 142 | free(wd); |
| 143 | |
| 144 | td->io_ops->data = NULL; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f) |
| 150 | { |
| 151 | int rc = 0; |
| 152 | HANDLE hFile; |
| 153 | DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED; |
| 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 | |
| 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 | |
| 175 | /* |
| 176 | * Inform Windows whether we're going to be doing sequential or |
| 177 | * random io so it can tune the Cache Manager |
| 178 | */ |
| 179 | if (td->o.td_ddir == TD_DDIR_READ || |
| 180 | td->o.td_ddir == TD_DDIR_WRITE) |
| 181 | flags |= FILE_FLAG_SEQUENTIAL_SCAN; |
| 182 | else |
| 183 | flags |= FILE_FLAG_RANDOM_ACCESS; |
| 184 | |
| 185 | if (!td_write(td) || read_only) |
| 186 | access = GENERIC_READ; |
| 187 | else |
| 188 | access = (GENERIC_READ | GENERIC_WRITE); |
| 189 | |
| 190 | if (td->o.create_on_open) |
| 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 | |
| 198 | if (f->hFile == INVALID_HANDLE_VALUE) |
| 199 | rc = 1; |
| 200 | |
| 201 | /* Only set up the completion port and thread if we're not just |
| 202 | * querying the device size */ |
| 203 | if (!rc && td->io_ops->data != NULL) { |
| 204 | struct thread_ctx *ctx; |
| 205 | struct windowsaio_data *wd; |
| 206 | |
| 207 | hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0); |
| 208 | |
| 209 | wd = td->io_ops->data; |
| 210 | wd->iothread_running = TRUE; |
| 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 | |
| 220 | if (rc || wd->iothread == NULL) |
| 221 | rc = 1; |
| 222 | } |
| 223 | |
| 224 | return rc; |
| 225 | } |
| 226 | |
| 227 | static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
| 228 | { |
| 229 | int rc = 0; |
| 230 | |
| 231 | dprint(FD_FILE, "fd close %s\n", f->file_name); |
| 232 | |
| 233 | if (f->hFile != INVALID_HANDLE_VALUE) { |
| 234 | if (!CloseHandle(f->hFile)) |
| 235 | rc = 1; |
| 236 | } |
| 237 | |
| 238 | f->hFile = INVALID_HANDLE_VALUE; |
| 239 | return rc; |
| 240 | } |
| 241 | |
| 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) { |
| 288 | fov->io_complete = FALSE; |
| 289 | fov->io_free = TRUE; |
| 290 | ResetEvent(fov->o.hEvent); |
| 291 | wd->aio_events[dequeued] = io_u; |
| 292 | dequeued++; |
| 293 | } |
| 294 | |
| 295 | if (dequeued >= min) |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | if (dequeued < min) { |
| 300 | status = WaitForSingleObject(wd->iocomplete_event, mswait); |
| 301 | if (status != WAIT_OBJECT_0 && dequeued >= min) |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count))) |
| 306 | break; |
| 307 | } while (1); |
| 308 | |
| 309 | return dequeued; |
| 310 | } |
| 311 | |
| 312 | static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u) |
| 313 | { |
| 314 | LPOVERLAPPED lpOvl = NULL; |
| 315 | struct windowsaio_data *wd; |
| 316 | DWORD iobytes; |
| 317 | BOOL success = FALSE; |
| 318 | int index; |
| 319 | int rc = FIO_Q_COMPLETED; |
| 320 | |
| 321 | fio_ro_check(td, io_u); |
| 322 | |
| 323 | wd = td->io_ops->data; |
| 324 | |
| 325 | for (index = 0; index < td->o.iodepth; index++) { |
| 326 | if (wd->ovls[index].io_free) |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | assert(index < td->o.iodepth); |
| 331 | |
| 332 | wd->ovls[index].io_free = FALSE; |
| 333 | wd->ovls[index].io_u = io_u; |
| 334 | lpOvl = &wd->ovls[index].o; |
| 335 | lpOvl->Internal = STATUS_PENDING; |
| 336 | lpOvl->InternalHigh = 0; |
| 337 | lpOvl->Offset = io_u->offset & 0xFFFFFFFF; |
| 338 | lpOvl->OffsetHigh = io_u->offset >> 32; |
| 339 | io_u->engine_data = &wd->ovls[index]; |
| 340 | |
| 341 | switch (io_u->ddir) { |
| 342 | case DDIR_WRITE: |
| 343 | success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl); |
| 344 | break; |
| 345 | case DDIR_READ: |
| 346 | success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl); |
| 347 | break; |
| 348 | case DDIR_SYNC: |
| 349 | case DDIR_DATASYNC: |
| 350 | case DDIR_SYNC_FILE_RANGE: |
| 351 | success = FlushFileBuffers(io_u->file->hFile); |
| 352 | if (!success) |
| 353 | io_u->error = GetLastError(); |
| 354 | |
| 355 | return FIO_Q_COMPLETED; |
| 356 | break; |
| 357 | case DDIR_TRIM: |
| 358 | log_err("manual TRIM isn't supported on Windows"); |
| 359 | io_u->error = 1; |
| 360 | io_u->resid = io_u->xfer_buflen; |
| 361 | return FIO_Q_COMPLETED; |
| 362 | break; |
| 363 | default: |
| 364 | assert(0); |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | if (success || GetLastError() == ERROR_IO_PENDING) |
| 369 | rc = FIO_Q_QUEUED; |
| 370 | else { |
| 371 | io_u->error = GetLastError(); |
| 372 | io_u->resid = io_u->xfer_buflen; |
| 373 | } |
| 374 | |
| 375 | return rc; |
| 376 | } |
| 377 | |
| 378 | /* Runs as a thread and waits for queued IO to complete */ |
| 379 | static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter) |
| 380 | { |
| 381 | OVERLAPPED *ovl; |
| 382 | struct fio_overlapped *fov; |
| 383 | struct io_u *io_u; |
| 384 | struct windowsaio_data *wd; |
| 385 | struct thread_ctx *ctx; |
| 386 | ULONG_PTR ulKey = 0; |
| 387 | DWORD bytes; |
| 388 | |
| 389 | ctx = (struct thread_ctx*)lpParameter; |
| 390 | wd = ctx->wd; |
| 391 | |
| 392 | do { |
| 393 | if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250)) |
| 394 | continue; |
| 395 | |
| 396 | fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o); |
| 397 | io_u = fov->io_u; |
| 398 | |
| 399 | if (ovl->Internal == ERROR_SUCCESS) { |
| 400 | io_u->resid = io_u->xfer_buflen - ovl->InternalHigh; |
| 401 | io_u->error = 0; |
| 402 | } else { |
| 403 | io_u->resid = io_u->xfer_buflen; |
| 404 | io_u->error = ovl->Internal; |
| 405 | } |
| 406 | |
| 407 | fov->io_complete = TRUE; |
| 408 | SetEvent(wd->iocomplete_event); |
| 409 | } while (ctx->wd->iothread_running); |
| 410 | |
| 411 | CloseHandle(ctx->iocp); |
| 412 | free(ctx); |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int fio_windowsaio_cancel(struct thread_data *td, |
| 417 | struct io_u *io_u) |
| 418 | { |
| 419 | int rc = 0; |
| 420 | |
| 421 | struct windowsaio_data *wd = td->io_ops->data; |
| 422 | |
| 423 | /* If we're running on Vista or newer, we can cancel individual IO requests */ |
| 424 | if (wd->pCancelIoEx != NULL) { |
| 425 | struct fio_overlapped *ovl = io_u->engine_data; |
| 426 | |
| 427 | if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o)) |
| 428 | rc = 1; |
| 429 | } else |
| 430 | rc = 1; |
| 431 | |
| 432 | return rc; |
| 433 | } |
| 434 | |
| 435 | static struct ioengine_ops ioengine = { |
| 436 | .name = "windowsaio", |
| 437 | .version = FIO_IOOPS_VERSION, |
| 438 | .init = fio_windowsaio_init, |
| 439 | .queue = fio_windowsaio_queue, |
| 440 | .cancel = fio_windowsaio_cancel, |
| 441 | .getevents = fio_windowsaio_getevents, |
| 442 | .event = fio_windowsaio_event, |
| 443 | .cleanup = fio_windowsaio_cleanup, |
| 444 | .open_file = fio_windowsaio_open_file, |
| 445 | .close_file = fio_windowsaio_close_file, |
| 446 | .get_file_size = generic_get_file_size |
| 447 | }; |
| 448 | |
| 449 | static void fio_init fio_posixaio_register(void) |
| 450 | { |
| 451 | register_ioengine(&ioengine); |
| 452 | } |
| 453 | |
| 454 | static void fio_exit fio_posixaio_unregister(void) |
| 455 | { |
| 456 | unregister_ioengine(&ioengine); |
| 457 | } |