null: 'td' isn't actually unused, unmark it
[fio.git] / engines / windowsaio.c
1 /*
2  * windowsaio engine
3  *
4  * IO engine using Windows IO Completion Ports.
5  */
6
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
16 typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
17
18 int geterrno_from_win_error (DWORD code, int deferrno);
19
20 struct fio_overlapped {
21         OVERLAPPED o;
22         struct io_u *io_u;
23         BOOL io_complete;
24 };
25
26 struct windowsaio_data {
27         struct io_u **aio_events;
28         HANDLE iocp;
29         HANDLE iothread;
30         HANDLE iocomplete_event;
31         BOOL iothread_running;
32 };
33
34 struct thread_ctx {
35         HANDLE iocp;
36         struct windowsaio_data *wd;
37 };
38
39 static BOOL timeout_expired(DWORD start_count, DWORD end_count);
40 static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
41                                         unsigned int max, struct timespec *t);
42 static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
43 static int fio_windowsaio_queue(struct thread_data *td,
44                                   struct io_u *io_u);
45 static void fio_windowsaio_cleanup(struct thread_data *td);
46 static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
47 static int fio_windowsaio_init(struct thread_data *td);
48 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
49 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
50
51 static int fio_windowsaio_init(struct thread_data *td)
52 {
53         struct windowsaio_data *wd;
54         int rc = 0;
55
56         wd = calloc(1, sizeof(struct windowsaio_data));
57         if (wd == NULL) {
58                  log_err("windowsaio: failed to allocate memory for engine data\n");
59                 rc = 1;
60         }
61
62         if (!rc) {
63                 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
64                 if (wd->aio_events == NULL) {
65                         log_err("windowsaio: failed to allocate memory for aio events list\n");
66                         rc = 1;
67                 }
68         }
69
70         if (!rc) {
71                 /* Create an auto-reset event */
72                 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
73                 if (wd->iocomplete_event == NULL) {
74                         log_err("windowsaio: failed to create io complete event handle\n");
75                         rc = 1;
76                 }
77         }
78
79         if (rc) {
80                 if (wd != NULL) {
81                         if (wd->aio_events != NULL)
82                                 free(wd->aio_events);
83
84                         free(wd);
85                 }
86         }
87
88         td->io_ops->data = wd;
89
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);
96                 if (hFile == INVALID_HANDLE_VALUE) {
97                         log_err("windowsaio: failed to create io completion port\n");
98                         rc = 1;
99                 }
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                 {
110                         log_err("windowsaio: failed to allocate memory for thread context structure\n");
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);
120                         if (wd->iothread == NULL)
121                                 log_err("windowsaio: failed to create io completion thread\n");
122                 }
123
124                 if (rc || wd->iothread == NULL)
125                         rc = 1;
126         }
127
128         return rc;
129 }
130
131 static void fio_windowsaio_cleanup(struct thread_data *td)
132 {
133         struct windowsaio_data *wd;
134
135         wd = td->io_ops->data;
136
137         if (wd != NULL) {
138                 wd->iothread_running = FALSE;
139                 WaitForSingleObject(wd->iothread, INFINITE);
140
141                 CloseHandle(wd->iothread);
142                 CloseHandle(wd->iocomplete_event);
143
144                 free(wd->aio_events);
145                 free(wd);
146
147                 td->io_ops->data = NULL;
148         }
149 }
150
151
152 static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
153 {
154         int rc = 0;
155         DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
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
162         if (f->filetype == FIO_TYPE_PIPE) {
163                 log_err("windowsaio: pipes are not supported\n");
164                 return 1;
165         }
166
167         if (!strcmp(f->file_name, "-")) {
168                 log_err("windowsaio: can't read/write to stdin/out\n");
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
177         /*
178          * Inform Windows whether we're going to be doing sequential or
179          * random io so it can tune the Cache Manager
180          */
181         if (td->o.td_ddir == TD_DDIR_READ  ||
182                 td->o.td_ddir == TD_DDIR_WRITE)
183                 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
184         else
185                 flags |= FILE_FLAG_RANDOM_ACCESS;
186
187         if (!td_write(td) || read_only)
188                 access = GENERIC_READ;
189         else
190                 access = (GENERIC_READ | GENERIC_WRITE);
191
192         if (td->o.create_on_open)
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
200         if (f->hFile == INVALID_HANDLE_VALUE) {
201                 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
202                 rc = 1;
203         }
204
205         /* Only set up the completion port and thread if we're not just
206          * querying the device size */
207         if (!rc && td->io_ops->data != NULL) {
208                 struct windowsaio_data *wd;
209
210                 wd = td->io_ops->data;
211
212                 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
213                         log_err("windowsaio: failed to create io completion port\n");
214                         rc = 1;
215                 }
216         }
217
218         return rc;
219 }
220
221 static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
222 {
223         int rc = 0;
224
225         dprint(FD_FILE, "fd close %s\n", f->file_name);
226
227         if (f->hFile != INVALID_HANDLE_VALUE) {
228                 if (!CloseHandle(f->hFile)) {
229                         log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
230                         rc = 1;
231                 }
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                                 ResetEvent(fov->o.hEvent);
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 >= min)
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, struct io_u *io_u)
308 {
309         struct fio_overlapped *o = io_u->engine_data;
310         LPOVERLAPPED lpOvl = &o->o;
311         DWORD iobytes;
312         BOOL success = FALSE;
313         int rc = FIO_Q_COMPLETED;
314
315         fio_ro_check(td, io_u);
316
317         lpOvl->Internal = STATUS_PENDING;
318         lpOvl->InternalHigh = 0;
319         lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
320         lpOvl->OffsetHigh = io_u->offset >> 32;
321
322         switch (io_u->ddir) {
323         case DDIR_WRITE:
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);
333                 if (!success) {
334                         log_err("windowsaio: failed to flush file buffers\n");
335                         io_u->error = win_to_posix_error(GetLastError());
336                 }
337
338                 return FIO_Q_COMPLETED;
339                 break;
340         case DDIR_TRIM:
341                 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
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);
348                 break;
349         }
350
351         if (success || GetLastError() == ERROR_IO_PENDING)
352                 rc = FIO_Q_QUEUED;
353         else {
354                 io_u->error = win_to_posix_error(GetLastError());
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 */
362 static 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 {
376                 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
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;
387                         io_u->error = win_to_posix_error(GetLastError());
388                 }
389
390                 fov->io_complete = TRUE;
391                 SetEvent(wd->iocomplete_event);
392         } while (ctx->wd->iothread_running);
393
394         CloseHandle(ctx->iocp);
395         free(ctx);
396         return 0;
397 }
398
399 static 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
410 static 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));
415         o->io_complete = FALSE;
416         o->io_u = io_u;
417         o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
418         if (o->o.hEvent == NULL) {
419                 log_err("windowsaio: failed to create event handle\n");
420                 free(o);
421                 return 1;
422         }
423
424         io_u->engine_data = o;
425         return 0;
426 }
427
428 static struct ioengine_ops ioengine = {
429         .name           = "windowsaio",
430         .version        = FIO_IOOPS_VERSION,
431         .init           = fio_windowsaio_init,
432         .queue          = fio_windowsaio_queue,
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,
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,
441 };
442
443 static void fio_init fio_windowsaio_register(void)
444 {
445         register_ioengine(&ioengine);
446 }
447
448 static void fio_exit fio_windowsaio_unregister(void)
449 {
450         unregister_ioengine(&ioengine);
451 }