engines/windowsaio: kill useless forward declarations
[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
ecc314ba
BC
145static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
146{
147 int rc = 0;
4e79098f 148 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
ecc314ba
BC
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
ecc314ba 155 if (f->filetype == FIO_TYPE_PIPE) {
03244c19 156 log_err("windowsaio: pipes are not supported\n");
ecc314ba
BC
157 return 1;
158 }
159
160 if (!strcmp(f->file_name, "-")) {
03244c19 161 log_err("windowsaio: can't read/write to stdin/out\n");
ecc314ba
BC
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
93bcfd20
BC
170 /*
171 * Inform Windows whether we're going to be doing sequential or
172 * random io so it can tune the Cache Manager
173 */
ecc314ba 174 if (td->o.td_ddir == TD_DDIR_READ ||
ea4500d8 175 td->o.td_ddir == TD_DDIR_WRITE)
ecc314ba 176 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
ecc314ba 177 else
ecc314ba 178 flags |= FILE_FLAG_RANDOM_ACCESS;
ecc314ba 179
ea4500d8 180 if (!td_write(td) || read_only)
ecc314ba
BC
181 access = GENERIC_READ;
182 else
183 access = (GENERIC_READ | GENERIC_WRITE);
184
93bcfd20 185 if (td->o.create_on_open)
ecc314ba
BC
186 openmode = OPEN_ALWAYS;
187 else
188 openmode = OPEN_EXISTING;
189
190 f->hFile = CreateFile(f->file_name, access, sharemode,
191 NULL, openmode, flags, NULL);
192
03244c19
BC
193 if (f->hFile == INVALID_HANDLE_VALUE) {
194 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
ecc314ba 195 rc = 1;
03244c19 196 }
ecc314ba 197
93bcfd20 198 /* Only set up the completion port and thread if we're not just
ecc314ba 199 * querying the device size */
565e784d 200 if (!rc && td->io_ops_data != NULL) {
40c5db35 201 struct windowsaio_data *wd;
ecc314ba 202
565e784d 203 wd = td->io_ops_data;
ecc314ba 204
03244c19
BC
205 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
206 log_err("windowsaio: failed to create io completion port\n");
ecc314ba 207 rc = 1;
03244c19 208 }
ecc314ba
BC
209 }
210
ecc314ba
BC
211 return rc;
212}
213
214static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
215{
4e79098f 216 int rc = 0;
1e42cc3d 217
9b836561
BC
218 dprint(FD_FILE, "fd close %s\n", f->file_name);
219
ecc314ba 220 if (f->hFile != INVALID_HANDLE_VALUE) {
03244c19
BC
221 if (!CloseHandle(f->hFile)) {
222 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
4e79098f 223 rc = 1;
03244c19 224 }
ecc314ba
BC
225 }
226
227 f->hFile = INVALID_HANDLE_VALUE;
4e79098f 228 return rc;
ecc314ba
BC
229}
230
67897036
BC
231static BOOL timeout_expired(DWORD start_count, DWORD end_count)
232{
233 BOOL expired = FALSE;
234 DWORD current_time;
235
236 current_time = GetTickCount();
237
238 if ((end_count > start_count) && current_time >= end_count)
239 expired = TRUE;
240 else if (current_time < start_count && current_time > end_count)
241 expired = TRUE;
242
243 return expired;
244}
245
246static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
247{
565e784d 248 struct windowsaio_data *wd = td->io_ops_data;
67897036
BC
249 return wd->aio_events[event];
250}
251
252static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
1f440ece
JA
253 unsigned int max,
254 const struct timespec *t)
67897036 255{
565e784d 256 struct windowsaio_data *wd = td->io_ops_data;
67897036
BC
257 unsigned int dequeued = 0;
258 struct io_u *io_u;
7b2dfab1 259 int i;
67897036
BC
260 struct fio_overlapped *fov;
261 DWORD start_count = 0;
262 DWORD end_count = 0;
263 DWORD status;
264 DWORD mswait = 250;
265
266 if (t != NULL) {
267 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
268 start_count = GetTickCount();
269 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
270 }
271
272 do {
7b2dfab1
BC
273 io_u_qiter(&td->io_u_all, io_u, i) {
274 if (!(io_u->flags & IO_U_F_FLIGHT))
275 continue;
276
67897036
BC
277 fov = (struct fio_overlapped*)io_u->engine_data;
278
279 if (fov->io_complete) {
40c5db35 280 fov->io_complete = FALSE;
67897036
BC
281 wd->aio_events[dequeued] = io_u;
282 dequeued++;
283 }
284
67897036 285 }
77e7b330
JR
286 if (dequeued >= min)
287 break;
67897036 288
40c5db35 289 if (dequeued < min) {
67897036 290 status = WaitForSingleObject(wd->iocomplete_event, mswait);
f9a58c2a 291 if (status != WAIT_OBJECT_0 && dequeued >= min)
03244c19 292 break;
67897036
BC
293 }
294
295 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
296 break;
297 } while (1);
298
299 return dequeued;
300}
301
40c5db35 302static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
67897036 303{
c73ed246
JA
304 struct fio_overlapped *o = io_u->engine_data;
305 LPOVERLAPPED lpOvl = &o->o;
93bcfd20 306 BOOL success = FALSE;
67897036
BC
307 int rc = FIO_Q_COMPLETED;
308
309 fio_ro_check(td, io_u);
310
77e7b330 311 lpOvl->Internal = 0;
4e79098f
BC
312 lpOvl->InternalHigh = 0;
313 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
314 lpOvl->OffsetHigh = io_u->offset >> 32;
67897036
BC
315
316 switch (io_u->ddir) {
40c5db35 317 case DDIR_WRITE:
77e7b330 318 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
67897036
BC
319 break;
320 case DDIR_READ:
77e7b330 321 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, NULL, lpOvl);
67897036
BC
322 break;
323 case DDIR_SYNC:
324 case DDIR_DATASYNC:
325 case DDIR_SYNC_FILE_RANGE:
326 success = FlushFileBuffers(io_u->file->hFile);
03244c19
BC
327 if (!success) {
328 log_err("windowsaio: failed to flush file buffers\n");
329 io_u->error = win_to_posix_error(GetLastError());
330 }
67897036
BC
331
332 return FIO_Q_COMPLETED;
333 break;
334 case DDIR_TRIM:
03244c19 335 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
67897036
BC
336 io_u->error = 1;
337 io_u->resid = io_u->xfer_buflen;
338 return FIO_Q_COMPLETED;
339 break;
340 default:
341 assert(0);
93bcfd20 342 break;
67897036
BC
343 }
344
40c5db35 345 if (success || GetLastError() == ERROR_IO_PENDING)
67897036 346 rc = FIO_Q_QUEUED;
40c5db35 347 else {
2277d5d5 348 io_u->error = win_to_posix_error(GetLastError());
67897036
BC
349 io_u->resid = io_u->xfer_buflen;
350 }
351
352 return rc;
353}
354
355/* Runs as a thread and waits for queued IO to complete */
356static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
357{
358 OVERLAPPED *ovl;
359 struct fio_overlapped *fov;
360 struct io_u *io_u;
361 struct windowsaio_data *wd;
362 struct thread_ctx *ctx;
363 ULONG_PTR ulKey = 0;
364 DWORD bytes;
365
366 ctx = (struct thread_ctx*)lpParameter;
367 wd = ctx->wd;
368
369 do {
66c098b8 370 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
67897036
BC
371 continue;
372
373 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
374 io_u = fov->io_u;
375
376 if (ovl->Internal == ERROR_SUCCESS) {
377 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
378 io_u->error = 0;
379 } else {
380 io_u->resid = io_u->xfer_buflen;
2277d5d5 381 io_u->error = win_to_posix_error(GetLastError());
67897036
BC
382 }
383
40c5db35 384 fov->io_complete = TRUE;
67897036
BC
385 SetEvent(wd->iocomplete_event);
386 } while (ctx->wd->iothread_running);
387
388 CloseHandle(ctx->iocp);
389 free(ctx);
390 return 0;
391}
392
c73ed246
JA
393static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
394{
395 struct fio_overlapped *o = io_u->engine_data;
396
397 if (o) {
c73ed246
JA
398 io_u->engine_data = NULL;
399 free(o);
400 }
401}
402
403static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
404{
405 struct fio_overlapped *o;
406
407 o = malloc(sizeof(*o));
b0106419 408 o->io_complete = FALSE;
c73ed246 409 o->io_u = io_u;
77e7b330 410 o->o.hEvent = NULL;
c73ed246
JA
411 io_u->engine_data = o;
412 return 0;
413}
414
ecc314ba
BC
415static struct ioengine_ops ioengine = {
416 .name = "windowsaio",
417 .version = FIO_IOOPS_VERSION,
418 .init = fio_windowsaio_init,
419 .queue = fio_windowsaio_queue,
ecc314ba
BC
420 .getevents = fio_windowsaio_getevents,
421 .event = fio_windowsaio_event,
422 .cleanup = fio_windowsaio_cleanup,
423 .open_file = fio_windowsaio_open_file,
424 .close_file = fio_windowsaio_close_file,
c73ed246
JA
425 .get_file_size = generic_get_file_size,
426 .io_u_init = fio_windowsaio_io_u_init,
427 .io_u_free = fio_windowsaio_io_u_free,
ecc314ba
BC
428};
429
c874d188 430static void fio_init fio_windowsaio_register(void)
ecc314ba
BC
431{
432 register_ioengine(&ioengine);
433}
434
c874d188 435static void fio_exit fio_windowsaio_unregister(void)
ecc314ba
BC
436{
437 unregister_ioengine(&ioengine);
438}