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_option_is_set(&td->o, cpumask))
112 fio_setaffinity(threadid, td->o.cpumask);
114 log_err("windowsaio: failed to create io completion thread\n");
117 if (rc || wd->iothread == NULL)
124 static void fio_windowsaio_cleanup(struct thread_data *td)
126 struct windowsaio_data *wd;
128 wd = td->io_ops_data;
131 wd->iothread_running = FALSE;
132 WaitForSingleObject(wd->iothread, INFINITE);
134 CloseHandle(wd->iothread);
135 CloseHandle(wd->iocomplete_event);
137 free(wd->aio_events);
140 td->io_ops_data = NULL;
144 static int windowsaio_invalidate_cache(struct fio_file *f)
147 DWORD isharemode = (FILE_SHARE_DELETE | FILE_SHARE_READ |
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
158 dprint(FD_IO, "windowaio: attempt invalidate cache for %s\n",
160 ihFile = CreateFile(f->file_name, 0, isharemode, NULL, OPEN_EXISTING,
161 FILE_FLAG_NO_BUFFERING, NULL);
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);
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);
182 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
185 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
186 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
187 DWORD openmode = OPEN_ALWAYS;
190 dprint(FD_FILE, "fd open %s\n", f->file_name);
192 if (f->filetype == FIO_TYPE_PIPE) {
193 log_err("windowsaio: pipes are not supported\n");
197 if (!strcmp(f->file_name, "-")) {
198 log_err("windowsaio: can't read/write to stdin/out\n");
203 flags |= FILE_FLAG_NO_BUFFERING;
205 flags |= FILE_FLAG_WRITE_THROUGH;
208 * Inform Windows whether we're going to be doing sequential or
209 * random IO so it can tune the Cache Manager
211 switch (td->o.fadvise_hint) {
214 flags |= FILE_FLAG_RANDOM_ACCESS;
216 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
219 flags |= FILE_FLAG_RANDOM_ACCESS;
221 case F_ADV_SEQUENTIAL:
222 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
227 log_err("fio: unknown fadvise type %d\n", td->o.fadvise_hint);
230 if (!td_write(td) || read_only)
231 access = GENERIC_READ;
233 access = (GENERIC_READ | GENERIC_WRITE);
235 if (td->o.create_on_open)
236 openmode = OPEN_ALWAYS;
238 openmode = OPEN_EXISTING;
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 */
242 if (td->o.invalidate_cache && !td->o.odirect)
243 windowsaio_invalidate_cache(f);
245 f->hFile = CreateFile(f->file_name, access, sharemode,
246 NULL, openmode, flags, NULL);
248 if (f->hFile == INVALID_HANDLE_VALUE) {
249 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
253 /* Only set up the completion port and thread if we're not just
254 * querying the device size */
255 if (!rc && td->io_ops_data != NULL) {
256 struct windowsaio_data *wd;
258 wd = td->io_ops_data;
260 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
261 log_err("windowsaio: failed to create io completion port\n");
269 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
273 dprint(FD_FILE, "fd close %s\n", f->file_name);
275 if (f->hFile != INVALID_HANDLE_VALUE) {
276 if (!CloseHandle(f->hFile)) {
277 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
282 f->hFile = INVALID_HANDLE_VALUE;
286 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
288 BOOL expired = FALSE;
291 current_time = GetTickCount();
293 if ((end_count > start_count) && current_time >= end_count)
295 else if (current_time < start_count && current_time > end_count)
301 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
303 struct windowsaio_data *wd = td->io_ops_data;
304 return wd->aio_events[event];
307 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
309 const struct timespec *t)
311 struct windowsaio_data *wd = td->io_ops_data;
312 unsigned int dequeued = 0;
315 struct fio_overlapped *fov;
316 DWORD start_count = 0;
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);
328 io_u_qiter(&td->io_u_all, io_u, i) {
329 if (!(io_u->flags & IO_U_F_FLIGHT))
332 fov = (struct fio_overlapped*)io_u->engine_data;
334 if (fov->io_complete) {
335 fov->io_complete = FALSE;
336 wd->aio_events[dequeued] = io_u;
344 if (dequeued < min) {
345 status = WaitForSingleObject(wd->iocomplete_event, mswait);
346 if (status != WAIT_OBJECT_0 && dequeued >= min)
350 if (dequeued >= min ||
351 (t != NULL && timeout_expired(start_count, end_count)))
358 static enum fio_q_status fio_windowsaio_queue(struct thread_data *td,
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,
376 io_u->xfer_buflen, NULL, lpOvl);
379 success = ReadFile(io_u->file->hFile, io_u->xfer_buf,
380 io_u->xfer_buflen, NULL, lpOvl);
384 case DDIR_SYNC_FILE_RANGE:
385 success = FlushFileBuffers(io_u->file->hFile);
387 log_err("windowsaio: failed to flush file buffers\n");
388 io_u->error = win_to_posix_error(GetLastError());
391 return FIO_Q_COMPLETED;
393 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
395 io_u->resid = io_u->xfer_buflen;
396 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;
429 ret = GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey,
431 if (!ret && ovl == NULL)
434 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
437 if (ovl->Internal == ERROR_SUCCESS) {
438 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
441 io_u->resid = io_u->xfer_buflen;
442 io_u->error = win_to_posix_error(GetLastError());
445 fov->io_complete = TRUE;
446 SetEvent(wd->iocomplete_event);
447 } while (ctx->wd->iothread_running);
449 CloseHandle(ctx->iocp);
454 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
456 struct fio_overlapped *o = io_u->engine_data;
459 io_u->engine_data = NULL;
464 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
466 struct fio_overlapped *o;
468 o = malloc(sizeof(*o));
469 o->io_complete = FALSE;
472 io_u->engine_data = o;
476 static struct ioengine_ops ioengine = {
477 .name = "windowsaio",
478 .version = FIO_IOOPS_VERSION,
479 .init = fio_windowsaio_init,
480 .queue = fio_windowsaio_queue,
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,
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,
491 static void fio_init fio_windowsaio_register(void)
493 register_ioengine(&ioengine);
496 static void fio_exit fio_windowsaio_unregister(void)
498 unregister_ioengine(&ioengine);