4 * IO engine using Windows IO Completion Ports.
16 typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
18 int geterrno_from_win_error (DWORD code, int deferrno);
20 struct fio_overlapped {
26 struct windowsaio_data {
27 struct io_u **aio_events;
30 HANDLE iocomplete_event;
31 BOOL iothread_running;
36 struct windowsaio_data *wd;
39 static BOOL timeout_expired(DWORD start_count, DWORD end_count);
40 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
41 unsigned int max, struct timespec *t);
42 static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
43 static int fio_windowsaio_queue(struct thread_data *td,
45 static void fio_windowsaio_cleanup(struct thread_data *td);
46 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
47 static int fio_windowsaio_init(struct thread_data *td);
48 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
49 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
51 static int fio_windowsaio_init(struct thread_data *td)
53 struct windowsaio_data *wd;
56 wd = calloc(1, sizeof(struct windowsaio_data));
58 log_err("windowsaio: failed to allocate memory for engine data\n");
63 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
64 if (wd->aio_events == NULL) {
65 log_err("windowsaio: failed to allocate memory for aio events list\n");
71 /* Create an auto-reset event */
72 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
73 if (wd->iocomplete_event == NULL) {
74 log_err("windowsaio: failed to create io complete event handle\n");
81 if (wd->aio_events != NULL)
88 td->io_ops->data = wd;
91 struct thread_ctx *ctx;
92 struct windowsaio_data *wd;
95 hFile = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
96 if (hFile == INVALID_HANDLE_VALUE) {
97 log_err("windowsaio: failed to create io completion port\n");
101 wd = td->io_ops->data;
102 wd->iothread_running = TRUE;
106 ctx = malloc(sizeof(struct thread_ctx));
108 if (!rc && ctx == NULL)
110 log_err("windowsaio: failed to allocate memory for thread context structure\n");
119 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
120 if (wd->iothread == NULL)
121 log_err("windowsaio: failed to create io completion thread\n");
124 if (rc || wd->iothread == NULL)
131 static void fio_windowsaio_cleanup(struct thread_data *td)
133 struct windowsaio_data *wd;
135 wd = td->io_ops->data;
138 wd->iothread_running = FALSE;
139 WaitForSingleObject(wd->iothread, INFINITE);
141 CloseHandle(wd->iothread);
142 CloseHandle(wd->iocomplete_event);
144 free(wd->aio_events);
147 td->io_ops->data = NULL;
152 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
155 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
156 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
157 DWORD openmode = OPEN_ALWAYS;
160 dprint(FD_FILE, "fd open %s\n", f->file_name);
162 if (f->filetype == FIO_TYPE_PIPE) {
163 log_err("windowsaio: pipes are not supported\n");
167 if (!strcmp(f->file_name, "-")) {
168 log_err("windowsaio: can't read/write to stdin/out\n");
173 flags |= FILE_FLAG_NO_BUFFERING;
175 flags |= FILE_FLAG_WRITE_THROUGH;
178 * Inform Windows whether we're going to be doing sequential or
179 * random io so it can tune the Cache Manager
181 if (td->o.td_ddir == TD_DDIR_READ ||
182 td->o.td_ddir == TD_DDIR_WRITE)
183 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
185 flags |= FILE_FLAG_RANDOM_ACCESS;
187 if (!td_write(td) || read_only)
188 access = GENERIC_READ;
190 access = (GENERIC_READ | GENERIC_WRITE);
192 if (td->o.create_on_open)
193 openmode = OPEN_ALWAYS;
195 openmode = OPEN_EXISTING;
197 f->hFile = CreateFile(f->file_name, access, sharemode,
198 NULL, openmode, flags, NULL);
200 if (f->hFile == INVALID_HANDLE_VALUE) {
201 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
205 /* Only set up the completion port and thread if we're not just
206 * querying the device size */
207 if (!rc && td->io_ops->data != NULL) {
208 struct windowsaio_data *wd;
210 wd = td->io_ops->data;
212 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
213 log_err("windowsaio: failed to create io completion port\n");
221 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
225 dprint(FD_FILE, "fd close %s\n", f->file_name);
227 if (f->hFile != INVALID_HANDLE_VALUE) {
228 if (!CloseHandle(f->hFile)) {
229 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
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;
285 ResetEvent(fov->o.hEvent);
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 >= min)
300 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
307 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
309 struct fio_overlapped *o = io_u->engine_data;
310 LPOVERLAPPED lpOvl = &o->o;
312 BOOL success = FALSE;
313 int rc = FIO_Q_COMPLETED;
315 fio_ro_check(td, io_u);
317 lpOvl->Internal = STATUS_PENDING;
318 lpOvl->InternalHigh = 0;
319 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
320 lpOvl->OffsetHigh = io_u->offset >> 32;
322 switch (io_u->ddir) {
324 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
327 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
331 case DDIR_SYNC_FILE_RANGE:
332 success = FlushFileBuffers(io_u->file->hFile);
334 log_err("windowsaio: failed to flush file buffers\n");
335 io_u->error = win_to_posix_error(GetLastError());
338 return FIO_Q_COMPLETED;
341 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
343 io_u->resid = io_u->xfer_buflen;
344 return FIO_Q_COMPLETED;
351 if (success || GetLastError() == ERROR_IO_PENDING)
354 io_u->error = win_to_posix_error(GetLastError());
355 io_u->resid = io_u->xfer_buflen;
361 /* Runs as a thread and waits for queued IO to complete */
362 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
365 struct fio_overlapped *fov;
367 struct windowsaio_data *wd;
368 struct thread_ctx *ctx;
372 ctx = (struct thread_ctx*)lpParameter;
376 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
379 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
382 if (ovl->Internal == ERROR_SUCCESS) {
383 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
386 io_u->resid = io_u->xfer_buflen;
387 io_u->error = win_to_posix_error(GetLastError());
390 fov->io_complete = TRUE;
391 SetEvent(wd->iocomplete_event);
392 } while (ctx->wd->iothread_running);
394 CloseHandle(ctx->iocp);
399 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
401 struct fio_overlapped *o = io_u->engine_data;
404 CloseHandle(o->o.hEvent);
405 io_u->engine_data = NULL;
410 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
412 struct fio_overlapped *o;
414 o = malloc(sizeof(*o));
415 o->io_complete = FALSE;
417 o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
418 if (o->o.hEvent == NULL) {
419 log_err("windowsaio: failed to create event handle\n");
424 io_u->engine_data = o;
428 static struct ioengine_ops ioengine = {
429 .name = "windowsaio",
430 .version = FIO_IOOPS_VERSION,
431 .init = fio_windowsaio_init,
432 .queue = fio_windowsaio_queue,
433 .getevents = fio_windowsaio_getevents,
434 .event = fio_windowsaio_event,
435 .cleanup = fio_windowsaio_cleanup,
436 .open_file = fio_windowsaio_open_file,
437 .close_file = fio_windowsaio_close_file,
438 .get_file_size = generic_get_file_size,
439 .io_u_init = fio_windowsaio_io_u_init,
440 .io_u_free = fio_windowsaio_io_u_free,
443 static void fio_init fio_windowsaio_register(void)
445 register_ioengine(&ioengine);
448 static void fio_exit fio_windowsaio_unregister(void)
450 unregister_ioengine(&ioengine);