Improve Windows windowsaio engine performance
[fio.git] / engines / windowsaio.c
1 /*
2  * Native Windows async IO engine
3  * Copyright (C) 2011 Bruce Cran <bruce@cran.org.uk>
4  */
5
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
15 typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
16
17 struct fio_overlapped {
18         OVERLAPPED o;
19         struct io_u *io_u;
20         BOOL io_complete;
21     BOOL io_free;
22 };
23
24 struct windowsaio_data {
25         struct fio_overlapped *ovls;
26         struct io_u **aio_events;
27         HANDLE iothread;
28         HANDLE iocomplete_event;
29         CANCELIOEX pCancelIoEx;
30         BOOL iothread_running;
31 };
32
33 struct thread_ctx {
34         HANDLE iocp;
35         struct windowsaio_data *wd;
36 };
37
38 static int fio_windowsaio_cancel(struct thread_data *td,
39                                struct io_u *io_u);
40 static BOOL timeout_expired(DWORD start_count, DWORD end_count);
41 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
42                                     unsigned int max, struct timespec *t);
43 static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
44 static int fio_windowsaio_queue(struct thread_data *td,
45                               struct io_u *io_u);
46 static void fio_windowsaio_cleanup(struct thread_data *td);
47 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
48 static int fio_windowsaio_init(struct thread_data *td);
49 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
50 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
51
52 int sync_file_range(int fd, off64_t offset, off64_t nbytes,
53                            unsigned int flags)
54 {
55         errno = ENOSYS;
56         return -1;
57 }
58
59 static int fio_windowsaio_init(struct thread_data *td)
60 {
61         struct windowsaio_data *wd;
62         HANDLE hKernel32Dll;
63         int rc = 0;
64         int i;
65
66         wd = malloc(sizeof(struct windowsaio_data));
67         if (wd != NULL)
68                 ZeroMemory(wd, sizeof(struct windowsaio_data));
69         else
70                 rc = 1;
71
72         if (!rc) {
73                 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
74                 if (wd->aio_events == NULL)
75                         rc = 1;
76         }
77
78         if (!rc) {
79                 wd->ovls = malloc(td->o.iodepth * sizeof(struct fio_overlapped));
80                 if (wd->ovls == NULL)
81                         rc = 1;
82         }
83
84         if (!rc) {
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             }
95         }
96
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
104         if (rc) {
105                 if (wd != NULL) {
106                         if (wd->ovls != NULL)
107                                 free(wd->ovls);
108                         if (wd->aio_events != NULL)
109                                 free(wd->aio_events);
110
111                         free(wd);
112                 }
113         }
114
115         hKernel32Dll = GetModuleHandle("kernel32.dll");
116         wd->pCancelIoEx = GetProcAddress(hKernel32Dll, "CancelIoEx");
117
118         td->io_ops->data = wd;
119         return rc;
120 }
121
122 static 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
149 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
150 {
151         int rc = 0;
152         HANDLE hFile;
153         DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
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
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
175         if (td->o.td_ddir == TD_DDIR_READ  ||
176                 td->o.td_ddir == TD_DDIR_WRITE)
177                 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
178         else
179                 flags |= FILE_FLAG_RANDOM_ACCESS;
180
181         if (!td_write(td) || read_only)
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
194         if (f->hFile == INVALID_HANDLE_VALUE)
195                 rc = 1;
196
197         /* Only set up the competion port and thread if we're not just
198          * querying the device size */
199     if (!rc && td->io_ops->data != NULL) {
200                 struct thread_ctx *ctx;
201         struct windowsaio_data *wd;
202                 hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
203
204         wd = td->io_ops->data;
205
206                 wd->iothread_running = TRUE;
207
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                 }
215
216                 if (rc || wd->iothread == NULL)
217                         rc = 1;
218         }
219
220         return rc;
221 }
222
223 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
224 {
225         int rc = 0;
226         
227         dprint(FD_FILE, "fd close %s\n", f->file_name);
228
229         if (f->hFile != INVALID_HANDLE_VALUE) {
230                 if (!CloseHandle(f->hFile))
231                         rc = 1;
232         }
233
234         f->hFile = INVALID_HANDLE_VALUE;
235         return rc;
236 }
237
238 static 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
253 static 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
259 static 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
307 static int fio_windowsaio_queue(struct thread_data *td,
308                               struct io_u *io_u)
309 {
310     LPOVERLAPPED lpOvl = NULL;
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
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     }
328
329     assert(index < td->o.iodepth);
330
331     lpOvl = &wd->ovls[index].o;
332     wd->ovls[index].io_u = io_u;
333         lpOvl->Internal = STATUS_PENDING;
334         lpOvl->InternalHigh = 0;
335         lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
336         lpOvl->OffsetHigh = io_u->offset >> 32;
337         lpOvl->Pointer = NULL;
338     io_u->engine_data = &wd->ovls[index];
339
340         switch (io_u->ddir) {
341     case DDIR_WRITE:
342                 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
343                 break;
344         case DDIR_READ:
345                 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
346                 break;
347         case DDIR_SYNC:
348         case DDIR_DATASYNC:
349         case DDIR_SYNC_FILE_RANGE:
350                 success = FlushFileBuffers(io_u->file->hFile);
351                 if (!success)
352                     io_u->error = GetLastError();
353
354                 return FIO_Q_COMPLETED;
355                 break;
356         case DDIR_TRIM:
357                 log_err("manual TRIM isn't supported on Windows");
358                 io_u->error = 1;
359                 io_u->resid = io_u->xfer_buflen;
360                 return FIO_Q_COMPLETED;
361                 break;
362         default:
363                 assert(0);
364         }
365
366     if (success || GetLastError() == ERROR_IO_PENDING) {
367                 rc = FIO_Q_QUEUED;
368         } else {
369                 io_u->error = GetLastError();
370                 io_u->resid = io_u->xfer_buflen;
371         }
372
373         return rc;
374 }
375
376 /* Runs as a thread and waits for queued IO to complete */
377 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
378 {
379         OVERLAPPED *ovl;
380         struct fio_overlapped *fov;
381         struct io_u *io_u;
382         struct windowsaio_data *wd;
383         struct thread_ctx *ctx;
384         ULONG_PTR ulKey = 0;
385         DWORD bytes;
386
387         ctx = (struct thread_ctx*)lpParameter;
388         wd = ctx->wd;
389
390         do {
391                 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250))
392                         continue;
393
394                 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
395                 io_u = fov->io_u;
396
397                 if (ovl->Internal == ERROR_SUCCESS) {
398                         io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
399                         io_u->error = 0;
400                 } else {
401                         io_u->resid = io_u->xfer_buflen;
402                         io_u->error = ovl->Internal;
403                 }
404
405         fov->io_complete = TRUE;
406                 SetEvent(wd->iocomplete_event);
407         } while (ctx->wd->iothread_running);
408
409         CloseHandle(ctx->iocp);
410         free(ctx);
411         return 0;
412 }
413
414 static int fio_windowsaio_cancel(struct thread_data *td,
415                                struct io_u *io_u)
416 {
417         int rc = 0;
418
419         struct windowsaio_data *wd = td->io_ops->data;
420
421         /* If we're running on Vista or newer, we can cancel individual IO requests */
422         if (wd->pCancelIoEx != NULL) {
423                 struct fio_overlapped *ovl = io_u->engine_data;
424                 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
425                         rc = 1;
426         } else
427                 rc = 1;
428
429         return rc;
430 }
431
432 static struct ioengine_ops ioengine = {
433         .name           = "windowsaio",
434         .version        = FIO_IOOPS_VERSION,
435         .init           = fio_windowsaio_init,
436         .queue          = fio_windowsaio_queue,
437         .cancel         = fio_windowsaio_cancel,
438         .getevents      = fio_windowsaio_getevents,
439         .event          = fio_windowsaio_event,
440         .cleanup        = fio_windowsaio_cleanup,
441         .open_file      = fio_windowsaio_open_file,
442         .close_file     = fio_windowsaio_close_file,
443         .get_file_size  = generic_get_file_size
444 };
445
446 static void fio_init fio_posixaio_register(void)
447 {
448         register_ioengine(&ioengine);
449 }
450
451 static void fio_exit fio_posixaio_unregister(void)
452 {
453         unregister_ioengine(&ioengine);
454 }