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.
18 #include <sys/types.h>
26 static FLIST_HEAD(engine_list);
28 static inline bool async_ioengine_sync_trim(struct thread_data *td,
31 return td_ioengine_flagged(td, FIO_ASYNCIO_SYNC_TRIM) &&
32 io_u->ddir == DDIR_TRIM;
35 static bool check_engine_ops(struct thread_data *td, struct ioengine_ops *ops)
37 if (ops->version != FIO_IOOPS_VERSION) {
38 log_err("bad ioops version %d (want %d)\n", ops->version,
44 log_err("%s: no queue handler\n", ops->name);
49 * sync engines only need a ->queue()
51 if (ops->flags & FIO_SYNCIO)
55 * async engines aren't reliable with offload
57 if ((td->o.io_submit_mode == IO_MODE_OFFLOAD) &&
58 (ops->flags & FIO_NO_OFFLOAD)) {
59 log_err("%s: can't be used with offloaded submit. Use a sync "
60 "engine\n", ops->name);
64 if (!ops->event || !ops->getevents) {
65 log_err("%s: no event/getevents handler\n", ops->name);
72 void unregister_ioengine(struct ioengine_ops *ops)
74 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
75 flist_del_init(&ops->list);
78 void register_ioengine(struct ioengine_ops *ops)
80 dprint(FD_IO, "ioengine %s registered\n", ops->name);
81 flist_add_tail(&ops->list, &engine_list);
84 static struct ioengine_ops *find_ioengine(const char *name)
86 struct ioengine_ops *ops;
87 struct flist_head *entry;
89 flist_for_each(entry, &engine_list) {
90 ops = flist_entry(entry, struct ioengine_ops, list);
91 if (!strcmp(name, ops->name))
98 #ifdef CONFIG_DYNAMIC_ENGINES
99 static void *dlopen_external(struct thread_data *td, const char *engine)
101 char engine_path[PATH_MAX];
104 sprintf(engine_path, "%s/fio-%s.so", FIO_EXT_ENG_DIR, engine);
106 dprint(FD_IO, "dlopen external %s\n", engine_path);
107 dlhandle = dlopen(engine_path, RTLD_LAZY);
109 log_info("Engine %s not found; Either name is invalid, was not built, or fio-engine-%s package is missing.\n",
115 #define dlopen_external(td, engine) (NULL)
118 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
119 const char *engine_lib)
121 struct ioengine_ops *ops;
124 if (!strncmp(engine_lib, "linuxaio", 8) ||
125 !strncmp(engine_lib, "aio", 3))
126 engine_lib = "libaio";
128 dprint(FD_IO, "dlopen engine %s\n", engine_lib);
131 dlhandle = dlopen(engine_lib, RTLD_LAZY);
133 dlhandle = dlopen_external(td, engine_lib);
135 td_vmsg(td, -1, dlerror(), "dlopen");
141 * Unlike the included modules, external engines should have a
142 * non-static ioengine structure that we can reference.
144 ops = dlsym(dlhandle, engine_lib);
146 ops = dlsym(dlhandle, "ioengine");
149 * For some external engines (like C++ ones) it is not that trivial
150 * to provide a non-static ionengine structure that we can reference.
151 * Instead we call a method which allocates the required ioengine
155 get_ioengine_t get_ioengine = dlsym(dlhandle, "get_ioengine");
162 td_vmsg(td, -1, dlerror(), "dlsym");
167 ops->dlhandle = dlhandle;
171 static struct ioengine_ops *__load_ioengine(const char *engine)
174 * linux libaio has alias names, so convert to what we want
176 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3)) {
177 dprint(FD_IO, "converting ioengine name: %s -> libaio\n",
182 dprint(FD_IO, "load ioengine %s\n", engine);
183 return find_ioengine(engine);
186 struct ioengine_ops *load_ioengine(struct thread_data *td)
188 struct ioengine_ops *ops = NULL;
192 * Use ->ioengine_so_path if an external ioengine path is specified.
193 * In this case, ->ioengine is "external" which also means the prefix
194 * for external ioengines "external:" is properly used.
196 name = td->o.ioengine_so_path ?: td->o.ioengine;
199 * Try to load ->ioengine first, and if failed try to dlopen(3) either
200 * ->ioengine or ->ioengine_so_path. This is redundant for an external
201 * ioengine with prefix, and also leaves the possibility of unexpected
202 * behavior (e.g. if the "external" ioengine exists), but we do this
203 * so as not to break job files not using the prefix.
205 ops = __load_ioengine(td->o.ioengine);
207 /* We do re-dlopen existing handles, for reference counting */
208 if (!ops || ops->dlhandle)
209 ops = dlopen_ioengine(td, name);
212 * If ops is NULL, we failed to load ->ioengine, and also failed to
213 * dlopen(3) either ->ioengine or ->ioengine_so_path as a path.
216 log_err("fio: engine %s not loadable\n", name);
221 * Check that the required methods are there.
223 if (check_engine_ops(td, ops))
230 * For cleaning up an ioengine which never made it to init().
232 void free_ioengine(struct thread_data *td)
234 assert(td != NULL && td->io_ops != NULL);
236 dprint(FD_IO, "free ioengine %s\n", td->io_ops->name);
238 if (td->eo && td->io_ops->options) {
239 options_free(td->io_ops->options, td->eo);
241 if (td->o.use_thread)
245 if (td->io_ops->dlhandle) {
246 dprint(FD_IO, "dlclose ioengine %s\n", td->io_ops->name);
247 dlclose(td->io_ops->dlhandle);
253 void close_ioengine(struct thread_data *td)
255 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
257 if (td->io_ops->cleanup) {
258 td->io_ops->cleanup(td);
259 td->io_ops_data = NULL;
265 int td_io_prep(struct thread_data *td, struct io_u *io_u)
267 dprint_io_u(io_u, "prep");
268 fio_ro_check(td, io_u);
270 lock_file(td, io_u->file, io_u->ddir);
272 if (td->io_ops->prep) {
273 int ret = td->io_ops->prep(td, io_u);
275 dprint(FD_IO, "prep: io_u %p: ret=%d\n", io_u, ret);
278 unlock_file(td, io_u->file);
285 int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
286 const struct timespec *t)
291 * For ioengine=rdma one side operation RDMA_WRITE or RDMA_READ,
292 * server side gets a message from the client
293 * side that the task is finished, and
294 * td->done is set to 1 after td_io_commit(). In this case,
295 * there is no need to reap complete event in server side.
300 if (min > 0 && td->io_ops->commit) {
301 r = td->io_ops->commit(td);
305 if (max > td->cur_depth)
311 if (max && td->io_ops->getevents)
312 r = td->io_ops->getevents(td, min, max, t);
316 * Reflect that our submitted requests were retrieved with
317 * whatever OS async calls are in the underlying engine.
319 td->io_u_in_flight -= r;
320 io_u_mark_complete(td, r);
322 td_verror(td, r, "get_events");
324 dprint(FD_IO, "getevents: %d\n", r);
328 enum fio_q_status td_io_queue(struct thread_data *td, struct io_u *io_u)
330 const enum fio_ddir ddir = acct_ddir(io_u);
331 unsigned long long buflen = io_u->xfer_buflen;
332 enum fio_q_status ret;
334 dprint_io_u(io_u, "queue");
335 fio_ro_check(td, io_u);
337 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
338 io_u_set(td, io_u, IO_U_F_FLIGHT);
341 * If overlap checking was enabled in offload mode we
342 * can release this lock that was acquired when we
343 * started the overlap check because the IO_U_F_FLIGHT
346 if (td_offload_overlap(td)) {
349 res = pthread_mutex_unlock(&overlap_check);
350 if (fio_unlikely(res != 0)) {
351 log_err("failed to unlock overlap check mutex, err: %i:%s", errno, strerror(errno));
356 assert(fio_file_open(io_u->file));
359 * If using a write iolog, store this entry.
366 if (td_ioengine_flagged(td, FIO_SYNCIO) ||
367 async_ioengine_sync_trim(td, io_u)) {
368 if (fio_fill_issue_time(td)) {
369 fio_gettime(&io_u->issue_time, NULL);
372 * only used for iolog
374 if (td->o.read_iolog_file)
375 memcpy(&td->last_issue, &io_u->issue_time,
376 sizeof(io_u->issue_time));
382 if (!(io_u->flags & IO_U_F_VER_LIST)) {
383 td->io_issues[ddir]++;
384 td->io_issue_bytes[ddir] += buflen;
386 td->rate_io_issue_bytes[ddir] += buflen;
389 ret = td->io_ops->queue(td, io_u);
390 zbd_queue_io_u(td, io_u, ret);
392 unlock_file(td, io_u->file);
394 if (ret == FIO_Q_BUSY && ddir_rw(ddir)) {
395 td->io_issues[ddir]--;
396 td->io_issue_bytes[ddir] -= buflen;
397 td->rate_io_issue_bytes[ddir] -= buflen;
398 io_u_clear(td, io_u, IO_U_F_FLIGHT);
402 * If an error was seen and the io engine didn't propagate it
403 * back to 'td', do so.
405 if (io_u->error && !td->error)
406 td_verror(td, io_u->error, "td_io_queue");
409 * Add warning for O_DIRECT so that users have an easier time
410 * spotting potentially bad alignment. If this triggers for the first
411 * IO, then it's likely an alignment problem or because the host fs
412 * does not support O_DIRECT
414 if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 &&
417 log_info("fio: first direct IO errored. File system may not "
418 "support direct IO, or iomem_align= is bad, or "
419 "invalid block size. Try setting direct=0.\n");
422 if (zbd_unaligned_write(io_u->error) &&
423 td->io_issues[io_u->ddir & 1] == 1 &&
424 td->o.zone_mode != ZONE_MODE_ZBD) {
425 log_info("fio: first I/O failed. If %s is a zoned block device, consider --zonemode=zbd\n",
426 io_u->file->file_name);
429 if (!td->io_ops->commit) {
430 io_u_mark_submit(td, 1);
431 io_u_mark_complete(td, 1);
434 if (ret == FIO_Q_COMPLETED) {
435 if (ddir_rw(io_u->ddir) ||
436 (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING)) {
437 io_u_mark_depth(td, 1);
438 td->ts.total_io_u[io_u->ddir]++;
440 } else if (ret == FIO_Q_QUEUED) {
443 if (ddir_rw(io_u->ddir) ||
444 (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING))
445 td->ts.total_io_u[io_u->ddir]++;
447 if (td->io_u_queued >= td->o.iodepth_batch)
451 if (!td_ioengine_flagged(td, FIO_SYNCIO) &&
452 !async_ioengine_sync_trim(td, io_u)) {
453 if (fio_fill_issue_time(td) &&
454 !td_ioengine_flagged(td, FIO_ASYNCIO_SETS_ISSUE_TIME)) {
455 fio_gettime(&io_u->issue_time, NULL);
458 * only used for iolog
460 if (td->o.read_iolog_file)
461 memcpy(&td->last_issue, &io_u->issue_time,
462 sizeof(io_u->issue_time));
469 int td_io_init(struct thread_data *td)
473 if (td->io_ops->init) {
474 ret = td->io_ops->init(td);
476 log_err("fio: io engine %s init failed.%s\n",
479 " Perhaps try reducing io depth?" : "");
489 void td_io_commit(struct thread_data *td)
493 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
495 if (!td->cur_depth || !td->io_u_queued)
498 io_u_mark_depth(td, td->io_u_queued);
500 if (td->io_ops->commit) {
501 ret = td->io_ops->commit(td);
503 td_verror(td, -ret, "io commit");
507 * Reflect that events were submitted as async IO requests.
509 td->io_u_in_flight += td->io_u_queued;
513 int td_io_open_file(struct thread_data *td, struct fio_file *f)
515 if (fio_file_closing(f)) {
517 * Open translates to undo closing.
519 fio_file_clear_closing(f);
523 assert(!fio_file_open(f));
525 assert(td->io_ops->open_file);
527 if (td->io_ops->open_file(td, f)) {
528 if (td->error == EINVAL && td->o.odirect)
529 log_err("fio: destination does not support O_DIRECT\n");
530 if (td->error == EMFILE) {
531 log_err("fio: try reducing/setting openfiles (failed"
532 " at %u of %u)\n", td->nr_open_files,
537 assert(!fio_file_open(f));
541 fio_file_reset(td, f);
542 fio_file_set_open(f);
543 fio_file_clear_closing(f);
544 disk_util_inc(f->du);
549 if (f->filetype == FIO_TYPE_PIPE) {
551 log_err("fio: can't seek on pipes (no random io)\n");
556 if (td_ioengine_flagged(td, FIO_DISKLESSIO))
559 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
562 if (td->o.fadvise_hint != F_ADV_NONE &&
563 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
566 if (td->o.fadvise_hint == F_ADV_TYPE) {
568 flags = POSIX_FADV_RANDOM;
570 flags = POSIX_FADV_SEQUENTIAL;
571 } else if (td->o.fadvise_hint == F_ADV_RANDOM)
572 flags = POSIX_FADV_RANDOM;
573 else if (td->o.fadvise_hint == F_ADV_SEQUENTIAL)
574 flags = POSIX_FADV_SEQUENTIAL;
575 #ifdef POSIX_FADV_NOREUSE
576 else if (td->o.fadvise_hint == F_ADV_NOREUSE)
577 flags = POSIX_FADV_NOREUSE;
580 log_err("fio: unknown fadvise type %d\n",
582 flags = POSIX_FADV_NORMAL;
585 if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
586 if (!fio_did_warn(FIO_WARN_FADVISE))
587 log_err("fio: fadvise hint failed\n");
590 #ifdef FIO_HAVE_WRITE_HINT
591 if (fio_option_is_set(&td->o, write_hint) &&
592 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
593 uint64_t hint = td->o.write_hint;
597 * For direct IO, we just need/want to set the hint on
598 * the file descriptor. For buffered IO, we need to set
602 cmd = F_SET_FILE_RW_HINT;
606 if (fcntl(f->fd, cmd, &hint) < 0) {
607 td_verror(td, errno, "fcntl write hint");
613 if (td->o.odirect && !OS_O_DIRECT && fio_set_directio(td, f))
617 log_file(td, f, FIO_LOG_OPEN_FILE);
620 disk_util_dec(f->du);
621 if (td->io_ops->close_file)
622 td->io_ops->close_file(td, f);
626 int td_io_close_file(struct thread_data *td, struct fio_file *f)
628 if (!fio_file_closing(f))
629 log_file(td, f, FIO_LOG_CLOSE_FILE);
632 * mark as closing, do real close when last io on it has completed
634 fio_file_set_closing(f);
636 return put_file(td, f);
639 int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
641 if (td->io_ops->unlink_file)
642 return td->io_ops->unlink_file(td, f);
646 ret = unlink(f->file_name);
654 int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
656 if (!td->io_ops->get_file_size)
659 return td->io_ops->get_file_size(td, f);
662 #ifdef CONFIG_DYNAMIC_ENGINES
663 /* Load all dynamic engines in FIO_EXT_ENG_DIR for enghelp command */
665 fio_load_dynamic_engines(struct thread_data *td)
667 DIR *dirhandle = NULL;
668 struct dirent *dirent = NULL;
669 char engine_path[PATH_MAX];
671 dirhandle = opendir(FIO_EXT_ENG_DIR);
675 while ((dirent = readdir(dirhandle)) != NULL) {
676 if (!strcmp(dirent->d_name, ".") ||
677 !strcmp(dirent->d_name, ".."))
680 sprintf(engine_path, "%s/%s", FIO_EXT_ENG_DIR, dirent->d_name);
681 dlopen_ioengine(td, engine_path);
687 #define fio_load_dynamic_engines(td) do { } while (0)
690 int fio_show_ioengine_help(const char *engine)
692 struct flist_head *entry;
693 struct thread_data td;
694 struct ioengine_ops *io_ops;
698 memset(&td, 0, sizeof(struct thread_data));
700 if (!engine || !*engine) {
701 log_info("Available IO engines:\n");
702 fio_load_dynamic_engines(&td);
703 flist_for_each(entry, &engine_list) {
704 io_ops = flist_entry(entry, struct ioengine_ops, list);
705 log_info("\t%s\n", io_ops->name);
709 sep = strchr(engine, ',');
715 td.o.ioengine = (char *)engine;
716 td.io_ops = load_ioengine(&td);
719 log_info("IO engine %s not found\n", engine);
723 if (td.io_ops->options)
724 ret = show_cmd_help(td.io_ops->options, sep);
726 log_info("IO engine %s has no options\n", td.io_ops->name);