windowsaio: add best effort cache invalidation
[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
97 if (!rc && ctx == NULL)
98 {
03244c19 99 log_err("windowsaio: failed to allocate memory for thread context structure\n");
5a90bb5f
BC
100 CloseHandle(hFile);
101 rc = 1;
102 }
103
104 if (!rc)
105 {
438bb1cf
BC
106 DWORD threadid;
107
5a90bb5f
BC
108 ctx->iocp = hFile;
109 ctx->wd = wd;
438bb1cf
BC
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
03244c19 115 log_err("windowsaio: failed to create io completion thread\n");
5a90bb5f
BC
116 }
117
118 if (rc || wd->iothread == NULL)
119 rc = 1;
120 }
121
4e79098f 122 return rc;
ecc314ba
BC
123}
124
67897036
BC
125static void fio_windowsaio_cleanup(struct thread_data *td)
126{
67897036
BC
127 struct windowsaio_data *wd;
128
565e784d 129 wd = td->io_ops_data;
67897036
BC
130
131 if (wd != NULL) {
40c5db35
JA
132 wd->iothread_running = FALSE;
133 WaitForSingleObject(wd->iothread, INFINITE);
67897036
BC
134
135 CloseHandle(wd->iothread);
136 CloseHandle(wd->iocomplete_event);
137
67897036 138 free(wd->aio_events);
67897036
BC
139 free(wd);
140
565e784d 141 td->io_ops_data = NULL;
67897036
BC
142 }
143}
144
8300eba5
SW
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
ecc314ba
BC
183static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
184{
185 int rc = 0;
4e79098f 186 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
ecc314ba
BC
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
ecc314ba 193 if (f->filetype == FIO_TYPE_PIPE) {
03244c19 194 log_err("windowsaio: pipes are not supported\n");
ecc314ba
BC
195 return 1;
196 }
197
198 if (!strcmp(f->file_name, "-")) {
03244c19 199 log_err("windowsaio: can't read/write to stdin/out\n");
ecc314ba
BC
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
93bcfd20
BC
208 /*
209 * Inform Windows whether we're going to be doing sequential or
f20a86a7 210 * random IO so it can tune the Cache Manager
93bcfd20 211 */
f20a86a7
SW
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:
ecc314ba 220 flags |= FILE_FLAG_RANDOM_ACCESS;
f20a86a7
SW
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 }
ecc314ba 230
ea4500d8 231 if (!td_write(td) || read_only)
ecc314ba
BC
232 access = GENERIC_READ;
233 else
234 access = (GENERIC_READ | GENERIC_WRITE);
235
93bcfd20 236 if (td->o.create_on_open)
ecc314ba
BC
237 openmode = OPEN_ALWAYS;
238 else
239 openmode = OPEN_EXISTING;
240
8300eba5
SW
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
ecc314ba
BC
247 f->hFile = CreateFile(f->file_name, access, sharemode,
248 NULL, openmode, flags, NULL);
249
03244c19
BC
250 if (f->hFile == INVALID_HANDLE_VALUE) {
251 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
ecc314ba 252 rc = 1;
03244c19 253 }
ecc314ba 254
93bcfd20 255 /* Only set up the completion port and thread if we're not just
ecc314ba 256 * querying the device size */
565e784d 257 if (!rc && td->io_ops_data != NULL) {
40c5db35 258 struct windowsaio_data *wd;
ecc314ba 259
565e784d 260 wd = td->io_ops_data;
ecc314ba 261
03244c19
BC
262 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
263 log_err("windowsaio: failed to create io completion port\n");
ecc314ba 264 rc = 1;
03244c19 265 }
ecc314ba
BC
266 }
267
ecc314ba
BC
268 return rc;
269}
270
271static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
272{
4e79098f 273 int rc = 0;
1e42cc3d 274
9b836561
BC
275 dprint(FD_FILE, "fd close %s\n", f->file_name);
276
ecc314ba 277 if (f->hFile != INVALID_HANDLE_VALUE) {
03244c19
BC
278 if (!CloseHandle(f->hFile)) {
279 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
4e79098f 280 rc = 1;
03244c19 281 }
ecc314ba
BC
282 }
283
284 f->hFile = INVALID_HANDLE_VALUE;
4e79098f 285 return rc;
ecc314ba
BC
286}
287
67897036
BC
288static BOOL timeout_expired(DWORD start_count, DWORD end_count)
289{
290 BOOL expired = FALSE;
291 DWORD current_time;
292
293 current_time = GetTickCount();
294
295 if ((end_count > start_count) && current_time >= end_count)
296 expired = TRUE;
297 else if (current_time < start_count && current_time > end_count)
298 expired = TRUE;
299
300 return expired;
301}
302
303static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
304{
565e784d 305 struct windowsaio_data *wd = td->io_ops_data;
67897036
BC
306 return wd->aio_events[event];
307}
308
309static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
1f440ece
JA
310 unsigned int max,
311 const struct timespec *t)
67897036 312{
565e784d 313 struct windowsaio_data *wd = td->io_ops_data;
67897036
BC
314 unsigned int dequeued = 0;
315 struct io_u *io_u;
7b2dfab1 316 int i;
67897036
BC
317 struct fio_overlapped *fov;
318 DWORD start_count = 0;
319 DWORD end_count = 0;
320 DWORD status;
321 DWORD mswait = 250;
322
323 if (t != NULL) {
324 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
325 start_count = GetTickCount();
326 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
327 }
328
329 do {
7b2dfab1
BC
330 io_u_qiter(&td->io_u_all, io_u, i) {
331 if (!(io_u->flags & IO_U_F_FLIGHT))
332 continue;
333
67897036
BC
334 fov = (struct fio_overlapped*)io_u->engine_data;
335
336 if (fov->io_complete) {
40c5db35 337 fov->io_complete = FALSE;
67897036
BC
338 wd->aio_events[dequeued] = io_u;
339 dequeued++;
340 }
341
67897036 342 }
77e7b330
JR
343 if (dequeued >= min)
344 break;
67897036 345
40c5db35 346 if (dequeued < min) {
67897036 347 status = WaitForSingleObject(wd->iocomplete_event, mswait);
f9a58c2a 348 if (status != WAIT_OBJECT_0 && dequeued >= min)
03244c19 349 break;
67897036
BC
350 }
351
352 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
353 break;
354 } while (1);
355
356 return dequeued;
357}
358
40c5db35 359static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
67897036 360{
c73ed246
JA
361 struct fio_overlapped *o = io_u->engine_data;
362 LPOVERLAPPED lpOvl = &o->o;
93bcfd20 363 BOOL success = FALSE;
67897036
BC
364 int rc = FIO_Q_COMPLETED;
365
366 fio_ro_check(td, io_u);
367
77e7b330 368 lpOvl->Internal = 0;
4e79098f
BC
369 lpOvl->InternalHigh = 0;
370 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
371 lpOvl->OffsetHigh = io_u->offset >> 32;
67897036
BC
372
373 switch (io_u->ddir) {
40c5db35 374 case DDIR_WRITE:
77e7b330 375 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
67897036
BC
376 break;
377 case DDIR_READ:
77e7b330 378 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
67897036
BC
379 break;
380 case DDIR_SYNC:
381 case DDIR_DATASYNC:
382 case DDIR_SYNC_FILE_RANGE:
383 success = FlushFileBuffers(io_u->file->hFile);
03244c19
BC
384 if (!success) {
385 log_err("windowsaio: failed to flush file buffers\n");
386 io_u->error = win_to_posix_error(GetLastError());
387 }
67897036
BC
388
389 return FIO_Q_COMPLETED;
390 break;
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;
396 break;
397 default:
398 assert(0);
93bcfd20 399 break;
67897036
BC
400 }
401
40c5db35 402 if (success || GetLastError() == ERROR_IO_PENDING)
67897036 403 rc = FIO_Q_QUEUED;
40c5db35 404 else {
2277d5d5 405 io_u->error = win_to_posix_error(GetLastError());
67897036
BC
406 io_u->resid = io_u->xfer_buflen;
407 }
408
409 return rc;
410}
411
412/* Runs as a thread and waits for queued IO to complete */
413static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
414{
415 OVERLAPPED *ovl;
416 struct fio_overlapped *fov;
417 struct io_u *io_u;
418 struct windowsaio_data *wd;
419 struct thread_ctx *ctx;
420 ULONG_PTR ulKey = 0;
421 DWORD bytes;
422
423 ctx = (struct thread_ctx*)lpParameter;
424 wd = ctx->wd;
425
426 do {
66c098b8 427 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
67897036
BC
428 continue;
429
430 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
431 io_u = fov->io_u;
432
433 if (ovl->Internal == ERROR_SUCCESS) {
434 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
435 io_u->error = 0;
436 } else {
437 io_u->resid = io_u->xfer_buflen;
2277d5d5 438 io_u->error = win_to_posix_error(GetLastError());
67897036
BC
439 }
440
40c5db35 441 fov->io_complete = TRUE;
67897036
BC
442 SetEvent(wd->iocomplete_event);
443 } while (ctx->wd->iothread_running);
444
445 CloseHandle(ctx->iocp);
446 free(ctx);
447 return 0;
448}
449
c73ed246
JA
450static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
451{
452 struct fio_overlapped *o = io_u->engine_data;
453
454 if (o) {
c73ed246
JA
455 io_u->engine_data = NULL;
456 free(o);
457 }
458}
459
460static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
461{
462 struct fio_overlapped *o;
463
464 o = malloc(sizeof(*o));
b0106419 465 o->io_complete = FALSE;
c73ed246 466 o->io_u = io_u;
77e7b330 467 o->o.hEvent = NULL;
c73ed246
JA
468 io_u->engine_data = o;
469 return 0;
470}
471
ecc314ba
BC
472static struct ioengine_ops ioengine = {
473 .name = "windowsaio",
474 .version = FIO_IOOPS_VERSION,
475 .init = fio_windowsaio_init,
476 .queue = fio_windowsaio_queue,
ecc314ba
BC
477 .getevents = fio_windowsaio_getevents,
478 .event = fio_windowsaio_event,
479 .cleanup = fio_windowsaio_cleanup,
480 .open_file = fio_windowsaio_open_file,
481 .close_file = fio_windowsaio_close_file,
c73ed246
JA
482 .get_file_size = generic_get_file_size,
483 .io_u_init = fio_windowsaio_io_u_init,
484 .io_u_free = fio_windowsaio_io_u_free,
ecc314ba
BC
485};
486
c874d188 487static void fio_init fio_windowsaio_register(void)
ecc314ba
BC
488{
489 register_ioengine(&ioengine);
490}
491
c874d188 492static void fio_exit fio_windowsaio_unregister(void)
ecc314ba
BC
493{
494 unregister_ioengine(&ioengine);
495}