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)
59 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
61 INIT_LIST_HEAD(&ops->list);
64 void register_ioengine(struct ioengine_ops *ops)
66 dprint(FD_IO, "ioengine %s registered\n", ops->name);
67 INIT_LIST_HEAD(&ops->list);
68 list_add_tail(&ops->list, &engine_list);
71 static struct ioengine_ops *find_ioengine(const char *name)
73 struct ioengine_ops *ops;
74 struct list_head *entry;
76 list_for_each(entry, &engine_list) {
77 ops = list_entry(entry, struct ioengine_ops, list);
78 if (!strcmp(name, ops->name))
85 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
86 const char *engine_lib)
88 struct ioengine_ops *ops;
91 dprint(FD_IO, "dload engine %s\n", engine_lib);
94 dlhandle = dlopen(engine_lib, RTLD_LAZY);
96 td_vmsg(td, -1, dlerror(), "dlopen");
101 * Unlike the included modules, external engines should have a
102 * non-static ioengine structure that we can reference.
104 ops = dlsym(dlhandle, "ioengine");
106 td_vmsg(td, -1, dlerror(), "dlsym");
111 ops->dlhandle = dlhandle;
115 struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
117 struct ioengine_ops *ops, *ret;
120 dprint(FD_IO, "load ioengine %s\n", name);
122 strncpy(engine, name, sizeof(engine) - 1);
125 * linux libaio has alias names, so convert to what we want
127 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
128 strcpy(engine, "libaio");
130 ops = find_ioengine(engine);
132 ops = dlopen_ioengine(td, name);
135 log_err("fio: engine %s not loadable\n", name);
140 * Check that the required methods are there.
142 if (check_engine_ops(ops))
145 ret = malloc(sizeof(*ret));
146 memcpy(ret, ops, sizeof(*ret));
152 void close_ioengine(struct thread_data *td)
154 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
156 if (td->io_ops->cleanup)
157 td->io_ops->cleanup(td);
159 if (td->io_ops->dlhandle)
160 dlclose(td->io_ops->dlhandle);
166 static void dprint_io_u(struct io_u *io_u, const char *p)
168 struct fio_file *f = io_u->file;
170 dprint(FD_IO, "%s: off=%llu/len=%lu/ddir=%d", p, io_u->offset,
171 io_u->buflen, io_u->ddir);
173 dprint(FD_IO, "/%s", f->file_name);
177 int td_io_prep(struct thread_data *td, struct io_u *io_u)
179 dprint_io_u(io_u, "prep");
180 fio_ro_check(td, io_u);
182 if (td->io_ops->prep)
183 return td->io_ops->prep(td, io_u);
188 int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
193 if (min > 0 && td->io_ops->commit) {
194 r = td->io_ops->commit(td);
200 if (td->io_ops->getevents)
201 r = td->io_ops->getevents(td, min, max, t);
203 dprint(FD_IO, "getevents: %d\n", r);
207 int td_io_queue(struct thread_data *td, struct io_u *io_u)
211 dprint_io_u(io_u, "queue");
212 fio_ro_check(td, io_u);
214 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
215 io_u->flags |= IO_U_F_FLIGHT;
217 assert(io_u->file->flags & FIO_FILE_OPEN);
222 if (td->io_ops->flags & FIO_SYNCIO) {
223 fio_gettime(&io_u->issue_time, NULL);
224 memcpy(&td->last_issue, &io_u->issue_time, sizeof(struct timeval));
227 * for a sync engine, set the timeout upfront
229 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
230 io_u_set_timeout(td);
233 if (io_u->ddir != DDIR_SYNC)
234 td->io_issues[io_u->ddir]++;
236 io_u_mark_depth(td, io_u);
238 ret = td->io_ops->queue(td, io_u);
240 if (ret == FIO_Q_QUEUED) {
244 if (td->io_u_queued > td->o.iodepth_batch) {
245 r = td_io_commit(td);
251 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
252 fio_gettime(&io_u->issue_time, NULL);
253 memcpy(&td->last_issue, &io_u->issue_time, sizeof(struct timeval));
256 * async engine, set the timeout here
258 if (ret == FIO_Q_QUEUED &&
259 mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
260 io_u_set_timeout(td);
266 int td_io_init(struct thread_data *td)
270 if (td->io_ops->init) {
271 ret = td->io_ops->init(td);
272 if (ret && td->o.iodepth > 1)
273 log_err("fio: io engine init failed. Perhaps try reducing io depth?\n");
279 int td_io_commit(struct thread_data *td)
281 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
287 if (td->io_ops->commit)
288 return td->io_ops->commit(td);
293 int td_io_open_file(struct thread_data *td, struct fio_file *f)
295 if (td->io_ops->open_file(td, f)) {
296 if (td->error == EINVAL && td->o.odirect)
297 log_err("fio: destination does not support O_DIRECT\n");
298 if (td->error == EMFILE)
299 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
304 if (f->filetype == FIO_TYPE_PIPE) {
306 log_err("fio: can't seek on pipes (no random io)\n");
311 f->last_free_lookup = 0;
312 f->last_completed_pos = 0;
313 f->last_pos = f->file_offset;
314 f->flags |= FIO_FILE_OPEN;
315 f->flags &= ~FIO_FILE_CLOSING;
317 if (td->io_ops->flags & FIO_DISKLESSIO)
320 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
323 if (td->o.fadvise_hint &&
324 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
329 flags = POSIX_FADV_RANDOM;
331 flags = POSIX_FADV_SEQUENTIAL;
333 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
334 td_verror(td, errno, "fadvise");
340 memset(f->file_map, 0, f->num_maps * sizeof(long));
343 log_file(td, f, FIO_LOG_OPEN_FILE);
348 if (td->io_ops->close_file)
349 td->io_ops->close_file(td, f);
353 void td_io_close_file(struct thread_data *td, struct fio_file *f)
355 if (!(f->flags & FIO_FILE_CLOSING))
356 log_file(td, f, FIO_LOG_CLOSE_FILE);
359 * mark as closing, do real close when last io on it has completed
361 f->flags |= FIO_FILE_CLOSING;