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 thread_data *td, 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)
45 * async engines aren't reliable with offload
47 if ((td->o.io_submit_mode == IO_MODE_OFFLOAD) &&
48 (ops->flags & FIO_NO_OFFLOAD)) {
49 log_err("%s: can't be used with offloaded submit. Use a sync "
50 "engine\n", ops->name);
54 if (!ops->event || !ops->getevents) {
55 log_err("%s: no event/getevents handler\n", ops->name);
62 void unregister_ioengine(struct ioengine_ops *ops)
64 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
65 flist_del_init(&ops->list);
68 void register_ioengine(struct ioengine_ops *ops)
70 dprint(FD_IO, "ioengine %s registered\n", ops->name);
71 flist_add_tail(&ops->list, &engine_list);
74 static struct ioengine_ops *find_ioengine(const char *name)
76 struct ioengine_ops *ops;
77 struct flist_head *entry;
79 flist_for_each(entry, &engine_list) {
80 ops = flist_entry(entry, struct ioengine_ops, list);
81 if (!strcmp(name, ops->name))
88 #ifdef CONFIG_DYNAMIC_ENGINES
89 static void *dlopen_external(struct thread_data *td, const char *engine)
91 char engine_path[PATH_MAX];
94 sprintf(engine_path, "%s/lib%s.so", FIO_EXT_ENG_DIR, engine);
96 dlhandle = dlopen(engine_path, RTLD_LAZY);
98 log_info("Engine %s not found; Either name is invalid, was not built, or fio-engine-%s package is missing.\n",
104 #define dlopen_external(td, engine) (NULL)
107 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
108 const char *engine_lib)
110 struct ioengine_ops *ops;
113 dprint(FD_IO, "dload engine %s\n", engine_lib);
116 dlhandle = dlopen(engine_lib, RTLD_LAZY);
118 dlhandle = dlopen_external(td, engine_lib);
120 td_vmsg(td, -1, dlerror(), "dlopen");
126 * Unlike the included modules, external engines should have a
127 * non-static ioengine structure that we can reference.
129 ops = dlsym(dlhandle, engine_lib);
131 ops = dlsym(dlhandle, "ioengine");
134 * For some external engines (like C++ ones) it is not that trivial
135 * to provide a non-static ionengine structure that we can reference.
136 * Instead we call a method which allocates the required ioengine
140 get_ioengine_t get_ioengine = dlsym(dlhandle, "get_ioengine");
147 td_vmsg(td, -1, dlerror(), "dlsym");
152 td->io_ops_dlhandle = dlhandle;
156 static struct ioengine_ops *__load_ioengine(const char *engine)
159 * linux libaio has alias names, so convert to what we want
161 if (!strncmp(engine, "linuxaio", 8)) {
162 dprint(FD_IO, "converting ioengine name: %s -> libaio\n",
167 dprint(FD_IO, "load ioengine %s\n", engine);
168 return find_ioengine(engine);
171 struct ioengine_ops *load_ioengine(struct thread_data *td)
173 struct ioengine_ops *ops = NULL;
177 * Use ->ioengine_so_path if an external ioengine path is specified.
178 * In this case, ->ioengine is "external" which also means the prefix
179 * for external ioengines "external:" is properly used.
181 name = td->o.ioengine_so_path ?: td->o.ioengine;
184 * Try to load ->ioengine first, and if failed try to dlopen(3) either
185 * ->ioengine or ->ioengine_so_path. This is redundant for an external
186 * ioengine with prefix, and also leaves the possibility of unexpected
187 * behavior (e.g. if the "external" ioengine exists), but we do this
188 * so as not to break job files not using the prefix.
190 ops = __load_ioengine(td->o.ioengine);
192 ops = dlopen_ioengine(td, name);
195 * If ops is NULL, we failed to load ->ioengine, and also failed to
196 * dlopen(3) either ->ioengine or ->ioengine_so_path as a path.
199 log_err("fio: engine %s not loadable\n", name);
204 * Check that the required methods are there.
206 if (check_engine_ops(td, ops))
213 * For cleaning up an ioengine which never made it to init().
215 void free_ioengine(struct thread_data *td)
217 dprint(FD_IO, "free ioengine %s\n", td->io_ops->name);
219 if (td->eo && td->io_ops->options) {
220 options_free(td->io_ops->options, td->eo);
225 if (td->io_ops_dlhandle) {
226 dlclose(td->io_ops_dlhandle);
227 td->io_ops_dlhandle = NULL;
233 void close_ioengine(struct thread_data *td)
235 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
237 if (td->io_ops->cleanup) {
238 td->io_ops->cleanup(td);
239 td->io_ops_data = NULL;
245 int td_io_prep(struct thread_data *td, struct io_u *io_u)
247 dprint_io_u(io_u, "prep");
248 fio_ro_check(td, io_u);
250 lock_file(td, io_u->file, io_u->ddir);
252 if (td->io_ops->prep) {
253 int ret = td->io_ops->prep(td, io_u);
255 dprint(FD_IO, "prep: io_u %p: ret=%d\n", io_u, ret);
258 unlock_file(td, io_u->file);
265 int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
266 const struct timespec *t)
271 * For ioengine=rdma one side operation RDMA_WRITE or RDMA_READ,
272 * server side gets a message from the client
273 * side that the task is finished, and
274 * td->done is set to 1 after td_io_commit(). In this case,
275 * there is no need to reap complete event in server side.
280 if (min > 0 && td->io_ops->commit) {
281 r = td->io_ops->commit(td);
285 if (max > td->cur_depth)
291 if (max && td->io_ops->getevents)
292 r = td->io_ops->getevents(td, min, max, t);
296 * Reflect that our submitted requests were retrieved with
297 * whatever OS async calls are in the underlying engine.
299 td->io_u_in_flight -= r;
300 io_u_mark_complete(td, r);
302 td_verror(td, r, "get_events");
304 dprint(FD_IO, "getevents: %d\n", r);
308 enum fio_q_status td_io_queue(struct thread_data *td, struct io_u *io_u)
310 const enum fio_ddir ddir = acct_ddir(io_u);
311 unsigned long long buflen = io_u->xfer_buflen;
312 enum fio_q_status ret;
314 dprint_io_u(io_u, "queue");
315 fio_ro_check(td, io_u);
317 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
318 io_u_set(td, io_u, IO_U_F_FLIGHT);
321 * If overlap checking was enabled in offload mode we
322 * can release this lock that was acquired when we
323 * started the overlap check because the IO_U_F_FLIGHT
326 if (td_offload_overlap(td)) {
327 int res = pthread_mutex_unlock(&overlap_check);
331 assert(fio_file_open(io_u->file));
334 * If using a write iolog, store this entry.
341 if (td_ioengine_flagged(td, FIO_SYNCIO) ||
342 (td_ioengine_flagged(td, FIO_ASYNCIO_SYNC_TRIM) &&
343 io_u->ddir == DDIR_TRIM)) {
344 if (fio_fill_issue_time(td))
345 fio_gettime(&io_u->issue_time, NULL);
348 * only used for iolog
350 if (td->o.read_iolog_file)
351 memcpy(&td->last_issue, &io_u->issue_time,
352 sizeof(io_u->issue_time));
357 if (!(io_u->flags & IO_U_F_VER_LIST)) {
358 td->io_issues[ddir]++;
359 td->io_issue_bytes[ddir] += buflen;
361 td->rate_io_issue_bytes[ddir] += buflen;
364 ret = td->io_ops->queue(td, io_u);
365 zbd_queue_io_u(td, io_u, ret);
367 unlock_file(td, io_u->file);
369 if (ret == FIO_Q_BUSY && ddir_rw(ddir)) {
370 td->io_issues[ddir]--;
371 td->io_issue_bytes[ddir] -= buflen;
372 td->rate_io_issue_bytes[ddir] -= buflen;
373 io_u_clear(td, io_u, IO_U_F_FLIGHT);
377 * If an error was seen and the io engine didn't propagate it
378 * back to 'td', do so.
380 if (io_u->error && !td->error)
381 td_verror(td, io_u->error, "td_io_queue");
384 * Add warning for O_DIRECT so that users have an easier time
385 * spotting potentially bad alignment. If this triggers for the first
386 * IO, then it's likely an alignment problem or because the host fs
387 * does not support O_DIRECT
389 if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 &&
392 log_info("fio: first direct IO errored. File system may not "
393 "support direct IO, or iomem_align= is bad, or "
394 "invalid block size. Try setting direct=0.\n");
397 if (zbd_unaligned_write(io_u->error) &&
398 td->io_issues[io_u->ddir & 1] == 1 &&
399 td->o.zone_mode != ZONE_MODE_ZBD) {
400 log_info("fio: first I/O failed. If %s is a zoned block device, consider --zonemode=zbd\n",
401 io_u->file->file_name);
404 if (!td->io_ops->commit) {
405 io_u_mark_submit(td, 1);
406 io_u_mark_complete(td, 1);
407 zbd_put_io_u(td, io_u);
410 if (ret == FIO_Q_COMPLETED) {
411 if (ddir_rw(io_u->ddir) ||
412 (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING)) {
413 io_u_mark_depth(td, 1);
414 td->ts.total_io_u[io_u->ddir]++;
416 } else if (ret == FIO_Q_QUEUED) {
419 if (ddir_rw(io_u->ddir) ||
420 (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING))
421 td->ts.total_io_u[io_u->ddir]++;
423 if (td->io_u_queued >= td->o.iodepth_batch)
427 if (!td_ioengine_flagged(td, FIO_SYNCIO) &&
428 (!td_ioengine_flagged(td, FIO_ASYNCIO_SYNC_TRIM) ||
429 io_u->ddir != DDIR_TRIM)) {
430 if (fio_fill_issue_time(td))
431 fio_gettime(&io_u->issue_time, NULL);
434 * only used for iolog
436 if (td->o.read_iolog_file)
437 memcpy(&td->last_issue, &io_u->issue_time,
438 sizeof(io_u->issue_time));
444 int td_io_init(struct thread_data *td)
448 if (td->io_ops->init) {
449 ret = td->io_ops->init(td);
451 log_err("fio: io engine %s init failed.%s\n",
454 " Perhaps try reducing io depth?" : "");
464 void td_io_commit(struct thread_data *td)
468 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
470 if (!td->cur_depth || !td->io_u_queued)
473 io_u_mark_depth(td, td->io_u_queued);
475 if (td->io_ops->commit) {
476 ret = td->io_ops->commit(td);
478 td_verror(td, -ret, "io commit");
482 * Reflect that events were submitted as async IO requests.
484 td->io_u_in_flight += td->io_u_queued;
488 int td_io_open_file(struct thread_data *td, struct fio_file *f)
490 if (fio_file_closing(f)) {
492 * Open translates to undo closing.
494 fio_file_clear_closing(f);
498 assert(!fio_file_open(f));
500 assert(td->io_ops->open_file);
502 if (td->io_ops->open_file(td, f)) {
503 if (td->error == EINVAL && td->o.odirect)
504 log_err("fio: destination does not support O_DIRECT\n");
505 if (td->error == EMFILE) {
506 log_err("fio: try reducing/setting openfiles (failed"
507 " at %u of %u)\n", td->nr_open_files,
512 assert(!fio_file_open(f));
516 fio_file_reset(td, f);
517 fio_file_set_open(f);
518 fio_file_clear_closing(f);
519 disk_util_inc(f->du);
524 if (f->filetype == FIO_TYPE_PIPE) {
526 log_err("fio: can't seek on pipes (no random io)\n");
531 if (td_ioengine_flagged(td, FIO_DISKLESSIO))
534 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
537 if (td->o.fadvise_hint != F_ADV_NONE &&
538 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
541 if (td->o.fadvise_hint == F_ADV_TYPE) {
543 flags = POSIX_FADV_RANDOM;
545 flags = POSIX_FADV_SEQUENTIAL;
546 } else if (td->o.fadvise_hint == F_ADV_RANDOM)
547 flags = POSIX_FADV_RANDOM;
548 else if (td->o.fadvise_hint == F_ADV_SEQUENTIAL)
549 flags = POSIX_FADV_SEQUENTIAL;
551 log_err("fio: unknown fadvise type %d\n",
553 flags = POSIX_FADV_NORMAL;
556 if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
557 if (!fio_did_warn(FIO_WARN_FADVISE))
558 log_err("fio: fadvise hint failed\n");
561 #ifdef FIO_HAVE_WRITE_HINT
562 if (fio_option_is_set(&td->o, write_hint) &&
563 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
564 uint64_t hint = td->o.write_hint;
568 * For direct IO, we just need/want to set the hint on
569 * the file descriptor. For buffered IO, we need to set
573 cmd = F_SET_FILE_RW_HINT;
577 if (fcntl(f->fd, cmd, &hint) < 0) {
578 td_verror(td, errno, "fcntl write hint");
584 if (td->o.odirect && !OS_O_DIRECT && fio_set_directio(td, f))
588 log_file(td, f, FIO_LOG_OPEN_FILE);
591 disk_util_dec(f->du);
592 if (td->io_ops->close_file)
593 td->io_ops->close_file(td, f);
597 int td_io_close_file(struct thread_data *td, struct fio_file *f)
599 if (!fio_file_closing(f))
600 log_file(td, f, FIO_LOG_CLOSE_FILE);
603 * mark as closing, do real close when last io on it has completed
605 fio_file_set_closing(f);
607 return put_file(td, f);
610 int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
612 if (td->io_ops->unlink_file)
613 return td->io_ops->unlink_file(td, f);
617 ret = unlink(f->file_name);
625 int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
627 if (!td->io_ops->get_file_size)
630 return td->io_ops->get_file_size(td, f);
633 int fio_show_ioengine_help(const char *engine)
635 struct flist_head *entry;
636 struct thread_data td;
637 struct ioengine_ops *io_ops;
641 if (!engine || !*engine) {
642 log_info("Available IO engines:\n");
643 flist_for_each(entry, &engine_list) {
644 io_ops = flist_entry(entry, struct ioengine_ops, list);
645 log_info("\t%s\n", io_ops->name);
649 sep = strchr(engine, ',');
655 memset(&td, 0, sizeof(struct thread_data));
656 td.o.ioengine = (char *)engine;
657 io_ops = load_ioengine(&td);
660 log_info("IO engine %s not found\n", engine);
665 ret = show_cmd_help(io_ops->options, sep);
667 log_info("IO engine %s has no options\n", io_ops->name);