add pmemblk engine
[fio.git] / ioengines.c
CommitLineData
ebac4655
JA
1/*
2 * The io parts of the fio tool, includes workers for sync and mmap'ed
3 * io, as well as both posix and linux libaio support.
4 *
5 * sync io is implemented on top of aio.
6 *
7 * This is not really specific to fio, if the get_io_u/put_io_u and
8 * structures was pulled into this as well it would be a perfectly
9 * generic io engine that could be used for other projects.
10 *
11 */
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
5c4e1dbc 15#include <string.h>
2866c82d 16#include <dlfcn.h>
ecc314ba 17#include <fcntl.h>
0c6e7517 18#include <assert.h>
8c16d840 19
ebac4655 20#include "fio.h"
7c9b1bce 21#include "diskutil.h"
ebac4655 22
01743ee1 23static FLIST_HEAD(engine_list);
5f350952 24
8c16d840
JA
25static int check_engine_ops(struct ioengine_ops *ops)
26{
5f350952 27 if (ops->version != FIO_IOOPS_VERSION) {
5ec10eaa
JA
28 log_err("bad ioops version %d (want %d)\n", ops->version,
29 FIO_IOOPS_VERSION);
5f350952
JA
30 return 1;
31 }
32
36167d82
JA
33 if (!ops->queue) {
34 log_err("%s: no queue handler\n", ops->name);
35 return 1;
36 }
37
38 /*
39 * sync engines only need a ->queue()
40 */
41 if (ops->flags & FIO_SYNCIO)
42 return 0;
5ec10eaa 43
8c16d840 44 if (!ops->event) {
36167d82 45 log_err("%s: no event handler\n", ops->name);
8c16d840
JA
46 return 1;
47 }
48 if (!ops->getevents) {
36167d82 49 log_err("%s: no getevents handler\n", ops->name);
8c16d840
JA
50 return 1;
51 }
5ec10eaa 52
8c16d840
JA
53 return 0;
54}
55
5f350952 56void unregister_ioengine(struct ioengine_ops *ops)
ebac4655 57{
ee56ad50 58 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
01743ee1
JA
59 flist_del(&ops->list);
60 INIT_FLIST_HEAD(&ops->list);
5f350952
JA
61}
62
b2fdda43 63void register_ioengine(struct ioengine_ops *ops)
5f350952 64{
ee56ad50 65 dprint(FD_IO, "ioengine %s registered\n", ops->name);
01743ee1
JA
66 INIT_FLIST_HEAD(&ops->list);
67 flist_add_tail(&ops->list, &engine_list);
5f350952
JA
68}
69
70static struct ioengine_ops *find_ioengine(const char *name)
71{
72 struct ioengine_ops *ops;
01743ee1 73 struct flist_head *entry;
ebac4655 74
01743ee1
JA
75 flist_for_each(entry, &engine_list) {
76 ops = flist_entry(entry, struct ioengine_ops, list);
bc5b77a8 77 if (!strcmp(name, ops->name))
5f350952
JA
78 return ops;
79 }
80
81 return NULL;
82}
83
84static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
85 const char *engine_lib)
86{
87 struct ioengine_ops *ops;
88 void *dlhandle;
89
ee56ad50
JA
90 dprint(FD_IO, "dload engine %s\n", engine_lib);
91
2866c82d
JA
92 dlerror();
93 dlhandle = dlopen(engine_lib, RTLD_LAZY);
d4dbaaa8 94 if (!dlhandle) {
e1161c32 95 td_vmsg(td, -1, dlerror(), "dlopen");
d4dbaaa8
JA
96 return NULL;
97 }
8756e4d4 98
da51c050
JA
99 /*
100 * Unlike the included modules, external engines should have a
101 * non-static ioengine structure that we can reference.
102 */
0abea0b7
DM
103 ops = dlsym(dlhandle, engine_lib);
104 if (!ops)
105 ops = dlsym(dlhandle, "ioengine");
a8075704
DG
106
107 /*
108 * For some external engines (like C++ ones) it is not that trivial
109 * to provide a non-static ionengine structure that we can reference.
110 * Instead we call a method which allocates the required ioengine
111 * structure.
112 */
113 if (!ops) {
114 get_ioengine_t get_ioengine = dlsym(dlhandle, "get_ioengine");
115
116 if (get_ioengine)
117 get_ioengine(&ops);
118 }
119
d4dbaaa8 120 if (!ops) {
e1161c32 121 td_vmsg(td, -1, dlerror(), "dlsym");
d4dbaaa8
JA
122 dlclose(dlhandle);
123 return NULL;
124 }
8756e4d4 125
5f350952
JA
126 ops->dlhandle = dlhandle;
127 return ops;
128}
129
130struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
131{
132 struct ioengine_ops *ops, *ret;
133 char engine[16];
134
ee56ad50
JA
135 dprint(FD_IO, "load ioengine %s\n", name);
136
5f350952
JA
137 strncpy(engine, name, sizeof(engine) - 1);
138
139 /*
140 * linux libaio has alias names, so convert to what we want
141 */
142 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
143 strcpy(engine, "libaio");
144
145 ops = find_ioengine(engine);
146 if (!ops)
147 ops = dlopen_ioengine(td, name);
148
149 if (!ops) {
150 log_err("fio: engine %s not loadable\n", name);
b902ceb5
JA
151 return NULL;
152 }
153
8c16d840
JA
154 /*
155 * Check that the required methods are there.
156 */
5f350952 157 if (check_engine_ops(ops))
8c16d840 158 return NULL;
8c16d840 159
84585003
JA
160 ret = malloc(sizeof(*ret));
161 memcpy(ret, ops, sizeof(*ret));
162 ret->data = NULL;
84585003
JA
163
164 return ret;
8756e4d4
JA
165}
166
de890a1e
SL
167/*
168 * For cleaning up an ioengine which never made it to init().
169 */
170void free_ioengine(struct thread_data *td)
8756e4d4 171{
de890a1e 172 dprint(FD_IO, "free ioengine %s\n", td->io_ops->name);
ee56ad50 173
de890a1e
SL
174 if (td->eo && td->io_ops->options) {
175 options_free(td->io_ops->options, td->eo);
176 free(td->eo);
177 td->eo = NULL;
2992b059 178 }
b990b5c0 179
5f350952
JA
180 if (td->io_ops->dlhandle)
181 dlclose(td->io_ops->dlhandle);
182
84585003
JA
183 free(td->io_ops);
184 td->io_ops = NULL;
b990b5c0 185}
10ba535a 186
de890a1e
SL
187void close_ioengine(struct thread_data *td)
188{
189 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
190
191 if (td->io_ops->cleanup) {
192 td->io_ops->cleanup(td);
193 td->io_ops->data = NULL;
194 }
195
196 free_ioengine(td);
197}
198
10ba535a
JA
199int td_io_prep(struct thread_data *td, struct io_u *io_u)
200{
ee56ad50 201 dprint_io_u(io_u, "prep");
7101d9c2
JA
202 fio_ro_check(td, io_u);
203
4d4e80f2 204 lock_file(td, io_u->file, io_u->ddir);
b2bd2bd9 205
2ba1c290
JA
206 if (td->io_ops->prep) {
207 int ret = td->io_ops->prep(td, io_u);
208
209 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
b2bd2bd9 210 if (ret)
4d4e80f2 211 unlock_file(td, io_u->file);
2ba1c290
JA
212 return ret;
213 }
10ba535a
JA
214
215 return 0;
216}
217
e7d2e616 218int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
1f440ece 219 const struct timespec *t)
10ba535a 220{
ee56ad50 221 int r = 0;
face81b2 222
a05d62b2
YR
223 /*
224 * For ioengine=rdma one side operation RDMA_WRITE or RDMA_READ,
225 * server side gets a message from the client
226 * side that the task is finished, and
227 * td->done is set to 1 after td_io_commit(). In this case,
228 * there is no need to reap complete event in server side.
229 */
230 if (td->done)
231 return 0;
232
ee56ad50
JA
233 if (min > 0 && td->io_ops->commit) {
234 r = td->io_ops->commit(td);
face81b2 235 if (r < 0)
ee56ad50 236 goto out;
face81b2 237 }
4950421a
JA
238 if (max > td->cur_depth)
239 max = td->cur_depth;
240 if (min > max)
241 max = min;
36167d82 242
ee56ad50 243 r = 0;
4950421a 244 if (max && td->io_ops->getevents)
ee56ad50
JA
245 r = td->io_ops->getevents(td, min, max, t);
246out:
422f9e4b
RM
247 if (r >= 0) {
248 /*
3fd9efbc 249 * Reflect that our submitted requests were retrieved with
422f9e4b
RM
250 * whatever OS async calls are in the underlying engine.
251 */
252 td->io_u_in_flight -= r;
838bc709 253 io_u_mark_complete(td, r);
422f9e4b 254 } else
7c639b14 255 td_verror(td, r, "get_events");
f3e11d05 256
ee56ad50
JA
257 dprint(FD_IO, "getevents: %d\n", r);
258 return r;
10ba535a
JA
259}
260
261int td_io_queue(struct thread_data *td, struct io_u *io_u)
262{
a9da8ab2
JA
263 const enum fio_ddir ddir = acct_ddir(io_u);
264 unsigned long buflen = io_u->xfer_buflen;
7e77dd02
JA
265 int ret;
266
ee56ad50 267 dprint_io_u(io_u, "queue");
7101d9c2
JA
268 fio_ro_check(td, io_u);
269
0c6e7517 270 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
a9da8ab2 271 io_u_set(io_u, IO_U_F_FLIGHT);
0c6e7517 272
d6aed795 273 assert(fio_file_open(io_u->file));
3d7b485f 274
bcd5abfa
JA
275 /*
276 * If using a write iolog, store this entry.
277 */
278 log_io_u(td, io_u);
279
11786802
JA
280 io_u->error = 0;
281 io_u->resid = 0;
282
433afcb4 283 if (td->io_ops->flags & FIO_SYNCIO) {
12d9d841 284 if (fio_fill_issue_time(td))
9520ebb9 285 fio_gettime(&io_u->issue_time, NULL);
d0c15328
JA
286
287 /*
288 * only used for iolog
289 */
290 if (td->o.read_iolog_file)
291 memcpy(&td->last_issue, &io_u->issue_time,
5ec10eaa 292 sizeof(struct timeval));
433afcb4
JA
293 }
294
a9da8ab2
JA
295 if (ddir_rw(ddir)) {
296 td->io_issues[ddir]++;
297 td->io_issue_bytes[ddir] += buflen;
50a8ce86 298 td->rate_io_issue_bytes[ddir] += buflen;
74d6277f 299 }
755200a3 300
7e77dd02 301 ret = td->io_ops->queue(td, io_u);
5aeb77df 302
4d4e80f2 303 unlock_file(td, io_u->file);
b2bd2bd9 304
a9da8ab2
JA
305 if (ret == FIO_Q_BUSY && ddir_rw(ddir)) {
306 td->io_issues[ddir]--;
307 td->io_issue_bytes[ddir] -= buflen;
50a8ce86 308 td->rate_io_issue_bytes[ddir] -= buflen;
cd8a19e6
JA
309 }
310
39a43d34
JA
311 /*
312 * If an error was seen and the io engine didn't propagate it
313 * back to 'td', do so.
314 */
315 if (io_u->error && !td->error)
316 td_verror(td, io_u->error, "td_io_queue");
317
cb211682
JA
318 /*
319 * Add warning for O_DIRECT so that users have an easier time
320 * spotting potentially bad alignment. If this triggers for the first
321 * IO, then it's likely an alignment problem or because the host fs
322 * does not support O_DIRECT
323 */
ff58fced 324 if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 &&
cb211682 325 td->o.odirect) {
214ac7e0 326
cb211682 327 log_info("fio: first direct IO errored. File system may not "
44cc9c8f
JA
328 "support direct IO, or iomem_align= is bad. Try "
329 "setting direct=0.\n");
cb211682
JA
330 }
331
77b451cc 332 if (!td->io_ops->commit || io_u->ddir == DDIR_TRIM) {
838bc709
JA
333 io_u_mark_submit(td, 1);
334 io_u_mark_complete(td, 1);
335 }
336
d8005759 337 if (ret == FIO_Q_COMPLETED) {
ff58fced 338 if (ddir_rw(io_u->ddir)) {
d8005759
JA
339 io_u_mark_depth(td, 1);
340 td->ts.total_io_u[io_u->ddir]++;
6eaf09d6 341 }
d8005759 342 } else if (ret == FIO_Q_QUEUED) {
eb7c8ae2
JA
343 int r;
344
e6727cbd
JA
345 td->io_u_queued++;
346
347 if (ddir_rw(io_u->ddir))
d8005759 348 td->ts.total_io_u[io_u->ddir]++;
d8005759
JA
349
350 if (td->io_u_queued >= td->o.iodepth_batch) {
eb7c8ae2
JA
351 r = td_io_commit(td);
352 if (r < 0)
353 return r;
354 }
355 }
cb5ab512 356
433afcb4 357 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
12d9d841 358 if (fio_fill_issue_time(td))
9520ebb9 359 fio_gettime(&io_u->issue_time, NULL);
d0c15328
JA
360
361 /*
362 * only used for iolog
363 */
364 if (td->o.read_iolog_file)
365 memcpy(&td->last_issue, &io_u->issue_time,
366 sizeof(struct timeval));
433afcb4
JA
367 }
368
7e77dd02 369 return ret;
10ba535a 370}
8c16d840
JA
371
372int td_io_init(struct thread_data *td)
373{
eeb12160 374 int ret = 0;
8c16d840 375
eeb12160
JA
376 if (td->io_ops->init) {
377 ret = td->io_ops->init(td);
5ec10eaa
JA
378 if (ret && td->o.iodepth > 1) {
379 log_err("fio: io engine init failed. Perhaps try"
380 " reducing io depth?\n");
381 }
7c973896
JA
382 if (!td->error)
383 td->error = ret;
eeb12160
JA
384 }
385
046395d7
JA
386 if (!ret && (td->io_ops->flags & FIO_NOIO))
387 td->flags |= TD_F_NOIO;
388
eeb12160 389 return ret;
8c16d840 390}
755200a3
JA
391
392int td_io_commit(struct thread_data *td)
393{
f3e11d05
JA
394 int ret;
395
ee56ad50
JA
396 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
397
d8005759 398 if (!td->cur_depth || !td->io_u_queued)
e1161c32 399 return 0;
cb5ab512 400
3fd9efbc 401 io_u_mark_depth(td, td->io_u_queued);
d8005759 402
f3e11d05
JA
403 if (td->io_ops->commit) {
404 ret = td->io_ops->commit(td);
405 if (ret)
406 td_verror(td, -ret, "io commit");
407 }
3fd9efbc 408
422f9e4b
RM
409 /*
410 * Reflect that events were submitted as async IO requests.
411 */
412 td->io_u_in_flight += td->io_u_queued;
413 td->io_u_queued = 0;
755200a3
JA
414
415 return 0;
416}
b5af8293
JA
417
418int td_io_open_file(struct thread_data *td, struct fio_file *f)
419{
22a57ba8
JA
420 assert(!fio_file_open(f));
421 assert(f->fd == -1);
422
413d6693
JA
423 if (td->io_ops->open_file(td, f)) {
424 if (td->error == EINVAL && td->o.odirect)
425 log_err("fio: destination does not support O_DIRECT\n");
5ec10eaa
JA
426 if (td->error == EMFILE) {
427 log_err("fio: try reducing/setting openfiles (failed"
428 " at %u of %u)\n", td->nr_open_files,
429 td->o.nr_files);
430 }
413d6693 431
22a57ba8
JA
432 assert(f->fd == -1);
433 assert(!fio_file_open(f));
413d6693
JA
434 return 1;
435 }
436
33c48814 437 fio_file_reset(td, f);
d6aed795
JA
438 fio_file_set_open(f);
439 fio_file_clear_closing(f);
c97bd0fa 440 disk_util_inc(f->du);
d5707a35
JA
441
442 td->nr_open_files++;
443 get_file(f);
444
66159828
JA
445 if (f->filetype == FIO_TYPE_PIPE) {
446 if (td_random(td)) {
447 log_err("fio: can't seek on pipes (no random io)\n");
448 goto err;
449 }
450 }
451
413d6693
JA
452 if (td->io_ops->flags & FIO_DISKLESSIO)
453 goto done;
454
455 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
456 goto err;
457
66159828
JA
458 if (td->o.fadvise_hint &&
459 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
413d6693
JA
460 int flags;
461
462 if (td_random(td))
463 flags = POSIX_FADV_RANDOM;
464 else
465 flags = POSIX_FADV_SEQUENTIAL;
466
ecc314ba 467 if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
413d6693
JA
468 td_verror(td, errno, "fadvise");
469 goto err;
470 }
7bb48f84 471 }
37659335
JA
472#ifdef FIO_HAVE_STREAMID
473 if (td->o.fadvise_stream &&
474 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
475 off_t stream = td->o.fadvise_stream;
476
477 if (posix_fadvise(f->fd, stream, f->io_size, POSIX_FADV_STREAMID) < 0) {
478 td_verror(td, errno, "fadvise streamid");
479 goto err;
480 }
481 }
482#endif
a978ba68 483
e116f2b9
JA
484#ifdef FIO_OS_DIRECTIO
485 /*
486 * Some OS's have a distinct call to mark the file non-buffered,
487 * instead of using O_DIRECT (Solaris)
488 */
489 if (td->o.odirect) {
490 int ret = fio_set_odirect(f->fd);
491
492 if (ret) {
493 td_verror(td, ret, "fio_set_odirect");
78e51c72 494 log_err("fio: the file system does not seem to support direct IO\n");
e116f2b9
JA
495 goto err;
496 }
497 }
498#endif
499
413d6693 500done:
f29b25a3 501 log_file(td, f, FIO_LOG_OPEN_FILE);
413d6693
JA
502 return 0;
503err:
c97bd0fa 504 disk_util_dec(f->du);
b284075a
JA
505 if (td->io_ops->close_file)
506 td->io_ops->close_file(td, f);
7bb48f84 507 return 1;
b5af8293
JA
508}
509
6977bcd0 510int td_io_close_file(struct thread_data *td, struct fio_file *f)
b5af8293 511{
d6aed795 512 if (!fio_file_closing(f))
f29b25a3
JA
513 log_file(td, f, FIO_LOG_CLOSE_FILE);
514
0ad920e7
JA
515 /*
516 * mark as closing, do real close when last io on it has completed
517 */
d6aed795 518 fio_file_set_closing(f);
0ad920e7 519
c97bd0fa 520 disk_util_dec(f->du);
cba5243b
JA
521
522 if (td->o.file_lock_mode != FILE_LOCK_NONE)
523 unlock_file_all(td, f);
29c1349f 524
6977bcd0 525 return put_file(td, f);
b5af8293 526}
df9c26b1 527
38ef9c90
CF
528int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
529{
530 if (td->io_ops->unlink_file)
531 return td->io_ops->unlink_file(td, f);
532 else
533 return unlink(f->file_name);
534}
535
df9c26b1
JA
536int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
537{
538 if (!td->io_ops->get_file_size)
539 return 0;
540
541 return td->io_ops->get_file_size(td, f);
542}
44f29692 543
effd6ff0
JA
544static int do_sync_file_range(const struct thread_data *td,
545 struct fio_file *f)
44f29692
JA
546{
547 off64_t offset, nbytes;
548
549 offset = f->first_write;
550 nbytes = f->last_write - f->first_write;
551
3843deb3
JA
552 if (!nbytes)
553 return 0;
44f29692 554
3843deb3 555 return sync_file_range(f->fd, offset, nbytes, td->o.sync_file_range);
44f29692 556}
0a28ecda 557
effd6ff0 558int do_io_u_sync(const struct thread_data *td, struct io_u *io_u)
0a28ecda
JA
559{
560 int ret;
561
562 if (io_u->ddir == DDIR_SYNC) {
563 ret = fsync(io_u->file->fd);
564 } else if (io_u->ddir == DDIR_DATASYNC) {
67bf9823 565#ifdef CONFIG_FDATASYNC
0a28ecda
JA
566 ret = fdatasync(io_u->file->fd);
567#else
568 ret = io_u->xfer_buflen;
569 io_u->error = EINVAL;
570#endif
571 } else if (io_u->ddir == DDIR_SYNC_FILE_RANGE)
572 ret = do_sync_file_range(td, io_u->file);
573 else {
574 ret = io_u->xfer_buflen;
575 io_u->error = EINVAL;
576 }
577
cb849a79
JA
578 if (ret < 0)
579 io_u->error = errno;
580
0a28ecda
JA
581 return ret;
582}
a5f3027c 583
effd6ff0 584int do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
a5f3027c
JA
585{
586#ifndef FIO_HAVE_TRIM
587 io_u->error = EINVAL;
ff58fced 588 return 0;
a5f3027c
JA
589#else
590 struct fio_file *f = io_u->file;
591 int ret;
592
c6404a44 593 ret = os_trim(f->fd, io_u->offset, io_u->xfer_buflen);
a5f3027c 594 if (!ret)
3fd9efbc 595 return io_u->xfer_buflen;
a5f3027c 596
ff58fced
JA
597 io_u->error = ret;
598 return 0;
a5f3027c
JA
599#endif
600}
de890a1e
SL
601
602int fio_show_ioengine_help(const char *engine)
603{
604 struct flist_head *entry;
605 struct thread_data td;
606 char *sep;
607 int ret = 1;
608
609 if (!engine || !*engine) {
610 log_info("Available IO engines:\n");
611 flist_for_each(entry, &engine_list) {
612 td.io_ops = flist_entry(entry, struct ioengine_ops,
613 list);
614 log_info("\t%s\n", td.io_ops->name);
615 }
616 return 0;
617 }
618 sep = strchr(engine, ',');
619 if (sep) {
620 *sep = 0;
621 sep++;
622 }
623
624 memset(&td, 0, sizeof(td));
625
626 td.io_ops = load_ioengine(&td, engine);
627 if (!td.io_ops) {
628 log_info("IO engine %s not found\n", engine);
629 return 1;
630 }
631
632 if (td.io_ops->options)
633 ret = show_cmd_help(td.io_ops->options, sep);
634 else
635 log_info("IO engine %s has no options\n", td.io_ops->name);
636
637 free_ioengine(&td);
638
639 return ret;
640}