4 * IO engine using Windows IO Completion Ports.
15 typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
17 int geterrno_from_win_error (DWORD code, int deferrno);
19 struct fio_overlapped {
25 struct windowsaio_data {
26 struct io_u **aio_events;
29 HANDLE iocomplete_event;
30 BOOL iothread_running;
35 struct windowsaio_data *wd;
38 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
40 static int fio_windowsaio_init(struct thread_data *td)
42 struct windowsaio_data *wd;
45 wd = calloc(1, sizeof(struct windowsaio_data));
47 log_err("windowsaio: failed to allocate memory for engine data\n");
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");
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");
70 if (wd->aio_events != NULL)
80 struct thread_ctx *ctx;
81 struct windowsaio_data *wd;
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");
91 wd->iothread_running = TRUE;
95 ctx = malloc(sizeof(struct thread_ctx));
97 if (!rc && ctx == NULL)
99 log_err("windowsaio: failed to allocate memory for thread context structure\n");
110 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, &threadid);
112 if (wd->iothread != NULL)
113 fio_setaffinity(threadid, td->o.cpumask);
115 log_err("windowsaio: failed to create io completion thread\n");
118 if (rc || wd->iothread == NULL)
125 static void fio_windowsaio_cleanup(struct thread_data *td)
127 struct windowsaio_data *wd;
129 wd = td->io_ops_data;
132 wd->iothread_running = FALSE;
133 WaitForSingleObject(wd->iothread, INFINITE);
135 CloseHandle(wd->iothread);
136 CloseHandle(wd->iocomplete_event);
138 free(wd->aio_events);
141 td->io_ops_data = NULL;
145 static int windowsaio_invalidate_cache(struct fio_file *f)
148 DWORD isharemode = (FILE_SHARE_DELETE | FILE_SHARE_READ |
154 * Encourage Windows to drop cached parts of a file by temporarily
155 * opening it for non-buffered access. Note: this will only work when
156 * the following is the only thing with the file open on the whole
159 dprint(FD_IO, "windowaio: attempt invalidate cache for %s\n",
161 ihFile = CreateFile(f->file_name, 0, isharemode, NULL, OPEN_EXISTING,
162 FILE_FLAG_NO_BUFFERING, NULL);
164 if (ihFile != INVALID_HANDLE_VALUE) {
165 if (!CloseHandle(ihFile)) {
166 error = GetLastError();
167 log_info("windowsaio: invalidation fd close %s "
168 "failed: error %d\n", f->file_name, error);
172 error = GetLastError();
173 if (error != ERROR_FILE_NOT_FOUND) {
174 log_info("windowsaio: cache invalidation of %s failed: "
175 "error %d\n", f->file_name, error);
183 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
186 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
187 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
188 DWORD openmode = OPEN_ALWAYS;
191 dprint(FD_FILE, "fd open %s\n", f->file_name);
193 if (f->filetype == FIO_TYPE_PIPE) {
194 log_err("windowsaio: pipes are not supported\n");
198 if (!strcmp(f->file_name, "-")) {
199 log_err("windowsaio: can't read/write to stdin/out\n");
204 flags |= FILE_FLAG_NO_BUFFERING;
206 flags |= FILE_FLAG_WRITE_THROUGH;
209 * Inform Windows whether we're going to be doing sequential or
210 * random IO so it can tune the Cache Manager
212 switch (td->o.fadvise_hint) {
215 flags |= FILE_FLAG_RANDOM_ACCESS;
217 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
220 flags |= FILE_FLAG_RANDOM_ACCESS;
222 case F_ADV_SEQUENTIAL:
223 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
228 log_err("fio: unknown fadvise type %d\n", td->o.fadvise_hint);
231 if (!td_write(td) || read_only)
232 access = GENERIC_READ;
234 access = (GENERIC_READ | GENERIC_WRITE);
236 if (td->o.create_on_open)
237 openmode = OPEN_ALWAYS;
239 openmode = OPEN_EXISTING;
241 /* If we're going to use direct I/O, Windows will try and invalidate
242 * its cache at that point so there's no need to do it here */
243 if (td->o.invalidate_cache && !td->o.odirect) {
244 windowsaio_invalidate_cache(f);
247 f->hFile = CreateFile(f->file_name, access, sharemode,
248 NULL, openmode, flags, NULL);
250 if (f->hFile == INVALID_HANDLE_VALUE) {
251 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
255 /* Only set up the completion port and thread if we're not just
256 * querying the device size */
257 if (!rc && td->io_ops_data != NULL) {
258 struct windowsaio_data *wd;
260 wd = td->io_ops_data;
262 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
263 log_err("windowsaio: failed to create io completion port\n");
271 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
275 dprint(FD_FILE, "fd close %s\n", f->file_name);
277 if (f->hFile != INVALID_HANDLE_VALUE) {
278 if (!CloseHandle(f->hFile)) {
279 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
284 f->hFile = INVALID_HANDLE_VALUE;
288 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
290 BOOL expired = FALSE;
293 current_time = GetTickCount();
295 if ((end_count > start_count) && current_time >= end_count)
297 else if (current_time < start_count && current_time > end_count)
303 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
305 struct windowsaio_data *wd = td->io_ops_data;
306 return wd->aio_events[event];
309 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
311 const struct timespec *t)
313 struct windowsaio_data *wd = td->io_ops_data;
314 unsigned int dequeued = 0;
317 struct fio_overlapped *fov;
318 DWORD start_count = 0;
324 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
325 start_count = GetTickCount();
326 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
330 io_u_qiter(&td->io_u_all, io_u, i) {
331 if (!(io_u->flags & IO_U_F_FLIGHT))
334 fov = (struct fio_overlapped*)io_u->engine_data;
336 if (fov->io_complete) {
337 fov->io_complete = FALSE;
338 wd->aio_events[dequeued] = io_u;
346 if (dequeued < min) {
347 status = WaitForSingleObject(wd->iocomplete_event, mswait);
348 if (status != WAIT_OBJECT_0 && dequeued >= min)
352 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
359 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
361 struct fio_overlapped *o = io_u->engine_data;
362 LPOVERLAPPED lpOvl = &o->o;
363 BOOL success = FALSE;
364 int rc = FIO_Q_COMPLETED;
366 fio_ro_check(td, io_u);
369 lpOvl->InternalHigh = 0;
370 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
371 lpOvl->OffsetHigh = io_u->offset >> 32;
373 switch (io_u->ddir) {
375 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
378 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
382 case DDIR_SYNC_FILE_RANGE:
383 success = FlushFileBuffers(io_u->file->hFile);
385 log_err("windowsaio: failed to flush file buffers\n");
386 io_u->error = win_to_posix_error(GetLastError());
389 return FIO_Q_COMPLETED;
392 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
394 io_u->resid = io_u->xfer_buflen;
395 return FIO_Q_COMPLETED;
402 if (success || GetLastError() == ERROR_IO_PENDING)
405 io_u->error = win_to_posix_error(GetLastError());
406 io_u->resid = io_u->xfer_buflen;
412 /* Runs as a thread and waits for queued IO to complete */
413 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
416 struct fio_overlapped *fov;
418 struct windowsaio_data *wd;
419 struct thread_ctx *ctx;
423 ctx = (struct thread_ctx*)lpParameter;
427 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
430 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
433 if (ovl->Internal == ERROR_SUCCESS) {
434 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
437 io_u->resid = io_u->xfer_buflen;
438 io_u->error = win_to_posix_error(GetLastError());
441 fov->io_complete = TRUE;
442 SetEvent(wd->iocomplete_event);
443 } while (ctx->wd->iothread_running);
445 CloseHandle(ctx->iocp);
450 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
452 struct fio_overlapped *o = io_u->engine_data;
455 io_u->engine_data = NULL;
460 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
462 struct fio_overlapped *o;
464 o = malloc(sizeof(*o));
465 o->io_complete = FALSE;
468 io_u->engine_data = o;
472 static struct ioengine_ops ioengine = {
473 .name = "windowsaio",
474 .version = FIO_IOOPS_VERSION,
475 .init = fio_windowsaio_init,
476 .queue = fio_windowsaio_queue,
477 .getevents = fio_windowsaio_getevents,
478 .event = fio_windowsaio_event,
479 .cleanup = fio_windowsaio_cleanup,
480 .open_file = fio_windowsaio_open_file,
481 .close_file = fio_windowsaio_close_file,
482 .get_file_size = generic_get_file_size,
483 .io_u_init = fio_windowsaio_io_u_init,
484 .io_u_free = fio_windowsaio_io_u_free,
487 static void fio_init fio_windowsaio_register(void)
489 register_ioengine(&ioengine);
492 static void fio_exit fio_windowsaio_unregister(void)
494 unregister_ioengine(&ioengine);