4 * IO engine using Windows IO Completion Ports.
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 BOOL iothread_running;
35 struct windowsaio_data *wd;
38 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
40 static int fio_windowsaio_init(struct thread_data *td)
42 struct windowsaio_data *wd;
45 wd = calloc(1, sizeof(struct windowsaio_data));
47 log_err("windowsaio: failed to allocate memory for engine data\n");
52 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
53 if (wd->aio_events == NULL) {
54 log_err("windowsaio: failed to allocate memory for aio events list\n");
60 /* Create an auto-reset event */
61 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
62 if (wd->iocomplete_event == NULL) {
63 log_err("windowsaio: failed to create io complete event handle\n");
70 if (wd->aio_events != NULL)
80 struct thread_ctx *ctx;
81 struct windowsaio_data *wd;
84 hFile = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
85 if (hFile == INVALID_HANDLE_VALUE) {
86 log_err("windowsaio: failed to create io completion port\n");
91 wd->iothread_running = TRUE;
95 ctx = malloc(sizeof(struct thread_ctx));
97 if (!rc && ctx == NULL)
99 log_err("windowsaio: failed to allocate memory for thread context structure\n");
110 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, &threadid);
112 if (wd->iothread != NULL)
113 fio_setaffinity(threadid, td->o.cpumask);
115 log_err("windowsaio: failed to create io completion thread\n");
118 if (rc || wd->iothread == NULL)
125 static void fio_windowsaio_cleanup(struct thread_data *td)
127 struct windowsaio_data *wd;
129 wd = td->io_ops_data;
132 wd->iothread_running = FALSE;
133 WaitForSingleObject(wd->iothread, INFINITE);
135 CloseHandle(wd->iothread);
136 CloseHandle(wd->iocomplete_event);
138 free(wd->aio_events);
141 td->io_ops_data = NULL;
145 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
148 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
149 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
150 DWORD openmode = OPEN_ALWAYS;
153 dprint(FD_FILE, "fd open %s\n", f->file_name);
155 if (f->filetype == FIO_TYPE_PIPE) {
156 log_err("windowsaio: pipes are not supported\n");
160 if (!strcmp(f->file_name, "-")) {
161 log_err("windowsaio: can't read/write to stdin/out\n");
166 flags |= FILE_FLAG_NO_BUFFERING;
168 flags |= FILE_FLAG_WRITE_THROUGH;
171 * Inform Windows whether we're going to be doing sequential or
172 * random io so it can tune the Cache Manager
174 if (td->o.td_ddir == TD_DDIR_READ ||
175 td->o.td_ddir == TD_DDIR_WRITE)
176 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
178 flags |= FILE_FLAG_RANDOM_ACCESS;
180 if (!td_write(td) || read_only)
181 access = GENERIC_READ;
183 access = (GENERIC_READ | GENERIC_WRITE);
185 if (td->o.create_on_open)
186 openmode = OPEN_ALWAYS;
188 openmode = OPEN_EXISTING;
190 f->hFile = CreateFile(f->file_name, access, sharemode,
191 NULL, openmode, flags, NULL);
193 if (f->hFile == INVALID_HANDLE_VALUE) {
194 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
198 /* Only set up the completion port and thread if we're not just
199 * querying the device size */
200 if (!rc && td->io_ops_data != NULL) {
201 struct windowsaio_data *wd;
203 wd = td->io_ops_data;
205 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
206 log_err("windowsaio: failed to create io completion port\n");
214 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
218 dprint(FD_FILE, "fd close %s\n", f->file_name);
220 if (f->hFile != INVALID_HANDLE_VALUE) {
221 if (!CloseHandle(f->hFile)) {
222 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
227 f->hFile = INVALID_HANDLE_VALUE;
231 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
233 BOOL expired = FALSE;
236 current_time = GetTickCount();
238 if ((end_count > start_count) && current_time >= end_count)
240 else if (current_time < start_count && current_time > end_count)
246 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
248 struct windowsaio_data *wd = td->io_ops_data;
249 return wd->aio_events[event];
252 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
254 const struct timespec *t)
256 struct windowsaio_data *wd = td->io_ops_data;
257 unsigned int dequeued = 0;
260 struct fio_overlapped *fov;
261 DWORD start_count = 0;
267 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
268 start_count = GetTickCount();
269 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
273 io_u_qiter(&td->io_u_all, io_u, i) {
274 if (!(io_u->flags & IO_U_F_FLIGHT))
277 fov = (struct fio_overlapped*)io_u->engine_data;
279 if (fov->io_complete) {
280 fov->io_complete = FALSE;
281 wd->aio_events[dequeued] = io_u;
289 if (dequeued < min) {
290 status = WaitForSingleObject(wd->iocomplete_event, mswait);
291 if (status != WAIT_OBJECT_0 && dequeued >= min)
295 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
302 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
304 struct fio_overlapped *o = io_u->engine_data;
305 LPOVERLAPPED lpOvl = &o->o;
306 BOOL success = FALSE;
307 int rc = FIO_Q_COMPLETED;
309 fio_ro_check(td, io_u);
312 lpOvl->InternalHigh = 0;
313 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
314 lpOvl->OffsetHigh = io_u->offset >> 32;
316 switch (io_u->ddir) {
318 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
321 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
325 case DDIR_SYNC_FILE_RANGE:
326 success = FlushFileBuffers(io_u->file->hFile);
328 log_err("windowsaio: failed to flush file buffers\n");
329 io_u->error = win_to_posix_error(GetLastError());
332 return FIO_Q_COMPLETED;
335 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
337 io_u->resid = io_u->xfer_buflen;
338 return FIO_Q_COMPLETED;
345 if (success || GetLastError() == ERROR_IO_PENDING)
348 io_u->error = win_to_posix_error(GetLastError());
349 io_u->resid = io_u->xfer_buflen;
355 /* Runs as a thread and waits for queued IO to complete */
356 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
359 struct fio_overlapped *fov;
361 struct windowsaio_data *wd;
362 struct thread_ctx *ctx;
366 ctx = (struct thread_ctx*)lpParameter;
370 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
373 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
376 if (ovl->Internal == ERROR_SUCCESS) {
377 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
380 io_u->resid = io_u->xfer_buflen;
381 io_u->error = win_to_posix_error(GetLastError());
384 fov->io_complete = TRUE;
385 SetEvent(wd->iocomplete_event);
386 } while (ctx->wd->iothread_running);
388 CloseHandle(ctx->iocp);
393 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
395 struct fio_overlapped *o = io_u->engine_data;
398 io_u->engine_data = NULL;
403 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
405 struct fio_overlapped *o;
407 o = malloc(sizeof(*o));
408 o->io_complete = FALSE;
411 io_u->engine_data = o;
415 static struct ioengine_ops ioengine = {
416 .name = "windowsaio",
417 .version = FIO_IOOPS_VERSION,
418 .init = fio_windowsaio_init,
419 .queue = fio_windowsaio_queue,
420 .getevents = fio_windowsaio_getevents,
421 .event = fio_windowsaio_event,
422 .cleanup = fio_windowsaio_cleanup,
423 .open_file = fio_windowsaio_open_file,
424 .close_file = fio_windowsaio_close_file,
425 .get_file_size = generic_get_file_size,
426 .io_u_init = fio_windowsaio_io_u_init,
427 .io_u_free = fio_windowsaio_io_u_free,
430 static void fio_init fio_windowsaio_register(void)
432 register_ioengine(&ioengine);
435 static void fio_exit fio_windowsaio_unregister(void)
437 unregister_ioengine(&ioengine);