blob: a81c7b884b5df6352cca79d5e47bb5eae47b2973 [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
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.
4 *
5 * sync io is implemented on top of aio.
6 *
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.
10 *
11 */
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
Jens Axboe5c4e1db2006-06-07 14:17:08 +020015#include <string.h>
Jens Axboe2866c822006-10-09 15:57:48 +020016#include <dlfcn.h>
Jens Axboe0c6e7512007-02-22 11:19:39 +010017#include <assert.h>
Jens Axboe8c16d842006-10-20 11:48:33 +020018
Jens Axboeebac4652005-12-08 15:25:21 +010019#include "fio.h"
Jens Axboeebac4652005-12-08 15:25:21 +010020
Jens Axboe5f350952006-11-07 15:20:59 +010021static LIST_HEAD(engine_list);
22
Jens Axboe8c16d842006-10-20 11:48:33 +020023static int check_engine_ops(struct ioengine_ops *ops)
24{
Jens Axboe5f350952006-11-07 15:20:59 +010025 if (ops->version != FIO_IOOPS_VERSION) {
26 log_err("bad ioops version %d (want %d)\n", ops->version, FIO_IOOPS_VERSION);
27 return 1;
28 }
29
Jens Axboe36167d82007-02-18 05:41:31 +010030 if (!ops->queue) {
31 log_err("%s: no queue handler\n", ops->name);
32 return 1;
33 }
34
35 /*
36 * sync engines only need a ->queue()
37 */
38 if (ops->flags & FIO_SYNCIO)
39 return 0;
40
Jens Axboe8c16d842006-10-20 11:48:33 +020041 if (!ops->event) {
Jens Axboe36167d82007-02-18 05:41:31 +010042 log_err("%s: no event handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020043 return 1;
44 }
45 if (!ops->getevents) {
Jens Axboe36167d82007-02-18 05:41:31 +010046 log_err("%s: no getevents handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020047 return 1;
48 }
49 if (!ops->queue) {
Jens Axboe36167d82007-02-18 05:41:31 +010050 log_err("%s: no queue handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020051 return 1;
52 }
53
54 return 0;
55}
56
Jens Axboe5f350952006-11-07 15:20:59 +010057void unregister_ioengine(struct ioengine_ops *ops)
Jens Axboeebac4652005-12-08 15:25:21 +010058{
Jens Axboeee56ad52008-02-01 10:30:20 +010059 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
Jens Axboe5f350952006-11-07 15:20:59 +010060 list_del(&ops->list);
61 INIT_LIST_HEAD(&ops->list);
62}
63
Jens Axboeb2fdda42007-02-20 20:52:51 +010064void register_ioengine(struct ioengine_ops *ops)
Jens Axboe5f350952006-11-07 15:20:59 +010065{
Jens Axboeee56ad52008-02-01 10:30:20 +010066 dprint(FD_IO, "ioengine %s registered\n", ops->name);
Jens Axboe5f350952006-11-07 15:20:59 +010067 INIT_LIST_HEAD(&ops->list);
68 list_add_tail(&ops->list, &engine_list);
Jens Axboe5f350952006-11-07 15:20:59 +010069}
70
71static struct ioengine_ops *find_ioengine(const char *name)
72{
73 struct ioengine_ops *ops;
74 struct list_head *entry;
Jens Axboe2866c822006-10-09 15:57:48 +020075
Jens Axboe5f350952006-11-07 15:20:59 +010076 list_for_each(entry, &engine_list) {
77 ops = list_entry(entry, struct ioengine_ops, list);
Jens Axboebc5b77a2007-02-28 09:32:54 +010078 if (!strcmp(name, ops->name))
Jens Axboe5f350952006-11-07 15:20:59 +010079 return ops;
80 }
81
82 return NULL;
83}
84
85static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
86 const char *engine_lib)
87{
88 struct ioengine_ops *ops;
89 void *dlhandle;
90
Jens Axboeee56ad52008-02-01 10:30:20 +010091 dprint(FD_IO, "dload engine %s\n", engine_lib);
92
Jens Axboe2866c822006-10-09 15:57:48 +020093 dlerror();
94 dlhandle = dlopen(engine_lib, RTLD_LAZY);
Jens Axboed4dbaaa2006-10-09 16:01:27 +020095 if (!dlhandle) {
Jens Axboee1161c32007-02-22 19:36:48 +010096 td_vmsg(td, -1, dlerror(), "dlopen");
Jens Axboed4dbaaa2006-10-09 16:01:27 +020097 return NULL;
98 }
Jens Axboe2866c822006-10-09 15:57:48 +020099
Jens Axboeda51c052006-11-07 16:02:11 +0100100 /*
101 * Unlike the included modules, external engines should have a
102 * non-static ioengine structure that we can reference.
103 */
Jens Axboe2866c822006-10-09 15:57:48 +0200104 ops = dlsym(dlhandle, "ioengine");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200105 if (!ops) {
Jens Axboee1161c32007-02-22 19:36:48 +0100106 td_vmsg(td, -1, dlerror(), "dlsym");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200107 dlclose(dlhandle);
108 return NULL;
109 }
Jens Axboe2866c822006-10-09 15:57:48 +0200110
Jens Axboe5f350952006-11-07 15:20:59 +0100111 ops->dlhandle = dlhandle;
112 return ops;
113}
114
115struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
116{
117 struct ioengine_ops *ops, *ret;
118 char engine[16];
119
Jens Axboeee56ad52008-02-01 10:30:20 +0100120 dprint(FD_IO, "load ioengine %s\n", name);
121
Jens Axboe5f350952006-11-07 15:20:59 +0100122 strncpy(engine, name, sizeof(engine) - 1);
123
124 /*
125 * linux libaio has alias names, so convert to what we want
126 */
127 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
128 strcpy(engine, "libaio");
129
130 ops = find_ioengine(engine);
131 if (!ops)
132 ops = dlopen_ioengine(td, name);
133
134 if (!ops) {
135 log_err("fio: engine %s not loadable\n", name);
Jens Axboeb902ceb2006-10-09 16:11:45 +0200136 return NULL;
137 }
138
Jens Axboe8c16d842006-10-20 11:48:33 +0200139 /*
140 * Check that the required methods are there.
141 */
Jens Axboe5f350952006-11-07 15:20:59 +0100142 if (check_engine_ops(ops))
Jens Axboe8c16d842006-10-20 11:48:33 +0200143 return NULL;
Jens Axboe8c16d842006-10-20 11:48:33 +0200144
Jens Axboe84585002006-10-19 20:26:22 +0200145 ret = malloc(sizeof(*ret));
146 memcpy(ret, ops, sizeof(*ret));
147 ret->data = NULL;
Jens Axboe84585002006-10-19 20:26:22 +0200148
149 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100150}
151
Jens Axboe2866c822006-10-09 15:57:48 +0200152void close_ioengine(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100153{
Jens Axboeee56ad52008-02-01 10:30:20 +0100154 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
155
Jens Axboe2866c822006-10-09 15:57:48 +0200156 if (td->io_ops->cleanup)
157 td->io_ops->cleanup(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100158
Jens Axboe5f350952006-11-07 15:20:59 +0100159 if (td->io_ops->dlhandle)
160 dlclose(td->io_ops->dlhandle);
161
Jens Axboe84585002006-10-19 20:26:22 +0200162 free(td->io_ops);
163 td->io_ops = NULL;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200164}
Jens Axboe10ba5352006-10-20 11:39:27 +0200165
166int td_io_prep(struct thread_data *td, struct io_u *io_u)
167{
Jens Axboeee56ad52008-02-01 10:30:20 +0100168 dprint_io_u(io_u, "prep");
Jens Axboe7101d9c2007-09-12 13:12:39 +0200169 fio_ro_check(td, io_u);
170
Jens Axboe2ba1c292008-02-01 13:16:38 +0100171 if (td->io_ops->prep) {
172 int ret = td->io_ops->prep(td, io_u);
173
174 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
175 return ret;
176 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200177
178 return 0;
179}
180
Jens Axboee7d2e612007-12-11 10:49:39 +0100181int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
Jens Axboe10ba5352006-10-20 11:39:27 +0200182 struct timespec *t)
183{
Jens Axboeee56ad52008-02-01 10:30:20 +0100184 int r = 0;
185
Jens Axboeface81b2007-02-26 10:40:03 +0100186 if (min > 0 && td->io_ops->commit) {
Jens Axboeee56ad52008-02-01 10:30:20 +0100187 r = td->io_ops->commit(td);
Jens Axboeface81b2007-02-26 10:40:03 +0100188 if (r < 0)
Jens Axboeee56ad52008-02-01 10:30:20 +0100189 goto out;
Jens Axboeface81b2007-02-26 10:40:03 +0100190 }
Jens Axboe36167d82007-02-18 05:41:31 +0100191
Jens Axboeee56ad52008-02-01 10:30:20 +0100192 r = 0;
193 if (td->io_ops->getevents)
194 r = td->io_ops->getevents(td, min, max, t);
195out:
196 dprint(FD_IO, "getevents: %d\n", r);
197 return r;
Jens Axboe10ba5352006-10-20 11:39:27 +0200198}
199
200int td_io_queue(struct thread_data *td, struct io_u *io_u)
201{
Jens Axboe7e77dd02007-02-20 10:57:34 +0100202 int ret;
203
Jens Axboeee56ad52008-02-01 10:30:20 +0100204 dprint_io_u(io_u, "queue");
Jens Axboe7101d9c2007-09-12 13:12:39 +0200205 fio_ro_check(td, io_u);
206
Jens Axboe0c6e7512007-02-22 11:19:39 +0100207 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
208 io_u->flags |= IO_U_F_FLIGHT;
209
Jens Axboe3d7b4852007-03-13 14:50:28 +0100210 assert(io_u->file->flags & FIO_FILE_OPEN);
211
Jens Axboe11786802007-03-05 18:33:22 +0100212 io_u->error = 0;
213 io_u->resid = 0;
214
Jens Axboe433afcb2007-02-22 10:39:01 +0100215 if (td->io_ops->flags & FIO_SYNCIO) {
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100216 fio_gettime(&io_u->issue_time, NULL);
Jens Axboea61edde2007-05-15 14:29:58 +0200217 memcpy(&td->last_issue, &io_u->issue_time, sizeof(struct timeval));
Jens Axboe10ba5352006-10-20 11:39:27 +0200218
Jens Axboe433afcb2007-02-22 10:39:01 +0100219 /*
220 * for a sync engine, set the timeout upfront
221 */
222 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
223 io_u_set_timeout(td);
224 }
225
Jens Axboe755200a2007-02-19 13:08:12 +0100226 if (io_u->ddir != DDIR_SYNC)
227 td->io_issues[io_u->ddir]++;
228
Jens Axboe7e77dd02007-02-20 10:57:34 +0100229 ret = td->io_ops->queue(td, io_u);
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100230
Jens Axboe163f8492008-02-04 10:56:26 +0100231 if (ret != FIO_Q_BUSY)
232 io_u_mark_depth(td, io_u);
233
Jens Axboeeb7c8ae2007-02-26 14:45:12 +0100234 if (ret == FIO_Q_QUEUED) {
235 int r;
236
Jens Axboecb5ab512007-02-26 12:57:09 +0100237 td->io_u_queued++;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100238 if (td->io_u_queued > td->o.iodepth_batch) {
Jens Axboeeb7c8ae2007-02-26 14:45:12 +0100239 r = td_io_commit(td);
240 if (r < 0)
241 return r;
242 }
243 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100244
Jens Axboe433afcb2007-02-22 10:39:01 +0100245 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100246 fio_gettime(&io_u->issue_time, NULL);
Jens Axboea61edde2007-05-15 14:29:58 +0200247 memcpy(&td->last_issue, &io_u->issue_time, sizeof(struct timeval));
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100248
Jens Axboe433afcb2007-02-22 10:39:01 +0100249 /*
250 * async engine, set the timeout here
251 */
252 if (ret == FIO_Q_QUEUED &&
253 mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
254 io_u_set_timeout(td);
255 }
256
Jens Axboe7e77dd02007-02-20 10:57:34 +0100257 return ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200258}
Jens Axboe8c16d842006-10-20 11:48:33 +0200259
260int td_io_init(struct thread_data *td)
261{
Jens Axboeeeb12162007-03-20 10:47:45 +0100262 int ret = 0;
Jens Axboe8c16d842006-10-20 11:48:33 +0200263
Jens Axboeeeb12162007-03-20 10:47:45 +0100264 if (td->io_ops->init) {
265 ret = td->io_ops->init(td);
266 if (ret && td->o.iodepth > 1)
Zach Brown9de6beb2007-09-19 08:42:17 +0200267 log_err("fio: io engine init failed. Perhaps try reducing io depth?\n");
Jens Axboeeeb12162007-03-20 10:47:45 +0100268 }
269
270 return ret;
Jens Axboe8c16d842006-10-20 11:48:33 +0200271}
Jens Axboe755200a2007-02-19 13:08:12 +0100272
273int td_io_commit(struct thread_data *td)
274{
Jens Axboeee56ad52008-02-01 10:30:20 +0100275 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
276
Jens Axboee1161c32007-02-22 19:36:48 +0100277 if (!td->cur_depth)
278 return 0;
Jens Axboecb5ab512007-02-26 12:57:09 +0100279
280 td->io_u_queued = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100281 if (td->io_ops->commit)
282 return td->io_ops->commit(td);
283
284 return 0;
285}
Jens Axboeb5af8292007-03-08 12:43:13 +0100286
287int td_io_open_file(struct thread_data *td, struct fio_file *f)
288{
Jens Axboe413d6692007-03-27 15:38:21 +0200289 if (td->io_ops->open_file(td, f)) {
290 if (td->error == EINVAL && td->o.odirect)
291 log_err("fio: destination does not support O_DIRECT\n");
292 if (td->error == EMFILE)
293 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
Jens Axboeb5af8292007-03-08 12:43:13 +0100294
Jens Axboe413d6692007-03-27 15:38:21 +0200295 return 1;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200296 }
Jens Axboea978ba62007-03-08 14:29:03 +0100297
Jens Axboe66159822007-04-16 21:54:24 +0200298 if (f->filetype == FIO_TYPE_PIPE) {
299 if (td_random(td)) {
300 log_err("fio: can't seek on pipes (no random io)\n");
301 goto err;
302 }
303 }
304
Jens Axboe413d6692007-03-27 15:38:21 +0200305 f->last_free_lookup = 0;
306 f->last_completed_pos = 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200307 f->last_pos = f->file_offset;
Jens Axboe413d6692007-03-27 15:38:21 +0200308 f->flags |= FIO_FILE_OPEN;
309 f->flags &= ~FIO_FILE_CLOSING;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200310
Jens Axboe413d6692007-03-27 15:38:21 +0200311 if (td->io_ops->flags & FIO_DISKLESSIO)
312 goto done;
313
314 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
315 goto err;
316
Jens Axboe66159822007-04-16 21:54:24 +0200317 if (td->o.fadvise_hint &&
318 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
319
Jens Axboe413d6692007-03-27 15:38:21 +0200320 int flags;
321
322 if (td_random(td))
323 flags = POSIX_FADV_RANDOM;
324 else
325 flags = POSIX_FADV_SEQUENTIAL;
326
327 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
328 td_verror(td, errno, "fadvise");
329 goto err;
330 }
331 }
332
333 if (f->file_map)
334 memset(f->file_map, 0, f->num_maps * sizeof(long));
335
336done:
Jens Axboef29b25a2007-07-23 08:56:43 +0200337 log_file(td, f, FIO_LOG_OPEN_FILE);
Jens Axboe413d6692007-03-27 15:38:21 +0200338 td->nr_open_files++;
339 get_file(f);
340 return 0;
341err:
Jens Axboeb2840752007-04-12 12:57:27 +0200342 if (td->io_ops->close_file)
343 td->io_ops->close_file(td, f);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200344 return 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100345}
346
Jens Axboe6977bcd2008-03-01 15:55:36 +0100347int td_io_close_file(struct thread_data *td, struct fio_file *f)
Jens Axboeb5af8292007-03-08 12:43:13 +0100348{
Jens Axboef29b25a2007-07-23 08:56:43 +0200349 if (!(f->flags & FIO_FILE_CLOSING))
350 log_file(td, f, FIO_LOG_CLOSE_FILE);
351
Jens Axboe0ad920e2007-03-13 11:06:45 +0100352 /*
353 * mark as closing, do real close when last io on it has completed
354 */
355 f->flags |= FIO_FILE_CLOSING;
356
Jens Axboe6977bcd2008-03-01 15:55:36 +0100357 return put_file(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100358}