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.
21 static LIST_HEAD(engine_list);
23 static int check_engine_ops(struct ioengine_ops *ops)
25 if (ops->version != FIO_IOOPS_VERSION) {
26 log_err("bad ioops version %d (want %d)\n", ops->version, FIO_IOOPS_VERSION);
31 log_err("%s: no queue handler\n", ops->name);
36 * sync engines only need a ->queue()
38 if (ops->flags & FIO_SYNCIO)
42 log_err("%s: no event handler\n", ops->name);
45 if (!ops->getevents) {
46 log_err("%s: no getevents handler\n", ops->name);
50 log_err("%s: no queue handler\n", ops->name);
57 void unregister_ioengine(struct ioengine_ops *ops)
60 INIT_LIST_HEAD(&ops->list);
63 void register_ioengine(struct ioengine_ops *ops)
65 INIT_LIST_HEAD(&ops->list);
66 list_add_tail(&ops->list, &engine_list);
69 static struct ioengine_ops *find_ioengine(const char *name)
71 struct ioengine_ops *ops;
72 struct list_head *entry;
74 list_for_each(entry, &engine_list) {
75 ops = list_entry(entry, struct ioengine_ops, list);
76 if (!strcmp(name, ops->name))
83 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
84 const char *engine_lib)
86 struct ioengine_ops *ops;
90 dlhandle = dlopen(engine_lib, RTLD_LAZY);
92 td_vmsg(td, -1, dlerror(), "dlopen");
97 * Unlike the included modules, external engines should have a
98 * non-static ioengine structure that we can reference.
100 ops = dlsym(dlhandle, "ioengine");
102 td_vmsg(td, -1, dlerror(), "dlsym");
107 ops->dlhandle = dlhandle;
111 struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
113 struct ioengine_ops *ops, *ret;
116 strncpy(engine, name, sizeof(engine) - 1);
119 * linux libaio has alias names, so convert to what we want
121 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
122 strcpy(engine, "libaio");
124 ops = find_ioengine(engine);
126 ops = dlopen_ioengine(td, name);
129 log_err("fio: engine %s not loadable\n", name);
134 * Check that the required methods are there.
136 if (check_engine_ops(ops))
139 ret = malloc(sizeof(*ret));
140 memcpy(ret, ops, sizeof(*ret));
146 void close_ioengine(struct thread_data *td)
148 if (td->io_ops->cleanup)
149 td->io_ops->cleanup(td);
151 if (td->io_ops->dlhandle)
152 dlclose(td->io_ops->dlhandle);
158 int td_io_prep(struct thread_data *td, struct io_u *io_u)
160 if (td->io_ops->prep)
161 return td->io_ops->prep(td, io_u);
166 int td_io_getevents(struct thread_data *td, int min, int max,
169 if (min > 0 && td->io_ops->commit) {
170 int r = td->io_ops->commit(td);
175 if (td->io_ops->getevents)
176 return td->io_ops->getevents(td, min, max, t);
181 int td_io_queue(struct thread_data *td, struct io_u *io_u)
185 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
186 io_u->flags |= IO_U_F_FLIGHT;
188 assert(io_u->file->flags & FIO_FILE_OPEN);
193 if (td->io_ops->flags & FIO_SYNCIO) {
194 fio_gettime(&io_u->issue_time, NULL);
197 * for a sync engine, set the timeout upfront
199 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
200 io_u_set_timeout(td);
203 if (io_u->ddir != DDIR_SYNC)
204 td->io_issues[io_u->ddir]++;
206 io_u_mark_depth(td, io_u);
208 ret = td->io_ops->queue(td, io_u);
210 if (ret == FIO_Q_QUEUED || ret == FIO_Q_COMPLETED)
211 get_file(io_u->file);
213 if (ret == FIO_Q_QUEUED) {
217 if (td->io_u_queued > td->o.iodepth_batch) {
218 r = td_io_commit(td);
224 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
225 fio_gettime(&io_u->issue_time, NULL);
228 * async engine, set the timeout here
230 if (ret == FIO_Q_QUEUED &&
231 mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
232 io_u_set_timeout(td);
238 int td_io_init(struct thread_data *td)
242 if (td->io_ops->init) {
243 ret = td->io_ops->init(td);
244 if (ret && td->o.iodepth > 1)
245 log_err("fio: io engine init failed. Perhaps try reducing io dpeth?\n");
251 int td_io_commit(struct thread_data *td)
257 if (td->io_ops->commit)
258 return td->io_ops->commit(td);
263 int td_io_open_file(struct thread_data *td, struct fio_file *f)
265 if (td->io_ops->open_file(td, f)) {
266 if (td->error == EINVAL && td->o.odirect)
267 log_err("fio: destination does not support O_DIRECT\n");
268 if (td->error == EMFILE)
269 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
274 if (f->filetype == FIO_TYPE_PIPE) {
276 log_err("fio: can't seek on pipes (no random io)\n");
281 f->last_free_lookup = 0;
282 f->last_completed_pos = 0;
284 f->flags |= FIO_FILE_OPEN;
285 f->flags &= ~FIO_FILE_CLOSING;
287 if (td->io_ops->flags & FIO_DISKLESSIO)
290 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
293 if (td->o.fadvise_hint &&
294 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
299 flags = POSIX_FADV_RANDOM;
301 flags = POSIX_FADV_SEQUENTIAL;
303 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
304 td_verror(td, errno, "fadvise");
310 memset(f->file_map, 0, f->num_maps * sizeof(long));
317 if (td->io_ops->close_file)
318 td->io_ops->close_file(td, f);
322 void td_io_close_file(struct thread_data *td, struct fio_file *f)
325 * mark as closing, do real close when last io on it has completed
327 f->flags |= FIO_FILE_CLOSING;