blob: 9de7ca16b2e3401ed877859db41f1047d957dd07 [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 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;
41
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 }
54
55 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 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 Axboe5f350952006-11-07 15:20:59 +010066 INIT_LIST_HEAD(&ops->list);
67 list_add_tail(&ops->list, &engine_list);
Jens Axboe5f350952006-11-07 15:20:59 +010068}
69
70static struct ioengine_ops *find_ioengine(const char *name)
71{
72 struct ioengine_ops *ops;
73 struct list_head *entry;
Jens Axboe2866c822006-10-09 15:57:48 +020074
Jens Axboe5f350952006-11-07 15:20:59 +010075 list_for_each(entry, &engine_list) {
76 ops = list_entry(entry, struct ioengine_ops, list);
Jens Axboebc5b77a2007-02-28 09:32:54 +010077 if (!strcmp(name, ops->name))
Jens Axboe5f350952006-11-07 15:20:59 +010078 return ops;
79 }
80
81 return NULL;
82}
83
84static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
85 const char *engine_lib)
86{
87 struct ioengine_ops *ops;
88 void *dlhandle;
89
Jens Axboe2866c822006-10-09 15:57:48 +020090 dlerror();
91 dlhandle = dlopen(engine_lib, RTLD_LAZY);
Jens Axboed4dbaaa2006-10-09 16:01:27 +020092 if (!dlhandle) {
Jens Axboee1161c32007-02-22 19:36:48 +010093 td_vmsg(td, -1, dlerror(), "dlopen");
Jens Axboed4dbaaa2006-10-09 16:01:27 +020094 return NULL;
95 }
Jens Axboe2866c822006-10-09 15:57:48 +020096
Jens Axboeda51c052006-11-07 16:02:11 +010097 /*
98 * Unlike the included modules, external engines should have a
99 * non-static ioengine structure that we can reference.
100 */
Jens Axboe2866c822006-10-09 15:57:48 +0200101 ops = dlsym(dlhandle, "ioengine");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200102 if (!ops) {
Jens Axboee1161c32007-02-22 19:36:48 +0100103 td_vmsg(td, -1, dlerror(), "dlsym");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200104 dlclose(dlhandle);
105 return NULL;
106 }
Jens Axboe2866c822006-10-09 15:57:48 +0200107
Jens Axboe5f350952006-11-07 15:20:59 +0100108 ops->dlhandle = dlhandle;
109 return ops;
110}
111
112struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
113{
114 struct ioengine_ops *ops, *ret;
115 char engine[16];
116
117 strncpy(engine, name, sizeof(engine) - 1);
118
119 /*
120 * linux libaio has alias names, so convert to what we want
121 */
122 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
123 strcpy(engine, "libaio");
124
125 ops = find_ioengine(engine);
126 if (!ops)
127 ops = dlopen_ioengine(td, name);
128
129 if (!ops) {
130 log_err("fio: engine %s not loadable\n", name);
Jens Axboeb902ceb2006-10-09 16:11:45 +0200131 return NULL;
132 }
133
Jens Axboe8c16d842006-10-20 11:48:33 +0200134 /*
135 * Check that the required methods are there.
136 */
Jens Axboe5f350952006-11-07 15:20:59 +0100137 if (check_engine_ops(ops))
Jens Axboe8c16d842006-10-20 11:48:33 +0200138 return NULL;
Jens Axboe8c16d842006-10-20 11:48:33 +0200139
Jens Axboe84585002006-10-19 20:26:22 +0200140 ret = malloc(sizeof(*ret));
141 memcpy(ret, ops, sizeof(*ret));
142 ret->data = NULL;
Jens Axboe84585002006-10-19 20:26:22 +0200143
144 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100145}
146
Jens Axboe2866c822006-10-09 15:57:48 +0200147void close_ioengine(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100148{
Jens Axboe2866c822006-10-09 15:57:48 +0200149 if (td->io_ops->cleanup)
150 td->io_ops->cleanup(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100151
Jens Axboe5f350952006-11-07 15:20:59 +0100152 if (td->io_ops->dlhandle)
153 dlclose(td->io_ops->dlhandle);
154
Jens Axboe84585002006-10-19 20:26:22 +0200155 free(td->io_ops);
156 td->io_ops = NULL;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200157}
Jens Axboe10ba5352006-10-20 11:39:27 +0200158
159int td_io_prep(struct thread_data *td, struct io_u *io_u)
160{
Jens Axboe36167d82007-02-18 05:41:31 +0100161 if (td->io_ops->prep)
162 return td->io_ops->prep(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200163
164 return 0;
165}
166
Jens Axboe10ba5352006-10-20 11:39:27 +0200167int td_io_getevents(struct thread_data *td, int min, int max,
168 struct timespec *t)
169{
Jens Axboeface81b2007-02-26 10:40:03 +0100170 if (min > 0 && td->io_ops->commit) {
171 int r = td->io_ops->commit(td);
172
173 if (r < 0)
174 return r;
175 }
Jens Axboe36167d82007-02-18 05:41:31 +0100176 if (td->io_ops->getevents)
177 return td->io_ops->getevents(td, min, max, t);
178
179 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200180}
181
182int td_io_queue(struct thread_data *td, struct io_u *io_u)
183{
Jens Axboe7e77dd02007-02-20 10:57:34 +0100184 int ret;
185
Jens Axboe0c6e7512007-02-22 11:19:39 +0100186 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
187 io_u->flags |= IO_U_F_FLIGHT;
188
Jens Axboe11786802007-03-05 18:33:22 +0100189 io_u->error = 0;
190 io_u->resid = 0;
191
Jens Axboe433afcb2007-02-22 10:39:01 +0100192 if (td->io_ops->flags & FIO_SYNCIO) {
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100193 fio_gettime(&io_u->issue_time, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200194
Jens Axboe433afcb2007-02-22 10:39:01 +0100195 /*
196 * for a sync engine, set the timeout upfront
197 */
198 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
199 io_u_set_timeout(td);
200 }
201
Jens Axboe755200a2007-02-19 13:08:12 +0100202 if (io_u->ddir != DDIR_SYNC)
203 td->io_issues[io_u->ddir]++;
204
Jens Axboe7e77dd02007-02-20 10:57:34 +0100205 ret = td->io_ops->queue(td, io_u);
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100206
Jens Axboeeb7c8ae2007-02-26 14:45:12 +0100207 if (ret == FIO_Q_QUEUED) {
208 int r;
209
Jens Axboecb5ab512007-02-26 12:57:09 +0100210 td->io_u_queued++;
Jens Axboeeb7c8ae2007-02-26 14:45:12 +0100211 if (td->io_u_queued > td->iodepth_batch) {
212 r = td_io_commit(td);
213 if (r < 0)
214 return r;
215 }
216 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100217
Jens Axboe433afcb2007-02-22 10:39:01 +0100218 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100219 fio_gettime(&io_u->issue_time, NULL);
220
Jens Axboe433afcb2007-02-22 10:39:01 +0100221 /*
222 * async engine, set the timeout here
223 */
224 if (ret == FIO_Q_QUEUED &&
225 mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
226 io_u_set_timeout(td);
227 }
228
Jens Axboe7e77dd02007-02-20 10:57:34 +0100229 return ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200230}
Jens Axboe8c16d842006-10-20 11:48:33 +0200231
232int td_io_init(struct thread_data *td)
233{
234 if (td->io_ops->init)
235 return td->io_ops->init(td);
236
237 return 0;
238}
Jens Axboe755200a2007-02-19 13:08:12 +0100239
240int td_io_commit(struct thread_data *td)
241{
Jens Axboee1161c32007-02-22 19:36:48 +0100242 if (!td->cur_depth)
243 return 0;
Jens Axboecb5ab512007-02-26 12:57:09 +0100244
245 td->io_u_queued = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100246 if (td->io_ops->commit)
247 return td->io_ops->commit(td);
248
249 return 0;
250}
Jens Axboeb5af8292007-03-08 12:43:13 +0100251
252int td_io_open_file(struct thread_data *td, struct fio_file *f)
253{
Jens Axboea978ba62007-03-08 14:29:03 +0100254 if (td->io_ops->open_file(td, f))
255 return 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100256
Jens Axboea978ba62007-03-08 14:29:03 +0100257 f->last_free_lookup = 0;
258 f->last_completed_pos = 0;
259 f->last_pos = 0;
260 f->open = 1;
261
262 if (f->file_map)
263 memset(f->file_map, 0, f->num_maps * sizeof(long));
264
265 td->nr_open_files++;
266 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100267}
268
269void td_io_close_file(struct thread_data *td, struct fio_file *f)
270{
Jens Axboe860a3042007-03-08 14:09:18 +0100271 if (f->open) {
272 if (td->io_ops->close_file)
273 td->io_ops->close_file(td, f);
274 td->nr_open_files--;
275 f->open = 0;
276 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100277}