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 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 CANCELIOEX pCancelIoEx;
31 BOOL iothread_running;
36 struct windowsaio_data *wd;
39 static int fio_windowsaio_cancel(struct thread_data *td,
41 static BOOL timeout_expired(DWORD start_count, DWORD end_count);
42 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
43 unsigned int max, struct timespec *t);
44 static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
45 static int fio_windowsaio_queue(struct thread_data *td,
47 static void fio_windowsaio_cleanup(struct thread_data *td);
48 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
49 static int fio_windowsaio_init(struct thread_data *td);
50 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
51 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
52 static int win_to_posix_error(DWORD winerr);
54 static int win_to_posix_error(DWORD winerr)
58 case ERROR_FILE_NOT_FOUND: return ENOENT;
59 case ERROR_PATH_NOT_FOUND: return ENOENT;
60 case ERROR_ACCESS_DENIED: return EACCES;
61 case ERROR_INVALID_HANDLE: return EBADF;
62 case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM;
63 case ERROR_INVALID_DATA: return EINVAL;
64 case ERROR_OUTOFMEMORY: return ENOMEM;
65 case ERROR_INVALID_DRIVE: return ENODEV;
66 case ERROR_NOT_SAME_DEVICE: return EXDEV;
67 case ERROR_WRITE_PROTECT: return EROFS;
68 case ERROR_BAD_UNIT: return ENODEV;
69 case ERROR_SHARING_VIOLATION: return EACCES;
70 case ERROR_LOCK_VIOLATION: return EACCES;
71 case ERROR_SHARING_BUFFER_EXCEEDED: return ENOLCK;
72 case ERROR_HANDLE_DISK_FULL: return ENOSPC;
73 case ERROR_NOT_SUPPORTED: return ENOSYS;
74 case ERROR_FILE_EXISTS: return EEXIST;
75 case ERROR_CANNOT_MAKE: return EPERM;
76 case ERROR_INVALID_PARAMETER: return EINVAL;
77 case ERROR_NO_PROC_SLOTS: return EAGAIN;
78 case ERROR_BROKEN_PIPE: return EPIPE;
79 case ERROR_OPEN_FAILED: return EIO;
80 case ERROR_NO_MORE_SEARCH_HANDLES: return ENFILE;
81 case ERROR_CALL_NOT_IMPLEMENTED: return ENOSYS;
82 case ERROR_INVALID_NAME: return ENOENT;
83 case ERROR_WAIT_NO_CHILDREN: return ECHILD;
84 case ERROR_CHILD_NOT_COMPLETE: return EBUSY;
85 case ERROR_DIR_NOT_EMPTY: return ENOTEMPTY;
86 case ERROR_SIGNAL_REFUSED: return EIO;
87 case ERROR_BAD_PATHNAME: return ENOENT;
88 case ERROR_SIGNAL_PENDING: return EBUSY;
89 case ERROR_MAX_THRDS_REACHED: return EAGAIN;
90 case ERROR_BUSY: return EBUSY;
91 case ERROR_ALREADY_EXISTS: return EEXIST;
92 case ERROR_NO_SIGNAL_SENT: return EIO;
93 case ERROR_FILENAME_EXCED_RANGE: return EINVAL;
94 case ERROR_META_EXPANSION_TOO_LONG: return EINVAL;
95 case ERROR_INVALID_SIGNAL_NUMBER: return EINVAL;
96 case ERROR_THREAD_1_INACTIVE: return EINVAL;
97 case ERROR_BAD_PIPE: return EINVAL;
98 case ERROR_PIPE_BUSY: return EBUSY;
99 case ERROR_NO_DATA: return EPIPE;
100 case ERROR_MORE_DATA: return EAGAIN;
101 case ERROR_DIRECTORY: return ENOTDIR;
102 case ERROR_PIPE_CONNECTED: return EBUSY;
103 case ERROR_NO_TOKEN: return EINVAL;
104 case ERROR_PROCESS_ABORTED: return EFAULT;
105 case ERROR_BAD_DEVICE: return ENODEV;
106 case ERROR_BAD_USERNAME: return EINVAL;
107 case ERROR_OPEN_FILES: return EAGAIN;
108 case ERROR_ACTIVE_CONNECTIONS: return EAGAIN;
109 case ERROR_DEVICE_IN_USE: return EAGAIN;
110 case ERROR_INVALID_AT_INTERRUPT_TIME: return EINTR;
111 case ERROR_IO_DEVICE: return EIO;
112 case ERROR_NOT_OWNER: return EPERM;
113 case ERROR_END_OF_MEDIA: return ENOSPC;
114 case ERROR_EOM_OVERFLOW: return ENOSPC;
115 case ERROR_BEGINNING_OF_MEDIA: return ESPIPE;
116 case ERROR_SETMARK_DETECTED: return ESPIPE;
117 case ERROR_NO_DATA_DETECTED: return ENOSPC;
118 case ERROR_POSSIBLE_DEADLOCK: return EDEADLOCK;
119 case ERROR_CRC: return EIO;
120 case ERROR_NEGATIVE_SEEK: return EINVAL;
121 case ERROR_DISK_FULL: return ENOSPC;
122 case ERROR_NOACCESS: return EFAULT;
123 case ERROR_FILE_INVALID: return ENXIO;
129 static int fio_windowsaio_init(struct thread_data *td)
131 struct windowsaio_data *wd;
135 wd = malloc(sizeof(struct windowsaio_data));
137 ZeroMemory(wd, sizeof(struct windowsaio_data));
142 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
143 if (wd->aio_events == NULL)
148 /* Create an auto-reset event */
149 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
150 if (wd->iocomplete_event == NULL)
156 if (wd->aio_events != NULL)
157 free(wd->aio_events);
163 hKernel32Dll = GetModuleHandle("kernel32.dll");
164 wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
165 td->io_ops->data = wd;
169 struct thread_ctx *ctx;
170 struct windowsaio_data *wd;
173 hFile = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
174 if (hFile == INVALID_HANDLE_VALUE)
177 wd = td->io_ops->data;
178 wd->iothread_running = TRUE;
182 ctx = malloc(sizeof(struct thread_ctx));
184 if (!rc && ctx == NULL)
186 log_err("fio: out of memory in windowsaio\n");
195 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
198 if (rc || wd->iothread == NULL)
205 static void fio_windowsaio_cleanup(struct thread_data *td)
207 struct windowsaio_data *wd;
209 wd = td->io_ops->data;
212 wd->iothread_running = FALSE;
213 WaitForSingleObject(wd->iothread, INFINITE);
215 CloseHandle(wd->iothread);
216 CloseHandle(wd->iocomplete_event);
218 free(wd->aio_events);
221 td->io_ops->data = NULL;
226 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
229 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
230 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
231 DWORD openmode = OPEN_ALWAYS;
234 dprint(FD_FILE, "fd open %s\n", f->file_name);
236 if (f->filetype == FIO_TYPE_PIPE) {
237 log_err("fio: windowsaio doesn't support pipes\n");
241 if (!strcmp(f->file_name, "-")) {
242 log_err("fio: can't read/write to stdin/out\n");
247 flags |= FILE_FLAG_NO_BUFFERING;
249 flags |= FILE_FLAG_WRITE_THROUGH;
252 * Inform Windows whether we're going to be doing sequential or
253 * random io so it can tune the Cache Manager
255 if (td->o.td_ddir == TD_DDIR_READ ||
256 td->o.td_ddir == TD_DDIR_WRITE)
257 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
259 flags |= FILE_FLAG_RANDOM_ACCESS;
261 if (!td_write(td) || read_only)
262 access = GENERIC_READ;
264 access = (GENERIC_READ | GENERIC_WRITE);
266 if (td->o.create_on_open)
267 openmode = OPEN_ALWAYS;
269 openmode = OPEN_EXISTING;
271 f->hFile = CreateFile(f->file_name, access, sharemode,
272 NULL, openmode, flags, NULL);
274 if (f->hFile == INVALID_HANDLE_VALUE)
277 /* Only set up the completion port and thread if we're not just
278 * querying the device size */
279 if (!rc && td->io_ops->data != NULL) {
280 struct windowsaio_data *wd;
282 wd = td->io_ops->data;
284 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL)
291 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
295 dprint(FD_FILE, "fd close %s\n", f->file_name);
297 if (f->hFile != INVALID_HANDLE_VALUE) {
298 if (!CloseHandle(f->hFile))
302 f->hFile = INVALID_HANDLE_VALUE;
306 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
308 BOOL expired = FALSE;
311 current_time = GetTickCount();
313 if ((end_count > start_count) && current_time >= end_count)
315 else if (current_time < start_count && current_time > end_count)
321 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
323 struct windowsaio_data *wd = td->io_ops->data;
324 return wd->aio_events[event];
327 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
328 unsigned int max, struct timespec *t)
330 struct windowsaio_data *wd = td->io_ops->data;
331 struct flist_head *entry;
332 unsigned int dequeued = 0;
334 struct fio_overlapped *fov;
335 DWORD start_count = 0;
341 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
342 start_count = GetTickCount();
343 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
347 flist_for_each(entry, &td->io_u_busylist) {
348 io_u = flist_entry(entry, struct io_u, list);
349 fov = (struct fio_overlapped*)io_u->engine_data;
351 if (fov->io_complete) {
352 fov->io_complete = FALSE;
353 ResetEvent(fov->o.hEvent);
354 wd->aio_events[dequeued] = io_u;
362 if (dequeued < min) {
363 status = WaitForSingleObject(wd->iocomplete_event, mswait);
364 if (status != WAIT_OBJECT_0 && dequeued >= min)
368 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
375 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
377 struct fio_overlapped *o = io_u->engine_data;
378 LPOVERLAPPED lpOvl = &o->o;
380 BOOL success = FALSE;
381 int rc = FIO_Q_COMPLETED;
383 fio_ro_check(td, io_u);
385 lpOvl->Internal = STATUS_PENDING;
386 lpOvl->InternalHigh = 0;
387 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
388 lpOvl->OffsetHigh = io_u->offset >> 32;
390 switch (io_u->ddir) {
392 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
395 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
399 case DDIR_SYNC_FILE_RANGE:
400 success = FlushFileBuffers(io_u->file->hFile);
402 io_u->error = win_to_posix_error(GetLastError());
404 return FIO_Q_COMPLETED;
407 log_err("manual TRIM isn't supported on Windows");
409 io_u->resid = io_u->xfer_buflen;
410 return FIO_Q_COMPLETED;
417 if (success || GetLastError() == ERROR_IO_PENDING)
420 io_u->error = win_to_posix_error(GetLastError());
421 io_u->resid = io_u->xfer_buflen;
427 /* Runs as a thread and waits for queued IO to complete */
428 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
431 struct fio_overlapped *fov;
433 struct windowsaio_data *wd;
434 struct thread_ctx *ctx;
438 ctx = (struct thread_ctx*)lpParameter;
442 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
445 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
448 if (ovl->Internal == ERROR_SUCCESS) {
449 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
452 io_u->resid = io_u->xfer_buflen;
453 io_u->error = win_to_posix_error(GetLastError());
456 fov->io_complete = TRUE;
457 SetEvent(wd->iocomplete_event);
458 } while (ctx->wd->iothread_running);
460 CloseHandle(ctx->iocp);
465 static int fio_windowsaio_cancel(struct thread_data *td,
470 struct windowsaio_data *wd = td->io_ops->data;
472 /* If we're running on Vista or newer, we can cancel individual IO requests */
473 if (wd->pCancelIoEx != NULL) {
474 struct fio_overlapped *ovl = io_u->engine_data;
476 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
484 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
486 struct fio_overlapped *o = io_u->engine_data;
489 CloseHandle(o->o.hEvent);
490 io_u->engine_data = NULL;
495 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
497 struct fio_overlapped *o;
499 o = malloc(sizeof(*o));
500 o->io_complete = FALSE;
502 o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
508 io_u->engine_data = o;
512 static struct ioengine_ops ioengine = {
513 .name = "windowsaio",
514 .version = FIO_IOOPS_VERSION,
515 .init = fio_windowsaio_init,
516 .queue = fio_windowsaio_queue,
517 .cancel = fio_windowsaio_cancel,
518 .getevents = fio_windowsaio_getevents,
519 .event = fio_windowsaio_event,
520 .cleanup = fio_windowsaio_cleanup,
521 .open_file = fio_windowsaio_open_file,
522 .close_file = fio_windowsaio_close_file,
523 .get_file_size = generic_get_file_size,
524 .io_u_init = fio_windowsaio_io_u_init,
525 .io_u_free = fio_windowsaio_io_u_free,
528 static void fio_init fio_posixaio_register(void)
530 register_ioengine(&ioengine);
533 static void fio_exit fio_posixaio_unregister(void)
535 unregister_ioengine(&ioengine);