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