2 * Native Windows async IO engine
3 * Copyright (C) 2011 Bruce Cran <bruce@cran.org.uk>
15 typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
17 struct fio_overlapped {
24 struct windowsaio_data {
25 struct fio_overlapped *ovls;
26 struct io_u **aio_events;
28 HANDLE iocomplete_event;
29 CANCELIOEX pCancelIoEx;
30 BOOL iothread_running;
35 struct windowsaio_data *wd;
38 static int fio_windowsaio_cancel(struct thread_data *td,
40 static BOOL timeout_expired(DWORD start_count, DWORD end_count);
41 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
42 unsigned int max, struct timespec *t);
43 static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
44 static int fio_windowsaio_queue(struct thread_data *td,
46 static void fio_windowsaio_cleanup(struct thread_data *td);
47 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
48 static int fio_windowsaio_init(struct thread_data *td);
49 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
50 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
52 int sync_file_range(int fd, off64_t offset, off64_t nbytes,
59 static int fio_windowsaio_init(struct thread_data *td)
61 struct windowsaio_data *wd;
66 wd = malloc(sizeof(struct windowsaio_data));
68 ZeroMemory(wd, sizeof(struct windowsaio_data));
73 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
74 if (wd->aio_events == NULL)
79 wd->ovls = malloc(td->o.iodepth * sizeof(struct fio_overlapped));
85 for (i = 0; i < td->o.iodepth; i++) {
86 wd->ovls[i].io_free = TRUE;
87 wd->ovls[i].io_complete = FALSE;
89 wd->ovls[i].o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
90 if (wd->ovls[i].o.hEvent == NULL) {
98 /* Create an auto-reset event */
99 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
100 if (wd->iocomplete_event == NULL)
106 if (wd->ovls != NULL)
108 if (wd->aio_events != NULL)
109 free(wd->aio_events);
115 hKernel32Dll = GetModuleHandle("kernel32.dll");
116 wd->pCancelIoEx = GetProcAddress(hKernel32Dll, "CancelIoEx");
118 td->io_ops->data = wd;
122 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 for (i = 0; i < td->o.iodepth; i++)
137 CloseHandle(wd->ovls[i].o.hEvent);
139 free(wd->aio_events);
143 td->io_ops->data = NULL;
148 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
152 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
153 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
154 DWORD openmode = OPEN_ALWAYS;
157 dprint(FD_FILE, "fd open %s\n", f->file_name);
159 if (f->filetype == FIO_TYPE_PIPE) {
160 log_err("fio: windowsaio doesn't support pipes\n");
164 if (!strcmp(f->file_name, "-")) {
165 log_err("fio: can't read/write to stdin/out\n");
170 flags |= FILE_FLAG_NO_BUFFERING;
172 flags |= FILE_FLAG_WRITE_THROUGH;
174 if (td->o.td_ddir == TD_DDIR_READ ||
175 td->o.td_ddir == TD_DDIR_WRITE)
176 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
178 flags |= FILE_FLAG_RANDOM_ACCESS;
180 if (!td_write(td) || read_only)
181 access = GENERIC_READ;
183 access = (GENERIC_READ | GENERIC_WRITE);
185 if (td->o.create_on_open > 0)
186 openmode = OPEN_ALWAYS;
188 openmode = OPEN_EXISTING;
190 f->hFile = CreateFile(f->file_name, access, sharemode,
191 NULL, openmode, flags, NULL);
193 if (f->hFile == INVALID_HANDLE_VALUE)
196 /* Only set up the competion port and thread if we're not just
197 * querying the device size */
198 if (!rc && td->io_ops->data != NULL) {
199 struct thread_ctx *ctx;
200 struct windowsaio_data *wd;
202 hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
204 wd = td->io_ops->data;
205 wd->iothread_running = TRUE;
208 ctx = malloc(sizeof(struct thread_ctx));
212 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
215 if (rc || wd->iothread == NULL)
222 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
226 dprint(FD_FILE, "fd close %s\n", f->file_name);
228 if (f->hFile != INVALID_HANDLE_VALUE) {
229 if (!CloseHandle(f->hFile))
233 f->hFile = INVALID_HANDLE_VALUE;
237 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
239 BOOL expired = FALSE;
242 current_time = GetTickCount();
244 if ((end_count > start_count) && current_time >= end_count)
246 else if (current_time < start_count && current_time > end_count)
252 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
254 struct windowsaio_data *wd = td->io_ops->data;
255 return wd->aio_events[event];
258 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
259 unsigned int max, struct timespec *t)
261 struct windowsaio_data *wd = td->io_ops->data;
262 struct flist_head *entry;
263 unsigned int dequeued = 0;
265 struct fio_overlapped *fov;
266 DWORD start_count = 0;
272 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
273 start_count = GetTickCount();
274 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
278 flist_for_each(entry, &td->io_u_busylist) {
279 io_u = flist_entry(entry, struct io_u, list);
280 fov = (struct fio_overlapped*)io_u->engine_data;
282 if (fov->io_complete) {
283 fov->io_complete = FALSE;
285 wd->aio_events[dequeued] = io_u;
293 if (dequeued < min) {
294 status = WaitForSingleObject(wd->iocomplete_event, mswait);
295 if (status != WAIT_OBJECT_0 && dequeued > 0)
299 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
306 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
308 LPOVERLAPPED lpOvl = NULL;
309 struct windowsaio_data *wd;
313 int rc = FIO_Q_COMPLETED;
315 fio_ro_check(td, io_u);
317 wd = td->io_ops->data;
319 for (index = 0; index < td->o.iodepth; index++) {
320 if (wd->ovls[index].io_free) {
321 wd->ovls[index].io_free = FALSE;
322 ResetEvent(wd->ovls[index].o.hEvent);
327 assert(index < td->o.iodepth);
329 lpOvl = &wd->ovls[index].o;
330 wd->ovls[index].io_u = io_u;
331 lpOvl->Internal = STATUS_PENDING;
332 lpOvl->InternalHigh = 0;
333 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
334 lpOvl->OffsetHigh = io_u->offset >> 32;
335 io_u->engine_data = &wd->ovls[index];
337 switch (io_u->ddir) {
339 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
342 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
346 case DDIR_SYNC_FILE_RANGE:
347 success = FlushFileBuffers(io_u->file->hFile);
349 io_u->error = GetLastError();
351 return FIO_Q_COMPLETED;
354 log_err("manual TRIM isn't supported on Windows");
356 io_u->resid = io_u->xfer_buflen;
357 return FIO_Q_COMPLETED;
363 if (success || GetLastError() == ERROR_IO_PENDING)
366 io_u->error = GetLastError();
367 io_u->resid = io_u->xfer_buflen;
373 /* Runs as a thread and waits for queued IO to complete */
374 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
377 struct fio_overlapped *fov;
379 struct windowsaio_data *wd;
380 struct thread_ctx *ctx;
384 ctx = (struct thread_ctx*)lpParameter;
388 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250))
391 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
394 if (ovl->Internal == ERROR_SUCCESS) {
395 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
398 io_u->resid = io_u->xfer_buflen;
399 io_u->error = ovl->Internal;
402 fov->io_complete = TRUE;
403 SetEvent(wd->iocomplete_event);
404 } while (ctx->wd->iothread_running);
406 CloseHandle(ctx->iocp);
411 static int fio_windowsaio_cancel(struct thread_data *td,
416 struct windowsaio_data *wd = td->io_ops->data;
418 /* If we're running on Vista or newer, we can cancel individual IO requests */
419 if (wd->pCancelIoEx != NULL) {
420 struct fio_overlapped *ovl = io_u->engine_data;
422 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
430 static struct ioengine_ops ioengine = {
431 .name = "windowsaio",
432 .version = FIO_IOOPS_VERSION,
433 .init = fio_windowsaio_init,
434 .queue = fio_windowsaio_queue,
435 .cancel = fio_windowsaio_cancel,
436 .getevents = fio_windowsaio_getevents,
437 .event = fio_windowsaio_event,
438 .cleanup = fio_windowsaio_cleanup,
439 .open_file = fio_windowsaio_open_file,
440 .close_file = fio_windowsaio_close_file,
441 .get_file_size = generic_get_file_size
444 static void fio_init fio_posixaio_register(void)
446 register_ioengine(&ioengine);
449 static void fio_exit fio_posixaio_unregister(void)
451 unregister_ioengine(&ioengine);