Commit | Line | Data |
---|---|---|
ebac4655 JA |
1 | /* |
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> | |
5c4e1dbc | 15 | #include <string.h> |
2866c82d | 16 | #include <dlfcn.h> |
0c6e7517 | 17 | #include <assert.h> |
8c16d840 | 18 | |
ebac4655 JA |
19 | #include "fio.h" |
20 | #include "os.h" | |
21 | ||
5f350952 JA |
22 | static LIST_HEAD(engine_list); |
23 | ||
8c16d840 JA |
24 | static int check_engine_ops(struct ioengine_ops *ops) |
25 | { | |
5f350952 JA |
26 | 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 | ||
36167d82 JA |
31 | 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 | ||
8c16d840 | 42 | if (!ops->event) { |
36167d82 | 43 | log_err("%s: no event handler\n", ops->name); |
8c16d840 JA |
44 | return 1; |
45 | } | |
46 | if (!ops->getevents) { | |
36167d82 | 47 | log_err("%s: no getevents handler\n", ops->name); |
8c16d840 JA |
48 | return 1; |
49 | } | |
50 | if (!ops->queue) { | |
36167d82 | 51 | log_err("%s: no queue handler\n", ops->name); |
8c16d840 JA |
52 | return 1; |
53 | } | |
54 | ||
55 | return 0; | |
56 | } | |
57 | ||
5f350952 | 58 | void unregister_ioengine(struct ioengine_ops *ops) |
ebac4655 | 59 | { |
5f350952 JA |
60 | list_del(&ops->list); |
61 | INIT_LIST_HEAD(&ops->list); | |
62 | } | |
63 | ||
b2fdda43 | 64 | void register_ioengine(struct ioengine_ops *ops) |
5f350952 | 65 | { |
5f350952 JA |
66 | INIT_LIST_HEAD(&ops->list); |
67 | list_add_tail(&ops->list, &engine_list); | |
5f350952 JA |
68 | } |
69 | ||
70 | static struct ioengine_ops *find_ioengine(const char *name) | |
71 | { | |
72 | struct ioengine_ops *ops; | |
73 | struct list_head *entry; | |
ebac4655 | 74 | |
5f350952 JA |
75 | list_for_each(entry, &engine_list) { |
76 | ops = list_entry(entry, struct ioengine_ops, list); | |
bc5b77a8 | 77 | if (!strcmp(name, ops->name)) |
5f350952 JA |
78 | return ops; |
79 | } | |
80 | ||
81 | return NULL; | |
82 | } | |
83 | ||
84 | static struct ioengine_ops *dlopen_ioengine(struct thread_data *td, | |
85 | const char *engine_lib) | |
86 | { | |
87 | struct ioengine_ops *ops; | |
88 | void *dlhandle; | |
89 | ||
2866c82d JA |
90 | dlerror(); |
91 | dlhandle = dlopen(engine_lib, RTLD_LAZY); | |
d4dbaaa8 | 92 | if (!dlhandle) { |
e1161c32 | 93 | td_vmsg(td, -1, dlerror(), "dlopen"); |
d4dbaaa8 JA |
94 | return NULL; |
95 | } | |
8756e4d4 | 96 | |
da51c050 JA |
97 | /* |
98 | * Unlike the included modules, external engines should have a | |
99 | * non-static ioengine structure that we can reference. | |
100 | */ | |
2866c82d | 101 | ops = dlsym(dlhandle, "ioengine"); |
d4dbaaa8 | 102 | if (!ops) { |
e1161c32 | 103 | td_vmsg(td, -1, dlerror(), "dlsym"); |
d4dbaaa8 JA |
104 | dlclose(dlhandle); |
105 | return NULL; | |
106 | } | |
8756e4d4 | 107 | |
5f350952 JA |
108 | ops->dlhandle = dlhandle; |
109 | return ops; | |
110 | } | |
111 | ||
112 | struct 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); | |
b902ceb5 JA |
131 | return NULL; |
132 | } | |
133 | ||
8c16d840 JA |
134 | /* |
135 | * Check that the required methods are there. | |
136 | */ | |
5f350952 | 137 | if (check_engine_ops(ops)) |
8c16d840 | 138 | return NULL; |
8c16d840 | 139 | |
84585003 JA |
140 | ret = malloc(sizeof(*ret)); |
141 | memcpy(ret, ops, sizeof(*ret)); | |
142 | ret->data = NULL; | |
84585003 JA |
143 | |
144 | return ret; | |
8756e4d4 JA |
145 | } |
146 | ||
2866c82d | 147 | void close_ioengine(struct thread_data *td) |
8756e4d4 | 148 | { |
2866c82d JA |
149 | if (td->io_ops->cleanup) |
150 | td->io_ops->cleanup(td); | |
b990b5c0 | 151 | |
5f350952 JA |
152 | if (td->io_ops->dlhandle) |
153 | dlclose(td->io_ops->dlhandle); | |
154 | ||
84585003 JA |
155 | free(td->io_ops); |
156 | td->io_ops = NULL; | |
b990b5c0 | 157 | } |
10ba535a JA |
158 | |
159 | int td_io_prep(struct thread_data *td, struct io_u *io_u) | |
160 | { | |
36167d82 JA |
161 | if (td->io_ops->prep) |
162 | return td->io_ops->prep(td, io_u); | |
10ba535a JA |
163 | |
164 | return 0; | |
165 | } | |
166 | ||
10ba535a JA |
167 | int td_io_getevents(struct thread_data *td, int min, int max, |
168 | struct timespec *t) | |
169 | { | |
face81b2 JA |
170 | if (min > 0 && td->io_ops->commit) { |
171 | int r = td->io_ops->commit(td); | |
172 | ||
173 | if (r < 0) | |
174 | return r; | |
175 | } | |
36167d82 JA |
176 | if (td->io_ops->getevents) |
177 | return td->io_ops->getevents(td, min, max, t); | |
178 | ||
179 | return 0; | |
10ba535a JA |
180 | } |
181 | ||
182 | int td_io_queue(struct thread_data *td, struct io_u *io_u) | |
183 | { | |
7e77dd02 JA |
184 | int ret; |
185 | ||
0c6e7517 JA |
186 | assert((io_u->flags & IO_U_F_FLIGHT) == 0); |
187 | io_u->flags |= IO_U_F_FLIGHT; | |
188 | ||
3d7b485f JA |
189 | assert(io_u->file->flags & FIO_FILE_OPEN); |
190 | ||
11786802 JA |
191 | io_u->error = 0; |
192 | io_u->resid = 0; | |
193 | ||
433afcb4 | 194 | if (td->io_ops->flags & FIO_SYNCIO) { |
5aeb77df | 195 | fio_gettime(&io_u->issue_time, NULL); |
10ba535a | 196 | |
433afcb4 JA |
197 | /* |
198 | * for a sync engine, set the timeout upfront | |
199 | */ | |
200 | if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT) | |
201 | io_u_set_timeout(td); | |
202 | } | |
203 | ||
755200a3 JA |
204 | if (io_u->ddir != DDIR_SYNC) |
205 | td->io_issues[io_u->ddir]++; | |
206 | ||
b3605062 JA |
207 | io_u_mark_depth(td, io_u); |
208 | ||
7e77dd02 | 209 | ret = td->io_ops->queue(td, io_u); |
5aeb77df | 210 | |
0ad920e7 JA |
211 | if (ret == FIO_Q_QUEUED || ret == FIO_Q_COMPLETED) |
212 | get_file(io_u->file); | |
213 | ||
eb7c8ae2 JA |
214 | if (ret == FIO_Q_QUEUED) { |
215 | int r; | |
216 | ||
cb5ab512 | 217 | td->io_u_queued++; |
2dc1bbeb | 218 | if (td->io_u_queued > td->o.iodepth_batch) { |
eb7c8ae2 JA |
219 | r = td_io_commit(td); |
220 | if (r < 0) | |
221 | return r; | |
222 | } | |
223 | } | |
cb5ab512 | 224 | |
433afcb4 | 225 | if ((td->io_ops->flags & FIO_SYNCIO) == 0) { |
5aeb77df JA |
226 | fio_gettime(&io_u->issue_time, NULL); |
227 | ||
433afcb4 JA |
228 | /* |
229 | * async engine, set the timeout here | |
230 | */ | |
231 | if (ret == FIO_Q_QUEUED && | |
232 | mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT) | |
233 | io_u_set_timeout(td); | |
234 | } | |
235 | ||
7e77dd02 | 236 | return ret; |
10ba535a | 237 | } |
8c16d840 JA |
238 | |
239 | int td_io_init(struct thread_data *td) | |
240 | { | |
eeb12160 | 241 | int ret = 0; |
8c16d840 | 242 | |
eeb12160 JA |
243 | if (td->io_ops->init) { |
244 | ret = td->io_ops->init(td); | |
245 | if (ret && td->o.iodepth > 1) | |
246 | log_err("fio: io engine init failed. Perhaps try reducing io dpeth?\n"); | |
247 | } | |
248 | ||
249 | return ret; | |
8c16d840 | 250 | } |
755200a3 JA |
251 | |
252 | int td_io_commit(struct thread_data *td) | |
253 | { | |
e1161c32 JA |
254 | if (!td->cur_depth) |
255 | return 0; | |
cb5ab512 JA |
256 | |
257 | td->io_u_queued = 0; | |
755200a3 JA |
258 | if (td->io_ops->commit) |
259 | return td->io_ops->commit(td); | |
260 | ||
261 | return 0; | |
262 | } | |
b5af8293 JA |
263 | |
264 | int td_io_open_file(struct thread_data *td, struct fio_file *f) | |
265 | { | |
a978ba68 JA |
266 | if (td->io_ops->open_file(td, f)) |
267 | return 1; | |
b5af8293 | 268 | |
a978ba68 JA |
269 | f->last_free_lookup = 0; |
270 | f->last_completed_pos = 0; | |
271 | f->last_pos = 0; | |
f11bd94d | 272 | f->flags |= FIO_FILE_OPEN; |
0ad920e7 | 273 | f->flags &= ~FIO_FILE_CLOSING; |
a978ba68 JA |
274 | |
275 | if (f->file_map) | |
276 | memset(f->file_map, 0, f->num_maps * sizeof(long)); | |
277 | ||
278 | td->nr_open_files++; | |
0ad920e7 | 279 | get_file(f); |
a978ba68 | 280 | return 0; |
b5af8293 JA |
281 | } |
282 | ||
283 | void td_io_close_file(struct thread_data *td, struct fio_file *f) | |
284 | { | |
0ad920e7 JA |
285 | /* |
286 | * mark as closing, do real close when last io on it has completed | |
287 | */ | |
288 | f->flags |= FIO_FILE_CLOSING; | |
289 | ||
290 | put_file(td, f); | |
b5af8293 | 291 | } |
0ad920e7 | 292 |