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