blob: a7ccad66f810b982b7a0d15b355aa6efcbde0334 [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"
20#include "os.h"
21
Jens Axboe5f350952006-11-07 15:20:59 +010022static LIST_HEAD(engine_list);
23
Jens Axboe8c16d842006-10-20 11:48:33 +020024static int check_engine_ops(struct ioengine_ops *ops)
25{
Jens Axboe5f350952006-11-07 15:20:59 +010026 if (ops->version != FIO_IOOPS_VERSION) {
27 log_err("bad ioops version %d (want %d)\n", ops->version, FIO_IOOPS_VERSION);
28 return 1;
29 }
30
Jens Axboe8c16d842006-10-20 11:48:33 +020031 /*
32 * cpu thread doesn't need to provide anything
33 */
34 if (ops->flags & FIO_CPUIO)
35 return 0;
36
Jens Axboe36167d82007-02-18 05:41:31 +010037 if (!ops->queue) {
38 log_err("%s: no queue handler\n", ops->name);
39 return 1;
40 }
41
42 /*
43 * sync engines only need a ->queue()
44 */
45 if (ops->flags & FIO_SYNCIO)
46 return 0;
47
Jens Axboe8c16d842006-10-20 11:48:33 +020048 if (!ops->event) {
Jens Axboe36167d82007-02-18 05:41:31 +010049 log_err("%s: no event handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020050 return 1;
51 }
52 if (!ops->getevents) {
Jens Axboe36167d82007-02-18 05:41:31 +010053 log_err("%s: no getevents handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020054 return 1;
55 }
56 if (!ops->queue) {
Jens Axboe36167d82007-02-18 05:41:31 +010057 log_err("%s: no queue handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020058 return 1;
59 }
60
61 return 0;
62}
63
Jens Axboe5f350952006-11-07 15:20:59 +010064void unregister_ioengine(struct ioengine_ops *ops)
Jens Axboeebac4652005-12-08 15:25:21 +010065{
Jens Axboe5f350952006-11-07 15:20:59 +010066 list_del(&ops->list);
67 INIT_LIST_HEAD(&ops->list);
68}
69
Jens Axboeb2fdda42007-02-20 20:52:51 +010070void register_ioengine(struct ioengine_ops *ops)
Jens Axboe5f350952006-11-07 15:20:59 +010071{
Jens Axboe5f350952006-11-07 15:20:59 +010072 INIT_LIST_HEAD(&ops->list);
73 list_add_tail(&ops->list, &engine_list);
Jens Axboe5f350952006-11-07 15:20:59 +010074}
75
76static struct ioengine_ops *find_ioengine(const char *name)
77{
78 struct ioengine_ops *ops;
79 struct list_head *entry;
Jens Axboe2866c822006-10-09 15:57:48 +020080
Jens Axboe5f350952006-11-07 15:20:59 +010081 list_for_each(entry, &engine_list) {
82 ops = list_entry(entry, struct ioengine_ops, list);
Jens Axboebc5b77a2007-02-28 09:32:54 +010083 if (!strcmp(name, ops->name))
Jens Axboe5f350952006-11-07 15:20:59 +010084 return ops;
85 }
86
87 return NULL;
88}
89
90static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
91 const char *engine_lib)
92{
93 struct ioengine_ops *ops;
94 void *dlhandle;
95
Jens Axboe2866c822006-10-09 15:57:48 +020096 dlerror();
97 dlhandle = dlopen(engine_lib, RTLD_LAZY);
Jens Axboed4dbaaa2006-10-09 16:01:27 +020098 if (!dlhandle) {
Jens Axboee1161c32007-02-22 19:36:48 +010099 td_vmsg(td, -1, dlerror(), "dlopen");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200100 return NULL;
101 }
Jens Axboe2866c822006-10-09 15:57:48 +0200102
Jens Axboeda51c052006-11-07 16:02:11 +0100103 /*
104 * Unlike the included modules, external engines should have a
105 * non-static ioengine structure that we can reference.
106 */
Jens Axboe2866c822006-10-09 15:57:48 +0200107 ops = dlsym(dlhandle, "ioengine");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200108 if (!ops) {
Jens Axboee1161c32007-02-22 19:36:48 +0100109 td_vmsg(td, -1, dlerror(), "dlsym");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200110 dlclose(dlhandle);
111 return NULL;
112 }
Jens Axboe2866c822006-10-09 15:57:48 +0200113
Jens Axboe5f350952006-11-07 15:20:59 +0100114 ops->dlhandle = dlhandle;
115 return ops;
116}
117
118struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
119{
120 struct ioengine_ops *ops, *ret;
121 char engine[16];
122
123 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 Axboe2866c822006-10-09 15:57:48 +0200155 if (td->io_ops->cleanup)
156 td->io_ops->cleanup(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100157
Jens Axboe5f350952006-11-07 15:20:59 +0100158 if (td->io_ops->dlhandle)
159 dlclose(td->io_ops->dlhandle);
160
Jens Axboe19a98c42007-02-22 12:26:20 +0100161#if 0
162 /* we can't do this for threads, so just leak it, it's exiting */
Jens Axboe84585002006-10-19 20:26:22 +0200163 free(td->io_ops);
Jens Axboe19a98c42007-02-22 12:26:20 +0100164#endif
Jens Axboe84585002006-10-19 20:26:22 +0200165 td->io_ops = NULL;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200166}
Jens Axboe10ba5352006-10-20 11:39:27 +0200167
168int td_io_prep(struct thread_data *td, struct io_u *io_u)
169{
Jens Axboe36167d82007-02-18 05:41:31 +0100170 if (td->io_ops->prep)
171 return td->io_ops->prep(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200172
173 return 0;
174}
175
Jens Axboe10ba5352006-10-20 11:39:27 +0200176int td_io_getevents(struct thread_data *td, int min, int max,
177 struct timespec *t)
178{
Jens Axboeface81b2007-02-26 10:40:03 +0100179 if (min > 0 && td->io_ops->commit) {
180 int r = td->io_ops->commit(td);
181
182 if (r < 0)
183 return r;
184 }
Jens Axboe36167d82007-02-18 05:41:31 +0100185 if (td->io_ops->getevents)
186 return td->io_ops->getevents(td, min, max, t);
187
188 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200189}
190
191int td_io_queue(struct thread_data *td, struct io_u *io_u)
192{
Jens Axboe7e77dd02007-02-20 10:57:34 +0100193 int ret;
194
Jens Axboe0c6e7512007-02-22 11:19:39 +0100195 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
196 io_u->flags |= IO_U_F_FLIGHT;
197
Jens Axboe433afcb2007-02-22 10:39:01 +0100198 if (td->io_ops->flags & FIO_SYNCIO) {
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100199 fio_gettime(&io_u->issue_time, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200200
Jens Axboe433afcb2007-02-22 10:39:01 +0100201 /*
202 * for a sync engine, set the timeout upfront
203 */
204 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
205 io_u_set_timeout(td);
206 }
207
Jens Axboe755200a2007-02-19 13:08:12 +0100208 if (io_u->ddir != DDIR_SYNC)
209 td->io_issues[io_u->ddir]++;
210
Jens Axboe7e77dd02007-02-20 10:57:34 +0100211 ret = td->io_ops->queue(td, io_u);
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100212
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 Axboeeb7c8ae2007-02-26 14:45:12 +0100217 if (td->io_u_queued > td->iodepth_batch) {
218 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{
240 if (td->io_ops->init)
241 return td->io_ops->init(td);
242
243 return 0;
244}
Jens Axboe755200a2007-02-19 13:08:12 +0100245
246int td_io_commit(struct thread_data *td)
247{
Jens Axboee1161c32007-02-22 19:36:48 +0100248 if (!td->cur_depth)
249 return 0;
Jens Axboecb5ab512007-02-26 12:57:09 +0100250
251 td->io_u_queued = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100252 if (td->io_ops->commit)
253 return td->io_ops->commit(td);
254
255 return 0;
256}