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