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);
140 free(wd->aio_events);
144 td->io_ops->data = NULL;
149 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
153 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
154 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
155 DWORD openmode = OPEN_ALWAYS;
158 dprint(FD_FILE, "fd open %s\n", f->file_name);
160 if (f->filetype == FIO_TYPE_PIPE) {
161 log_err("fio: windowsaio doesn't support pipes\n");
165 if (!strcmp(f->file_name, "-")) {
166 log_err("fio: can't read/write to stdin/out\n");
171 flags |= FILE_FLAG_NO_BUFFERING;
173 flags |= FILE_FLAG_WRITE_THROUGH;
175 if (td->o.td_ddir == TD_DDIR_READ ||
176 td->o.td_ddir == TD_DDIR_WRITE)
177 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
179 flags |= FILE_FLAG_RANDOM_ACCESS;
181 if (!td_write(td) || read_only)
182 access = GENERIC_READ;
184 access = (GENERIC_READ | GENERIC_WRITE);
186 if (td->o.create_on_open > 0)
187 openmode = OPEN_ALWAYS;
189 openmode = OPEN_EXISTING;
191 f->hFile = CreateFile(f->file_name, access, sharemode,
192 NULL, openmode, flags, NULL);
194 if (f->hFile == INVALID_HANDLE_VALUE)
197 /* Only set up the competion port and thread if we're not just
198 * querying the device size */
199 if (!rc && td->io_ops->data != NULL) {
200 struct thread_ctx *ctx;
201 struct windowsaio_data *wd;
202 hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
204 wd = td->io_ops->data;
206 wd->iothread_running = TRUE;
209 ctx = malloc(sizeof(struct thread_ctx));
213 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
216 if (rc || wd->iothread == NULL)
223 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
227 dprint(FD_FILE, "fd close %s\n", f->file_name);
229 if (f->hFile != INVALID_HANDLE_VALUE) {
230 if (!CloseHandle(f->hFile))
234 f->hFile = INVALID_HANDLE_VALUE;
238 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
240 BOOL expired = FALSE;
243 current_time = GetTickCount();
245 if ((end_count > start_count) && current_time >= end_count)
247 else if (current_time < start_count && current_time > end_count)
253 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
255 struct windowsaio_data *wd = td->io_ops->data;
256 return wd->aio_events[event];
259 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
260 unsigned int max, struct timespec *t)
262 struct windowsaio_data *wd = td->io_ops->data;
263 struct flist_head *entry;
264 unsigned int dequeued = 0;
266 struct fio_overlapped *fov;
267 DWORD start_count = 0;
273 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
274 start_count = GetTickCount();
275 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
279 flist_for_each(entry, &td->io_u_busylist) {
280 io_u = flist_entry(entry, struct io_u, list);
281 fov = (struct fio_overlapped*)io_u->engine_data;
283 if (fov->io_complete) {
284 fov->io_complete = FALSE;
286 wd->aio_events[dequeued] = io_u;
294 if (dequeued < min) {
295 status = WaitForSingleObject(wd->iocomplete_event, mswait);
296 if (status != WAIT_OBJECT_0 && dequeued > 0)
300 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
307 static int fio_windowsaio_queue(struct thread_data *td,
310 LPOVERLAPPED lpOvl = NULL;
311 struct windowsaio_data *wd;
315 int rc = FIO_Q_COMPLETED;
317 fio_ro_check(td, io_u);
319 wd = td->io_ops->data;
321 for (index = 0; index < td->o.iodepth; index++) {
322 if (wd->ovls[index].io_free) {
323 wd->ovls[index].io_free = FALSE;
324 ResetEvent(wd->ovls[index].o.hEvent);
329 assert(index < td->o.iodepth);
331 lpOvl = &wd->ovls[index].o;
332 wd->ovls[index].io_u = io_u;
333 lpOvl->Internal = STATUS_PENDING;
334 lpOvl->InternalHigh = 0;
335 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
336 lpOvl->OffsetHigh = io_u->offset >> 32;
337 lpOvl->Pointer = NULL;
338 io_u->engine_data = &wd->ovls[index];
340 switch (io_u->ddir) {
342 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
345 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
349 case DDIR_SYNC_FILE_RANGE:
350 success = FlushFileBuffers(io_u->file->hFile);
352 io_u->error = GetLastError();
354 return FIO_Q_COMPLETED;
357 log_err("manual TRIM isn't supported on Windows");
359 io_u->resid = io_u->xfer_buflen;
360 return FIO_Q_COMPLETED;
366 if (success || GetLastError() == ERROR_IO_PENDING) {
369 io_u->error = GetLastError();
370 io_u->resid = io_u->xfer_buflen;
376 /* Runs as a thread and waits for queued IO to complete */
377 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
380 struct fio_overlapped *fov;
382 struct windowsaio_data *wd;
383 struct thread_ctx *ctx;
387 ctx = (struct thread_ctx*)lpParameter;
391 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250))
394 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
397 if (ovl->Internal == ERROR_SUCCESS) {
398 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
401 io_u->resid = io_u->xfer_buflen;
402 io_u->error = ovl->Internal;
405 fov->io_complete = TRUE;
406 SetEvent(wd->iocomplete_event);
407 } while (ctx->wd->iothread_running);
409 CloseHandle(ctx->iocp);
414 static int fio_windowsaio_cancel(struct thread_data *td,
419 struct windowsaio_data *wd = td->io_ops->data;
421 /* If we're running on Vista or newer, we can cancel individual IO requests */
422 if (wd->pCancelIoEx != NULL) {
423 struct fio_overlapped *ovl = io_u->engine_data;
424 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
432 static struct ioengine_ops ioengine = {
433 .name = "windowsaio",
434 .version = FIO_IOOPS_VERSION,
435 .init = fio_windowsaio_init,
436 .queue = fio_windowsaio_queue,
437 .cancel = fio_windowsaio_cancel,
438 .getevents = fio_windowsaio_getevents,
439 .event = fio_windowsaio_event,
440 .cleanup = fio_windowsaio_cleanup,
441 .open_file = fio_windowsaio_open_file,
442 .close_file = fio_windowsaio_close_file,
443 .get_file_size = generic_get_file_size
446 static void fio_init fio_posixaio_register(void)
448 register_ioengine(&ioengine);
451 static void fio_exit fio_posixaio_unregister(void)
453 unregister_ioengine(&ioengine);