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;
82 strncpy(engine, name, sizeof(engine) - 1);
84 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
85 strcpy(engine, "libaio");
87 list_for_each(entry, &engine_list) {
88 ops = list_entry(entry, struct ioengine_ops, list);
89 if (!strcmp(engine, ops->name))
96 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
97 const char *engine_lib)
99 struct ioengine_ops *ops;
103 dlhandle = dlopen(engine_lib, RTLD_LAZY);
105 td_vmsg(td, -1, dlerror(), "dlopen");
110 * Unlike the included modules, external engines should have a
111 * non-static ioengine structure that we can reference.
113 ops = dlsym(dlhandle, "ioengine");
115 td_vmsg(td, -1, dlerror(), "dlsym");
120 ops->dlhandle = dlhandle;
124 struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
126 struct ioengine_ops *ops, *ret;
129 strncpy(engine, name, sizeof(engine) - 1);
132 * linux libaio has alias names, so convert to what we want
134 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
135 strcpy(engine, "libaio");
137 ops = find_ioengine(engine);
139 ops = dlopen_ioengine(td, name);
142 log_err("fio: engine %s not loadable\n", name);
147 * Check that the required methods are there.
149 if (check_engine_ops(ops))
152 ret = malloc(sizeof(*ret));
153 memcpy(ret, ops, sizeof(*ret));
159 void close_ioengine(struct thread_data *td)
161 if (td->io_ops->cleanup)
162 td->io_ops->cleanup(td);
164 if (td->io_ops->dlhandle)
165 dlclose(td->io_ops->dlhandle);
168 /* we can't do this for threads, so just leak it, it's exiting */
174 int td_io_prep(struct thread_data *td, struct io_u *io_u)
176 if (td->io_ops->prep)
177 return td->io_ops->prep(td, io_u);
182 int td_io_getevents(struct thread_data *td, int min, int max,
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;
198 if (td->io_ops->flags & FIO_SYNCIO) {
199 fio_gettime(&io_u->issue_time, NULL);
202 * for a sync engine, set the timeout upfront
204 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
205 io_u_set_timeout(td);
208 if (io_u->ddir != DDIR_SYNC)
209 td->io_issues[io_u->ddir]++;
211 ret = td->io_ops->queue(td, io_u);
213 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
214 fio_gettime(&io_u->issue_time, NULL);
217 * async engine, set the timeout here
219 if (ret == FIO_Q_QUEUED &&
220 mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
221 io_u_set_timeout(td);
227 int td_io_init(struct thread_data *td)
229 if (td->io_ops->init)
230 return td->io_ops->init(td);
235 int td_io_commit(struct thread_data *td)
239 if (td->io_ops->commit)
240 return td->io_ops->commit(td);