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.
22 static LIST_HEAD(engine_list);
24 static int check_engine_ops(struct ioengine_ops *ops)
26 if (ops->version != FIO_IOOPS_VERSION) {
27 log_err("bad ioops version %d (want %d)\n", ops->version, FIO_IOOPS_VERSION);
32 * cpu thread doesn't need to provide anything
34 if (ops->flags & FIO_CPUIO)
38 log_err("%s: no queue handler\n", ops->name);
43 * sync engines only need a ->queue()
45 if (ops->flags & FIO_SYNCIO)
49 log_err("%s: no event handler\n", ops->name);
52 if (!ops->getevents) {
53 log_err("%s: no getevents handler\n", ops->name);
57 log_err("%s: no queue handler\n", ops->name);
64 void unregister_ioengine(struct ioengine_ops *ops)
67 INIT_LIST_HEAD(&ops->list);
70 void register_ioengine(struct ioengine_ops *ops)
72 INIT_LIST_HEAD(&ops->list);
73 list_add_tail(&ops->list, &engine_list);
76 static struct ioengine_ops *find_ioengine(const char *name)
78 struct ioengine_ops *ops;
79 struct list_head *entry;
81 list_for_each(entry, &engine_list) {
82 ops = list_entry(entry, struct ioengine_ops, list);
83 if (!strcmp(name, ops->name))
90 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
91 const char *engine_lib)
93 struct ioengine_ops *ops;
97 dlhandle = dlopen(engine_lib, RTLD_LAZY);
99 td_vmsg(td, -1, dlerror(), "dlopen");
104 * Unlike the included modules, external engines should have a
105 * non-static ioengine structure that we can reference.
107 ops = dlsym(dlhandle, "ioengine");
109 td_vmsg(td, -1, dlerror(), "dlsym");
114 ops->dlhandle = dlhandle;
118 struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
120 struct ioengine_ops *ops, *ret;
123 strncpy(engine, name, sizeof(engine) - 1);
126 * linux libaio has alias names, so convert to what we want
128 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
129 strcpy(engine, "libaio");
131 ops = find_ioengine(engine);
133 ops = dlopen_ioengine(td, name);
136 log_err("fio: engine %s not loadable\n", name);
141 * Check that the required methods are there.
143 if (check_engine_ops(ops))
146 ret = malloc(sizeof(*ret));
147 memcpy(ret, ops, sizeof(*ret));
153 void close_ioengine(struct thread_data *td)
155 if (td->io_ops->cleanup)
156 td->io_ops->cleanup(td);
158 if (td->io_ops->dlhandle)
159 dlclose(td->io_ops->dlhandle);
162 /* we can't do this for threads, so just leak it, it's exiting */
168 int td_io_prep(struct thread_data *td, struct io_u *io_u)
170 if (td->io_ops->prep)
171 return td->io_ops->prep(td, io_u);
176 int td_io_getevents(struct thread_data *td, int min, int max,
179 if (min > 0 && td->io_ops->commit) {
180 int r = td->io_ops->commit(td);
185 if (td->io_ops->getevents)
186 return td->io_ops->getevents(td, min, max, t);
191 int td_io_queue(struct thread_data *td, struct io_u *io_u)
195 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
196 io_u->flags |= IO_U_F_FLIGHT;
201 if (td->io_ops->flags & FIO_SYNCIO) {
202 fio_gettime(&io_u->issue_time, NULL);
205 * for a sync engine, set the timeout upfront
207 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
208 io_u_set_timeout(td);
211 if (io_u->ddir != DDIR_SYNC)
212 td->io_issues[io_u->ddir]++;
214 ret = td->io_ops->queue(td, io_u);
216 if (ret == FIO_Q_QUEUED) {
220 if (td->io_u_queued > td->iodepth_batch) {
221 r = td_io_commit(td);
227 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
228 fio_gettime(&io_u->issue_time, NULL);
231 * async engine, set the timeout here
233 if (ret == FIO_Q_QUEUED &&
234 mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
235 io_u_set_timeout(td);
241 int td_io_init(struct thread_data *td)
243 if (td->io_ops->init)
244 return td->io_ops->init(td);
249 int td_io_commit(struct thread_data *td)
255 if (td->io_ops->commit)
256 return td->io_ops->commit(td);
261 int td_io_open_file(struct thread_data *td, struct fio_file *f)
263 if (!td->io_ops->open_file(td, f)) {
272 void td_io_close_file(struct thread_data *td, struct fio_file *f)
275 if (td->io_ops->close_file)
276 td->io_ops->close_file(td, f);