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, const 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)
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");
120 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, &threadid);
122 if (wd->iothread != NULL)
123 fio_setaffinity(threadid, td->o.cpumask);
125 log_err("windowsaio: failed to create io completion thread\n");
128 if (rc || wd->iothread == NULL)
135 static void fio_windowsaio_cleanup(struct thread_data *td)
137 struct windowsaio_data *wd;
139 wd = td->io_ops_data;
142 wd->iothread_running = FALSE;
143 WaitForSingleObject(wd->iothread, INFINITE);
145 CloseHandle(wd->iothread);
146 CloseHandle(wd->iocomplete_event);
148 free(wd->aio_events);
151 td->io_ops_data = NULL;
156 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
159 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
160 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
161 DWORD openmode = OPEN_ALWAYS;
164 dprint(FD_FILE, "fd open %s\n", f->file_name);
166 if (f->filetype == FIO_TYPE_PIPE) {
167 log_err("windowsaio: pipes are not supported\n");
171 if (!strcmp(f->file_name, "-")) {
172 log_err("windowsaio: can't read/write to stdin/out\n");
177 flags |= FILE_FLAG_NO_BUFFERING;
179 flags |= FILE_FLAG_WRITE_THROUGH;
182 * Inform Windows whether we're going to be doing sequential or
183 * random io so it can tune the Cache Manager
185 if (td->o.td_ddir == TD_DDIR_READ ||
186 td->o.td_ddir == TD_DDIR_WRITE)
187 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
189 flags |= FILE_FLAG_RANDOM_ACCESS;
191 if (!td_write(td) || read_only)
192 access = GENERIC_READ;
194 access = (GENERIC_READ | GENERIC_WRITE);
196 if (td->o.create_on_open)
197 openmode = OPEN_ALWAYS;
199 openmode = OPEN_EXISTING;
201 f->hFile = CreateFile(f->file_name, access, sharemode,
202 NULL, openmode, flags, NULL);
204 if (f->hFile == INVALID_HANDLE_VALUE) {
205 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
209 /* Only set up the completion port and thread if we're not just
210 * querying the device size */
211 if (!rc && td->io_ops_data != NULL) {
212 struct windowsaio_data *wd;
214 wd = td->io_ops_data;
216 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
217 log_err("windowsaio: failed to create io completion port\n");
225 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
229 dprint(FD_FILE, "fd close %s\n", f->file_name);
231 if (f->hFile != INVALID_HANDLE_VALUE) {
232 if (!CloseHandle(f->hFile)) {
233 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
238 f->hFile = INVALID_HANDLE_VALUE;
242 static BOOL timeout_expired(DWORD start_count, DWORD end_count)
244 BOOL expired = FALSE;
247 current_time = GetTickCount();
249 if ((end_count > start_count) && current_time >= end_count)
251 else if (current_time < start_count && current_time > end_count)
257 static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
259 struct windowsaio_data *wd = td->io_ops_data;
260 return wd->aio_events[event];
263 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
265 const struct timespec *t)
267 struct windowsaio_data *wd = td->io_ops_data;
268 unsigned int dequeued = 0;
271 struct fio_overlapped *fov;
272 DWORD start_count = 0;
278 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
279 start_count = GetTickCount();
280 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
284 io_u_qiter(&td->io_u_all, io_u, i) {
285 if (!(io_u->flags & IO_U_F_FLIGHT))
288 fov = (struct fio_overlapped*)io_u->engine_data;
290 if (fov->io_complete) {
291 fov->io_complete = FALSE;
292 wd->aio_events[dequeued] = io_u;
300 if (dequeued < min) {
301 status = WaitForSingleObject(wd->iocomplete_event, mswait);
302 if (status != WAIT_OBJECT_0 && dequeued >= min)
306 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
313 static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
315 struct fio_overlapped *o = io_u->engine_data;
316 LPOVERLAPPED lpOvl = &o->o;
317 BOOL success = FALSE;
318 int rc = FIO_Q_COMPLETED;
320 fio_ro_check(td, io_u);
323 lpOvl->InternalHigh = 0;
324 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
325 lpOvl->OffsetHigh = io_u->offset >> 32;
327 switch (io_u->ddir) {
329 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
332 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
336 case DDIR_SYNC_FILE_RANGE:
337 success = FlushFileBuffers(io_u->file->hFile);
339 log_err("windowsaio: failed to flush file buffers\n");
340 io_u->error = win_to_posix_error(GetLastError());
343 return FIO_Q_COMPLETED;
346 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
348 io_u->resid = io_u->xfer_buflen;
349 return FIO_Q_COMPLETED;
356 if (success || GetLastError() == ERROR_IO_PENDING)
359 io_u->error = win_to_posix_error(GetLastError());
360 io_u->resid = io_u->xfer_buflen;
366 /* Runs as a thread and waits for queued IO to complete */
367 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
370 struct fio_overlapped *fov;
372 struct windowsaio_data *wd;
373 struct thread_ctx *ctx;
377 ctx = (struct thread_ctx*)lpParameter;
381 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
384 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
387 if (ovl->Internal == ERROR_SUCCESS) {
388 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
391 io_u->resid = io_u->xfer_buflen;
392 io_u->error = win_to_posix_error(GetLastError());
395 fov->io_complete = TRUE;
396 SetEvent(wd->iocomplete_event);
397 } while (ctx->wd->iothread_running);
399 CloseHandle(ctx->iocp);
404 static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
406 struct fio_overlapped *o = io_u->engine_data;
409 io_u->engine_data = NULL;
414 static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
416 struct fio_overlapped *o;
418 o = malloc(sizeof(*o));
419 o->io_complete = FALSE;
422 io_u->engine_data = o;
426 static struct ioengine_ops ioengine = {
427 .name = "windowsaio",
428 .version = FIO_IOOPS_VERSION,
429 .init = fio_windowsaio_init,
430 .queue = fio_windowsaio_queue,
431 .getevents = fio_windowsaio_getevents,
432 .event = fio_windowsaio_event,
433 .cleanup = fio_windowsaio_cleanup,
434 .open_file = fio_windowsaio_open_file,
435 .close_file = fio_windowsaio_close_file,
436 .get_file_size = generic_get_file_size,
437 .io_u_init = fio_windowsaio_io_u_init,
438 .io_u_free = fio_windowsaio_io_u_free,
441 static void fio_init fio_windowsaio_register(void)
443 register_ioengine(&ioengine);
446 static void fio_exit fio_windowsaio_unregister(void)
448 unregister_ioengine(&ioengine);