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