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 FLIST_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,
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)
60 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
61 flist_del(&ops->list);
62 INIT_FLIST_HEAD(&ops->list);
65 void register_ioengine(struct ioengine_ops *ops)
67 dprint(FD_IO, "ioengine %s registered\n", ops->name);
68 INIT_FLIST_HEAD(&ops->list);
69 flist_add_tail(&ops->list, &engine_list);
72 static struct ioengine_ops *find_ioengine(const char *name)
74 struct ioengine_ops *ops;
75 struct flist_head *entry;
77 flist_for_each(entry, &engine_list) {
78 ops = flist_entry(entry, struct ioengine_ops, list);
79 if (!strcmp(name, ops->name))
86 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
87 const char *engine_lib)
89 struct ioengine_ops *ops;
92 dprint(FD_IO, "dload engine %s\n", engine_lib);
95 dlhandle = dlopen(engine_lib, RTLD_LAZY);
97 td_vmsg(td, -1, dlerror(), "dlopen");
102 * Unlike the included modules, external engines should have a
103 * non-static ioengine structure that we can reference.
105 ops = dlsym(dlhandle, "ioengine");
107 td_vmsg(td, -1, dlerror(), "dlsym");
112 ops->dlhandle = dlhandle;
116 struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
118 struct ioengine_ops *ops, *ret;
121 dprint(FD_IO, "load ioengine %s\n", name);
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 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
157 if (td->io_ops->cleanup) {
158 td->io_ops->cleanup(td);
159 td->io_ops->data = NULL;
162 if (td->io_ops->dlhandle)
163 dlclose(td->io_ops->dlhandle);
169 int td_io_prep(struct thread_data *td, struct io_u *io_u)
171 dprint_io_u(io_u, "prep");
172 fio_ro_check(td, io_u);
174 lock_file(td, io_u->file, io_u->ddir);
176 if (td->io_ops->prep) {
177 int ret = td->io_ops->prep(td, io_u);
179 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
181 unlock_file(td, io_u->file);
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);
198 if (max > td->cur_depth)
204 if (max && td->io_ops->getevents)
205 r = td->io_ops->getevents(td, min, max, t);
208 io_u_mark_complete(td, r);
209 dprint(FD_IO, "getevents: %d\n", r);
213 static inline int get_issue_time(struct thread_data *td)
215 if (td->o.read_iolog_file ||
216 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
222 int td_io_queue(struct thread_data *td, struct io_u *io_u)
226 dprint_io_u(io_u, "queue");
227 fio_ro_check(td, io_u);
229 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
230 io_u->flags |= IO_U_F_FLIGHT;
232 assert(io_u->file->flags & FIO_FILE_OPEN);
237 if (td->io_ops->flags & FIO_SYNCIO) {
238 if (get_issue_time(td))
239 fio_gettime(&io_u->issue_time, NULL);
242 * only used for iolog
244 if (td->o.read_iolog_file)
245 memcpy(&td->last_issue, &io_u->issue_time,
246 sizeof(struct timeval));
249 if (io_u->ddir != DDIR_SYNC)
250 td->io_issues[io_u->ddir]++;
252 ret = td->io_ops->queue(td, io_u);
254 unlock_file(td, io_u->file);
256 if (!td->io_ops->commit) {
257 io_u_mark_submit(td, 1);
258 io_u_mark_complete(td, 1);
261 if (ret == FIO_Q_COMPLETED) {
262 if (io_u->ddir != DDIR_SYNC) {
263 io_u_mark_depth(td, 1);
264 td->ts.total_io_u[io_u->ddir]++;
266 } else if (ret == FIO_Q_QUEUED) {
269 if (io_u->ddir != DDIR_SYNC) {
271 td->ts.total_io_u[io_u->ddir]++;
274 if (td->io_u_queued >= td->o.iodepth_batch) {
275 r = td_io_commit(td);
281 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
282 if (get_issue_time(td))
283 fio_gettime(&io_u->issue_time, NULL);
286 * only used for iolog
288 if (td->o.read_iolog_file)
289 memcpy(&td->last_issue, &io_u->issue_time,
290 sizeof(struct timeval));
296 int td_io_init(struct thread_data *td)
300 if (td->io_ops->init) {
301 ret = td->io_ops->init(td);
302 if (ret && td->o.iodepth > 1) {
303 log_err("fio: io engine init failed. Perhaps try"
304 " reducing io depth?\n");
311 int td_io_commit(struct thread_data *td)
313 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
315 if (!td->cur_depth || !td->io_u_queued)
318 io_u_mark_depth(td, td->io_u_queued);
321 if (td->io_ops->commit)
322 return td->io_ops->commit(td);
327 int td_io_open_file(struct thread_data *td, struct fio_file *f)
329 if (td->io_ops->open_file(td, f)) {
330 if (td->error == EINVAL && td->o.odirect)
331 log_err("fio: destination does not support O_DIRECT\n");
332 if (td->error == EMFILE) {
333 log_err("fio: try reducing/setting openfiles (failed"
334 " at %u of %u)\n", td->nr_open_files,
342 f->flags |= FIO_FILE_OPEN;
343 f->flags &= ~FIO_FILE_CLOSING;
348 if (f->filetype == FIO_TYPE_PIPE) {
350 log_err("fio: can't seek on pipes (no random io)\n");
355 if (td->io_ops->flags & FIO_DISKLESSIO)
358 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
361 if (td->o.fadvise_hint &&
362 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
366 flags = POSIX_FADV_RANDOM;
368 flags = POSIX_FADV_SEQUENTIAL;
370 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
371 td_verror(td, errno, "fadvise");
377 memset(f->file_map, 0, f->num_maps * sizeof(int));
379 #ifdef FIO_OS_DIRECTIO
381 * Some OS's have a distinct call to mark the file non-buffered,
382 * instead of using O_DIRECT (Solaris)
385 int ret = fio_set_odirect(f->fd);
388 td_verror(td, ret, "fio_set_odirect");
395 log_file(td, f, FIO_LOG_OPEN_FILE);
398 if (td->io_ops->close_file)
399 td->io_ops->close_file(td, f);
403 int td_io_close_file(struct thread_data *td, struct fio_file *f)
405 if (!(f->flags & FIO_FILE_CLOSING))
406 log_file(td, f, FIO_LOG_CLOSE_FILE);
409 * mark as closing, do real close when last io on it has completed
411 f->flags |= FIO_FILE_CLOSING;
413 unlock_file_all(td, f);
415 return put_file(td, f);