2 * Native Windows async IO engine
3 * Copyright (C) 2012 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 = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
117 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;
176 * Inform Windows whether we're going to be doing sequential or
177 * random io so it can tune the Cache Manager
179 if (td->o.td_ddir == TD_DDIR_READ ||
180 td->o.td_ddir == TD_DDIR_WRITE)
181 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
183 flags |= FILE_FLAG_RANDOM_ACCESS;
185 if (!td_write(td) || read_only)
186 access = GENERIC_READ;
188 access = (GENERIC_READ | GENERIC_WRITE);
190 if (td->o.create_on_open)
191 openmode = OPEN_ALWAYS;
193 openmode = OPEN_EXISTING;
195 f->hFile = CreateFile(f->file_name, access, sharemode,
196 NULL, openmode, flags, NULL);
198 if (f->hFile == INVALID_HANDLE_VALUE)
201 /* Only set up the completion port and thread if we're not just
202 * querying the device size */
203 if (!rc && td->io_ops->data != NULL) {
204 struct thread_ctx *ctx;
205 struct windowsaio_data *wd;
207 hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
209 wd = td->io_ops->data;
210 wd->iothread_running = TRUE;
213 ctx = malloc(sizeof(struct thread_ctx));
217 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
220 if (rc || wd->iothread == NULL)
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))
238 f->hFile = INVALID_HANDLE_VALUE;
242 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
244 BOOL expired = FALSE;
247 current_time = GetTickCount();
249 if ((end_count > start_count) && current_time >= end_count)
251 else if (current_time < start_count && current_time > end_count)
257 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
259 struct windowsaio_data *wd = td->io_ops->data;
260 return wd->aio_events[event];
263 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
264 unsigned int max, struct timespec *t)
266 struct windowsaio_data *wd = td->io_ops->data;
267 struct flist_head *entry;
268 unsigned int dequeued = 0;
270 struct fio_overlapped *fov;
271 DWORD start_count = 0;
277 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
278 start_count = GetTickCount();
279 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
283 flist_for_each(entry, &td->io_u_busylist) {
284 io_u = flist_entry(entry, struct io_u, list);
285 fov = (struct fio_overlapped*)io_u->engine_data;
287 if (fov->io_complete) {
288 fov->io_complete = FALSE;
290 ResetEvent(fov->o.hEvent);
291 wd->aio_events[dequeued] = io_u;
299 if (dequeued < min) {
300 status = WaitForSingleObject(wd->iocomplete_event, mswait);
301 if (status != WAIT_OBJECT_0 && dequeued >= min)
305 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
312 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
314 LPOVERLAPPED lpOvl = NULL;
315 struct windowsaio_data *wd;
317 BOOL success = FALSE;
319 int rc = FIO_Q_COMPLETED;
321 fio_ro_check(td, io_u);
323 wd = td->io_ops->data;
325 for (index = 0; index < td->o.iodepth; index++) {
326 if (wd->ovls[index].io_free)
330 assert(index < td->o.iodepth);
332 wd->ovls[index].io_free = FALSE;
333 wd->ovls[index].io_u = io_u;
334 lpOvl = &wd->ovls[index].o;
335 lpOvl->Internal = STATUS_PENDING;
336 lpOvl->InternalHigh = 0;
337 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
338 lpOvl->OffsetHigh = io_u->offset >> 32;
339 io_u->engine_data = &wd->ovls[index];
341 switch (io_u->ddir) {
343 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
346 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
350 case DDIR_SYNC_FILE_RANGE:
351 success = FlushFileBuffers(io_u->file->hFile);
353 io_u->error = GetLastError();
355 return FIO_Q_COMPLETED;
358 log_err("manual TRIM isn't supported on Windows");
360 io_u->resid = io_u->xfer_buflen;
361 return FIO_Q_COMPLETED;
368 if (success || GetLastError() == ERROR_IO_PENDING)
371 io_u->error = GetLastError();
372 io_u->resid = io_u->xfer_buflen;
378 /* Runs as a thread and waits for queued IO to complete */
379 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
382 struct fio_overlapped *fov;
384 struct windowsaio_data *wd;
385 struct thread_ctx *ctx;
389 ctx = (struct thread_ctx*)lpParameter;
393 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250))
396 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
399 if (ovl->Internal == ERROR_SUCCESS) {
400 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
403 io_u->resid = io_u->xfer_buflen;
404 io_u->error = ovl->Internal;
407 fov->io_complete = TRUE;
408 SetEvent(wd->iocomplete_event);
409 } while (ctx->wd->iothread_running);
411 CloseHandle(ctx->iocp);
416 static int fio_windowsaio_cancel(struct thread_data *td,
421 struct windowsaio_data *wd = td->io_ops->data;
423 /* If we're running on Vista or newer, we can cancel individual IO requests */
424 if (wd->pCancelIoEx != NULL) {
425 struct fio_overlapped *ovl = io_u->engine_data;
427 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
435 static struct ioengine_ops ioengine = {
436 .name = "windowsaio",
437 .version = FIO_IOOPS_VERSION,
438 .init = fio_windowsaio_init,
439 .queue = fio_windowsaio_queue,
440 .cancel = fio_windowsaio_cancel,
441 .getevents = fio_windowsaio_getevents,
442 .event = fio_windowsaio_event,
443 .cleanup = fio_windowsaio_cleanup,
444 .open_file = fio_windowsaio_open_file,
445 .close_file = fio_windowsaio_close_file,
446 .get_file_size = generic_get_file_size
449 static void fio_init fio_posixaio_register(void)
451 register_ioengine(&ioengine);
454 static void fio_exit fio_posixaio_unregister(void)
456 unregister_ioengine(&ioengine);