stat: fix wrong type used for 32-bit compiles
[fio.git] / engines / windowsaio.c
... / ...
CommitLineData
1/*
2 * windowsaio engine
3 *
4 * IO engine using Windows IO Completion Ports.
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <signal.h>
11#include <errno.h>
12#include <windows.h>
13
14#include "../fio.h"
15
16typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
17
18int geterrno_from_win_error (DWORD code, int deferrno);
19
20struct fio_overlapped {
21 OVERLAPPED o;
22 struct io_u *io_u;
23 BOOL io_complete;
24};
25
26struct windowsaio_data {
27 struct io_u **aio_events;
28 HANDLE iocp;
29 HANDLE iothread;
30 HANDLE iocomplete_event;
31 CANCELIOEX pCancelIoEx;
32 BOOL iothread_running;
33};
34
35struct thread_ctx {
36 HANDLE iocp;
37 struct windowsaio_data *wd;
38};
39
40static int fio_windowsaio_cancel(struct thread_data *td,
41 struct io_u *io_u);
42static BOOL timeout_expired(DWORD start_count, DWORD end_count);
43static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
44 unsigned int max, struct timespec *t);
45static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
46static int fio_windowsaio_queue(struct thread_data *td,
47 struct io_u *io_u);
48static void fio_windowsaio_cleanup(struct thread_data *td);
49static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
50static int fio_windowsaio_init(struct thread_data *td);
51static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
52static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
53
54static int fio_windowsaio_init(struct thread_data *td)
55{
56 struct windowsaio_data *wd;
57 HANDLE hKernel32Dll;
58 int rc = 0;
59
60 wd = calloc(1, sizeof(struct windowsaio_data));
61 if (wd == NULL) {
62 log_err("windowsaio: failed to allocate memory for engine data\n");
63 rc = 1;
64 }
65
66 if (!rc) {
67 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
68 if (wd->aio_events == NULL) {
69 log_err("windowsaio: failed to allocate memory for aio events list\n");
70 rc = 1;
71 }
72 }
73
74 if (!rc) {
75 /* Create an auto-reset event */
76 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
77 if (wd->iocomplete_event == NULL) {
78 log_err("windowsaio: failed to create io complete event handle\n");
79 rc = 1;
80 }
81 }
82
83 if (rc) {
84 if (wd != NULL) {
85 if (wd->aio_events != NULL)
86 free(wd->aio_events);
87
88 free(wd);
89 }
90 }
91
92 hKernel32Dll = GetModuleHandle("kernel32.dll");
93 wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
94 td->io_ops->data = wd;
95
96 if (!rc) {
97 struct thread_ctx *ctx;
98 struct windowsaio_data *wd;
99 HANDLE hFile;
100
101 hFile = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
102 if (hFile == INVALID_HANDLE_VALUE) {
103 log_err("windowsaio: failed to create io completion port\n");
104 rc = 1;
105 }
106
107 wd = td->io_ops->data;
108 wd->iothread_running = TRUE;
109 wd->iocp = hFile;
110
111 if (!rc)
112 ctx = malloc(sizeof(struct thread_ctx));
113
114 if (!rc && ctx == NULL)
115 {
116 log_err("windowsaio: failed to allocate memory for thread context structure\n");
117 CloseHandle(hFile);
118 rc = 1;
119 }
120
121 if (!rc)
122 {
123 ctx->iocp = hFile;
124 ctx->wd = wd;
125 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
126 if (wd->iothread == NULL)
127 log_err("windowsaio: failed to create io completion thread\n");
128 }
129
130 if (rc || wd->iothread == NULL)
131 rc = 1;
132 }
133
134 return rc;
135}
136
137static void fio_windowsaio_cleanup(struct thread_data *td)
138{
139 struct windowsaio_data *wd;
140
141 wd = td->io_ops->data;
142
143 if (wd != NULL) {
144 wd->iothread_running = FALSE;
145 WaitForSingleObject(wd->iothread, INFINITE);
146
147 CloseHandle(wd->iothread);
148 CloseHandle(wd->iocomplete_event);
149
150 free(wd->aio_events);
151 free(wd);
152
153 td->io_ops->data = NULL;
154 }
155}
156
157
158static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
159{
160 int rc = 0;
161 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
162 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
163 DWORD openmode = OPEN_ALWAYS;
164 DWORD access;
165
166 dprint(FD_FILE, "fd open %s\n", f->file_name);
167
168 if (f->filetype == FIO_TYPE_PIPE) {
169 log_err("windowsaio: pipes are not supported\n");
170 return 1;
171 }
172
173 if (!strcmp(f->file_name, "-")) {
174 log_err("windowsaio: can't read/write to stdin/out\n");
175 return 1;
176 }
177
178 if (td->o.odirect)
179 flags |= FILE_FLAG_NO_BUFFERING;
180 if (td->o.sync_io)
181 flags |= FILE_FLAG_WRITE_THROUGH;
182
183 /*
184 * Inform Windows whether we're going to be doing sequential or
185 * random io so it can tune the Cache Manager
186 */
187 if (td->o.td_ddir == TD_DDIR_READ ||
188 td->o.td_ddir == TD_DDIR_WRITE)
189 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
190 else
191 flags |= FILE_FLAG_RANDOM_ACCESS;
192
193 if (!td_write(td) || read_only)
194 access = GENERIC_READ;
195 else
196 access = (GENERIC_READ | GENERIC_WRITE);
197
198 if (td->o.create_on_open)
199 openmode = OPEN_ALWAYS;
200 else
201 openmode = OPEN_EXISTING;
202
203 f->hFile = CreateFile(f->file_name, access, sharemode,
204 NULL, openmode, flags, NULL);
205
206 if (f->hFile == INVALID_HANDLE_VALUE) {
207 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
208 rc = 1;
209 }
210
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;
215
216 wd = td->io_ops->data;
217
218 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
219 log_err("windowsaio: failed to create io completion port\n");
220 rc = 1;
221 }
222 }
223
224 return rc;
225}
226
227static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
228{
229 int rc = 0;
230
231 dprint(FD_FILE, "fd close %s\n", f->file_name);
232
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);
236 rc = 1;
237 }
238 }
239
240 f->hFile = INVALID_HANDLE_VALUE;
241 return rc;
242}
243
244static BOOL timeout_expired(DWORD start_count, DWORD end_count)
245{
246 BOOL expired = FALSE;
247 DWORD current_time;
248
249 current_time = GetTickCount();
250
251 if ((end_count > start_count) && current_time >= end_count)
252 expired = TRUE;
253 else if (current_time < start_count && current_time > end_count)
254 expired = TRUE;
255
256 return expired;
257}
258
259static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
260{
261 struct windowsaio_data *wd = td->io_ops->data;
262 return wd->aio_events[event];
263}
264
265static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
266 unsigned int max, struct timespec *t)
267{
268 struct windowsaio_data *wd = td->io_ops->data;
269 struct flist_head *entry;
270 unsigned int dequeued = 0;
271 struct io_u *io_u;
272 struct fio_overlapped *fov;
273 DWORD start_count = 0;
274 DWORD end_count = 0;
275 DWORD status;
276 DWORD mswait = 250;
277
278 if (t != NULL) {
279 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
280 start_count = GetTickCount();
281 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
282 }
283
284 do {
285 flist_for_each(entry, &td->io_u_busylist) {
286 io_u = flist_entry(entry, struct io_u, list);
287 fov = (struct fio_overlapped*)io_u->engine_data;
288
289 if (fov->io_complete) {
290 fov->io_complete = FALSE;
291 ResetEvent(fov->o.hEvent);
292 wd->aio_events[dequeued] = io_u;
293 dequeued++;
294 }
295
296 if (dequeued >= min)
297 break;
298 }
299
300 if (dequeued < min) {
301 status = WaitForSingleObject(wd->iocomplete_event, mswait);
302 if (status != WAIT_OBJECT_0 && dequeued >= min)
303 break;
304 }
305
306 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
307 break;
308 } while (1);
309
310 return dequeued;
311}
312
313static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
314{
315 struct fio_overlapped *o = io_u->engine_data;
316 LPOVERLAPPED lpOvl = &o->o;
317 DWORD iobytes;
318 BOOL success = FALSE;
319 int rc = FIO_Q_COMPLETED;
320
321 fio_ro_check(td, io_u);
322
323 lpOvl->Internal = STATUS_PENDING;
324 lpOvl->InternalHigh = 0;
325 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
326 lpOvl->OffsetHigh = io_u->offset >> 32;
327
328 switch (io_u->ddir) {
329 case DDIR_WRITE:
330 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
331 break;
332 case DDIR_READ:
333 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
334 break;
335 case DDIR_SYNC:
336 case DDIR_DATASYNC:
337 case DDIR_SYNC_FILE_RANGE:
338 success = FlushFileBuffers(io_u->file->hFile);
339 if (!success) {
340 log_err("windowsaio: failed to flush file buffers\n");
341 io_u->error = win_to_posix_error(GetLastError());
342 }
343
344 return FIO_Q_COMPLETED;
345 break;
346 case DDIR_TRIM:
347 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
348 io_u->error = 1;
349 io_u->resid = io_u->xfer_buflen;
350 return FIO_Q_COMPLETED;
351 break;
352 default:
353 assert(0);
354 break;
355 }
356
357 if (success || GetLastError() == ERROR_IO_PENDING)
358 rc = FIO_Q_QUEUED;
359 else {
360 io_u->error = win_to_posix_error(GetLastError());
361 io_u->resid = io_u->xfer_buflen;
362 }
363
364 return rc;
365}
366
367/* Runs as a thread and waits for queued IO to complete */
368static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
369{
370 OVERLAPPED *ovl;
371 struct fio_overlapped *fov;
372 struct io_u *io_u;
373 struct windowsaio_data *wd;
374 struct thread_ctx *ctx;
375 ULONG_PTR ulKey = 0;
376 DWORD bytes;
377
378 ctx = (struct thread_ctx*)lpParameter;
379 wd = ctx->wd;
380
381 do {
382 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
383 continue;
384
385 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
386 io_u = fov->io_u;
387
388 if (ovl->Internal == ERROR_SUCCESS) {
389 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
390 io_u->error = 0;
391 } else {
392 io_u->resid = io_u->xfer_buflen;
393 io_u->error = win_to_posix_error(GetLastError());
394 }
395
396 fov->io_complete = TRUE;
397 SetEvent(wd->iocomplete_event);
398 } while (ctx->wd->iothread_running);
399
400 CloseHandle(ctx->iocp);
401 free(ctx);
402 return 0;
403}
404
405static int fio_windowsaio_cancel(struct thread_data *td,
406 struct io_u *io_u)
407{
408 int rc = 0;
409
410 struct windowsaio_data *wd = td->io_ops->data;
411
412 /* If we're running on Vista or newer, we can cancel individual IO requests */
413 if (wd->pCancelIoEx != NULL) {
414 struct fio_overlapped *ovl = io_u->engine_data;
415
416 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o)) {
417 log_err("windowsaio: failed to cancel io\n");
418 rc = 1;
419 }
420 } else
421 rc = 1;
422
423 return rc;
424}
425
426static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
427{
428 struct fio_overlapped *o = io_u->engine_data;
429
430 if (o) {
431 CloseHandle(o->o.hEvent);
432 io_u->engine_data = NULL;
433 free(o);
434 }
435}
436
437static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
438{
439 struct fio_overlapped *o;
440
441 o = malloc(sizeof(*o));
442 o->io_complete = FALSE;
443 o->io_u = io_u;
444 o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
445 if (o->o.hEvent == NULL) {
446 log_err("windowsaio: failed to create event handle\n");
447 free(o);
448 return 1;
449 }
450
451 io_u->engine_data = o;
452 return 0;
453}
454
455static struct ioengine_ops ioengine = {
456 .name = "windowsaio",
457 .version = FIO_IOOPS_VERSION,
458 .init = fio_windowsaio_init,
459 .queue = fio_windowsaio_queue,
460 .cancel = fio_windowsaio_cancel,
461 .getevents = fio_windowsaio_getevents,
462 .event = fio_windowsaio_event,
463 .cleanup = fio_windowsaio_cleanup,
464 .open_file = fio_windowsaio_open_file,
465 .close_file = fio_windowsaio_close_file,
466 .get_file_size = generic_get_file_size,
467 .io_u_init = fio_windowsaio_io_u_init,
468 .io_u_free = fio_windowsaio_io_u_free,
469};
470
471static void fio_init fio_windowsaio_register(void)
472{
473 register_ioengine(&ioengine);
474}
475
476static void fio_exit fio_windowsaio_unregister(void)
477{
478 unregister_ioengine(&ioengine);
479}