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 switch (td->o.fadvise_hint) {
177 flags |= FILE_FLAG_RANDOM_ACCESS;
179 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
182 flags |= FILE_FLAG_RANDOM_ACCESS;
184 case F_ADV_SEQUENTIAL:
185 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
190 log_err("fio: unknown fadvise type %d\n", td->o.fadvise_hint);
193 if (!td_write(td) || read_only)
194 access = GENERIC_READ;
196 access = (GENERIC_READ | GENERIC_WRITE);
198 if (td->o.create_on_open)
199 openmode = OPEN_ALWAYS;
201 openmode = OPEN_EXISTING;
203 f->hFile = CreateFile(f->file_name, access, sharemode,
204 NULL, openmode, flags, NULL);
206 if (f->hFile == INVALID_HANDLE_VALUE) {
207 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
211 /* Only set up the completion port and thread if we're not just
212 * querying the device size */
213 if (!rc && td->io_ops_data != NULL) {
214 struct windowsaio_data *wd;
216 wd = td->io_ops_data;
218 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
219 log_err("windowsaio: failed to create io completion port\n");
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)) {
235 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
240 f->hFile = INVALID_HANDLE_VALUE;
244 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
246 BOOL expired = FALSE;
249 current_time = GetTickCount();
251 if ((end_count > start_count) && current_time >= end_count)
253 else if (current_time < start_count && current_time > end_count)
259 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
261 struct windowsaio_data *wd = td->io_ops_data;
262 return wd->aio_events[event];
265 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
267 const struct timespec *t)
269 struct windowsaio_data *wd = td->io_ops_data;
270 unsigned int dequeued = 0;
273 struct fio_overlapped *fov;
274 DWORD start_count = 0;
280 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
281 start_count = GetTickCount();
282 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
286 io_u_qiter(&td->io_u_all, io_u, i) {
287 if (!(io_u->flags & IO_U_F_FLIGHT))
290 fov = (struct fio_overlapped*)io_u->engine_data;
292 if (fov->io_complete) {
293 fov->io_complete = FALSE;
294 wd->aio_events[dequeued] = io_u;
302 if (dequeued < min) {
303 status = WaitForSingleObject(wd->iocomplete_event, mswait);
304 if (status != WAIT_OBJECT_0 && dequeued >= min)
308 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
315 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
317 struct fio_overlapped *o = io_u->engine_data;
318 LPOVERLAPPED lpOvl = &o->o;
319 BOOL success = FALSE;
320 int rc = FIO_Q_COMPLETED;
322 fio_ro_check(td, io_u);
325 lpOvl->InternalHigh = 0;
326 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
327 lpOvl->OffsetHigh = io_u->offset >> 32;
329 switch (io_u->ddir) {
331 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
334 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
338 case DDIR_SYNC_FILE_RANGE:
339 success = FlushFileBuffers(io_u->file->hFile);
341 log_err("windowsaio: failed to flush file buffers\n");
342 io_u->error = win_to_posix_error(GetLastError());
345 return FIO_Q_COMPLETED;
348 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
350 io_u->resid = io_u->xfer_buflen;
351 return FIO_Q_COMPLETED;
358 if (success || GetLastError() == ERROR_IO_PENDING)
361 io_u->error = win_to_posix_error(GetLastError());
362 io_u->resid = io_u->xfer_buflen;
368 /* Runs as a thread and waits for queued IO to complete */
369 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
372 struct fio_overlapped *fov;
374 struct windowsaio_data *wd;
375 struct thread_ctx *ctx;
379 ctx = (struct thread_ctx*)lpParameter;
383 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
386 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
389 if (ovl->Internal == ERROR_SUCCESS) {
390 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
393 io_u->resid = io_u->xfer_buflen;
394 io_u->error = win_to_posix_error(GetLastError());
397 fov->io_complete = TRUE;
398 SetEvent(wd->iocomplete_event);
399 } while (ctx->wd->iothread_running);
401 CloseHandle(ctx->iocp);
406 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
408 struct fio_overlapped *o = io_u->engine_data;
411 io_u->engine_data = NULL;
416 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
418 struct fio_overlapped *o;
420 o = malloc(sizeof(*o));
421 o->io_complete = FALSE;
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);