Windows mkdir() fix
[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 windowsaio_invalidate_cache(struct fio_file *f)
146{
147 DWORD error;
148 DWORD isharemode = (FILE_SHARE_DELETE | FILE_SHARE_READ |
149 FILE_SHARE_WRITE);
150 HANDLE ihFile;
151 int rc = 0;
152
153 /*
154 * Encourage Windows to drop cached parts of a file by temporarily
155 * opening it for non-buffered access. Note: this will only work when
156 * the following is the only thing with the file open on the whole
157 * system.
158 */
159 dprint(FD_IO, "windowaio: attempt invalidate cache for %s\n",
160 f->file_name);
161 ihFile = CreateFile(f->file_name, 0, isharemode, NULL, OPEN_EXISTING,
162 FILE_FLAG_NO_BUFFERING, NULL);
163
164 if (ihFile != INVALID_HANDLE_VALUE) {
165 if (!CloseHandle(ihFile)) {
166 error = GetLastError();
167 log_info("windowsaio: invalidation fd close %s "
168 "failed: error %d\n", f->file_name, error);
169 rc = 1;
170 }
171 } else {
172 error = GetLastError();
173 if (error != ERROR_FILE_NOT_FOUND) {
174 log_info("windowsaio: cache invalidation of %s failed: "
175 "error %d\n", f->file_name, error);
176 rc = 1;
177 }
178 }
179
180 return rc;
181}
182
183static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
184{
185 int rc = 0;
186 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
187 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
188 DWORD openmode = OPEN_ALWAYS;
189 DWORD access;
190
191 dprint(FD_FILE, "fd open %s\n", f->file_name);
192
193 if (f->filetype == FIO_TYPE_PIPE) {
194 log_err("windowsaio: pipes are not supported\n");
195 return 1;
196 }
197
198 if (!strcmp(f->file_name, "-")) {
199 log_err("windowsaio: can't read/write to stdin/out\n");
200 return 1;
201 }
202
203 if (td->o.odirect)
204 flags |= FILE_FLAG_NO_BUFFERING;
205 if (td->o.sync_io)
206 flags |= FILE_FLAG_WRITE_THROUGH;
207
208 /*
209 * Inform Windows whether we're going to be doing sequential or
210 * random IO so it can tune the Cache Manager
211 */
212 switch (td->o.fadvise_hint) {
213 case F_ADV_TYPE:
214 if (td_random(td))
215 flags |= FILE_FLAG_RANDOM_ACCESS;
216 else
217 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
218 break;
219 case F_ADV_RANDOM:
220 flags |= FILE_FLAG_RANDOM_ACCESS;
221 break;
222 case F_ADV_SEQUENTIAL:
223 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
224 break;
225 case F_ADV_NONE:
226 break;
227 default:
228 log_err("fio: unknown fadvise type %d\n", td->o.fadvise_hint);
229 }
230
231 if (!td_write(td) || read_only)
232 access = GENERIC_READ;
233 else
234 access = (GENERIC_READ | GENERIC_WRITE);
235
236 if (td->o.create_on_open)
237 openmode = OPEN_ALWAYS;
238 else
239 openmode = OPEN_EXISTING;
240
241 /* If we're going to use direct I/O, Windows will try and invalidate
242 * its cache at that point so there's no need to do it here */
243 if (td->o.invalidate_cache && !td->o.odirect)
244 windowsaio_invalidate_cache(f);
245
246 f->hFile = CreateFile(f->file_name, access, sharemode,
247 NULL, openmode, flags, NULL);
248
249 if (f->hFile == INVALID_HANDLE_VALUE) {
250 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
251 rc = 1;
252 }
253
254 /* Only set up the completion port and thread if we're not just
255 * querying the device size */
256 if (!rc && td->io_ops_data != NULL) {
257 struct windowsaio_data *wd;
258
259 wd = td->io_ops_data;
260
261 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
262 log_err("windowsaio: failed to create io completion port\n");
263 rc = 1;
264 }
265 }
266
267 return rc;
268}
269
270static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
271{
272 int rc = 0;
273
274 dprint(FD_FILE, "fd close %s\n", f->file_name);
275
276 if (f->hFile != INVALID_HANDLE_VALUE) {
277 if (!CloseHandle(f->hFile)) {
278 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
279 rc = 1;
280 }
281 }
282
283 f->hFile = INVALID_HANDLE_VALUE;
284 return rc;
285}
286
287static BOOL timeout_expired(DWORD start_count, DWORD end_count)
288{
289 BOOL expired = FALSE;
290 DWORD current_time;
291
292 current_time = GetTickCount();
293
294 if ((end_count > start_count) && current_time >= end_count)
295 expired = TRUE;
296 else if (current_time < start_count && current_time > end_count)
297 expired = TRUE;
298
299 return expired;
300}
301
302static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
303{
304 struct windowsaio_data *wd = td->io_ops_data;
305 return wd->aio_events[event];
306}
307
308static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
309 unsigned int max,
310 const struct timespec *t)
311{
312 struct windowsaio_data *wd = td->io_ops_data;
313 unsigned int dequeued = 0;
314 struct io_u *io_u;
315 int i;
316 struct fio_overlapped *fov;
317 DWORD start_count = 0;
318 DWORD end_count = 0;
319 DWORD status;
320 DWORD mswait = 250;
321
322 if (t != NULL) {
323 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
324 start_count = GetTickCount();
325 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
326 }
327
328 do {
329 io_u_qiter(&td->io_u_all, io_u, i) {
330 if (!(io_u->flags & IO_U_F_FLIGHT))
331 continue;
332
333 fov = (struct fio_overlapped*)io_u->engine_data;
334
335 if (fov->io_complete) {
336 fov->io_complete = FALSE;
337 wd->aio_events[dequeued] = io_u;
338 dequeued++;
339 }
340
341 }
342 if (dequeued >= min)
343 break;
344
345 if (dequeued < min) {
346 status = WaitForSingleObject(wd->iocomplete_event, mswait);
347 if (status != WAIT_OBJECT_0 && dequeued >= min)
348 break;
349 }
350
351 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
352 break;
353 } while (1);
354
355 return dequeued;
356}
357
358static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
359{
360 struct fio_overlapped *o = io_u->engine_data;
361 LPOVERLAPPED lpOvl = &o->o;
362 BOOL success = FALSE;
363 int rc = FIO_Q_COMPLETED;
364
365 fio_ro_check(td, io_u);
366
367 lpOvl->Internal = 0;
368 lpOvl->InternalHigh = 0;
369 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
370 lpOvl->OffsetHigh = io_u->offset >> 32;
371
372 switch (io_u->ddir) {
373 case DDIR_WRITE:
374 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
375 break;
376 case DDIR_READ:
377 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
378 break;
379 case DDIR_SYNC:
380 case DDIR_DATASYNC:
381 case DDIR_SYNC_FILE_RANGE:
382 success = FlushFileBuffers(io_u->file->hFile);
383 if (!success) {
384 log_err("windowsaio: failed to flush file buffers\n");
385 io_u->error = win_to_posix_error(GetLastError());
386 }
387
388 return FIO_Q_COMPLETED;
389 break;
390 case DDIR_TRIM:
391 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
392 io_u->error = 1;
393 io_u->resid = io_u->xfer_buflen;
394 return FIO_Q_COMPLETED;
395 break;
396 default:
397 assert(0);
398 break;
399 }
400
401 if (success || GetLastError() == ERROR_IO_PENDING)
402 rc = FIO_Q_QUEUED;
403 else {
404 io_u->error = win_to_posix_error(GetLastError());
405 io_u->resid = io_u->xfer_buflen;
406 }
407
408 return rc;
409}
410
411/* Runs as a thread and waits for queued IO to complete */
412static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
413{
414 OVERLAPPED *ovl;
415 struct fio_overlapped *fov;
416 struct io_u *io_u;
417 struct windowsaio_data *wd;
418 struct thread_ctx *ctx;
419 ULONG_PTR ulKey = 0;
420 DWORD bytes;
421
422 ctx = (struct thread_ctx*)lpParameter;
423 wd = ctx->wd;
424
425 do {
426 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
427 continue;
428
429 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
430 io_u = fov->io_u;
431
432 if (ovl->Internal == ERROR_SUCCESS) {
433 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
434 io_u->error = 0;
435 } else {
436 io_u->resid = io_u->xfer_buflen;
437 io_u->error = win_to_posix_error(GetLastError());
438 }
439
440 fov->io_complete = TRUE;
441 SetEvent(wd->iocomplete_event);
442 } while (ctx->wd->iothread_running);
443
444 CloseHandle(ctx->iocp);
445 free(ctx);
446 return 0;
447}
448
449static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
450{
451 struct fio_overlapped *o = io_u->engine_data;
452
453 if (o) {
454 io_u->engine_data = NULL;
455 free(o);
456 }
457}
458
459static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
460{
461 struct fio_overlapped *o;
462
463 o = malloc(sizeof(*o));
464 o->io_complete = FALSE;
465 o->io_u = io_u;
466 o->o.hEvent = NULL;
467 io_u->engine_data = o;
468 return 0;
469}
470
471static struct ioengine_ops ioengine = {
472 .name = "windowsaio",
473 .version = FIO_IOOPS_VERSION,
474 .init = fio_windowsaio_init,
475 .queue = fio_windowsaio_queue,
476 .getevents = fio_windowsaio_getevents,
477 .event = fio_windowsaio_event,
478 .cleanup = fio_windowsaio_cleanup,
479 .open_file = fio_windowsaio_open_file,
480 .close_file = fio_windowsaio_close_file,
481 .get_file_size = generic_get_file_size,
482 .io_u_init = fio_windowsaio_io_u_init,
483 .io_u_free = fio_windowsaio_io_u_free,
484};
485
486static void fio_init fio_windowsaio_register(void)
487{
488 register_ioengine(&ioengine);
489}
490
491static void fio_exit fio_windowsaio_unregister(void)
492{
493 unregister_ioengine(&ioengine);
494}