2 * Native Windows async IO engine
3 * Copyright (C) 2010 Bruce Cran <bruce@cran.org.uk>
16 BOOL windowsaio_debug = FALSE;
18 struct windowsaio_data {
19 struct io_u **aio_events;
20 unsigned int ioFinished;
33 struct windowsaio_data *wd;
36 static void PrintError(LPCSTR lpszFunction);
37 static int fio_windowsaio_cancel(struct thread_data *td,
39 static DWORD GetEndCount(DWORD startCount, struct timespec *t);
40 static BOOL TimedOut(DWORD startCount, DWORD endCount);
41 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
42 unsigned int max, struct timespec *t);
43 static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
44 static int fio_windowsaio_queue(struct thread_data *td,
46 static void fio_windowsaio_cleanup(struct thread_data *td);
47 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
48 static int fio_windowsaio_init(struct thread_data *td);
49 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
50 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
52 static void PrintError(LPCSTR lpszFunction)
54 // Retrieve the system error message for the last-error code
57 DWORD dw = GetLastError();
60 FORMAT_MESSAGE_ALLOCATE_BUFFER |
61 FORMAT_MESSAGE_FROM_SYSTEM |
62 FORMAT_MESSAGE_IGNORE_INSERTS,
65 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
69 log_err("%s - %s", lpszFunction, lpMsgBuf);
73 static int fio_windowsaio_cancel(struct thread_data *td,
79 bSuccess = CancelIo(io_u->file->hFile);
87 static DWORD GetEndCount(DWORD startCount, struct timespec *t)
89 DWORD endCount = startCount;
94 endCount += (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
98 static BOOL TimedOut(DWORD startCount, DWORD endCount)
100 BOOL expired = FALSE;
103 if (startCount == 0 || endCount == 0)
106 currentTime = GetTickCount();
108 if ((endCount > startCount) && currentTime >= endCount)
110 else if (currentTime < startCount && currentTime > endCount)
113 if (windowsaio_debug)
114 printf("windowsaio: timedout = %d\n", expired);
119 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
120 unsigned int max, struct timespec *t)
122 struct windowsaio_data *wd = td->io_ops->data;
123 struct flist_head *entry;
124 unsigned int dequeued = 0;
126 DWORD startCount = 0, endCount = 0;
127 BOOL timedout = FALSE;
130 if (windowsaio_debug)
131 printf("getevents (min %d, max %d)\n", min, max);
134 startCount = GetTickCount();
135 endCount = GetEndCount(startCount, t);
138 while (dequeued < min && !timedout) {
140 flist_for_each(entry, &td->io_u_busylist) {
141 io_u = flist_entry(entry, struct io_u, list);
149 wd->aio_events[r] = io_u;
152 if (windowsaio_debug)
153 printf("dequeued %d\n", dequeued);
159 if (TimedOut(startCount, endCount))
162 if (dequeued < min && !timedout)
166 if (windowsaio_debug)
167 printf("leave getevents (%d)\n", dequeued);
172 static struct io_u *fio_windowsaio_event(struct thread_data *td, int event)
174 struct windowsaio_data *wd = td->io_ops->data;
175 return wd->aio_events[event];
178 static int fio_windowsaio_queue(struct thread_data *td,
183 BOOL bSuccess = TRUE;
186 fio_ro_check(td, io_u);
188 if (windowsaio_debug)
189 printf("enqueue enter\n");
191 fov = malloc(sizeof(FIO_OVERLAPPED));
192 ZeroMemory(fov, sizeof(FIO_OVERLAPPED));
196 fov->o.Offset = io_u->offset & 0xFFFFFFFF;
197 fov->o.OffsetHigh = io_u->offset >> 32;
198 fov->o.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
201 if (fov->o.hEvent == NULL) {
202 PrintError(__func__);
206 if (io_u->ddir == DDIR_WRITE)
207 bSuccess = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &ioBytes, &fov->o);
208 else if (io_u->ddir == DDIR_READ)
209 bSuccess = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &ioBytes, &fov->o);
210 else if (io_u->ddir == DDIR_SYNC ||
211 io_u->ddir == DDIR_DATASYNC ||
212 io_u->ddir == DDIR_SYNC_FILE_RANGE)
214 FlushFileBuffers(io_u->file->hFile);
215 return FIO_Q_COMPLETED;
216 } else if (io_u->ddir == DDIR_TRIM) {
217 log_info("explicit TRIM isn't supported on Windows");
218 return FIO_Q_COMPLETED;
223 io_u->resid = io_u->xfer_buflen - fov->o.InternalHigh;
225 rc = FIO_Q_COMPLETED;
226 } else if (!bSuccess && GetLastError() == ERROR_IO_PENDING) {
229 PrintError(__func__);
230 io_u->error = GetLastError();
231 io_u->resid = io_u->xfer_buflen;
232 rc = FIO_Q_COMPLETED;
235 if (windowsaio_debug)
236 printf("enqueue - leave (offset %llu)\n", io_u->offset);
241 static void fio_windowsaio_cleanup(struct thread_data *td)
243 struct windowsaio_data *wd;
245 if (windowsaio_debug)
246 printf("windowsaio: cleanup - enter\n");
248 wd = td->io_ops->data;
251 while (wd->stopped == FALSE)
255 CloseHandle(wd->hThread);
256 free(wd->aio_events);
257 wd->aio_events = NULL;
259 td->io_ops->data = NULL;
262 if (windowsaio_debug)
263 printf("windowsaio: cleanup - leave\n");
266 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
271 struct windowsaio_data *wd;
273 struct thread_ctx *ctx;
279 ctx = (struct thread_ctx*)lpParameter;
283 if (windowsaio_debug)
284 printf("windowsaio: IoCompletionRoutine - enter\n");
286 while (ctx->wd->running) {
287 bSuccess = GetQueuedCompletionStatus(ctx->ioCP, &bytes, &ulKey, &ovl, 500);
289 if (windowsaio_debug)
290 printf("GetQueuedCompletionStatus returned %d\n", bSuccess);
293 if (GetLastError() == WAIT_TIMEOUT) {
296 PrintError(__func__);
301 fov = CONTAINING_RECORD(ovl, FIO_OVERLAPPED, o);
304 if (windowsaio_debug) {
306 printf("IoCompletionRoutine - got already completed IO\n");
308 printf("IoCompletionRoutine - completed %d IO\n", ctx->wd->ioFinished);
314 ctx->wd->ioFinished++;
316 if (ovl->Internal == ERROR_SUCCESS) {
317 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
320 io_u->resid = io_u->xfer_buflen;
325 CloseHandle(ovl->hEvent);
329 bSuccess = CloseHandle(ctx->ioCP);
331 PrintError(__func__);
333 if (windowsaio_debug)
334 printf("windowsaio: IoCompletionRoutine - leave\n");
336 ctx->wd->stopped = TRUE;
341 static int fio_windowsaio_init(struct thread_data *td)
344 struct windowsaio_data *wd;
346 if (windowsaio_debug)
347 printf("windowsaio: init\n");
349 wd = malloc(sizeof(struct windowsaio_data));
351 ZeroMemory(wd, sizeof(*wd));
352 wd->aio_events = malloc((td->o.iodepth + 1) * sizeof(struct io_u *));
353 ZeroMemory(wd->aio_events, (td->o.iodepth + 1) * sizeof(struct io_u *));
355 td->io_ops->data = wd;
359 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
363 DWORD flags = FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
364 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
365 DWORD openmode = OPEN_ALWAYS;
368 dprint(FD_FILE, "fd open %s\n", f->file_name);
370 if (windowsaio_debug)
371 printf("windowsaio: open file %s - enter\n", f->file_name);
373 if (f->filetype == FIO_TYPE_PIPE) {
374 log_err("fio: windowsaio doesn't support pipes\n");
378 if (!strcmp(f->file_name, "-")) {
379 log_err("fio: can't read/write to stdin/out\n");
384 flags |= FILE_FLAG_NO_BUFFERING;
386 flags |= FILE_FLAG_WRITE_THROUGH;
389 if (td->o.td_ddir == TD_DDIR_READ ||
390 td->o.td_ddir == TD_DDIR_WRITE ||
391 td->o.td_ddir == TD_DDIR_RANDRW)
393 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
397 flags |= FILE_FLAG_RANDOM_ACCESS;
400 if (td_read(td) || read_only)
401 access = GENERIC_READ;
403 access = (GENERIC_READ | GENERIC_WRITE);
405 if (td->o.create_on_open > 0)
406 openmode = OPEN_ALWAYS;
408 openmode = OPEN_EXISTING;
410 f->hFile = CreateFile(f->file_name, access, sharemode,
411 NULL, openmode, flags, NULL);
413 if (f->hFile == INVALID_HANDLE_VALUE) {
414 log_err("Failed to open %s\n", f->file_name);
415 PrintError(__func__);
419 /* Only set up the competion port and thread if we're not just
420 * querying the device size */
421 if (!rc && td->io_ops->data != NULL) {
422 struct windowsaio_data *wd;
423 struct thread_ctx *ctx;
424 hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
426 wd = td->io_ops->data;
430 ctx = malloc(sizeof(struct thread_ctx));
434 wd->hThread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
436 if (wd->hThread == NULL) {
437 PrintError(__func__);
442 if (windowsaio_debug)
443 printf("windowsaio: open file - leave (%d)\n", rc);
448 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
452 if (windowsaio_debug)
453 printf("windowsaio: close file\n");
455 if (f->hFile != INVALID_HANDLE_VALUE) {
456 bSuccess = CloseHandle(f->hFile);
458 PrintError(__func__);
461 f->hFile = INVALID_HANDLE_VALUE;
465 static struct ioengine_ops ioengine = {
466 .name = "windowsaio",
467 .version = FIO_IOOPS_VERSION,
468 .init = fio_windowsaio_init,
469 .queue = fio_windowsaio_queue,
470 .cancel = fio_windowsaio_cancel,
471 .getevents = fio_windowsaio_getevents,
472 .event = fio_windowsaio_event,
473 .cleanup = fio_windowsaio_cleanup,
474 .open_file = fio_windowsaio_open_file,
475 .close_file = fio_windowsaio_close_file,
476 .get_file_size = generic_get_file_size
479 static void fio_init fio_posixaio_register(void)
481 register_ioengine(&ioengine);
484 static void fio_exit fio_posixaio_unregister(void)
486 unregister_ioengine(&ioengine);