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 log_err("%s: no queue handler\n", ops->name);
37 * sync engines only need a ->queue()
39 if (ops->flags & FIO_SYNCIO)
43 log_err("%s: no event handler\n", ops->name);
46 if (!ops->getevents) {
47 log_err("%s: no getevents handler\n", ops->name);
51 log_err("%s: no queue handler\n", ops->name);
58 void unregister_ioengine(struct ioengine_ops *ops)
61 INIT_LIST_HEAD(&ops->list);
64 void register_ioengine(struct ioengine_ops *ops)
66 INIT_LIST_HEAD(&ops->list);
67 list_add_tail(&ops->list, &engine_list);
70 static struct ioengine_ops *find_ioengine(const char *name)
72 struct ioengine_ops *ops;
73 struct list_head *entry;
75 list_for_each(entry, &engine_list) {
76 ops = list_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;
91 dlhandle = dlopen(engine_lib, RTLD_LAZY);
93 td_vmsg(td, -1, dlerror(), "dlopen");
98 * Unlike the included modules, external engines should have a
99 * non-static ioengine structure that we can reference.
101 ops = dlsym(dlhandle, "ioengine");
103 td_vmsg(td, -1, dlerror(), "dlsym");
108 ops->dlhandle = dlhandle;
112 struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
114 struct ioengine_ops *ops, *ret;
117 strncpy(engine, name, sizeof(engine) - 1);
120 * linux libaio has alias names, so convert to what we want
122 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
123 strcpy(engine, "libaio");
125 ops = find_ioengine(engine);
127 ops = dlopen_ioengine(td, name);
130 log_err("fio: engine %s not loadable\n", name);
135 * Check that the required methods are there.
137 if (check_engine_ops(ops))
140 ret = malloc(sizeof(*ret));
141 memcpy(ret, ops, sizeof(*ret));
147 void close_ioengine(struct thread_data *td)
149 if (td->io_ops->cleanup)
150 td->io_ops->cleanup(td);
152 if (td->io_ops->dlhandle)
153 dlclose(td->io_ops->dlhandle);
159 int td_io_prep(struct thread_data *td, struct io_u *io_u)
161 if (td->io_ops->prep)
162 return td->io_ops->prep(td, io_u);
167 int td_io_getevents(struct thread_data *td, int min, int max,
170 if (min > 0 && td->io_ops->commit) {
171 int r = td->io_ops->commit(td);
176 if (td->io_ops->getevents)
177 return td->io_ops->getevents(td, min, max, t);
182 int td_io_queue(struct thread_data *td, struct io_u *io_u)
186 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
187 io_u->flags |= IO_U_F_FLIGHT;
189 assert(io_u->file->flags & FIO_FILE_OPEN);
194 if (td->io_ops->flags & FIO_SYNCIO) {
195 fio_gettime(&io_u->issue_time, NULL);
198 * for a sync engine, set the timeout upfront
200 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
201 io_u_set_timeout(td);
204 if (io_u->ddir != DDIR_SYNC)
205 td->io_issues[io_u->ddir]++;
207 io_u_mark_depth(td, io_u);
209 ret = td->io_ops->queue(td, io_u);
211 if (ret == FIO_Q_QUEUED || ret == FIO_Q_COMPLETED)
212 get_file(io_u->file);
214 if (ret == FIO_Q_QUEUED) {
218 if (td->io_u_queued > td->o.iodepth_batch) {
219 r = td_io_commit(td);
225 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
226 fio_gettime(&io_u->issue_time, NULL);
229 * async engine, set the timeout here
231 if (ret == FIO_Q_QUEUED &&
232 mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
233 io_u_set_timeout(td);
239 int td_io_init(struct thread_data *td)
243 if (td->io_ops->init) {
244 ret = td->io_ops->init(td);
245 if (ret && td->o.iodepth > 1)
246 log_err("fio: io engine init failed. Perhaps try reducing io dpeth?\n");
252 int td_io_commit(struct thread_data *td)
258 if (td->io_ops->commit)
259 return td->io_ops->commit(td);
264 int td_io_open_file(struct thread_data *td, struct fio_file *f)
266 if (td->io_ops->open_file(td, f))
269 f->last_free_lookup = 0;
270 f->last_completed_pos = 0;
272 f->flags |= FIO_FILE_OPEN;
273 f->flags &= ~FIO_FILE_CLOSING;
276 memset(f->file_map, 0, f->num_maps * sizeof(long));
283 void td_io_close_file(struct thread_data *td, struct fio_file *f)
286 * mark as closing, do real close when last io on it has completed
288 f->flags |= FIO_FILE_CLOSING;