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