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) {
98 log_err("windowsaio: failed to allocate memory for thread context structure\n");
108 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, &threadid);
110 if (wd->iothread != NULL)
111 fio_setaffinity(threadid, td->o.cpumask);
113 log_err("windowsaio: failed to create io completion thread\n");
116 if (rc || wd->iothread == NULL)
123 static void fio_windowsaio_cleanup(struct thread_data *td)
125 struct windowsaio_data *wd;
127 wd = td->io_ops_data;
130 wd->iothread_running = FALSE;
131 WaitForSingleObject(wd->iothread, INFINITE);
133 CloseHandle(wd->iothread);
134 CloseHandle(wd->iocomplete_event);
136 free(wd->aio_events);
139 td->io_ops_data = NULL;
143 static int windowsaio_invalidate_cache(struct fio_file *f)
146 DWORD isharemode = (FILE_SHARE_DELETE | FILE_SHARE_READ |
152 * Encourage Windows to drop cached parts of a file by temporarily
153 * opening it for non-buffered access. Note: this will only work when
154 * the following is the only thing with the file open on the whole
157 dprint(FD_IO, "windowaio: attempt invalidate cache for %s\n",
159 ihFile = CreateFile(f->file_name, 0, isharemode, NULL, OPEN_EXISTING,
160 FILE_FLAG_NO_BUFFERING, NULL);
162 if (ihFile != INVALID_HANDLE_VALUE) {
163 if (!CloseHandle(ihFile)) {
164 error = GetLastError();
165 log_info("windowsaio: invalidation fd close %s "
166 "failed: error %d\n", f->file_name, error);
170 error = GetLastError();
171 if (error != ERROR_FILE_NOT_FOUND) {
172 log_info("windowsaio: cache invalidation of %s failed: "
173 "error %d\n", f->file_name, error);
181 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
184 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
185 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
186 DWORD openmode = OPEN_ALWAYS;
189 dprint(FD_FILE, "fd open %s\n", f->file_name);
191 if (f->filetype == FIO_TYPE_PIPE) {
192 log_err("windowsaio: pipes are not supported\n");
196 if (!strcmp(f->file_name, "-")) {
197 log_err("windowsaio: can't read/write to stdin/out\n");
202 flags |= FILE_FLAG_NO_BUFFERING;
204 flags |= FILE_FLAG_WRITE_THROUGH;
207 * Inform Windows whether we're going to be doing sequential or
208 * random IO so it can tune the Cache Manager
210 switch (td->o.fadvise_hint) {
213 flags |= FILE_FLAG_RANDOM_ACCESS;
215 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
218 flags |= FILE_FLAG_RANDOM_ACCESS;
220 case F_ADV_SEQUENTIAL:
221 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
226 log_err("fio: unknown fadvise type %d\n", td->o.fadvise_hint);
229 if (!td_write(td) || read_only)
230 access = GENERIC_READ;
232 access = (GENERIC_READ | GENERIC_WRITE);
234 if (td->o.create_on_open)
235 openmode = OPEN_ALWAYS;
237 openmode = OPEN_EXISTING;
239 /* If we're going to use direct I/O, Windows will try and invalidate
240 * its cache at that point so there's no need to do it here */
241 if (td->o.invalidate_cache && !td->o.odirect)
242 windowsaio_invalidate_cache(f);
244 f->hFile = CreateFile(f->file_name, access, sharemode,
245 NULL, openmode, flags, NULL);
247 if (f->hFile == INVALID_HANDLE_VALUE) {
248 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
252 /* Only set up the completion port and thread if we're not just
253 * querying the device size */
254 if (!rc && td->io_ops_data != NULL) {
255 struct windowsaio_data *wd;
257 wd = td->io_ops_data;
259 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
260 log_err("windowsaio: failed to create io completion port\n");
268 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
272 dprint(FD_FILE, "fd close %s\n", f->file_name);
274 if (f->hFile != INVALID_HANDLE_VALUE) {
275 if (!CloseHandle(f->hFile)) {
276 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
281 f->hFile = INVALID_HANDLE_VALUE;
285 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
287 BOOL expired = FALSE;
290 current_time = GetTickCount();
292 if ((end_count > start_count) && current_time >= end_count)
294 else if (current_time < start_count && current_time > end_count)
300 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
302 struct windowsaio_data *wd = td->io_ops_data;
303 return wd->aio_events[event];
306 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
308 const struct timespec *t)
310 struct windowsaio_data *wd = td->io_ops_data;
311 unsigned int dequeued = 0;
314 struct fio_overlapped *fov;
315 DWORD start_count = 0;
321 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
322 start_count = GetTickCount();
323 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
327 io_u_qiter(&td->io_u_all, io_u, i) {
328 if (!(io_u->flags & IO_U_F_FLIGHT))
331 fov = (struct fio_overlapped*)io_u->engine_data;
333 if (fov->io_complete) {
334 fov->io_complete = FALSE;
335 wd->aio_events[dequeued] = io_u;
343 if (dequeued < min) {
344 status = WaitForSingleObject(wd->iocomplete_event, mswait);
345 if (status != WAIT_OBJECT_0 && dequeued >= min)
349 if (dequeued >= min ||
350 (t != NULL && timeout_expired(start_count, end_count)))
357 static enum fio_q_status fio_windowsaio_queue(struct thread_data *td,
360 struct fio_overlapped *o = io_u->engine_data;
361 LPOVERLAPPED lpOvl = &o->o;
362 BOOL success = FALSE;
363 int rc = FIO_Q_COMPLETED;
365 fio_ro_check(td, io_u);
368 lpOvl->InternalHigh = 0;
369 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
370 lpOvl->OffsetHigh = io_u->offset >> 32;
372 switch (io_u->ddir) {
374 success = WriteFile(io_u->file->hFile, io_u->xfer_buf,
375 io_u->xfer_buflen, NULL, lpOvl);
378 success = ReadFile(io_u->file->hFile, io_u->xfer_buf,
379 io_u->xfer_buflen, NULL, lpOvl);
383 case DDIR_SYNC_FILE_RANGE:
384 success = FlushFileBuffers(io_u->file->hFile);
386 log_err("windowsaio: failed to flush file buffers\n");
387 io_u->error = win_to_posix_error(GetLastError());
390 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;
401 if (success || GetLastError() == ERROR_IO_PENDING)
404 io_u->error = win_to_posix_error(GetLastError());
405 io_u->resid = io_u->xfer_buflen;
411 /* Runs as a thread and waits for queued IO to complete */
412 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
415 struct fio_overlapped *fov;
417 struct windowsaio_data *wd;
418 struct thread_ctx *ctx;
422 ctx = (struct thread_ctx*)lpParameter;
428 ret = GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey,
430 if (!ret && ovl == NULL)
433 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
436 if (ovl->Internal == ERROR_SUCCESS) {
437 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
440 io_u->resid = io_u->xfer_buflen;
441 io_u->error = win_to_posix_error(GetLastError());
444 fov->io_complete = TRUE;
445 SetEvent(wd->iocomplete_event);
446 } while (ctx->wd->iothread_running);
448 CloseHandle(ctx->iocp);
453 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
455 struct fio_overlapped *o = io_u->engine_data;
458 io_u->engine_data = NULL;
463 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
465 struct fio_overlapped *o;
467 o = malloc(sizeof(*o));
468 o->io_complete = FALSE;
471 io_u->engine_data = o;
475 static struct ioengine_ops ioengine = {
476 .name = "windowsaio",
477 .version = FIO_IOOPS_VERSION,
478 .init = fio_windowsaio_init,
479 .queue = fio_windowsaio_queue,
480 .getevents = fio_windowsaio_getevents,
481 .event = fio_windowsaio_event,
482 .cleanup = fio_windowsaio_cleanup,
483 .open_file = fio_windowsaio_open_file,
484 .close_file = fio_windowsaio_close_file,
485 .get_file_size = generic_get_file_size,
486 .io_u_init = fio_windowsaio_io_u_init,
487 .io_u_free = fio_windowsaio_io_u_free,
490 static void fio_init fio_windowsaio_register(void)
492 register_ioengine(&ioengine);
495 static void fio_exit fio_windowsaio_unregister(void)
497 unregister_ioengine(&ioengine);