blob: 9daeaab0bb6b599291f2dd1c24163323405cb501 [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 Axboe01743ee2008-06-02 12:19:19 +020021static FLIST_HEAD(engine_list);
Jens Axboe5f350952006-11-07 15:20:59 +010022
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) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010026 log_err("bad ioops version %d (want %d)\n", ops->version,
27 FIO_IOOPS_VERSION);
Jens Axboe5f350952006-11-07 15:20:59 +010028 return 1;
29 }
30
Jens Axboe36167d82007-02-18 05:41:31 +010031 if (!ops->queue) {
32 log_err("%s: no queue handler\n", ops->name);
33 return 1;
34 }
35
36 /*
37 * sync engines only need a ->queue()
38 */
39 if (ops->flags & FIO_SYNCIO)
40 return 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010041
Jens Axboe8c16d842006-10-20 11:48:33 +020042 if (!ops->event) {
Jens Axboe36167d82007-02-18 05:41:31 +010043 log_err("%s: no event handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020044 return 1;
45 }
46 if (!ops->getevents) {
Jens Axboe36167d82007-02-18 05:41:31 +010047 log_err("%s: no getevents handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020048 return 1;
49 }
50 if (!ops->queue) {
Jens Axboe36167d82007-02-18 05:41:31 +010051 log_err("%s: no queue handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020052 return 1;
53 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +010054
Jens Axboe8c16d842006-10-20 11:48:33 +020055 return 0;
56}
57
Jens Axboe5f350952006-11-07 15:20:59 +010058void unregister_ioengine(struct ioengine_ops *ops)
Jens Axboeebac4652005-12-08 15:25:21 +010059{
Jens Axboeee56ad52008-02-01 10:30:20 +010060 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
Jens Axboe01743ee2008-06-02 12:19:19 +020061 flist_del(&ops->list);
62 INIT_FLIST_HEAD(&ops->list);
Jens Axboe5f350952006-11-07 15:20:59 +010063}
64
Jens Axboeb2fdda42007-02-20 20:52:51 +010065void register_ioengine(struct ioengine_ops *ops)
Jens Axboe5f350952006-11-07 15:20:59 +010066{
Jens Axboeee56ad52008-02-01 10:30:20 +010067 dprint(FD_IO, "ioengine %s registered\n", ops->name);
Jens Axboe01743ee2008-06-02 12:19:19 +020068 INIT_FLIST_HEAD(&ops->list);
69 flist_add_tail(&ops->list, &engine_list);
Jens Axboe5f350952006-11-07 15:20:59 +010070}
71
72static struct ioengine_ops *find_ioengine(const char *name)
73{
74 struct ioengine_ops *ops;
Jens Axboe01743ee2008-06-02 12:19:19 +020075 struct flist_head *entry;
Jens Axboe2866c822006-10-09 15:57:48 +020076
Jens Axboe01743ee2008-06-02 12:19:19 +020077 flist_for_each(entry, &engine_list) {
78 ops = flist_entry(entry, struct ioengine_ops, list);
Jens Axboebc5b77a2007-02-28 09:32:54 +010079 if (!strcmp(name, ops->name))
Jens Axboe5f350952006-11-07 15:20:59 +010080 return ops;
81 }
82
83 return NULL;
84}
85
86static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
87 const char *engine_lib)
88{
89 struct ioengine_ops *ops;
90 void *dlhandle;
91
Jens Axboeee56ad52008-02-01 10:30:20 +010092 dprint(FD_IO, "dload engine %s\n", engine_lib);
93
Jens Axboe2866c822006-10-09 15:57:48 +020094 dlerror();
95 dlhandle = dlopen(engine_lib, RTLD_LAZY);
Jens Axboed4dbaaa2006-10-09 16:01:27 +020096 if (!dlhandle) {
Jens Axboee1161c32007-02-22 19:36:48 +010097 td_vmsg(td, -1, dlerror(), "dlopen");
Jens Axboed4dbaaa2006-10-09 16:01:27 +020098 return NULL;
99 }
Jens Axboe2866c822006-10-09 15:57:48 +0200100
Jens Axboeda51c052006-11-07 16:02:11 +0100101 /*
102 * Unlike the included modules, external engines should have a
103 * non-static ioengine structure that we can reference.
104 */
Jens Axboe2866c822006-10-09 15:57:48 +0200105 ops = dlsym(dlhandle, "ioengine");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200106 if (!ops) {
Jens Axboee1161c32007-02-22 19:36:48 +0100107 td_vmsg(td, -1, dlerror(), "dlsym");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200108 dlclose(dlhandle);
109 return NULL;
110 }
Jens Axboe2866c822006-10-09 15:57:48 +0200111
Jens Axboe5f350952006-11-07 15:20:59 +0100112 ops->dlhandle = dlhandle;
113 return ops;
114}
115
116struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
117{
118 struct ioengine_ops *ops, *ret;
119 char engine[16];
120
Jens Axboeee56ad52008-02-01 10:30:20 +0100121 dprint(FD_IO, "load ioengine %s\n", name);
122
Jens Axboe5f350952006-11-07 15:20:59 +0100123 strncpy(engine, name, sizeof(engine) - 1);
124
125 /*
126 * linux libaio has alias names, so convert to what we want
127 */
128 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
129 strcpy(engine, "libaio");
130
131 ops = find_ioengine(engine);
132 if (!ops)
133 ops = dlopen_ioengine(td, name);
134
135 if (!ops) {
136 log_err("fio: engine %s not loadable\n", name);
Jens Axboeb902ceb2006-10-09 16:11:45 +0200137 return NULL;
138 }
139
Jens Axboe8c16d842006-10-20 11:48:33 +0200140 /*
141 * Check that the required methods are there.
142 */
Jens Axboe5f350952006-11-07 15:20:59 +0100143 if (check_engine_ops(ops))
Jens Axboe8c16d842006-10-20 11:48:33 +0200144 return NULL;
Jens Axboe8c16d842006-10-20 11:48:33 +0200145
Jens Axboe84585002006-10-19 20:26:22 +0200146 ret = malloc(sizeof(*ret));
147 memcpy(ret, ops, sizeof(*ret));
148 ret->data = NULL;
Jens Axboe84585002006-10-19 20:26:22 +0200149
150 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100151}
152
Jens Axboe2866c822006-10-09 15:57:48 +0200153void close_ioengine(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100154{
Jens Axboeee56ad52008-02-01 10:30:20 +0100155 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
156
Jens Axboe2992b052008-05-30 22:07:12 +0200157 if (td->io_ops->cleanup) {
Jens Axboe2866c822006-10-09 15:57:48 +0200158 td->io_ops->cleanup(td);
Jens Axboe2992b052008-05-30 22:07:12 +0200159 td->io_ops->data = NULL;
160 }
Jens Axboeebac4652005-12-08 15:25:21 +0100161
Jens Axboe5f350952006-11-07 15:20:59 +0100162 if (td->io_ops->dlhandle)
163 dlclose(td->io_ops->dlhandle);
164
Jens Axboe84585002006-10-19 20:26:22 +0200165 free(td->io_ops);
166 td->io_ops = NULL;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200167}
Jens Axboe10ba5352006-10-20 11:39:27 +0200168
169int td_io_prep(struct thread_data *td, struct io_u *io_u)
170{
Jens Axboeee56ad52008-02-01 10:30:20 +0100171 dprint_io_u(io_u, "prep");
Jens Axboe7101d9c2007-09-12 13:12:39 +0200172 fio_ro_check(td, io_u);
173
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100174 lock_file(td, io_u->file, io_u->ddir);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100175
Jens Axboe2ba1c292008-02-01 13:16:38 +0100176 if (td->io_ops->prep) {
177 int ret = td->io_ops->prep(td, io_u);
178
179 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100180 if (ret)
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100181 unlock_file(td, io_u->file);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100182 return ret;
183 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200184
185 return 0;
186}
187
Jens Axboee7d2e612007-12-11 10:49:39 +0100188int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
Jens Axboe10ba5352006-10-20 11:39:27 +0200189 struct timespec *t)
190{
Jens Axboeee56ad52008-02-01 10:30:20 +0100191 int r = 0;
192
Jens Axboeface81b2007-02-26 10:40:03 +0100193 if (min > 0 && td->io_ops->commit) {
Jens Axboeee56ad52008-02-01 10:30:20 +0100194 r = td->io_ops->commit(td);
Jens Axboeface81b2007-02-26 10:40:03 +0100195 if (r < 0)
Jens Axboeee56ad52008-02-01 10:30:20 +0100196 goto out;
Jens Axboeface81b2007-02-26 10:40:03 +0100197 }
Jens Axboe49504212008-06-05 09:03:30 +0200198 if (max > td->cur_depth)
199 max = td->cur_depth;
200 if (min > max)
201 max = min;
Jens Axboe36167d82007-02-18 05:41:31 +0100202
Jens Axboeee56ad52008-02-01 10:30:20 +0100203 r = 0;
Jens Axboe49504212008-06-05 09:03:30 +0200204 if (max && td->io_ops->getevents)
Jens Axboeee56ad52008-02-01 10:30:20 +0100205 r = td->io_ops->getevents(td, min, max, t);
206out:
Jens Axboe838bc702008-05-22 13:08:23 +0200207 if (r >= 0)
208 io_u_mark_complete(td, r);
Jens Axboeee56ad52008-02-01 10:30:20 +0100209 dprint(FD_IO, "getevents: %d\n", r);
210 return r;
Jens Axboe10ba5352006-10-20 11:39:27 +0200211}
212
Jens Axboe9520ebb2008-10-16 21:03:27 +0200213static inline int get_issue_time(struct thread_data *td)
214{
215 if (td->o.read_iolog_file ||
216 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
217 return 1;
218
219 return 0;
220}
221
Jens Axboe10ba5352006-10-20 11:39:27 +0200222int td_io_queue(struct thread_data *td, struct io_u *io_u)
223{
Jens Axboe7e77dd02007-02-20 10:57:34 +0100224 int ret;
225
Jens Axboeee56ad52008-02-01 10:30:20 +0100226 dprint_io_u(io_u, "queue");
Jens Axboe7101d9c2007-09-12 13:12:39 +0200227 fio_ro_check(td, io_u);
228
Jens Axboe0c6e7512007-02-22 11:19:39 +0100229 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
230 io_u->flags |= IO_U_F_FLIGHT;
231
Jens Axboe3d7b4852007-03-13 14:50:28 +0100232 assert(io_u->file->flags & FIO_FILE_OPEN);
233
Jens Axboe11786802007-03-05 18:33:22 +0100234 io_u->error = 0;
235 io_u->resid = 0;
236
Jens Axboe433afcb2007-02-22 10:39:01 +0100237 if (td->io_ops->flags & FIO_SYNCIO) {
Jens Axboe9520ebb2008-10-16 21:03:27 +0200238 if (get_issue_time(td))
239 fio_gettime(&io_u->issue_time, NULL);
Jens Axboed0c15322008-10-16 20:33:51 +0200240
241 /*
242 * only used for iolog
243 */
244 if (td->o.read_iolog_file)
245 memcpy(&td->last_issue, &io_u->issue_time,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100246 sizeof(struct timeval));
Jens Axboe433afcb2007-02-22 10:39:01 +0100247 }
248
Jens Axboe755200a2007-02-19 13:08:12 +0100249 if (io_u->ddir != DDIR_SYNC)
250 td->io_issues[io_u->ddir]++;
251
Jens Axboe7e77dd02007-02-20 10:57:34 +0100252 ret = td->io_ops->queue(td, io_u);
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100253
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100254 unlock_file(td, io_u->file);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100255
Jens Axboe838bc702008-05-22 13:08:23 +0200256 if (!td->io_ops->commit) {
257 io_u_mark_submit(td, 1);
258 io_u_mark_complete(td, 1);
259 }
260
Jens Axboed8005752008-05-15 09:49:09 +0200261 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]++;
265 }
266 } else if (ret == FIO_Q_QUEUED) {
Jens Axboeeb7c8ae2007-02-26 14:45:12 +0100267 int r;
268
Jens Axboed8005752008-05-15 09:49:09 +0200269 if (io_u->ddir != DDIR_SYNC) {
270 td->io_u_queued++;
271 td->ts.total_io_u[io_u->ddir]++;
272 }
273
274 if (td->io_u_queued >= td->o.iodepth_batch) {
Jens Axboeeb7c8ae2007-02-26 14:45:12 +0100275 r = td_io_commit(td);
276 if (r < 0)
277 return r;
278 }
279 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100280
Jens Axboe433afcb2007-02-22 10:39:01 +0100281 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
Jens Axboe9520ebb2008-10-16 21:03:27 +0200282 if (get_issue_time(td))
283 fio_gettime(&io_u->issue_time, NULL);
Jens Axboed0c15322008-10-16 20:33:51 +0200284
285 /*
286 * only used for iolog
287 */
288 if (td->o.read_iolog_file)
289 memcpy(&td->last_issue, &io_u->issue_time,
290 sizeof(struct timeval));
Jens Axboe433afcb2007-02-22 10:39:01 +0100291 }
292
Jens Axboe7e77dd02007-02-20 10:57:34 +0100293 return ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200294}
Jens Axboe8c16d842006-10-20 11:48:33 +0200295
296int td_io_init(struct thread_data *td)
297{
Jens Axboeeeb12162007-03-20 10:47:45 +0100298 int ret = 0;
Jens Axboe8c16d842006-10-20 11:48:33 +0200299
Jens Axboeeeb12162007-03-20 10:47:45 +0100300 if (td->io_ops->init) {
301 ret = td->io_ops->init(td);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100302 if (ret && td->o.iodepth > 1) {
303 log_err("fio: io engine init failed. Perhaps try"
304 " reducing io depth?\n");
305 }
Jens Axboeeeb12162007-03-20 10:47:45 +0100306 }
307
308 return ret;
Jens Axboe8c16d842006-10-20 11:48:33 +0200309}
Jens Axboe755200a2007-02-19 13:08:12 +0100310
311int td_io_commit(struct thread_data *td)
312{
Jens Axboeee56ad52008-02-01 10:30:20 +0100313 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
314
Jens Axboed8005752008-05-15 09:49:09 +0200315 if (!td->cur_depth || !td->io_u_queued)
Jens Axboee1161c32007-02-22 19:36:48 +0100316 return 0;
Jens Axboecb5ab512007-02-26 12:57:09 +0100317
Jens Axboed8005752008-05-15 09:49:09 +0200318 io_u_mark_depth(td, td->io_u_queued);
Jens Axboecb5ab512007-02-26 12:57:09 +0100319 td->io_u_queued = 0;
Jens Axboed8005752008-05-15 09:49:09 +0200320
Jens Axboe755200a2007-02-19 13:08:12 +0100321 if (td->io_ops->commit)
322 return td->io_ops->commit(td);
323
324 return 0;
325}
Jens Axboeb5af8292007-03-08 12:43:13 +0100326
327int td_io_open_file(struct thread_data *td, struct fio_file *f)
328{
Jens Axboe413d6692007-03-27 15:38:21 +0200329 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");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100332 if (td->error == EMFILE) {
333 log_err("fio: try reducing/setting openfiles (failed"
334 " at %u of %u)\n", td->nr_open_files,
335 td->o.nr_files);
336 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100337
Jens Axboe413d6692007-03-27 15:38:21 +0200338 return 1;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200339 }
Jens Axboea978ba62007-03-08 14:29:03 +0100340
Jens Axboed5707a32008-06-04 15:13:02 +0200341 fio_file_reset(f);
342 f->flags |= FIO_FILE_OPEN;
343 f->flags &= ~FIO_FILE_CLOSING;
344
345 td->nr_open_files++;
346 get_file(f);
347
Jens Axboe66159822007-04-16 21:54:24 +0200348 if (f->filetype == FIO_TYPE_PIPE) {
349 if (td_random(td)) {
350 log_err("fio: can't seek on pipes (no random io)\n");
351 goto err;
352 }
353 }
354
Jens Axboe413d6692007-03-27 15:38:21 +0200355 if (td->io_ops->flags & FIO_DISKLESSIO)
356 goto done;
357
358 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
359 goto err;
360
Jens Axboe66159822007-04-16 21:54:24 +0200361 if (td->o.fadvise_hint &&
362 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
Jens Axboe413d6692007-03-27 15:38:21 +0200363 int flags;
364
365 if (td_random(td))
366 flags = POSIX_FADV_RANDOM;
367 else
368 flags = POSIX_FADV_SEQUENTIAL;
369
370 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
371 td_verror(td, errno, "fadvise");
372 goto err;
373 }
374 }
375
376 if (f->file_map)
Jens Axboede605662008-05-31 00:21:12 +0200377 memset(f->file_map, 0, f->num_maps * sizeof(int));
Jens Axboe413d6692007-03-27 15:38:21 +0200378
Jens Axboee116f2b2008-06-04 15:14:24 +0200379#ifdef FIO_OS_DIRECTIO
380 /*
381 * Some OS's have a distinct call to mark the file non-buffered,
382 * instead of using O_DIRECT (Solaris)
383 */
384 if (td->o.odirect) {
385 int ret = fio_set_odirect(f->fd);
386
387 if (ret) {
388 td_verror(td, ret, "fio_set_odirect");
389 goto err;
390 }
391 }
392#endif
393
Jens Axboe413d6692007-03-27 15:38:21 +0200394done:
Jens Axboef29b25a2007-07-23 08:56:43 +0200395 log_file(td, f, FIO_LOG_OPEN_FILE);
Jens Axboe413d6692007-03-27 15:38:21 +0200396 return 0;
397err:
Jens Axboeb2840752007-04-12 12:57:27 +0200398 if (td->io_ops->close_file)
399 td->io_ops->close_file(td, f);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200400 return 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100401}
402
Jens Axboe6977bcd2008-03-01 15:55:36 +0100403int td_io_close_file(struct thread_data *td, struct fio_file *f)
Jens Axboeb5af8292007-03-08 12:43:13 +0100404{
Jens Axboef29b25a2007-07-23 08:56:43 +0200405 if (!(f->flags & FIO_FILE_CLOSING))
406 log_file(td, f, FIO_LOG_CLOSE_FILE);
407
Jens Axboe0ad920e2007-03-13 11:06:45 +0100408 /*
409 * mark as closing, do real close when last io on it has completed
410 */
411 f->flags |= FIO_FILE_CLOSING;
412
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100413 unlock_file_all(td, f);
Jens Axboe29c13492008-03-01 19:25:20 +0100414
Jens Axboe6977bcd2008-03-01 15:55:36 +0100415 return put_file(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100416}