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