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.
5 * sync io is implemented on top of aio.
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.
23 static FLIST_HEAD(engine_list);
25 static bool check_engine_ops(struct ioengine_ops *ops)
27 if (ops->version != FIO_IOOPS_VERSION) {
28 log_err("bad ioops version %d (want %d)\n", ops->version,
34 log_err("%s: no queue handler\n", ops->name);
39 * sync engines only need a ->queue()
41 if (ops->flags & FIO_SYNCIO)
44 if (!ops->event || !ops->getevents) {
45 log_err("%s: no event/getevents handler\n", ops->name);
52 void unregister_ioengine(struct ioengine_ops *ops)
54 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
55 flist_del(&ops->list);
56 INIT_FLIST_HEAD(&ops->list);
59 void register_ioengine(struct ioengine_ops *ops)
61 dprint(FD_IO, "ioengine %s registered\n", ops->name);
62 INIT_FLIST_HEAD(&ops->list);
63 flist_add_tail(&ops->list, &engine_list);
66 static struct ioengine_ops *find_ioengine(const char *name)
68 struct ioengine_ops *ops;
69 struct flist_head *entry;
71 flist_for_each(entry, &engine_list) {
72 ops = flist_entry(entry, struct ioengine_ops, list);
73 if (!strcmp(name, ops->name))
80 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
81 const char *engine_lib)
83 struct ioengine_ops *ops;
86 dprint(FD_IO, "dload engine %s\n", engine_lib);
89 dlhandle = dlopen(engine_lib, RTLD_LAZY);
91 td_vmsg(td, -1, dlerror(), "dlopen");
96 * Unlike the included modules, external engines should have a
97 * non-static ioengine structure that we can reference.
99 ops = dlsym(dlhandle, engine_lib);
101 ops = dlsym(dlhandle, "ioengine");
104 * For some external engines (like C++ ones) it is not that trivial
105 * to provide a non-static ionengine structure that we can reference.
106 * Instead we call a method which allocates the required ioengine
110 get_ioengine_t get_ioengine = dlsym(dlhandle, "get_ioengine");
117 td_vmsg(td, -1, dlerror(), "dlsym");
122 td->io_ops_dlhandle = dlhandle;
126 static struct ioengine_ops *__load_ioengine(const char *name)
130 engine[sizeof(engine) - 1] = '\0';
131 strncpy(engine, name, sizeof(engine) - 1);
134 * linux libaio has alias names, so convert to what we want
136 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
137 strcpy(engine, "libaio");
139 dprint(FD_IO, "load ioengine %s\n", engine);
140 return find_ioengine(engine);
143 struct ioengine_ops *load_ioengine(struct thread_data *td)
145 struct ioengine_ops *ops = NULL;
146 const char *name = NULL;
148 if (strcmp(td->o.ioengine, "external")) {
149 name = td->o.ioengine;
150 ops = __load_ioengine(name);
151 } else if (td->o.ioengine_so_path) {
152 name = td->o.ioengine_so_path;
153 ops = dlopen_ioengine(td, name);
155 log_err("fio: missing external ioengine path\n");
158 log_err("fio: engine %s not loadable\n", name);
163 * Check that the required methods are there.
165 if (check_engine_ops(ops))
172 * For cleaning up an ioengine which never made it to init().
174 void free_ioengine(struct thread_data *td)
176 dprint(FD_IO, "free ioengine %s\n", td->io_ops->name);
178 if (td->eo && td->io_ops->options) {
179 options_free(td->io_ops->options, td->eo);
184 if (td->io_ops_dlhandle)
185 dlclose(td->io_ops_dlhandle);
190 void close_ioengine(struct thread_data *td)
192 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
194 if (td->io_ops->cleanup) {
195 td->io_ops->cleanup(td);
196 td->io_ops_data = NULL;
202 int td_io_prep(struct thread_data *td, struct io_u *io_u)
204 dprint_io_u(io_u, "prep");
205 fio_ro_check(td, io_u);
207 lock_file(td, io_u->file, io_u->ddir);
209 if (td->io_ops->prep) {
210 int ret = td->io_ops->prep(td, io_u);
212 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
214 unlock_file(td, io_u->file);
221 int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
222 const struct timespec *t)
227 * For ioengine=rdma one side operation RDMA_WRITE or RDMA_READ,
228 * server side gets a message from the client
229 * side that the task is finished, and
230 * td->done is set to 1 after td_io_commit(). In this case,
231 * there is no need to reap complete event in server side.
236 if (min > 0 && td->io_ops->commit) {
237 r = td->io_ops->commit(td);
241 if (max > td->cur_depth)
247 if (max && td->io_ops->getevents)
248 r = td->io_ops->getevents(td, min, max, t);
252 * Reflect that our submitted requests were retrieved with
253 * whatever OS async calls are in the underlying engine.
255 td->io_u_in_flight -= r;
256 io_u_mark_complete(td, r);
258 td_verror(td, r, "get_events");
260 dprint(FD_IO, "getevents: %d\n", r);
264 int td_io_queue(struct thread_data *td, struct io_u *io_u)
266 const enum fio_ddir ddir = acct_ddir(io_u);
267 unsigned long buflen = io_u->xfer_buflen;
270 dprint_io_u(io_u, "queue");
271 fio_ro_check(td, io_u);
273 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
274 io_u_set(td, io_u, IO_U_F_FLIGHT);
276 assert(fio_file_open(io_u->file));
279 * If using a write iolog, store this entry.
286 if (td_ioengine_flagged(td, FIO_SYNCIO)) {
287 if (fio_fill_issue_time(td))
288 fio_gettime(&io_u->issue_time, NULL);
291 * only used for iolog
293 if (td->o.read_iolog_file)
294 memcpy(&td->last_issue, &io_u->issue_time,
295 sizeof(io_u->issue_time));
299 td->io_issues[ddir]++;
300 td->io_issue_bytes[ddir] += buflen;
301 td->rate_io_issue_bytes[ddir] += buflen;
304 ret = td->io_ops->queue(td, io_u);
306 unlock_file(td, io_u->file);
308 if (ret == FIO_Q_BUSY && ddir_rw(ddir)) {
309 td->io_issues[ddir]--;
310 td->io_issue_bytes[ddir] -= buflen;
311 td->rate_io_issue_bytes[ddir] -= buflen;
312 io_u_clear(td, io_u, IO_U_F_FLIGHT);
316 * If an error was seen and the io engine didn't propagate it
317 * back to 'td', do so.
319 if (io_u->error && !td->error)
320 td_verror(td, io_u->error, "td_io_queue");
323 * Add warning for O_DIRECT so that users have an easier time
324 * spotting potentially bad alignment. If this triggers for the first
325 * IO, then it's likely an alignment problem or because the host fs
326 * does not support O_DIRECT
328 if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 &&
331 log_info("fio: first direct IO errored. File system may not "
332 "support direct IO, or iomem_align= is bad. Try "
333 "setting direct=0.\n");
336 if (!td->io_ops->commit || io_u->ddir == DDIR_TRIM) {
337 io_u_mark_submit(td, 1);
338 io_u_mark_complete(td, 1);
341 if (ret == FIO_Q_COMPLETED) {
342 if (ddir_rw(io_u->ddir)) {
343 io_u_mark_depth(td, 1);
344 td->ts.total_io_u[io_u->ddir]++;
346 } else if (ret == FIO_Q_QUEUED) {
351 if (ddir_rw(io_u->ddir))
352 td->ts.total_io_u[io_u->ddir]++;
354 if (td->io_u_queued >= td->o.iodepth_batch) {
355 r = td_io_commit(td);
361 if (!td_ioengine_flagged(td, FIO_SYNCIO)) {
362 if (fio_fill_issue_time(td))
363 fio_gettime(&io_u->issue_time, NULL);
366 * only used for iolog
368 if (td->o.read_iolog_file)
369 memcpy(&td->last_issue, &io_u->issue_time,
370 sizeof(io_u->issue_time));
376 int td_io_init(struct thread_data *td)
380 if (td->io_ops->init) {
381 ret = td->io_ops->init(td);
383 log_err("fio: io engine %s init failed.%s\n",
386 " Perhaps try reducing io depth?" : "");
396 int td_io_commit(struct thread_data *td)
400 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
402 if (!td->cur_depth || !td->io_u_queued)
405 io_u_mark_depth(td, td->io_u_queued);
407 if (td->io_ops->commit) {
408 ret = td->io_ops->commit(td);
410 td_verror(td, -ret, "io commit");
414 * Reflect that events were submitted as async IO requests.
416 td->io_u_in_flight += td->io_u_queued;
422 int td_io_open_file(struct thread_data *td, struct fio_file *f)
424 assert(!fio_file_open(f));
427 if (td->io_ops->open_file(td, f)) {
428 if (td->error == EINVAL && td->o.odirect)
429 log_err("fio: destination does not support O_DIRECT\n");
430 if (td->error == EMFILE) {
431 log_err("fio: try reducing/setting openfiles (failed"
432 " at %u of %u)\n", td->nr_open_files,
437 assert(!fio_file_open(f));
441 fio_file_reset(td, f);
442 fio_file_set_open(f);
443 fio_file_clear_closing(f);
444 disk_util_inc(f->du);
449 if (f->filetype == FIO_TYPE_PIPE) {
451 log_err("fio: can't seek on pipes (no random io)\n");
456 if (td_ioengine_flagged(td, FIO_DISKLESSIO))
459 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
462 if (td->o.fadvise_hint != F_ADV_NONE &&
463 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
466 if (td->o.fadvise_hint == F_ADV_TYPE) {
468 flags = POSIX_FADV_RANDOM;
470 flags = POSIX_FADV_SEQUENTIAL;
471 } else if (td->o.fadvise_hint == F_ADV_RANDOM)
472 flags = POSIX_FADV_RANDOM;
473 else if (td->o.fadvise_hint == F_ADV_SEQUENTIAL)
474 flags = POSIX_FADV_SEQUENTIAL;
476 log_err("fio: unknown fadvise type %d\n",
478 flags = POSIX_FADV_NORMAL;
481 if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
482 td_verror(td, errno, "fadvise");
486 #ifdef FIO_HAVE_WRITE_HINT
487 if (fio_option_is_set(&td->o, write_hint) &&
488 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
489 uint64_t hint = td->o.write_hint;
493 * For direct IO, we just need/want to set the hint on
494 * the file descriptor. For buffered IO, we need to set
498 cmd = F_SET_FILE_RW_HINT;
502 if (fcntl(f->fd, cmd, &hint) < 0) {
503 td_verror(td, errno, "fcntl write hint");
509 if (td->o.odirect && !OS_O_DIRECT && fio_set_directio(td, f))
513 log_file(td, f, FIO_LOG_OPEN_FILE);
516 disk_util_dec(f->du);
517 if (td->io_ops->close_file)
518 td->io_ops->close_file(td, f);
522 int td_io_close_file(struct thread_data *td, struct fio_file *f)
524 if (!fio_file_closing(f))
525 log_file(td, f, FIO_LOG_CLOSE_FILE);
528 * mark as closing, do real close when last io on it has completed
530 fio_file_set_closing(f);
532 disk_util_dec(f->du);
534 if (td->o.file_lock_mode != FILE_LOCK_NONE)
535 unlock_file_all(td, f);
537 return put_file(td, f);
540 int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
542 if (td->io_ops->unlink_file)
543 return td->io_ops->unlink_file(td, f);
547 ret = unlink(f->file_name);
555 int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
557 if (!td->io_ops->get_file_size)
560 return td->io_ops->get_file_size(td, f);
563 int fio_show_ioengine_help(const char *engine)
565 struct flist_head *entry;
566 struct ioengine_ops *io_ops;
570 if (!engine || !*engine) {
571 log_info("Available IO engines:\n");
572 flist_for_each(entry, &engine_list) {
573 io_ops = flist_entry(entry, struct ioengine_ops, list);
574 log_info("\t%s\n", io_ops->name);
578 sep = strchr(engine, ',');
584 io_ops = __load_ioengine(engine);
586 log_info("IO engine %s not found\n", engine);
591 ret = show_cmd_help(io_ops->options, sep);
593 log_info("IO engine %s has no options\n", io_ops->name);