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 log_err("windowsaio: failed to create io completion thread\n");
111 else if (fio_option_is_set(&td->o, cpumask))
112 fio_setaffinity(threadid, td->o.cpumask);
115 if (rc || wd->iothread == NULL)
122 static void fio_windowsaio_cleanup(struct thread_data *td)
124 struct windowsaio_data *wd;
126 wd = td->io_ops_data;
129 wd->iothread_running = FALSE;
130 WaitForSingleObject(wd->iothread, INFINITE);
132 CloseHandle(wd->iothread);
133 CloseHandle(wd->iocomplete_event);
135 free(wd->aio_events);
138 td->io_ops_data = NULL;
142 static int windowsaio_invalidate_cache(struct fio_file *f)
145 DWORD isharemode = (FILE_SHARE_DELETE | FILE_SHARE_READ |
151 * Encourage Windows to drop cached parts of a file by temporarily
152 * opening it for non-buffered access. Note: this will only work when
153 * the following is the only thing with the file open on the whole
156 dprint(FD_IO, "windowaio: attempt invalidate cache for %s\n",
158 ihFile = CreateFile(f->file_name, 0, isharemode, NULL, OPEN_EXISTING,
159 FILE_FLAG_NO_BUFFERING, NULL);
161 if (ihFile != INVALID_HANDLE_VALUE) {
162 if (!CloseHandle(ihFile)) {
163 error = GetLastError();
164 log_info("windowsaio: invalidation fd close %s failed: error %lu\n",
165 f->file_name, error);
169 error = GetLastError();
170 if (error != ERROR_FILE_NOT_FOUND) {
171 log_info("windowsaio: cache invalidation of %s failed: error %lu\n",
172 f->file_name, error);
180 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
183 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
184 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
185 DWORD openmode = OPEN_ALWAYS;
188 dprint(FD_FILE, "fd open %s\n", f->file_name);
190 if (f->filetype == FIO_TYPE_PIPE) {
191 log_err("windowsaio: pipes are not supported\n");
195 if (!strcmp(f->file_name, "-")) {
196 log_err("windowsaio: can't read/write to stdin/out\n");
201 flags |= FILE_FLAG_NO_BUFFERING;
203 flags |= FILE_FLAG_WRITE_THROUGH;
206 * Inform Windows whether we're going to be doing sequential or
207 * random IO so it can tune the Cache Manager
209 switch (td->o.fadvise_hint) {
212 flags |= FILE_FLAG_RANDOM_ACCESS;
214 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
217 flags |= FILE_FLAG_RANDOM_ACCESS;
219 case F_ADV_SEQUENTIAL:
220 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
225 log_err("fio: unknown fadvise type %d\n", td->o.fadvise_hint);
228 if (!td_write(td) || read_only)
229 access = GENERIC_READ;
231 access = (GENERIC_READ | GENERIC_WRITE);
233 if (td->o.create_on_open)
234 openmode = OPEN_ALWAYS;
236 openmode = OPEN_EXISTING;
238 /* If we're going to use direct I/O, Windows will try and invalidate
239 * its cache at that point so there's no need to do it here */
240 if (td->o.invalidate_cache && !td->o.odirect)
241 windowsaio_invalidate_cache(f);
243 f->hFile = CreateFile(f->file_name, access, sharemode,
244 NULL, openmode, flags, NULL);
246 if (f->hFile == INVALID_HANDLE_VALUE) {
247 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
251 /* Only set up the completion port and thread if we're not just
252 * querying the device size */
253 if (!rc && td->io_ops_data != NULL) {
254 struct windowsaio_data *wd;
256 wd = td->io_ops_data;
258 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
259 log_err("windowsaio: failed to create io completion port\n");
267 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
271 dprint(FD_FILE, "fd close %s\n", f->file_name);
273 if (f->hFile != INVALID_HANDLE_VALUE) {
274 if (!CloseHandle(f->hFile)) {
275 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
280 f->hFile = INVALID_HANDLE_VALUE;
284 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
286 BOOL expired = FALSE;
289 current_time = GetTickCount();
291 if ((end_count > start_count) && current_time >= end_count)
293 else if (current_time < start_count && current_time > end_count)
299 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
301 struct windowsaio_data *wd = td->io_ops_data;
302 return wd->aio_events[event];
305 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
307 const struct timespec *t)
309 struct windowsaio_data *wd = td->io_ops_data;
310 unsigned int dequeued = 0;
313 struct fio_overlapped *fov;
314 DWORD start_count = 0;
320 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
321 start_count = GetTickCount();
322 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
326 io_u_qiter(&td->io_u_all, io_u, i) {
327 if (!(io_u->flags & IO_U_F_FLIGHT))
330 fov = (struct fio_overlapped*)io_u->engine_data;
332 if (fov->io_complete) {
333 fov->io_complete = FALSE;
334 wd->aio_events[dequeued] = io_u;
342 if (dequeued < min) {
343 status = WaitForSingleObject(wd->iocomplete_event, mswait);
344 if (status != WAIT_OBJECT_0 && dequeued >= min)
348 if (dequeued >= min ||
349 (t != NULL && timeout_expired(start_count, end_count)))
356 static enum fio_q_status fio_windowsaio_queue(struct thread_data *td,
359 struct fio_overlapped *o = io_u->engine_data;
360 LPOVERLAPPED lpOvl = &o->o;
361 BOOL success = FALSE;
362 int rc = FIO_Q_COMPLETED;
364 fio_ro_check(td, io_u);
367 lpOvl->InternalHigh = 0;
368 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
369 lpOvl->OffsetHigh = io_u->offset >> 32;
371 switch (io_u->ddir) {
373 success = WriteFile(io_u->file->hFile, io_u->xfer_buf,
374 io_u->xfer_buflen, NULL, lpOvl);
377 success = ReadFile(io_u->file->hFile, io_u->xfer_buf,
378 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;
391 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
393 io_u->resid = io_u->xfer_buflen;
394 return FIO_Q_COMPLETED;
400 if (success || GetLastError() == ERROR_IO_PENDING)
403 io_u->error = win_to_posix_error(GetLastError());
404 io_u->resid = io_u->xfer_buflen;
410 /* Runs as a thread and waits for queued IO to complete */
411 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
414 struct fio_overlapped *fov;
416 struct windowsaio_data *wd;
417 struct thread_ctx *ctx;
421 ctx = (struct thread_ctx*)lpParameter;
427 ret = GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey,
429 if (!ret && ovl == NULL)
432 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
435 if (ovl->Internal == ERROR_SUCCESS) {
436 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
439 io_u->resid = io_u->xfer_buflen;
440 io_u->error = win_to_posix_error(GetLastError());
443 fov->io_complete = TRUE;
444 SetEvent(wd->iocomplete_event);
445 } while (ctx->wd->iothread_running);
447 CloseHandle(ctx->iocp);
452 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
454 struct fio_overlapped *o = io_u->engine_data;
457 io_u->engine_data = NULL;
462 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
464 struct fio_overlapped *o;
466 o = malloc(sizeof(*o));
467 o->io_complete = FALSE;
470 io_u->engine_data = o;
474 static struct ioengine_ops ioengine = {
475 .name = "windowsaio",
476 .version = FIO_IOOPS_VERSION,
477 .init = fio_windowsaio_init,
478 .queue = fio_windowsaio_queue,
479 .getevents = fio_windowsaio_getevents,
480 .event = fio_windowsaio_event,
481 .cleanup = fio_windowsaio_cleanup,
482 .open_file = fio_windowsaio_open_file,
483 .close_file = fio_windowsaio_close_file,
484 .get_file_size = generic_get_file_size,
485 .io_u_init = fio_windowsaio_io_u_init,
486 .io_u_free = fio_windowsaio_io_u_free,
489 static void fio_init fio_windowsaio_register(void)
491 register_ioengine(&ioengine);
494 static void fio_exit fio_windowsaio_unregister(void)
496 unregister_ioengine(&ioengine);