windowsaio: style fixes
[fio.git] / engines / windowsaio.c
CommitLineData
ecc314ba
BC
1/*
2 * Native Windows async IO engine
ea4500d8 3 * Copyright (C) 2011 Bruce Cran <bruce@cran.org.uk>
ecc314ba
BC
4 */
5
ecc314ba
BC
6#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <signal.h>
10#include <errno.h>
11#include <windows.h>
12
13#include "../fio.h"
14
ea4500d8
BC
15typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
16
67897036
BC
17struct fio_overlapped {
18 OVERLAPPED o;
19 struct io_u *io_u;
20 BOOL io_complete;
40c5db35 21 BOOL io_free;
67897036 22};
ecc314ba 23
9b836561 24struct windowsaio_data {
67897036 25 struct fio_overlapped *ovls;
9b836561 26 struct io_u **aio_events;
67897036 27 HANDLE iothread;
9b836561 28 HANDLE iocomplete_event;
ea4500d8 29 CANCELIOEX pCancelIoEx;
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
ecc314ba
BC
38static int fio_windowsaio_cancel(struct thread_data *td,
39 struct io_u *io_u);
67897036 40static BOOL timeout_expired(DWORD start_count, DWORD end_count);
ecc314ba
BC
41static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
42 unsigned int max, struct timespec *t);
43static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
44static int fio_windowsaio_queue(struct thread_data *td,
45 struct io_u *io_u);
46static void fio_windowsaio_cleanup(struct thread_data *td);
47static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
48static int fio_windowsaio_init(struct thread_data *td);
49static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
50static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
51
9b836561
BC
52int sync_file_range(int fd, off64_t offset, off64_t nbytes,
53 unsigned int flags)
54{
55 errno = ENOSYS;
56 return -1;
57}
58
ecc314ba
BC
59static int fio_windowsaio_init(struct thread_data *td)
60{
ecc314ba 61 struct windowsaio_data *wd;
ea4500d8 62 HANDLE hKernel32Dll;
9b836561 63 int rc = 0;
67897036 64 int i;
ecc314ba 65
ecc314ba 66 wd = malloc(sizeof(struct windowsaio_data));
9b836561
BC
67 if (wd != NULL)
68 ZeroMemory(wd, sizeof(struct windowsaio_data));
69 else
70 rc = 1;
ecc314ba 71
9b836561
BC
72 if (!rc) {
73 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
74 if (wd->aio_events == NULL)
75 rc = 1;
e4db9fec
BC
76 }
77
9b836561 78 if (!rc) {
67897036
BC
79 wd->ovls = malloc(td->o.iodepth * sizeof(struct fio_overlapped));
80 if (wd->ovls == NULL)
9b836561 81 rc = 1;
e4db9fec
BC
82 }
83
9b836561 84 if (!rc) {
67897036
BC
85 for (i = 0; i < td->o.iodepth; i++) {
86 wd->ovls[i].io_free = TRUE;
87 wd->ovls[i].io_complete = FALSE;
88
89 wd->ovls[i].o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
90 if (wd->ovls[i].o.hEvent == NULL) {
91 rc = 1;
92 break;
93 }
94 }
9b836561 95 }
e4db9fec 96
9b836561
BC
97 if (!rc) {
98 /* Create an auto-reset event */
99 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
100 if (wd->iocomplete_event == NULL)
101 rc = 1;
102 }
103
9b836561 104 if (rc) {
9b836561
BC
105 if (wd != NULL) {
106 if (wd->ovls != NULL)
107 free(wd->ovls);
9b836561
BC
108 if (wd->aio_events != NULL)
109 free(wd->aio_events);
110
111 free(wd);
112 }
113 }
ecc314ba 114
ea4500d8
BC
115 hKernel32Dll = GetModuleHandle("kernel32.dll");
116 wd->pCancelIoEx = GetProcAddress(hKernel32Dll, "CancelIoEx");
117
ecc314ba 118 td->io_ops->data = wd;
4e79098f 119 return rc;
ecc314ba
BC
120}
121
67897036
BC
122static void fio_windowsaio_cleanup(struct thread_data *td)
123{
124 int i;
125 struct windowsaio_data *wd;
126
127 wd = td->io_ops->data;
128
129 if (wd != NULL) {
40c5db35
JA
130 wd->iothread_running = FALSE;
131 WaitForSingleObject(wd->iothread, INFINITE);
67897036
BC
132
133 CloseHandle(wd->iothread);
134 CloseHandle(wd->iocomplete_event);
135
40c5db35 136 for (i = 0; i < td->o.iodepth; i++)
67897036 137 CloseHandle(wd->ovls[i].o.hEvent);
67897036
BC
138
139 free(wd->aio_events);
140 free(wd->ovls);
141 free(wd);
142
143 td->io_ops->data = NULL;
144 }
145}
146
147
ecc314ba
BC
148static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
149{
150 int rc = 0;
151 HANDLE hFile;
4e79098f 152 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
ecc314ba
BC
153 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
154 DWORD openmode = OPEN_ALWAYS;
155 DWORD access;
156
157 dprint(FD_FILE, "fd open %s\n", f->file_name);
158
ecc314ba
BC
159 if (f->filetype == FIO_TYPE_PIPE) {
160 log_err("fio: windowsaio doesn't support pipes\n");
161 return 1;
162 }
163
164 if (!strcmp(f->file_name, "-")) {
165 log_err("fio: can't read/write to stdin/out\n");
166 return 1;
167 }
168
169 if (td->o.odirect)
170 flags |= FILE_FLAG_NO_BUFFERING;
171 if (td->o.sync_io)
172 flags |= FILE_FLAG_WRITE_THROUGH;
173
ecc314ba 174 if (td->o.td_ddir == TD_DDIR_READ ||
ea4500d8 175 td->o.td_ddir == TD_DDIR_WRITE)
ecc314ba 176 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
ecc314ba 177 else
ecc314ba 178 flags |= FILE_FLAG_RANDOM_ACCESS;
ecc314ba 179
ea4500d8 180 if (!td_write(td) || read_only)
ecc314ba
BC
181 access = GENERIC_READ;
182 else
183 access = (GENERIC_READ | GENERIC_WRITE);
184
185 if (td->o.create_on_open > 0)
186 openmode = OPEN_ALWAYS;
187 else
188 openmode = OPEN_EXISTING;
189
190 f->hFile = CreateFile(f->file_name, access, sharemode,
191 NULL, openmode, flags, NULL);
192
4e79098f 193 if (f->hFile == INVALID_HANDLE_VALUE)
ecc314ba 194 rc = 1;
ecc314ba
BC
195
196 /* Only set up the competion port and thread if we're not just
197 * querying the device size */
40c5db35 198 if (!rc && td->io_ops->data != NULL) {
ecc314ba 199 struct thread_ctx *ctx;
40c5db35 200 struct windowsaio_data *wd;
ecc314ba 201
40c5db35 202 hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
ea4500d8 203
40c5db35 204 wd = td->io_ops->data;
9b836561 205 wd->iothread_running = TRUE;
ecc314ba 206
9b836561
BC
207 if (!rc) {
208 ctx = malloc(sizeof(struct thread_ctx));
209 ctx->iocp = hFile;
210 ctx->wd = wd;
211
212 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
213 }
ecc314ba 214
4e79098f 215 if (rc || wd->iothread == NULL)
ecc314ba 216 rc = 1;
ecc314ba
BC
217 }
218
ecc314ba
BC
219 return rc;
220}
221
222static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
223{
4e79098f 224 int rc = 0;
1e42cc3d 225
9b836561
BC
226 dprint(FD_FILE, "fd close %s\n", f->file_name);
227
ecc314ba 228 if (f->hFile != INVALID_HANDLE_VALUE) {
9b836561 229 if (!CloseHandle(f->hFile))
4e79098f 230 rc = 1;
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,
259 unsigned int max, struct timespec *t)
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;
67897036
BC
284 fov->io_free = TRUE;
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
BC
294 status = WaitForSingleObject(wd->iocomplete_event, mswait);
295 if (status != WAIT_OBJECT_0 && dequeued > 0)
296 break;
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{
1e42cc3d 308 LPOVERLAPPED lpOvl = NULL;
67897036
BC
309 struct windowsaio_data *wd;
310 DWORD iobytes;
311 BOOL success;
312 int index;
313 int rc = FIO_Q_COMPLETED;
314
315 fio_ro_check(td, io_u);
316
317 wd = td->io_ops->data;
318
1e42cc3d
BC
319 for (index = 0; index < td->o.iodepth; index++) {
320 if (wd->ovls[index].io_free) {
321 wd->ovls[index].io_free = FALSE;
322 ResetEvent(wd->ovls[index].o.hEvent);
323 break;
324 }
325 }
67897036 326
1e42cc3d 327 assert(index < td->o.iodepth);
67897036 328
1e42cc3d
BC
329 lpOvl = &wd->ovls[index].o;
330 wd->ovls[index].io_u = io_u;
4e79098f
BC
331 lpOvl->Internal = STATUS_PENDING;
332 lpOvl->InternalHigh = 0;
333 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
334 lpOvl->OffsetHigh = io_u->offset >> 32;
1e42cc3d 335 io_u->engine_data = &wd->ovls[index];
67897036
BC
336
337 switch (io_u->ddir) {
40c5db35 338 case DDIR_WRITE:
67897036
BC
339 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
340 break;
341 case DDIR_READ:
342 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
343 break;
344 case DDIR_SYNC:
345 case DDIR_DATASYNC:
346 case DDIR_SYNC_FILE_RANGE:
347 success = FlushFileBuffers(io_u->file->hFile);
348 if (!success)
349 io_u->error = GetLastError();
350
351 return FIO_Q_COMPLETED;
352 break;
353 case DDIR_TRIM:
354 log_err("manual TRIM isn't supported on Windows");
355 io_u->error = 1;
356 io_u->resid = io_u->xfer_buflen;
357 return FIO_Q_COMPLETED;
358 break;
359 default:
360 assert(0);
361 }
362
40c5db35 363 if (success || GetLastError() == ERROR_IO_PENDING)
67897036 364 rc = FIO_Q_QUEUED;
40c5db35 365 else {
67897036
BC
366 io_u->error = GetLastError();
367 io_u->resid = io_u->xfer_buflen;
368 }
369
370 return rc;
371}
372
373/* Runs as a thread and waits for queued IO to complete */
374static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
375{
376 OVERLAPPED *ovl;
377 struct fio_overlapped *fov;
378 struct io_u *io_u;
379 struct windowsaio_data *wd;
380 struct thread_ctx *ctx;
381 ULONG_PTR ulKey = 0;
382 DWORD bytes;
383
384 ctx = (struct thread_ctx*)lpParameter;
385 wd = ctx->wd;
386
387 do {
388 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250))
389 continue;
390
391 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
392 io_u = fov->io_u;
393
394 if (ovl->Internal == ERROR_SUCCESS) {
395 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
396 io_u->error = 0;
397 } else {
398 io_u->resid = io_u->xfer_buflen;
399 io_u->error = ovl->Internal;
400 }
401
40c5db35 402 fov->io_complete = TRUE;
67897036
BC
403 SetEvent(wd->iocomplete_event);
404 } while (ctx->wd->iothread_running);
405
406 CloseHandle(ctx->iocp);
407 free(ctx);
408 return 0;
409}
410
411static int fio_windowsaio_cancel(struct thread_data *td,
412 struct io_u *io_u)
413{
414 int rc = 0;
415
416 struct windowsaio_data *wd = td->io_ops->data;
417
418 /* If we're running on Vista or newer, we can cancel individual IO requests */
419 if (wd->pCancelIoEx != NULL) {
420 struct fio_overlapped *ovl = io_u->engine_data;
40c5db35 421
67897036
BC
422 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
423 rc = 1;
424 } else
425 rc = 1;
426
427 return rc;
428}
429
ecc314ba
BC
430static struct ioengine_ops ioengine = {
431 .name = "windowsaio",
432 .version = FIO_IOOPS_VERSION,
433 .init = fio_windowsaio_init,
434 .queue = fio_windowsaio_queue,
435 .cancel = fio_windowsaio_cancel,
436 .getevents = fio_windowsaio_getevents,
437 .event = fio_windowsaio_event,
438 .cleanup = fio_windowsaio_cleanup,
439 .open_file = fio_windowsaio_open_file,
440 .close_file = fio_windowsaio_close_file,
441 .get_file_size = generic_get_file_size
442};
443
444static void fio_init fio_posixaio_register(void)
445{
446 register_ioengine(&ioengine);
447}
448
449static void fio_exit fio_posixaio_unregister(void)
450{
451 unregister_ioengine(&ioengine);
452}