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 BOOL timeout_expired(DWORD start_count, DWORD end_count);
39 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
40 unsigned int max, struct timespec *t);
41 static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
42 static int fio_windowsaio_queue(struct thread_data *td,
44 static void fio_windowsaio_cleanup(struct thread_data *td);
45 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
46 static int fio_windowsaio_init(struct thread_data *td);
47 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
48 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
50 static int fio_windowsaio_init(struct thread_data *td)
52 struct windowsaio_data *wd;
55 wd = calloc(1, sizeof(struct windowsaio_data));
57 log_err("windowsaio: failed to allocate memory for engine data\n");
62 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
63 if (wd->aio_events == NULL) {
64 log_err("windowsaio: failed to allocate memory for aio events list\n");
70 /* Create an auto-reset event */
71 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
72 if (wd->iocomplete_event == NULL) {
73 log_err("windowsaio: failed to create io complete event handle\n");
80 if (wd->aio_events != NULL)
87 td->io_ops->data = wd;
90 struct thread_ctx *ctx;
91 struct windowsaio_data *wd;
94 hFile = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
95 if (hFile == INVALID_HANDLE_VALUE) {
96 log_err("windowsaio: failed to create io completion port\n");
100 wd = td->io_ops->data;
101 wd->iothread_running = TRUE;
105 ctx = malloc(sizeof(struct thread_ctx));
107 if (!rc && ctx == NULL)
109 log_err("windowsaio: failed to allocate memory for thread context structure\n");
118 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
119 if (wd->iothread == NULL)
120 log_err("windowsaio: failed to create io completion thread\n");
123 if (rc || wd->iothread == NULL)
130 static void fio_windowsaio_cleanup(struct thread_data *td)
132 struct windowsaio_data *wd;
134 wd = td->io_ops->data;
137 wd->iothread_running = FALSE;
138 WaitForSingleObject(wd->iothread, INFINITE);
140 CloseHandle(wd->iothread);
141 CloseHandle(wd->iocomplete_event);
143 free(wd->aio_events);
146 td->io_ops->data = NULL;
151 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
154 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
155 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
156 DWORD openmode = OPEN_ALWAYS;
159 dprint(FD_FILE, "fd open %s\n", f->file_name);
161 if (f->filetype == FIO_TYPE_PIPE) {
162 log_err("windowsaio: pipes are not supported\n");
166 if (!strcmp(f->file_name, "-")) {
167 log_err("windowsaio: can't read/write to stdin/out\n");
172 flags |= FILE_FLAG_NO_BUFFERING;
174 flags |= FILE_FLAG_WRITE_THROUGH;
177 * Inform Windows whether we're going to be doing sequential or
178 * random io so it can tune the Cache Manager
180 if (td->o.td_ddir == TD_DDIR_READ ||
181 td->o.td_ddir == TD_DDIR_WRITE)
182 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
184 flags |= FILE_FLAG_RANDOM_ACCESS;
186 if (!td_write(td) || read_only)
187 access = GENERIC_READ;
189 access = (GENERIC_READ | GENERIC_WRITE);
191 if (td->o.create_on_open)
192 openmode = OPEN_ALWAYS;
194 openmode = OPEN_EXISTING;
196 f->hFile = CreateFile(f->file_name, access, sharemode,
197 NULL, openmode, flags, NULL);
199 if (f->hFile == INVALID_HANDLE_VALUE) {
200 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
204 /* Only set up the completion port and thread if we're not just
205 * querying the device size */
206 if (!rc && td->io_ops->data != NULL) {
207 struct windowsaio_data *wd;
209 wd = td->io_ops->data;
211 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
212 log_err("windowsaio: failed to create io completion port\n");
220 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
224 dprint(FD_FILE, "fd close %s\n", f->file_name);
226 if (f->hFile != INVALID_HANDLE_VALUE) {
227 if (!CloseHandle(f->hFile)) {
228 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
233 f->hFile = INVALID_HANDLE_VALUE;
237 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
239 BOOL expired = FALSE;
242 current_time = GetTickCount();
244 if ((end_count > start_count) && current_time >= end_count)
246 else if (current_time < start_count && current_time > end_count)
252 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
254 struct windowsaio_data *wd = td->io_ops->data;
255 return wd->aio_events[event];
258 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
259 unsigned int max, struct timespec *t)
261 struct windowsaio_data *wd = td->io_ops->data;
262 struct flist_head *entry;
263 unsigned int dequeued = 0;
265 struct fio_overlapped *fov;
266 DWORD start_count = 0;
272 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
273 start_count = GetTickCount();
274 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
278 flist_for_each(entry, &td->io_u_busylist) {
279 io_u = flist_entry(entry, struct io_u, list);
280 fov = (struct fio_overlapped*)io_u->engine_data;
282 if (fov->io_complete) {
283 fov->io_complete = FALSE;
284 ResetEvent(fov->o.hEvent);
285 wd->aio_events[dequeued] = io_u;
293 if (dequeued < min) {
294 status = WaitForSingleObject(wd->iocomplete_event, mswait);
295 if (status != WAIT_OBJECT_0 && dequeued >= min)
299 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
306 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
308 struct fio_overlapped *o = io_u->engine_data;
309 LPOVERLAPPED lpOvl = &o->o;
311 BOOL success = FALSE;
312 int rc = FIO_Q_COMPLETED;
314 fio_ro_check(td, io_u);
316 lpOvl->Internal = STATUS_PENDING;
317 lpOvl->InternalHigh = 0;
318 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
319 lpOvl->OffsetHigh = io_u->offset >> 32;
321 switch (io_u->ddir) {
323 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
326 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
330 case DDIR_SYNC_FILE_RANGE:
331 success = FlushFileBuffers(io_u->file->hFile);
333 log_err("windowsaio: failed to flush file buffers\n");
334 io_u->error = win_to_posix_error(GetLastError());
337 return FIO_Q_COMPLETED;
340 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
342 io_u->resid = io_u->xfer_buflen;
343 return FIO_Q_COMPLETED;
350 if (success || GetLastError() == ERROR_IO_PENDING)
353 io_u->error = win_to_posix_error(GetLastError());
354 io_u->resid = io_u->xfer_buflen;
360 /* Runs as a thread and waits for queued IO to complete */
361 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
364 struct fio_overlapped *fov;
366 struct windowsaio_data *wd;
367 struct thread_ctx *ctx;
371 ctx = (struct thread_ctx*)lpParameter;
375 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
378 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
381 if (ovl->Internal == ERROR_SUCCESS) {
382 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
385 io_u->resid = io_u->xfer_buflen;
386 io_u->error = win_to_posix_error(GetLastError());
389 fov->io_complete = TRUE;
390 SetEvent(wd->iocomplete_event);
391 } while (ctx->wd->iothread_running);
393 CloseHandle(ctx->iocp);
398 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
400 struct fio_overlapped *o = io_u->engine_data;
403 CloseHandle(o->o.hEvent);
404 io_u->engine_data = NULL;
409 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
411 struct fio_overlapped *o;
413 o = malloc(sizeof(*o));
414 o->io_complete = FALSE;
416 o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
417 if (o->o.hEvent == NULL) {
418 log_err("windowsaio: failed to create event handle\n");
423 io_u->engine_data = o;
427 static struct ioengine_ops ioengine = {
428 .name = "windowsaio",
429 .version = FIO_IOOPS_VERSION,
430 .init = fio_windowsaio_init,
431 .queue = fio_windowsaio_queue,
432 .getevents = fio_windowsaio_getevents,
433 .event = fio_windowsaio_event,
434 .cleanup = fio_windowsaio_cleanup,
435 .open_file = fio_windowsaio_open_file,
436 .close_file = fio_windowsaio_close_file,
437 .get_file_size = generic_get_file_size,
438 .io_u_init = fio_windowsaio_io_u_init,
439 .io_u_free = fio_windowsaio_io_u_free,
442 static void fio_init fio_windowsaio_register(void)
444 register_ioengine(&ioengine);
447 static void fio_exit fio_windowsaio_unregister(void)
449 unregister_ioengine(&ioengine);