blob: 34ae91671a4348c43bacdf9f7eddc2f5e81f020d [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 Axboe5f350952006-11-07 15:20:59 +010059 list_del(&ops->list);
60 INIT_LIST_HEAD(&ops->list);
61}
62
Jens Axboeb2fdda42007-02-20 20:52:51 +010063void register_ioengine(struct ioengine_ops *ops)
Jens Axboe5f350952006-11-07 15:20:59 +010064{
Jens Axboe5f350952006-11-07 15:20:59 +010065 INIT_LIST_HEAD(&ops->list);
66 list_add_tail(&ops->list, &engine_list);
Jens Axboe5f350952006-11-07 15:20:59 +010067}
68
69static struct ioengine_ops *find_ioengine(const char *name)
70{
71 struct ioengine_ops *ops;
72 struct list_head *entry;
Jens Axboe2866c822006-10-09 15:57:48 +020073
Jens Axboe5f350952006-11-07 15:20:59 +010074 list_for_each(entry, &engine_list) {
75 ops = list_entry(entry, struct ioengine_ops, list);
Jens Axboebc5b77a2007-02-28 09:32:54 +010076 if (!strcmp(name, ops->name))
Jens Axboe5f350952006-11-07 15:20:59 +010077 return ops;
78 }
79
80 return NULL;
81}
82
83static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
84 const char *engine_lib)
85{
86 struct ioengine_ops *ops;
87 void *dlhandle;
88
Jens Axboe2866c822006-10-09 15:57:48 +020089 dlerror();
90 dlhandle = dlopen(engine_lib, RTLD_LAZY);
Jens Axboed4dbaaa2006-10-09 16:01:27 +020091 if (!dlhandle) {
Jens Axboee1161c32007-02-22 19:36:48 +010092 td_vmsg(td, -1, dlerror(), "dlopen");
Jens Axboed4dbaaa2006-10-09 16:01:27 +020093 return NULL;
94 }
Jens Axboe2866c822006-10-09 15:57:48 +020095
Jens Axboeda51c052006-11-07 16:02:11 +010096 /*
97 * Unlike the included modules, external engines should have a
98 * non-static ioengine structure that we can reference.
99 */
Jens Axboe2866c822006-10-09 15:57:48 +0200100 ops = dlsym(dlhandle, "ioengine");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200101 if (!ops) {
Jens Axboee1161c32007-02-22 19:36:48 +0100102 td_vmsg(td, -1, dlerror(), "dlsym");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200103 dlclose(dlhandle);
104 return NULL;
105 }
Jens Axboe2866c822006-10-09 15:57:48 +0200106
Jens Axboe5f350952006-11-07 15:20:59 +0100107 ops->dlhandle = dlhandle;
108 return ops;
109}
110
111struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
112{
113 struct ioengine_ops *ops, *ret;
114 char engine[16];
115
116 strncpy(engine, name, sizeof(engine) - 1);
117
118 /*
119 * linux libaio has alias names, so convert to what we want
120 */
121 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
122 strcpy(engine, "libaio");
123
124 ops = find_ioengine(engine);
125 if (!ops)
126 ops = dlopen_ioengine(td, name);
127
128 if (!ops) {
129 log_err("fio: engine %s not loadable\n", name);
Jens Axboeb902ceb2006-10-09 16:11:45 +0200130 return NULL;
131 }
132
Jens Axboe8c16d842006-10-20 11:48:33 +0200133 /*
134 * Check that the required methods are there.
135 */
Jens Axboe5f350952006-11-07 15:20:59 +0100136 if (check_engine_ops(ops))
Jens Axboe8c16d842006-10-20 11:48:33 +0200137 return NULL;
Jens Axboe8c16d842006-10-20 11:48:33 +0200138
Jens Axboe84585002006-10-19 20:26:22 +0200139 ret = malloc(sizeof(*ret));
140 memcpy(ret, ops, sizeof(*ret));
141 ret->data = NULL;
Jens Axboe84585002006-10-19 20:26:22 +0200142
143 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100144}
145
Jens Axboe2866c822006-10-09 15:57:48 +0200146void close_ioengine(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100147{
Jens Axboe2866c822006-10-09 15:57:48 +0200148 if (td->io_ops->cleanup)
149 td->io_ops->cleanup(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100150
Jens Axboe5f350952006-11-07 15:20:59 +0100151 if (td->io_ops->dlhandle)
152 dlclose(td->io_ops->dlhandle);
153
Jens Axboe84585002006-10-19 20:26:22 +0200154 free(td->io_ops);
155 td->io_ops = NULL;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200156}
Jens Axboe10ba5352006-10-20 11:39:27 +0200157
158int td_io_prep(struct thread_data *td, struct io_u *io_u)
159{
Jens Axboe36167d82007-02-18 05:41:31 +0100160 if (td->io_ops->prep)
161 return td->io_ops->prep(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200162
163 return 0;
164}
165
Jens Axboe10ba5352006-10-20 11:39:27 +0200166int td_io_getevents(struct thread_data *td, int min, int max,
167 struct timespec *t)
168{
Jens Axboeface81b2007-02-26 10:40:03 +0100169 if (min > 0 && td->io_ops->commit) {
170 int r = td->io_ops->commit(td);
171
172 if (r < 0)
173 return r;
174 }
Jens Axboe36167d82007-02-18 05:41:31 +0100175 if (td->io_ops->getevents)
176 return td->io_ops->getevents(td, min, max, t);
177
178 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200179}
180
181int td_io_queue(struct thread_data *td, struct io_u *io_u)
182{
Jens Axboe7e77dd02007-02-20 10:57:34 +0100183 int ret;
184
Jens Axboe0c6e7512007-02-22 11:19:39 +0100185 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
186 io_u->flags |= IO_U_F_FLIGHT;
187
Jens Axboe3d7b4852007-03-13 14:50:28 +0100188 assert(io_u->file->flags & FIO_FILE_OPEN);
189
Jens Axboe11786802007-03-05 18:33:22 +0100190 io_u->error = 0;
191 io_u->resid = 0;
192
Jens Axboe433afcb2007-02-22 10:39:01 +0100193 if (td->io_ops->flags & FIO_SYNCIO) {
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100194 fio_gettime(&io_u->issue_time, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200195
Jens Axboe433afcb2007-02-22 10:39:01 +0100196 /*
197 * for a sync engine, set the timeout upfront
198 */
199 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
200 io_u_set_timeout(td);
201 }
202
Jens Axboe755200a2007-02-19 13:08:12 +0100203 if (io_u->ddir != DDIR_SYNC)
204 td->io_issues[io_u->ddir]++;
205
Jens Axboeb3605062007-03-12 14:19:47 +0100206 io_u_mark_depth(td, io_u);
207
Jens Axboe7e77dd02007-02-20 10:57:34 +0100208 ret = td->io_ops->queue(td, io_u);
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100209
Jens Axboe0ad920e2007-03-13 11:06:45 +0100210 if (ret == FIO_Q_QUEUED || ret == FIO_Q_COMPLETED)
211 get_file(io_u->file);
212
Jens Axboeeb7c8ae2007-02-26 14:45:12 +0100213 if (ret == FIO_Q_QUEUED) {
214 int r;
215
Jens Axboecb5ab512007-02-26 12:57:09 +0100216 td->io_u_queued++;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100217 if (td->io_u_queued > td->o.iodepth_batch) {
Jens Axboeeb7c8ae2007-02-26 14:45:12 +0100218 r = td_io_commit(td);
219 if (r < 0)
220 return r;
221 }
222 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100223
Jens Axboe433afcb2007-02-22 10:39:01 +0100224 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100225 fio_gettime(&io_u->issue_time, NULL);
226
Jens Axboe433afcb2007-02-22 10:39:01 +0100227 /*
228 * async engine, set the timeout here
229 */
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);
233 }
234
Jens Axboe7e77dd02007-02-20 10:57:34 +0100235 return ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200236}
Jens Axboe8c16d842006-10-20 11:48:33 +0200237
238int td_io_init(struct thread_data *td)
239{
Jens Axboeeeb12162007-03-20 10:47:45 +0100240 int ret = 0;
Jens Axboe8c16d842006-10-20 11:48:33 +0200241
Jens Axboeeeb12162007-03-20 10:47:45 +0100242 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");
246 }
247
248 return ret;
Jens Axboe8c16d842006-10-20 11:48:33 +0200249}
Jens Axboe755200a2007-02-19 13:08:12 +0100250
251int td_io_commit(struct thread_data *td)
252{
Jens Axboee1161c32007-02-22 19:36:48 +0100253 if (!td->cur_depth)
254 return 0;
Jens Axboecb5ab512007-02-26 12:57:09 +0100255
256 td->io_u_queued = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100257 if (td->io_ops->commit)
258 return td->io_ops->commit(td);
259
260 return 0;
261}
Jens Axboeb5af8292007-03-08 12:43:13 +0100262
263int td_io_open_file(struct thread_data *td, struct fio_file *f)
264{
Jens Axboe413d6692007-03-27 15:38:21 +0200265 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);
Jens Axboeb5af8292007-03-08 12:43:13 +0100270
Jens Axboe413d6692007-03-27 15:38:21 +0200271 return 1;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200272 }
Jens Axboea978ba62007-03-08 14:29:03 +0100273
Jens Axboe66159822007-04-16 21:54:24 +0200274 if (f->filetype == FIO_TYPE_PIPE) {
275 if (td_random(td)) {
276 log_err("fio: can't seek on pipes (no random io)\n");
277 goto err;
278 }
279 }
280
Jens Axboe413d6692007-03-27 15:38:21 +0200281 f->last_free_lookup = 0;
282 f->last_completed_pos = 0;
283 f->last_pos = 0;
284 f->flags |= FIO_FILE_OPEN;
285 f->flags &= ~FIO_FILE_CLOSING;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200286
Jens Axboe413d6692007-03-27 15:38:21 +0200287 if (td->io_ops->flags & FIO_DISKLESSIO)
288 goto done;
289
290 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
291 goto err;
292
Jens Axboe66159822007-04-16 21:54:24 +0200293 if (td->o.fadvise_hint &&
294 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
295
Jens Axboe413d6692007-03-27 15:38:21 +0200296 int flags;
297
298 if (td_random(td))
299 flags = POSIX_FADV_RANDOM;
300 else
301 flags = POSIX_FADV_SEQUENTIAL;
302
303 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
304 td_verror(td, errno, "fadvise");
305 goto err;
306 }
307 }
308
309 if (f->file_map)
310 memset(f->file_map, 0, f->num_maps * sizeof(long));
311
312done:
313 td->nr_open_files++;
314 get_file(f);
315 return 0;
316err:
Jens Axboeb2840752007-04-12 12:57:27 +0200317 if (td->io_ops->close_file)
318 td->io_ops->close_file(td, f);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200319 return 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100320}
321
322void td_io_close_file(struct thread_data *td, struct fio_file *f)
323{
Jens Axboe0ad920e2007-03-13 11:06:45 +0100324 /*
325 * mark as closing, do real close when last io on it has completed
326 */
327 f->flags |= FIO_FILE_CLOSING;
328
329 put_file(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100330}