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 {
26 struct windowsaio_data {
27 struct fio_overlapped *ovls;
28 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);
53 static int win_to_poxix_error(DWORD winerr);
55 static int win_to_poxix_error(DWORD winerr)
59 case ERROR_FILE_NOT_FOUND: return ENOENT;
60 case ERROR_PATH_NOT_FOUND: return ENOENT;
61 case ERROR_ACCESS_DENIED: return EACCES;
62 case ERROR_INVALID_HANDLE: return EBADF;
63 case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM;
64 case ERROR_INVALID_DATA: return EINVAL;
65 case ERROR_OUTOFMEMORY: return ENOMEM;
66 case ERROR_INVALID_DRIVE: return ENODEV;
67 case ERROR_NOT_SAME_DEVICE: return EXDEV;
68 case ERROR_WRITE_PROTECT: return EROFS;
69 case ERROR_BAD_UNIT: return ENODEV;
70 case ERROR_SHARING_VIOLATION: return EACCES;
71 case ERROR_LOCK_VIOLATION: return EACCES;
72 case ERROR_SHARING_BUFFER_EXCEEDED: return ENOLCK;
73 case ERROR_HANDLE_DISK_FULL: return ENOSPC;
74 case ERROR_NOT_SUPPORTED: return ENOSYS;
75 case ERROR_FILE_EXISTS: return EEXIST;
76 case ERROR_CANNOT_MAKE: return EPERM;
77 case ERROR_INVALID_PARAMETER: return EINVAL;
78 case ERROR_NO_PROC_SLOTS: return EAGAIN;
79 case ERROR_BROKEN_PIPE: return EPIPE;
80 case ERROR_OPEN_FAILED: return EIO;
81 case ERROR_NO_MORE_SEARCH_HANDLES: return ENFILE;
82 case ERROR_CALL_NOT_IMPLEMENTED: return ENOSYS;
83 case ERROR_INVALID_NAME: return ENOENT;
84 case ERROR_WAIT_NO_CHILDREN: return ECHILD;
85 case ERROR_CHILD_NOT_COMPLETE: return EBUSY;
86 case ERROR_DIR_NOT_EMPTY: return ENOTEMPTY;
87 case ERROR_SIGNAL_REFUSED: return EIO;
88 case ERROR_BAD_PATHNAME: return ENOENT;
89 case ERROR_SIGNAL_PENDING: return EBUSY;
90 case ERROR_MAX_THRDS_REACHED: return EAGAIN;
91 case ERROR_BUSY: return EBUSY;
92 case ERROR_ALREADY_EXISTS: return EEXIST;
93 case ERROR_NO_SIGNAL_SENT: return EIO;
94 case ERROR_FILENAME_EXCED_RANGE: return EINVAL;
95 case ERROR_META_EXPANSION_TOO_LONG: return EINVAL;
96 case ERROR_INVALID_SIGNAL_NUMBER: return EINVAL;
97 case ERROR_THREAD_1_INACTIVE: return EINVAL;
98 case ERROR_BAD_PIPE: return EINVAL;
99 case ERROR_PIPE_BUSY: return EBUSY;
100 case ERROR_NO_DATA: return EPIPE;
101 case ERROR_MORE_DATA: return EAGAIN;
102 case ERROR_DIRECTORY: return ENOTDIR;
103 case ERROR_PIPE_CONNECTED: return EBUSY;
104 case ERROR_NO_TOKEN: return EINVAL;
105 case ERROR_PROCESS_ABORTED: return EFAULT;
106 case ERROR_BAD_DEVICE: return ENODEV;
107 case ERROR_BAD_USERNAME: return EINVAL;
108 case ERROR_OPEN_FILES: return EAGAIN;
109 case ERROR_ACTIVE_CONNECTIONS: return EAGAIN;
110 case ERROR_DEVICE_IN_USE: return EAGAIN;
111 case ERROR_INVALID_AT_INTERRUPT_TIME: return EINTR;
112 case ERROR_IO_DEVICE: return EIO;
113 case ERROR_NOT_OWNER: return EPERM;
114 case ERROR_END_OF_MEDIA: return ENOSPC;
115 case ERROR_EOM_OVERFLOW: return ENOSPC;
116 case ERROR_BEGINNING_OF_MEDIA: return ESPIPE;
117 case ERROR_SETMARK_DETECTED: return ESPIPE;
118 case ERROR_NO_DATA_DETECTED: return ENOSPC;
119 case ERROR_POSSIBLE_DEADLOCK: return EDEADLOCK;
120 case ERROR_CRC: return EIO;
121 case ERROR_NEGATIVE_SEEK: return EINVAL;
122 case ERROR_DISK_FULL: return ENOSPC;
123 case ERROR_NOACCESS: return EFAULT;
124 case ERROR_FILE_INVALID: return ENXIO;
130 int sync_file_range(int fd, off64_t offset, off64_t nbytes,
137 static int fio_windowsaio_init(struct thread_data *td)
139 struct windowsaio_data *wd;
144 wd = malloc(sizeof(struct windowsaio_data));
146 ZeroMemory(wd, sizeof(struct windowsaio_data));
151 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
152 if (wd->aio_events == NULL)
157 wd->ovls = malloc(td->o.iodepth * sizeof(struct fio_overlapped));
158 if (wd->ovls == NULL)
163 for (i = 0; i < td->o.iodepth; i++) {
164 wd->ovls[i].io_free = TRUE;
165 wd->ovls[i].io_complete = FALSE;
167 wd->ovls[i].o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
168 if (wd->ovls[i].o.hEvent == NULL) {
176 /* Create an auto-reset event */
177 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
178 if (wd->iocomplete_event == NULL)
184 if (wd->ovls != NULL)
186 if (wd->aio_events != NULL)
187 free(wd->aio_events);
193 hKernel32Dll = GetModuleHandle("kernel32.dll");
194 wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
195 td->io_ops->data = wd;
200 static void fio_windowsaio_cleanup(struct thread_data *td)
203 struct windowsaio_data *wd;
205 wd = td->io_ops->data;
208 wd->iothread_running = FALSE;
209 WaitForSingleObject(wd->iothread, INFINITE);
211 CloseHandle(wd->iothread);
212 CloseHandle(wd->iocomplete_event);
214 for (i = 0; i < td->o.iodepth; i++) {
215 CloseHandle(wd->ovls[i].o.hEvent);
218 free(wd->aio_events);
222 td->io_ops->data = NULL;
227 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
231 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
232 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
233 DWORD openmode = OPEN_ALWAYS;
236 dprint(FD_FILE, "fd open %s\n", f->file_name);
238 if (f->filetype == FIO_TYPE_PIPE) {
239 log_err("fio: windowsaio doesn't support pipes\n");
243 if (!strcmp(f->file_name, "-")) {
244 log_err("fio: can't read/write to stdin/out\n");
249 flags |= FILE_FLAG_NO_BUFFERING;
251 flags |= FILE_FLAG_WRITE_THROUGH;
254 * Inform Windows whether we're going to be doing sequential or
255 * random io so it can tune the Cache Manager
257 if (td->o.td_ddir == TD_DDIR_READ ||
258 td->o.td_ddir == TD_DDIR_WRITE)
259 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
261 flags |= FILE_FLAG_RANDOM_ACCESS;
263 if (!td_write(td) || read_only)
264 access = GENERIC_READ;
266 access = (GENERIC_READ | GENERIC_WRITE);
268 if (td->o.create_on_open)
269 openmode = OPEN_ALWAYS;
271 openmode = OPEN_EXISTING;
273 f->hFile = CreateFile(f->file_name, access, sharemode,
274 NULL, openmode, flags, NULL);
276 if (f->hFile == INVALID_HANDLE_VALUE)
279 /* Only set up the completion port and thread if we're not just
280 * querying the device size */
281 if (!rc && td->io_ops->data != NULL) {
282 struct thread_ctx *ctx;
283 struct windowsaio_data *wd;
285 hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
287 wd = td->io_ops->data;
288 wd->iothread_running = TRUE;
291 ctx = malloc(sizeof(struct thread_ctx));
295 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
298 if (rc || wd->iothread == NULL)
305 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
309 dprint(FD_FILE, "fd close %s\n", f->file_name);
311 if (f->hFile != INVALID_HANDLE_VALUE) {
312 if (!CloseHandle(f->hFile))
316 f->hFile = INVALID_HANDLE_VALUE;
320 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
322 BOOL expired = FALSE;
325 current_time = GetTickCount();
327 if ((end_count > start_count) && current_time >= end_count)
329 else if (current_time < start_count && current_time > end_count)
335 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
337 struct windowsaio_data *wd = td->io_ops->data;
338 return wd->aio_events[event];
341 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
342 unsigned int max, struct timespec *t)
344 struct windowsaio_data *wd = td->io_ops->data;
345 struct flist_head *entry;
346 unsigned int dequeued = 0;
348 struct fio_overlapped *fov;
349 DWORD start_count = 0;
355 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
356 start_count = GetTickCount();
357 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
361 flist_for_each(entry, &td->io_u_busylist) {
362 io_u = flist_entry(entry, struct io_u, list);
363 fov = (struct fio_overlapped*)io_u->engine_data;
365 if (fov->io_complete) {
366 fov->io_complete = FALSE;
368 ResetEvent(fov->o.hEvent);
369 wd->aio_events[dequeued] = io_u;
377 if (dequeued < min) {
378 status = WaitForSingleObject(wd->iocomplete_event, mswait);
379 if (status != WAIT_OBJECT_0 && dequeued >= min)
383 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
390 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
392 LPOVERLAPPED lpOvl = NULL;
393 struct windowsaio_data *wd;
395 BOOL success = FALSE;
397 int rc = FIO_Q_COMPLETED;
399 fio_ro_check(td, io_u);
401 wd = td->io_ops->data;
403 for (index = 0; index < td->o.iodepth; index++) {
404 if (wd->ovls[index].io_free)
408 assert(index < td->o.iodepth);
410 wd->ovls[index].io_free = FALSE;
411 wd->ovls[index].io_u = io_u;
412 lpOvl = &wd->ovls[index].o;
413 lpOvl->Internal = STATUS_PENDING;
414 lpOvl->InternalHigh = 0;
415 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
416 lpOvl->OffsetHigh = io_u->offset >> 32;
417 io_u->engine_data = &wd->ovls[index];
419 switch (io_u->ddir) {
421 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
424 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
428 case DDIR_SYNC_FILE_RANGE:
429 success = FlushFileBuffers(io_u->file->hFile);
431 io_u->error = win_to_poxix_error(GetLastError());
433 return FIO_Q_COMPLETED;
436 log_err("manual TRIM isn't supported on Windows");
438 io_u->resid = io_u->xfer_buflen;
439 return FIO_Q_COMPLETED;
446 if (success || GetLastError() == ERROR_IO_PENDING)
449 io_u->error = win_to_poxix_error(GetLastError());
450 io_u->resid = io_u->xfer_buflen;
456 /* Runs as a thread and waits for queued IO to complete */
457 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
460 struct fio_overlapped *fov;
462 struct windowsaio_data *wd;
463 struct thread_ctx *ctx;
467 ctx = (struct thread_ctx*)lpParameter;
471 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
474 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
477 if (ovl->Internal == ERROR_SUCCESS) {
478 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
481 io_u->resid = io_u->xfer_buflen;
482 io_u->error = win_to_poxix_error(GetLastError());
485 fov->io_complete = TRUE;
486 SetEvent(wd->iocomplete_event);
487 } while (ctx->wd->iothread_running);
489 CloseHandle(ctx->iocp);
494 static int fio_windowsaio_cancel(struct thread_data *td,
499 struct windowsaio_data *wd = td->io_ops->data;
501 /* If we're running on Vista or newer, we can cancel individual IO requests */
502 if (wd->pCancelIoEx != NULL) {
503 struct fio_overlapped *ovl = io_u->engine_data;
505 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
513 static struct ioengine_ops ioengine = {
514 .name = "windowsaio",
515 .version = FIO_IOOPS_VERSION,
516 .init = fio_windowsaio_init,
517 .queue = fio_windowsaio_queue,
518 .cancel = fio_windowsaio_cancel,
519 .getevents = fio_windowsaio_getevents,
520 .event = fio_windowsaio_event,
521 .cleanup = fio_windowsaio_cleanup,
522 .open_file = fio_windowsaio_open_file,
523 .close_file = fio_windowsaio_close_file,
524 .get_file_size = generic_get_file_size
527 static void fio_init fio_posixaio_register(void)
529 register_ioengine(&ioengine);
532 static void fio_exit fio_posixaio_unregister(void)
534 unregister_ioengine(&ioengine);