windowsaio: fix file offset bug
[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;
21 BOOL io_free;
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) {
130 wd->iothread_running = FALSE;
131 WaitForSingleObject(wd->iothread, INFINITE);
132
133 CloseHandle(wd->iothread);
134 CloseHandle(wd->iocomplete_event);
135
136 for (i = 0; i < td->o.iodepth; i++) {
137 CloseHandle(wd->ovls[i].o.hEvent);
138 }
139
140 free(wd->aio_events);
141 free(wd->ovls);
142 free(wd);
143
144 td->io_ops->data = NULL;
145 }
146}
147
148
ecc314ba
BC
149static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
150{
151 int rc = 0;
152 HANDLE hFile;
4e79098f 153 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
ecc314ba
BC
154 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
155 DWORD openmode = OPEN_ALWAYS;
156 DWORD access;
157
158 dprint(FD_FILE, "fd open %s\n", f->file_name);
159
ecc314ba
BC
160 if (f->filetype == FIO_TYPE_PIPE) {
161 log_err("fio: windowsaio doesn't support pipes\n");
162 return 1;
163 }
164
165 if (!strcmp(f->file_name, "-")) {
166 log_err("fio: can't read/write to stdin/out\n");
167 return 1;
168 }
169
170 if (td->o.odirect)
171 flags |= FILE_FLAG_NO_BUFFERING;
172 if (td->o.sync_io)
173 flags |= FILE_FLAG_WRITE_THROUGH;
174
ecc314ba 175 if (td->o.td_ddir == TD_DDIR_READ ||
ea4500d8 176 td->o.td_ddir == TD_DDIR_WRITE)
ecc314ba 177 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
ecc314ba 178 else
ecc314ba 179 flags |= FILE_FLAG_RANDOM_ACCESS;
ecc314ba 180
ea4500d8 181 if (!td_write(td) || read_only)
ecc314ba
BC
182 access = GENERIC_READ;
183 else
184 access = (GENERIC_READ | GENERIC_WRITE);
185
186 if (td->o.create_on_open > 0)
187 openmode = OPEN_ALWAYS;
188 else
189 openmode = OPEN_EXISTING;
190
191 f->hFile = CreateFile(f->file_name, access, sharemode,
192 NULL, openmode, flags, NULL);
193
4e79098f 194 if (f->hFile == INVALID_HANDLE_VALUE)
ecc314ba 195 rc = 1;
ecc314ba
BC
196
197 /* Only set up the competion port and thread if we're not just
198 * querying the device size */
4e79098f 199 if (!rc && td->io_ops->data != NULL) {
ecc314ba 200 struct thread_ctx *ctx;
ea4500d8 201 struct windowsaio_data *wd;
ecc314ba
BC
202 hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
203
ea4500d8
BC
204 wd = td->io_ops->data;
205
9b836561 206 wd->iothread_running = TRUE;
ecc314ba 207
9b836561
BC
208 if (!rc) {
209 ctx = malloc(sizeof(struct thread_ctx));
210 ctx->iocp = hFile;
211 ctx->wd = wd;
212
213 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
214 }
ecc314ba 215
4e79098f 216 if (rc || wd->iothread == NULL)
ecc314ba 217 rc = 1;
ecc314ba
BC
218 }
219
ecc314ba
BC
220 return rc;
221}
222
223static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
224{
4e79098f 225 int rc = 0;
1e42cc3d 226
9b836561
BC
227 dprint(FD_FILE, "fd close %s\n", f->file_name);
228
ecc314ba 229 if (f->hFile != INVALID_HANDLE_VALUE) {
9b836561 230 if (!CloseHandle(f->hFile))
4e79098f 231 rc = 1;
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,
260 unsigned int max, struct timespec *t)
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) {
284 fov->io_complete = FALSE;
285 fov->io_free = TRUE;
286 wd->aio_events[dequeued] = io_u;
287 dequeued++;
288 }
289
290 if (dequeued >= min)
291 break;
292 }
293
294 if (dequeued < min) {
295 status = WaitForSingleObject(wd->iocomplete_event, mswait);
296 if (status != WAIT_OBJECT_0 && dequeued > 0)
297 break;
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
307static int fio_windowsaio_queue(struct thread_data *td,
308 struct io_u *io_u)
309{
1e42cc3d 310 LPOVERLAPPED lpOvl = NULL;
67897036
BC
311 struct windowsaio_data *wd;
312 DWORD iobytes;
313 BOOL success;
314 int index;
315 int rc = FIO_Q_COMPLETED;
316
317 fio_ro_check(td, io_u);
318
319 wd = td->io_ops->data;
320
1e42cc3d
BC
321 for (index = 0; index < td->o.iodepth; index++) {
322 if (wd->ovls[index].io_free) {
323 wd->ovls[index].io_free = FALSE;
324 ResetEvent(wd->ovls[index].o.hEvent);
325 break;
326 }
327 }
67897036 328
1e42cc3d 329 assert(index < td->o.iodepth);
67897036 330
1e42cc3d
BC
331 lpOvl = &wd->ovls[index].o;
332 wd->ovls[index].io_u = io_u;
4e79098f
BC
333 lpOvl->Internal = STATUS_PENDING;
334 lpOvl->InternalHigh = 0;
335 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
336 lpOvl->OffsetHigh = io_u->offset >> 32;
1e42cc3d 337 io_u->engine_data = &wd->ovls[index];
67897036
BC
338
339 switch (io_u->ddir) {
340 case DDIR_WRITE:
341 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
342 break;
343 case DDIR_READ:
344 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
345 break;
346 case DDIR_SYNC:
347 case DDIR_DATASYNC:
348 case DDIR_SYNC_FILE_RANGE:
349 success = FlushFileBuffers(io_u->file->hFile);
350 if (!success)
351 io_u->error = GetLastError();
352
353 return FIO_Q_COMPLETED;
354 break;
355 case DDIR_TRIM:
356 log_err("manual TRIM isn't supported on Windows");
357 io_u->error = 1;
358 io_u->resid = io_u->xfer_buflen;
359 return FIO_Q_COMPLETED;
360 break;
361 default:
362 assert(0);
363 }
364
4e79098f 365 if (success || GetLastError() == ERROR_IO_PENDING) {
67897036 366 rc = FIO_Q_QUEUED;
67897036 367 } else {
67897036
BC
368 io_u->error = GetLastError();
369 io_u->resid = io_u->xfer_buflen;
370 }
371
372 return rc;
373}
374
375/* Runs as a thread and waits for queued IO to complete */
376static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
377{
378 OVERLAPPED *ovl;
379 struct fio_overlapped *fov;
380 struct io_u *io_u;
381 struct windowsaio_data *wd;
382 struct thread_ctx *ctx;
383 ULONG_PTR ulKey = 0;
384 DWORD bytes;
385
386 ctx = (struct thread_ctx*)lpParameter;
387 wd = ctx->wd;
388
389 do {
390 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250))
391 continue;
392
393 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
394 io_u = fov->io_u;
395
396 if (ovl->Internal == ERROR_SUCCESS) {
397 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
398 io_u->error = 0;
399 } else {
400 io_u->resid = io_u->xfer_buflen;
401 io_u->error = ovl->Internal;
402 }
403
404 fov->io_complete = TRUE;
405 SetEvent(wd->iocomplete_event);
406 } while (ctx->wd->iothread_running);
407
408 CloseHandle(ctx->iocp);
409 free(ctx);
410 return 0;
411}
412
413static int fio_windowsaio_cancel(struct thread_data *td,
414 struct io_u *io_u)
415{
416 int rc = 0;
417
418 struct windowsaio_data *wd = td->io_ops->data;
419
420 /* If we're running on Vista or newer, we can cancel individual IO requests */
421 if (wd->pCancelIoEx != NULL) {
422 struct fio_overlapped *ovl = io_u->engine_data;
423 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
424 rc = 1;
425 } else
426 rc = 1;
427
428 return rc;
429}
430
ecc314ba
BC
431static struct ioengine_ops ioengine = {
432 .name = "windowsaio",
433 .version = FIO_IOOPS_VERSION,
434 .init = fio_windowsaio_init,
435 .queue = fio_windowsaio_queue,
436 .cancel = fio_windowsaio_cancel,
437 .getevents = fio_windowsaio_getevents,
438 .event = fio_windowsaio_event,
439 .cleanup = fio_windowsaio_cleanup,
440 .open_file = fio_windowsaio_open_file,
441 .close_file = fio_windowsaio_close_file,
442 .get_file_size = generic_get_file_size
443};
444
445static void fio_init fio_posixaio_register(void)
446{
447 register_ioengine(&ioengine);
448}
449
450static void fio_exit fio_posixaio_unregister(void)
451{
452 unregister_ioengine(&ioengine);
453}