blob: ab5b2245131fb1abb5d2e6b51a94227522e78991 [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 Axboe8c16d842006-10-20 11:48:33 +020017
Jens Axboeebac4652005-12-08 15:25:21 +010018#include "fio.h"
19#include "os.h"
20
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 Axboe8c16d842006-10-20 11:48:33 +020030 /*
31 * cpu thread doesn't need to provide anything
32 */
33 if (ops->flags & FIO_CPUIO)
34 return 0;
35
Jens Axboe36167d82007-02-18 05:41:31 +010036 if (!ops->queue) {
37 log_err("%s: no queue handler\n", ops->name);
38 return 1;
39 }
40
41 /*
42 * sync engines only need a ->queue()
43 */
44 if (ops->flags & FIO_SYNCIO)
45 return 0;
46
Jens Axboe8c16d842006-10-20 11:48:33 +020047 if (!ops->event) {
Jens Axboe36167d82007-02-18 05:41:31 +010048 log_err("%s: no event handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020049 return 1;
50 }
51 if (!ops->getevents) {
Jens Axboe36167d82007-02-18 05:41:31 +010052 log_err("%s: no getevents handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020053 return 1;
54 }
55 if (!ops->queue) {
Jens Axboe36167d82007-02-18 05:41:31 +010056 log_err("%s: no queue handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020057 return 1;
58 }
59
60 return 0;
61}
62
Jens Axboe5f350952006-11-07 15:20:59 +010063void unregister_ioengine(struct ioengine_ops *ops)
Jens Axboeebac4652005-12-08 15:25:21 +010064{
Jens Axboe5f350952006-11-07 15:20:59 +010065 list_del(&ops->list);
66 INIT_LIST_HEAD(&ops->list);
67}
68
Jens Axboeb2fdda42007-02-20 20:52:51 +010069void register_ioengine(struct ioengine_ops *ops)
Jens Axboe5f350952006-11-07 15:20:59 +010070{
Jens Axboe5f350952006-11-07 15:20:59 +010071 INIT_LIST_HEAD(&ops->list);
72 list_add_tail(&ops->list, &engine_list);
Jens Axboe5f350952006-11-07 15:20:59 +010073}
74
75static struct ioengine_ops *find_ioengine(const char *name)
76{
77 struct ioengine_ops *ops;
78 struct list_head *entry;
79 char engine[16];
Jens Axboeebac4652005-12-08 15:25:21 +010080
Jens Axboe0bbab0e2006-11-02 09:18:36 +010081 strncpy(engine, name, sizeof(engine) - 1);
Jens Axboeebac4652005-12-08 15:25:21 +010082
Jens Axboe2866c822006-10-09 15:57:48 +020083 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
84 strcpy(engine, "libaio");
85
Jens Axboe5f350952006-11-07 15:20:59 +010086 list_for_each(entry, &engine_list) {
87 ops = list_entry(entry, struct ioengine_ops, list);
88 if (!strcmp(engine, ops->name))
89 return ops;
90 }
91
92 return NULL;
93}
94
95static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
96 const char *engine_lib)
97{
98 struct ioengine_ops *ops;
99 void *dlhandle;
100
Jens Axboe2866c822006-10-09 15:57:48 +0200101 dlerror();
102 dlhandle = dlopen(engine_lib, RTLD_LAZY);
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200103 if (!dlhandle) {
104 td_vmsg(td, -1, dlerror());
105 return NULL;
106 }
Jens Axboe2866c822006-10-09 15:57:48 +0200107
Jens Axboeda51c052006-11-07 16:02:11 +0100108 /*
109 * Unlike the included modules, external engines should have a
110 * non-static ioengine structure that we can reference.
111 */
Jens Axboe2866c822006-10-09 15:57:48 +0200112 ops = dlsym(dlhandle, "ioengine");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200113 if (!ops) {
114 td_vmsg(td, -1, dlerror());
115 dlclose(dlhandle);
116 return NULL;
117 }
Jens Axboe2866c822006-10-09 15:57:48 +0200118
Jens Axboe5f350952006-11-07 15:20:59 +0100119 ops->dlhandle = dlhandle;
120 return ops;
121}
122
123struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
124{
125 struct ioengine_ops *ops, *ret;
126 char engine[16];
127
128 strncpy(engine, name, sizeof(engine) - 1);
129
130 /*
131 * linux libaio has alias names, so convert to what we want
132 */
133 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
134 strcpy(engine, "libaio");
135
136 ops = find_ioengine(engine);
137 if (!ops)
138 ops = dlopen_ioengine(td, name);
139
140 if (!ops) {
141 log_err("fio: engine %s not loadable\n", name);
Jens Axboeb902ceb2006-10-09 16:11:45 +0200142 return NULL;
143 }
144
Jens Axboe8c16d842006-10-20 11:48:33 +0200145 /*
146 * Check that the required methods are there.
147 */
Jens Axboe5f350952006-11-07 15:20:59 +0100148 if (check_engine_ops(ops))
Jens Axboe8c16d842006-10-20 11:48:33 +0200149 return NULL;
Jens Axboe8c16d842006-10-20 11:48:33 +0200150
Jens Axboe84585002006-10-19 20:26:22 +0200151 ret = malloc(sizeof(*ret));
152 memcpy(ret, ops, sizeof(*ret));
153 ret->data = NULL;
Jens Axboe84585002006-10-19 20:26:22 +0200154
155 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100156}
157
Jens Axboe2866c822006-10-09 15:57:48 +0200158void close_ioengine(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100159{
Jens Axboe2866c822006-10-09 15:57:48 +0200160 if (td->io_ops->cleanup)
161 td->io_ops->cleanup(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100162
Jens Axboe5f350952006-11-07 15:20:59 +0100163 if (td->io_ops->dlhandle)
164 dlclose(td->io_ops->dlhandle);
165
Jens Axboe84585002006-10-19 20:26:22 +0200166 free(td->io_ops);
167 td->io_ops = NULL;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200168}
Jens Axboe10ba5352006-10-20 11:39:27 +0200169
170int td_io_prep(struct thread_data *td, struct io_u *io_u)
171{
Jens Axboe36167d82007-02-18 05:41:31 +0100172 if (td->io_ops->prep)
173 return td->io_ops->prep(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200174
175 return 0;
176}
177
Jens Axboe10ba5352006-10-20 11:39:27 +0200178int td_io_getevents(struct thread_data *td, int min, int max,
179 struct timespec *t)
180{
Jens Axboe36167d82007-02-18 05:41:31 +0100181 if (td->io_ops->getevents)
182 return td->io_ops->getevents(td, min, max, t);
183
184 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200185}
186
187int td_io_queue(struct thread_data *td, struct io_u *io_u)
188{
Jens Axboe7e77dd02007-02-20 10:57:34 +0100189 int ret;
190
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100191 if (td->io_ops->flags & FIO_SYNCIO)
192 fio_gettime(&io_u->issue_time, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200193
Jens Axboe755200a2007-02-19 13:08:12 +0100194 if (io_u->ddir != DDIR_SYNC)
195 td->io_issues[io_u->ddir]++;
196
Jens Axboe7e77dd02007-02-20 10:57:34 +0100197 ret = td->io_ops->queue(td, io_u);
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100198
199 if ((td->io_ops->flags & FIO_SYNCIO) == 0)
200 fio_gettime(&io_u->issue_time, NULL);
201
Jens Axboe7e77dd02007-02-20 10:57:34 +0100202 return ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200203}
Jens Axboe8c16d842006-10-20 11:48:33 +0200204
205int td_io_init(struct thread_data *td)
206{
207 if (td->io_ops->init)
208 return td->io_ops->init(td);
209
210 return 0;
211}
Jens Axboe755200a2007-02-19 13:08:12 +0100212
213int td_io_commit(struct thread_data *td)
214{
215 if (td->io_ops->commit)
216 return td->io_ops->commit(td);
217
218 return 0;
219}