fio: fix aio trim completion latencies
[fio.git] / engines / windowsaio.c
CommitLineData
ecc314ba 1/*
03244c19
BC
2 * windowsaio engine
3 *
4 * IO engine using Windows IO Completion Ports.
ecc314ba
BC
5 */
6
ecc314ba
BC
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <signal.h>
11#include <errno.h>
ecc314ba
BC
12
13#include "../fio.h"
14
ea4500d8
BC
15typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
16
66c098b8
BC
17int geterrno_from_win_error (DWORD code, int deferrno);
18
67897036
BC
19struct fio_overlapped {
20 OVERLAPPED o;
21 struct io_u *io_u;
22 BOOL io_complete;
67897036 23};
ecc314ba 24
9b836561 25struct windowsaio_data {
9b836561 26 struct io_u **aio_events;
5a90bb5f 27 HANDLE iocp;
67897036 28 HANDLE iothread;
9b836561 29 HANDLE iocomplete_event;
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 38static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
ecc314ba 39
ecc314ba
BC
40static int fio_windowsaio_init(struct thread_data *td)
41{
ecc314ba 42 struct windowsaio_data *wd;
9b836561 43 int rc = 0;
ecc314ba 44
03244c19
BC
45 wd = calloc(1, sizeof(struct windowsaio_data));
46 if (wd == NULL) {
47 log_err("windowsaio: failed to allocate memory for engine data\n");
9b836561 48 rc = 1;
03244c19 49 }
ecc314ba 50
9b836561
BC
51 if (!rc) {
52 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
03244c19
BC
53 if (wd->aio_events == NULL) {
54 log_err("windowsaio: failed to allocate memory for aio events list\n");
9b836561 55 rc = 1;
03244c19 56 }
e4db9fec
BC
57 }
58
9b836561
BC
59 if (!rc) {
60 /* Create an auto-reset event */
61 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
03244c19
BC
62 if (wd->iocomplete_event == NULL) {
63 log_err("windowsaio: failed to create io complete event handle\n");
9b836561 64 rc = 1;
03244c19 65 }
9b836561
BC
66 }
67
9b836561 68 if (rc) {
9b836561 69 if (wd != NULL) {
9b836561
BC
70 if (wd->aio_events != NULL)
71 free(wd->aio_events);
72
73 free(wd);
74 }
75 }
ecc314ba 76
565e784d 77 td->io_ops_data = wd;
93bcfd20 78
5a90bb5f
BC
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);
03244c19
BC
85 if (hFile == INVALID_HANDLE_VALUE) {
86 log_err("windowsaio: failed to create io completion port\n");
5a90bb5f 87 rc = 1;
03244c19 88 }
5a90bb5f 89
565e784d 90 wd = td->io_ops_data;
5a90bb5f
BC
91 wd->iothread_running = TRUE;
92 wd->iocp = hFile;
93
94 if (!rc)
95 ctx = malloc(sizeof(struct thread_ctx));
96
1633aa61 97 if (!rc && ctx == NULL) {
03244c19 98 log_err("windowsaio: failed to allocate memory for thread context structure\n");
5a90bb5f
BC
99 CloseHandle(hFile);
100 rc = 1;
101 }
102
1633aa61 103 if (!rc) {
438bb1cf
BC
104 DWORD threadid;
105
5a90bb5f
BC
106 ctx->iocp = hFile;
107 ctx->wd = wd;
438bb1cf
BC
108 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, &threadid);
109
110 if (wd->iothread != NULL)
111 fio_setaffinity(threadid, td->o.cpumask);
112 else
03244c19 113 log_err("windowsaio: failed to create io completion thread\n");
5a90bb5f
BC
114 }
115
116 if (rc || wd->iothread == NULL)
117 rc = 1;
118 }
119
4e79098f 120 return rc;
ecc314ba
BC
121}
122
67897036
BC
123static void fio_windowsaio_cleanup(struct thread_data *td)
124{
67897036
BC
125 struct windowsaio_data *wd;
126
565e784d 127 wd = td->io_ops_data;
67897036
BC
128
129 if (wd != NULL) {
40c5db35
JA
130 wd->iothread_running = FALSE;
131 WaitForSingleObject(wd->iothread, INFINITE);
67897036
BC
132
133 CloseHandle(wd->iothread);
134 CloseHandle(wd->iocomplete_event);
135
67897036 136 free(wd->aio_events);
67897036
BC
137 free(wd);
138
565e784d 139 td->io_ops_data = NULL;
67897036
BC
140 }
141}
142
8300eba5
SW
143static int windowsaio_invalidate_cache(struct fio_file *f)
144{
145 DWORD error;
146 DWORD isharemode = (FILE_SHARE_DELETE | FILE_SHARE_READ |
1633aa61 147 FILE_SHARE_WRITE);
8300eba5
SW
148 HANDLE ihFile;
149 int rc = 0;
150
151 /*
152 * Encourage Windows to drop cached parts of a file by temporarily
153 * opening it for non-buffered access. Note: this will only work when
154 * the following is the only thing with the file open on the whole
155 * system.
156 */
157 dprint(FD_IO, "windowaio: attempt invalidate cache for %s\n",
158 f->file_name);
159 ihFile = CreateFile(f->file_name, 0, isharemode, NULL, OPEN_EXISTING,
160 FILE_FLAG_NO_BUFFERING, NULL);
161
162 if (ihFile != INVALID_HANDLE_VALUE) {
163 if (!CloseHandle(ihFile)) {
164 error = GetLastError();
165 log_info("windowsaio: invalidation fd close %s "
166 "failed: error %d\n", f->file_name, error);
167 rc = 1;
168 }
169 } else {
170 error = GetLastError();
171 if (error != ERROR_FILE_NOT_FOUND) {
172 log_info("windowsaio: cache invalidation of %s failed: "
173 "error %d\n", f->file_name, error);
174 rc = 1;
175 }
176 }
177
178 return rc;
179}
180
ecc314ba
BC
181static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
182{
183 int rc = 0;
4e79098f 184 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
ecc314ba
BC
185 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
186 DWORD openmode = OPEN_ALWAYS;
187 DWORD access;
188
189 dprint(FD_FILE, "fd open %s\n", f->file_name);
190
ecc314ba 191 if (f->filetype == FIO_TYPE_PIPE) {
03244c19 192 log_err("windowsaio: pipes are not supported\n");
ecc314ba
BC
193 return 1;
194 }
195
196 if (!strcmp(f->file_name, "-")) {
03244c19 197 log_err("windowsaio: can't read/write to stdin/out\n");
ecc314ba
BC
198 return 1;
199 }
200
201 if (td->o.odirect)
202 flags |= FILE_FLAG_NO_BUFFERING;
203 if (td->o.sync_io)
204 flags |= FILE_FLAG_WRITE_THROUGH;
205
93bcfd20
BC
206 /*
207 * Inform Windows whether we're going to be doing sequential or
f20a86a7 208 * random IO so it can tune the Cache Manager
93bcfd20 209 */
f20a86a7
SW
210 switch (td->o.fadvise_hint) {
211 case F_ADV_TYPE:
212 if (td_random(td))
213 flags |= FILE_FLAG_RANDOM_ACCESS;
214 else
215 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
216 break;
217 case F_ADV_RANDOM:
ecc314ba 218 flags |= FILE_FLAG_RANDOM_ACCESS;
f20a86a7
SW
219 break;
220 case F_ADV_SEQUENTIAL:
221 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
222 break;
223 case F_ADV_NONE:
224 break;
225 default:
226 log_err("fio: unknown fadvise type %d\n", td->o.fadvise_hint);
227 }
ecc314ba 228
ea4500d8 229 if (!td_write(td) || read_only)
ecc314ba
BC
230 access = GENERIC_READ;
231 else
232 access = (GENERIC_READ | GENERIC_WRITE);
233
93bcfd20 234 if (td->o.create_on_open)
ecc314ba
BC
235 openmode = OPEN_ALWAYS;
236 else
237 openmode = OPEN_EXISTING;
238
8300eba5
SW
239 /* If we're going to use direct I/O, Windows will try and invalidate
240 * its cache at that point so there's no need to do it here */
3e9ae304 241 if (td->o.invalidate_cache && !td->o.odirect)
8300eba5 242 windowsaio_invalidate_cache(f);
8300eba5 243
ecc314ba
BC
244 f->hFile = CreateFile(f->file_name, access, sharemode,
245 NULL, openmode, flags, NULL);
246
03244c19
BC
247 if (f->hFile == INVALID_HANDLE_VALUE) {
248 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
ecc314ba 249 rc = 1;
03244c19 250 }
ecc314ba 251
93bcfd20 252 /* Only set up the completion port and thread if we're not just
ecc314ba 253 * querying the device size */
565e784d 254 if (!rc && td->io_ops_data != NULL) {
40c5db35 255 struct windowsaio_data *wd;
ecc314ba 256
565e784d 257 wd = td->io_ops_data;
ecc314ba 258
03244c19
BC
259 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
260 log_err("windowsaio: failed to create io completion port\n");
ecc314ba 261 rc = 1;
03244c19 262 }
ecc314ba
BC
263 }
264
ecc314ba
BC
265 return rc;
266}
267
268static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
269{
4e79098f 270 int rc = 0;
1e42cc3d 271
9b836561
BC
272 dprint(FD_FILE, "fd close %s\n", f->file_name);
273
ecc314ba 274 if (f->hFile != INVALID_HANDLE_VALUE) {
03244c19
BC
275 if (!CloseHandle(f->hFile)) {
276 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
4e79098f 277 rc = 1;
03244c19 278 }
ecc314ba
BC
279 }
280
281 f->hFile = INVALID_HANDLE_VALUE;
4e79098f 282 return rc;
ecc314ba
BC
283}
284
67897036
BC
285static BOOL timeout_expired(DWORD start_count, DWORD end_count)
286{
287 BOOL expired = FALSE;
288 DWORD current_time;
289
290 current_time = GetTickCount();
291
292 if ((end_count > start_count) && current_time >= end_count)
293 expired = TRUE;
294 else if (current_time < start_count && current_time > end_count)
295 expired = TRUE;
296
297 return expired;
298}
299
300static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
301{
565e784d 302 struct windowsaio_data *wd = td->io_ops_data;
67897036
BC
303 return wd->aio_events[event];
304}
305
306static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
1f440ece
JA
307 unsigned int max,
308 const struct timespec *t)
67897036 309{
565e784d 310 struct windowsaio_data *wd = td->io_ops_data;
67897036
BC
311 unsigned int dequeued = 0;
312 struct io_u *io_u;
7b2dfab1 313 int i;
67897036
BC
314 struct fio_overlapped *fov;
315 DWORD start_count = 0;
316 DWORD end_count = 0;
317 DWORD status;
318 DWORD mswait = 250;
319
320 if (t != NULL) {
321 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
322 start_count = GetTickCount();
323 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
324 }
325
326 do {
7b2dfab1
BC
327 io_u_qiter(&td->io_u_all, io_u, i) {
328 if (!(io_u->flags & IO_U_F_FLIGHT))
329 continue;
330
67897036
BC
331 fov = (struct fio_overlapped*)io_u->engine_data;
332
333 if (fov->io_complete) {
40c5db35 334 fov->io_complete = FALSE;
67897036
BC
335 wd->aio_events[dequeued] = io_u;
336 dequeued++;
337 }
338
67897036 339 }
77e7b330
JR
340 if (dequeued >= min)
341 break;
67897036 342
40c5db35 343 if (dequeued < min) {
67897036 344 status = WaitForSingleObject(wd->iocomplete_event, mswait);
f9a58c2a 345 if (status != WAIT_OBJECT_0 && dequeued >= min)
03244c19 346 break;
67897036
BC
347 }
348
1633aa61
JA
349 if (dequeued >= min ||
350 (t != NULL && timeout_expired(start_count, end_count)))
67897036
BC
351 break;
352 } while (1);
353
354 return dequeued;
355}
356
2e4ef4fb
JA
357static enum fio_q_status fio_windowsaio_queue(struct thread_data *td,
358 struct io_u *io_u)
67897036 359{
c73ed246
JA
360 struct fio_overlapped *o = io_u->engine_data;
361 LPOVERLAPPED lpOvl = &o->o;
93bcfd20 362 BOOL success = FALSE;
67897036
BC
363 int rc = FIO_Q_COMPLETED;
364
365 fio_ro_check(td, io_u);
366
77e7b330 367 lpOvl->Internal = 0;
4e79098f
BC
368 lpOvl->InternalHigh = 0;
369 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
370 lpOvl->OffsetHigh = io_u->offset >> 32;
67897036
BC
371
372 switch (io_u->ddir) {
40c5db35 373 case DDIR_WRITE:
1633aa61
JA
374 success = WriteFile(io_u->file->hFile, io_u->xfer_buf,
375 io_u->xfer_buflen, NULL, lpOvl);
67897036
BC
376 break;
377 case DDIR_READ:
1633aa61
JA
378 success = ReadFile(io_u->file->hFile, io_u->xfer_buf,
379 io_u->xfer_buflen, NULL, lpOvl);
67897036
BC
380 break;
381 case DDIR_SYNC:
382 case DDIR_DATASYNC:
383 case DDIR_SYNC_FILE_RANGE:
384 success = FlushFileBuffers(io_u->file->hFile);
03244c19
BC
385 if (!success) {
386 log_err("windowsaio: failed to flush file buffers\n");
387 io_u->error = win_to_posix_error(GetLastError());
388 }
67897036
BC
389
390 return FIO_Q_COMPLETED;
67897036 391 case DDIR_TRIM:
03244c19 392 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
67897036
BC
393 io_u->error = 1;
394 io_u->resid = io_u->xfer_buflen;
395 return FIO_Q_COMPLETED;
67897036
BC
396 default:
397 assert(0);
93bcfd20 398 break;
67897036
BC
399 }
400
40c5db35 401 if (success || GetLastError() == ERROR_IO_PENDING)
67897036 402 rc = FIO_Q_QUEUED;
40c5db35 403 else {
2277d5d5 404 io_u->error = win_to_posix_error(GetLastError());
67897036
BC
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 {
1633aa61
JA
426 BOOL ret;
427
428 ret = GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey,
429 &ovl, 250);
430 if (!ret && ovl == NULL)
67897036
BC
431 continue;
432
433 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
434 io_u = fov->io_u;
435
436 if (ovl->Internal == ERROR_SUCCESS) {
437 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
438 io_u->error = 0;
439 } else {
440 io_u->resid = io_u->xfer_buflen;
2277d5d5 441 io_u->error = win_to_posix_error(GetLastError());
67897036
BC
442 }
443
40c5db35 444 fov->io_complete = TRUE;
67897036
BC
445 SetEvent(wd->iocomplete_event);
446 } while (ctx->wd->iothread_running);
447
448 CloseHandle(ctx->iocp);
449 free(ctx);
450 return 0;
451}
452
c73ed246
JA
453static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
454{
455 struct fio_overlapped *o = io_u->engine_data;
456
457 if (o) {
c73ed246
JA
458 io_u->engine_data = NULL;
459 free(o);
460 }
461}
462
463static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
464{
465 struct fio_overlapped *o;
466
467 o = malloc(sizeof(*o));
b0106419 468 o->io_complete = FALSE;
c73ed246 469 o->io_u = io_u;
77e7b330 470 o->o.hEvent = NULL;
c73ed246
JA
471 io_u->engine_data = o;
472 return 0;
473}
474
ecc314ba
BC
475static struct ioengine_ops ioengine = {
476 .name = "windowsaio",
477 .version = FIO_IOOPS_VERSION,
478 .init = fio_windowsaio_init,
479 .queue = fio_windowsaio_queue,
ecc314ba
BC
480 .getevents = fio_windowsaio_getevents,
481 .event = fio_windowsaio_event,
482 .cleanup = fio_windowsaio_cleanup,
483 .open_file = fio_windowsaio_open_file,
484 .close_file = fio_windowsaio_close_file,
c73ed246
JA
485 .get_file_size = generic_get_file_size,
486 .io_u_init = fio_windowsaio_io_u_init,
487 .io_u_free = fio_windowsaio_io_u_free,
ecc314ba
BC
488};
489
c874d188 490static void fio_init fio_windowsaio_register(void)
ecc314ba
BC
491{
492 register_ioengine(&ioengine);
493}
494
c874d188 495static void fio_exit fio_windowsaio_unregister(void)
ecc314ba
BC
496{
497 unregister_ioengine(&ioengine);
498}