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 int 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)
45 log_err("%s: no event handler\n", ops->name);
48 if (!ops->getevents) {
49 log_err("%s: no getevents handler\n", ops->name);
56 void unregister_ioengine(struct ioengine_ops *ops)
58 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
59 flist_del(&ops->list);
60 INIT_FLIST_HEAD(&ops->list);
63 void register_ioengine(struct ioengine_ops *ops)
65 dprint(FD_IO, "ioengine %s registered\n", ops->name);
66 INIT_FLIST_HEAD(&ops->list);
67 flist_add_tail(&ops->list, &engine_list);
70 static struct ioengine_ops *find_ioengine(const char *name)
72 struct ioengine_ops *ops;
73 struct flist_head *entry;
75 flist_for_each(entry, &engine_list) {
76 ops = flist_entry(entry, struct ioengine_ops, list);
77 if (!strcmp(name, ops->name))
84 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
85 const char *engine_lib)
87 struct ioengine_ops *ops;
90 dprint(FD_IO, "dload engine %s\n", engine_lib);
93 dlhandle = dlopen(engine_lib, RTLD_LAZY);
95 td_vmsg(td, -1, dlerror(), "dlopen");
100 * Unlike the included modules, external engines should have a
101 * non-static ioengine structure that we can reference.
103 ops = dlsym(dlhandle, engine_lib);
105 ops = dlsym(dlhandle, "ioengine");
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
114 get_ioengine_t get_ioengine = dlsym(dlhandle, "get_ioengine");
121 td_vmsg(td, -1, dlerror(), "dlsym");
126 ops->dlhandle = dlhandle;
130 struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
132 struct ioengine_ops *ops, *ret;
135 dprint(FD_IO, "load ioengine %s\n", name);
137 strncpy(engine, name, sizeof(engine) - 1);
140 * linux libaio has alias names, so convert to what we want
142 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
143 strcpy(engine, "libaio");
145 ops = find_ioengine(engine);
147 ops = dlopen_ioengine(td, name);
150 log_err("fio: engine %s not loadable\n", name);
155 * Check that the required methods are there.
157 if (check_engine_ops(ops))
160 ret = malloc(sizeof(*ret));
161 memcpy(ret, ops, sizeof(*ret));
168 * For cleaning up an ioengine which never made it to init().
170 void free_ioengine(struct thread_data *td)
172 dprint(FD_IO, "free ioengine %s\n", td->io_ops->name);
174 if (td->eo && td->io_ops->options) {
175 options_free(td->io_ops->options, td->eo);
180 if (td->io_ops->dlhandle)
181 dlclose(td->io_ops->dlhandle);
187 void close_ioengine(struct thread_data *td)
189 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
191 if (td->io_ops->cleanup) {
192 td->io_ops->cleanup(td);
193 td->io_ops->data = NULL;
199 int td_io_prep(struct thread_data *td, struct io_u *io_u)
201 dprint_io_u(io_u, "prep");
202 fio_ro_check(td, io_u);
204 lock_file(td, io_u->file, io_u->ddir);
206 if (td->io_ops->prep) {
207 int ret = td->io_ops->prep(td, io_u);
209 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
211 unlock_file(td, io_u->file);
218 int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
219 const struct timespec *t)
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.
233 if (min > 0 && td->io_ops->commit) {
234 r = td->io_ops->commit(td);
238 if (max > td->cur_depth)
244 if (max && td->io_ops->getevents)
245 r = td->io_ops->getevents(td, min, max, t);
249 * Reflect that our submitted requests were retrieved with
250 * whatever OS async calls are in the underlying engine.
252 td->io_u_in_flight -= r;
253 io_u_mark_complete(td, r);
255 td_verror(td, r, "get_events");
257 dprint(FD_IO, "getevents: %d\n", r);
261 int td_io_queue(struct thread_data *td, struct io_u *io_u)
263 const enum fio_ddir ddir = acct_ddir(io_u);
264 unsigned long buflen = io_u->xfer_buflen;
267 dprint_io_u(io_u, "queue");
268 fio_ro_check(td, io_u);
270 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
271 io_u_set(io_u, IO_U_F_FLIGHT);
273 assert(fio_file_open(io_u->file));
276 * If using a write iolog, store this entry.
283 if (td->io_ops->flags & FIO_SYNCIO) {
284 if (fio_fill_issue_time(td))
285 fio_gettime(&io_u->issue_time, NULL);
288 * only used for iolog
290 if (td->o.read_iolog_file)
291 memcpy(&td->last_issue, &io_u->issue_time,
292 sizeof(struct timeval));
296 td->io_issues[ddir]++;
297 td->io_issue_bytes[ddir] += buflen;
298 td->rate_io_issue_bytes[ddir] += buflen;
301 ret = td->io_ops->queue(td, io_u);
303 unlock_file(td, io_u->file);
305 if (ret == FIO_Q_BUSY && ddir_rw(ddir)) {
306 td->io_issues[ddir]--;
307 td->io_issue_bytes[ddir] -= buflen;
308 td->rate_io_issue_bytes[ddir] -= buflen;
312 * If an error was seen and the io engine didn't propagate it
313 * back to 'td', do so.
315 if (io_u->error && !td->error)
316 td_verror(td, io_u->error, "td_io_queue");
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
324 if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 &&
327 log_info("fio: first direct IO errored. File system may not "
328 "support direct IO, or iomem_align= is bad. Try "
329 "setting direct=0.\n");
332 if (!td->io_ops->commit || io_u->ddir == DDIR_TRIM) {
333 io_u_mark_submit(td, 1);
334 io_u_mark_complete(td, 1);
337 if (ret == FIO_Q_COMPLETED) {
338 if (ddir_rw(io_u->ddir)) {
339 io_u_mark_depth(td, 1);
340 td->ts.total_io_u[io_u->ddir]++;
342 } else if (ret == FIO_Q_QUEUED) {
347 if (ddir_rw(io_u->ddir))
348 td->ts.total_io_u[io_u->ddir]++;
350 if (td->io_u_queued >= td->o.iodepth_batch) {
351 r = td_io_commit(td);
357 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
358 if (fio_fill_issue_time(td))
359 fio_gettime(&io_u->issue_time, NULL);
362 * only used for iolog
364 if (td->o.read_iolog_file)
365 memcpy(&td->last_issue, &io_u->issue_time,
366 sizeof(struct timeval));
372 int td_io_init(struct thread_data *td)
376 if (td->io_ops->init) {
377 ret = td->io_ops->init(td);
378 if (ret && td->o.iodepth > 1) {
379 log_err("fio: io engine init failed. Perhaps try"
380 " reducing io depth?\n");
386 if (!ret && (td->io_ops->flags & FIO_NOIO))
387 td->flags |= TD_F_NOIO;
392 int td_io_commit(struct thread_data *td)
396 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
398 if (!td->cur_depth || !td->io_u_queued)
401 io_u_mark_depth(td, td->io_u_queued);
403 if (td->io_ops->commit) {
404 ret = td->io_ops->commit(td);
406 td_verror(td, -ret, "io commit");
410 * Reflect that events were submitted as async IO requests.
412 td->io_u_in_flight += td->io_u_queued;
418 int td_io_open_file(struct thread_data *td, struct fio_file *f)
420 assert(!fio_file_open(f));
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");
426 if (td->error == EMFILE) {
427 log_err("fio: try reducing/setting openfiles (failed"
428 " at %u of %u)\n", td->nr_open_files,
433 assert(!fio_file_open(f));
437 fio_file_reset(td, f);
438 fio_file_set_open(f);
439 fio_file_clear_closing(f);
440 disk_util_inc(f->du);
445 if (f->filetype == FIO_TYPE_PIPE) {
447 log_err("fio: can't seek on pipes (no random io)\n");
452 if (td->io_ops->flags & FIO_DISKLESSIO)
455 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
458 if (td->o.fadvise_hint &&
459 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
463 flags = POSIX_FADV_RANDOM;
465 flags = POSIX_FADV_SEQUENTIAL;
467 if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
468 td_verror(td, errno, "fadvise");
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;
477 if (posix_fadvise(f->fd, stream, f->io_size, POSIX_FADV_STREAMID) < 0) {
478 td_verror(td, errno, "fadvise streamid");
484 #ifdef FIO_OS_DIRECTIO
486 * Some OS's have a distinct call to mark the file non-buffered,
487 * instead of using O_DIRECT (Solaris)
490 int ret = fio_set_odirect(f->fd);
493 td_verror(td, ret, "fio_set_odirect");
494 log_err("fio: the file system does not seem to support direct IO\n");
501 log_file(td, f, FIO_LOG_OPEN_FILE);
504 disk_util_dec(f->du);
505 if (td->io_ops->close_file)
506 td->io_ops->close_file(td, f);
510 int td_io_close_file(struct thread_data *td, struct fio_file *f)
512 if (!fio_file_closing(f))
513 log_file(td, f, FIO_LOG_CLOSE_FILE);
516 * mark as closing, do real close when last io on it has completed
518 fio_file_set_closing(f);
520 disk_util_dec(f->du);
522 if (td->o.file_lock_mode != FILE_LOCK_NONE)
523 unlock_file_all(td, f);
525 return put_file(td, f);
528 int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
530 if (td->io_ops->unlink_file)
531 return td->io_ops->unlink_file(td, f);
533 return unlink(f->file_name);
536 int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
538 if (!td->io_ops->get_file_size)
541 return td->io_ops->get_file_size(td, f);
544 static int do_sync_file_range(const struct thread_data *td,
547 off64_t offset, nbytes;
549 offset = f->first_write;
550 nbytes = f->last_write - f->first_write;
555 return sync_file_range(f->fd, offset, nbytes, td->o.sync_file_range);
558 int do_io_u_sync(const struct thread_data *td, struct io_u *io_u)
562 if (io_u->ddir == DDIR_SYNC) {
563 ret = fsync(io_u->file->fd);
564 } else if (io_u->ddir == DDIR_DATASYNC) {
565 #ifdef CONFIG_FDATASYNC
566 ret = fdatasync(io_u->file->fd);
568 ret = io_u->xfer_buflen;
569 io_u->error = EINVAL;
571 } else if (io_u->ddir == DDIR_SYNC_FILE_RANGE)
572 ret = do_sync_file_range(td, io_u->file);
574 ret = io_u->xfer_buflen;
575 io_u->error = EINVAL;
584 int do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
586 #ifndef FIO_HAVE_TRIM
587 io_u->error = EINVAL;
590 struct fio_file *f = io_u->file;
593 ret = os_trim(f->fd, io_u->offset, io_u->xfer_buflen);
595 return io_u->xfer_buflen;
602 int fio_show_ioengine_help(const char *engine)
604 struct flist_head *entry;
605 struct thread_data td;
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,
614 log_info("\t%s\n", td.io_ops->name);
618 sep = strchr(engine, ',');
624 memset(&td, 0, sizeof(td));
626 td.io_ops = load_ioengine(&td, engine);
628 log_info("IO engine %s not found\n", engine);
632 if (td.io_ops->options)
633 ret = show_cmd_help(td.io_ops->options, sep);
635 log_info("IO engine %s has no options\n", td.io_ops->name);