4 * IO engine using Windows IO Completion Ports.
16 typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
18 int geterrno_from_win_error (DWORD code, int deferrno);
20 struct fio_overlapped {
26 struct windowsaio_data {
27 struct io_u **aio_events;
30 HANDLE iocomplete_event;
31 CANCELIOEX pCancelIoEx;
32 BOOL iothread_running;
37 struct windowsaio_data *wd;
40 static int fio_windowsaio_cancel(struct thread_data *td,
42 static BOOL timeout_expired(DWORD start_count, DWORD end_count);
43 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
44 unsigned int max, struct timespec *t);
45 static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
46 static int fio_windowsaio_queue(struct thread_data *td,
48 static void fio_windowsaio_cleanup(struct thread_data *td);
49 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
50 static int fio_windowsaio_init(struct thread_data *td);
51 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
52 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
54 static int fio_windowsaio_init(struct thread_data *td)
56 struct windowsaio_data *wd;
60 wd = calloc(1, sizeof(struct windowsaio_data));
62 log_err("windowsaio: failed to allocate memory for engine data\n");
67 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
68 if (wd->aio_events == NULL) {
69 log_err("windowsaio: failed to allocate memory for aio events list\n");
75 /* Create an auto-reset event */
76 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
77 if (wd->iocomplete_event == NULL) {
78 log_err("windowsaio: failed to create io complete event handle\n");
85 if (wd->aio_events != NULL)
92 hKernel32Dll = GetModuleHandle("kernel32.dll");
93 wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
94 td->io_ops->data = wd;
97 struct thread_ctx *ctx;
98 struct windowsaio_data *wd;
101 hFile = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
102 if (hFile == INVALID_HANDLE_VALUE) {
103 log_err("windowsaio: failed to create io completion port\n");
107 wd = td->io_ops->data;
108 wd->iothread_running = TRUE;
112 ctx = malloc(sizeof(struct thread_ctx));
114 if (!rc && ctx == NULL)
116 log_err("windowsaio: failed to allocate memory for thread context structure\n");
125 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
126 if (wd->iothread == NULL)
127 log_err("windowsaio: failed to create io completion thread\n");
130 if (rc || wd->iothread == NULL)
137 static void fio_windowsaio_cleanup(struct thread_data *td)
139 struct windowsaio_data *wd;
141 wd = td->io_ops->data;
144 wd->iothread_running = FALSE;
145 WaitForSingleObject(wd->iothread, INFINITE);
147 CloseHandle(wd->iothread);
148 CloseHandle(wd->iocomplete_event);
150 free(wd->aio_events);
153 td->io_ops->data = NULL;
158 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
161 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
162 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
163 DWORD openmode = OPEN_ALWAYS;
166 dprint(FD_FILE, "fd open %s\n", f->file_name);
168 if (f->filetype == FIO_TYPE_PIPE) {
169 log_err("windowsaio: pipes are not supported\n");
173 if (!strcmp(f->file_name, "-")) {
174 log_err("windowsaio: can't read/write to stdin/out\n");
179 flags |= FILE_FLAG_NO_BUFFERING;
181 flags |= FILE_FLAG_WRITE_THROUGH;
184 * Inform Windows whether we're going to be doing sequential or
185 * random io so it can tune the Cache Manager
187 if (td->o.td_ddir == TD_DDIR_READ ||
188 td->o.td_ddir == TD_DDIR_WRITE)
189 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
191 flags |= FILE_FLAG_RANDOM_ACCESS;
193 if (!td_write(td) || read_only)
194 access = GENERIC_READ;
196 access = (GENERIC_READ | GENERIC_WRITE);
198 if (td->o.create_on_open)
199 openmode = OPEN_ALWAYS;
201 openmode = OPEN_EXISTING;
203 f->hFile = CreateFile(f->file_name, access, sharemode,
204 NULL, openmode, flags, NULL);
206 if (f->hFile == INVALID_HANDLE_VALUE) {
207 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
211 /* Only set up the completion port and thread if we're not just
212 * querying the device size */
213 if (!rc && td->io_ops->data != NULL) {
214 struct windowsaio_data *wd;
216 wd = td->io_ops->data;
218 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
219 log_err("windowsaio: failed to create io completion port\n");
227 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
231 dprint(FD_FILE, "fd close %s\n", f->file_name);
233 if (f->hFile != INVALID_HANDLE_VALUE) {
234 if (!CloseHandle(f->hFile)) {
235 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
240 f->hFile = INVALID_HANDLE_VALUE;
244 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
246 BOOL expired = FALSE;
249 current_time = GetTickCount();
251 if ((end_count > start_count) && current_time >= end_count)
253 else if (current_time < start_count && current_time > end_count)
259 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
261 struct windowsaio_data *wd = td->io_ops->data;
262 return wd->aio_events[event];
265 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
266 unsigned int max, struct timespec *t)
268 struct windowsaio_data *wd = td->io_ops->data;
269 struct flist_head *entry;
270 unsigned int dequeued = 0;
272 struct fio_overlapped *fov;
273 DWORD start_count = 0;
279 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
280 start_count = GetTickCount();
281 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
285 flist_for_each(entry, &td->io_u_busylist) {
286 io_u = flist_entry(entry, struct io_u, list);
287 fov = (struct fio_overlapped*)io_u->engine_data;
289 if (fov->io_complete) {
290 fov->io_complete = FALSE;
291 ResetEvent(fov->o.hEvent);
292 wd->aio_events[dequeued] = io_u;
300 if (dequeued < min) {
301 status = WaitForSingleObject(wd->iocomplete_event, mswait);
302 if (status != WAIT_OBJECT_0 && dequeued >= min)
306 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
313 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
315 struct fio_overlapped *o = io_u->engine_data;
316 LPOVERLAPPED lpOvl = &o->o;
318 BOOL success = FALSE;
319 int rc = FIO_Q_COMPLETED;
321 fio_ro_check(td, io_u);
323 lpOvl->Internal = STATUS_PENDING;
324 lpOvl->InternalHigh = 0;
325 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
326 lpOvl->OffsetHigh = io_u->offset >> 32;
328 switch (io_u->ddir) {
330 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
333 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
337 case DDIR_SYNC_FILE_RANGE:
338 success = FlushFileBuffers(io_u->file->hFile);
340 log_err("windowsaio: failed to flush file buffers\n");
341 io_u->error = win_to_posix_error(GetLastError());
344 return FIO_Q_COMPLETED;
347 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
349 io_u->resid = io_u->xfer_buflen;
350 return FIO_Q_COMPLETED;
357 if (success || GetLastError() == ERROR_IO_PENDING)
360 io_u->error = win_to_posix_error(GetLastError());
361 io_u->resid = io_u->xfer_buflen;
367 /* Runs as a thread and waits for queued IO to complete */
368 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
371 struct fio_overlapped *fov;
373 struct windowsaio_data *wd;
374 struct thread_ctx *ctx;
378 ctx = (struct thread_ctx*)lpParameter;
382 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
385 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
388 if (ovl->Internal == ERROR_SUCCESS) {
389 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
392 io_u->resid = io_u->xfer_buflen;
393 io_u->error = win_to_posix_error(GetLastError());
396 fov->io_complete = TRUE;
397 SetEvent(wd->iocomplete_event);
398 } while (ctx->wd->iothread_running);
400 CloseHandle(ctx->iocp);
405 static int fio_windowsaio_cancel(struct thread_data *td,
410 struct windowsaio_data *wd = td->io_ops->data;
412 /* If we're running on Vista or newer, we can cancel individual IO requests */
413 if (wd->pCancelIoEx != NULL) {
414 struct fio_overlapped *ovl = io_u->engine_data;
416 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o)) {
417 log_err("windowsaio: failed to cancel io\n");
426 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
428 struct fio_overlapped *o = io_u->engine_data;
431 CloseHandle(o->o.hEvent);
432 io_u->engine_data = NULL;
437 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
439 struct fio_overlapped *o;
441 o = malloc(sizeof(*o));
442 o->io_complete = FALSE;
444 o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
445 if (o->o.hEvent == NULL) {
446 log_err("windowsaio: failed to create event handle\n");
451 io_u->engine_data = o;
455 static struct ioengine_ops ioengine = {
456 .name = "windowsaio",
457 .version = FIO_IOOPS_VERSION,
458 .init = fio_windowsaio_init,
459 .queue = fio_windowsaio_queue,
460 .cancel = fio_windowsaio_cancel,
461 .getevents = fio_windowsaio_getevents,
462 .event = fio_windowsaio_event,
463 .cleanup = fio_windowsaio_cleanup,
464 .open_file = fio_windowsaio_open_file,
465 .close_file = fio_windowsaio_close_file,
466 .get_file_size = generic_get_file_size,
467 .io_u_init = fio_windowsaio_io_u_init,
468 .io_u_free = fio_windowsaio_io_u_free,
471 static void fio_init fio_windowsaio_register(void)
473 register_ioengine(&ioengine);
476 static void fio_exit fio_windowsaio_unregister(void)
478 unregister_ioengine(&ioengine);