Fix bad interaction with file open/close and queuing
[fio.git] / ioengines.c
... / ...
CommitLineData
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>
15#include <string.h>
16#include <dlfcn.h>
17#include <assert.h>
18
19#include "fio.h"
20
21static LIST_HEAD(engine_list);
22
23static int check_engine_ops(struct ioengine_ops *ops)
24{
25 if (ops->version != FIO_IOOPS_VERSION) {
26 log_err("bad ioops version %d (want %d)\n", ops->version,
27 FIO_IOOPS_VERSION);
28 return 1;
29 }
30
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
42 if (!ops->event) {
43 log_err("%s: no event handler\n", ops->name);
44 return 1;
45 }
46 if (!ops->getevents) {
47 log_err("%s: no getevents handler\n", ops->name);
48 return 1;
49 }
50 if (!ops->queue) {
51 log_err("%s: no queue handler\n", ops->name);
52 return 1;
53 }
54
55 return 0;
56}
57
58void unregister_ioengine(struct ioengine_ops *ops)
59{
60 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
61 list_del(&ops->list);
62 INIT_LIST_HEAD(&ops->list);
63}
64
65void register_ioengine(struct ioengine_ops *ops)
66{
67 dprint(FD_IO, "ioengine %s registered\n", ops->name);
68 INIT_LIST_HEAD(&ops->list);
69 list_add_tail(&ops->list, &engine_list);
70}
71
72static struct ioengine_ops *find_ioengine(const char *name)
73{
74 struct ioengine_ops *ops;
75 struct list_head *entry;
76
77 list_for_each(entry, &engine_list) {
78 ops = list_entry(entry, struct ioengine_ops, list);
79 if (!strcmp(name, ops->name))
80 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
92 dprint(FD_IO, "dload engine %s\n", engine_lib);
93
94 dlerror();
95 dlhandle = dlopen(engine_lib, RTLD_LAZY);
96 if (!dlhandle) {
97 td_vmsg(td, -1, dlerror(), "dlopen");
98 return NULL;
99 }
100
101 /*
102 * Unlike the included modules, external engines should have a
103 * non-static ioengine structure that we can reference.
104 */
105 ops = dlsym(dlhandle, "ioengine");
106 if (!ops) {
107 td_vmsg(td, -1, dlerror(), "dlsym");
108 dlclose(dlhandle);
109 return NULL;
110 }
111
112 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
121 dprint(FD_IO, "load ioengine %s\n", name);
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);
137 return NULL;
138 }
139
140 /*
141 * Check that the required methods are there.
142 */
143 if (check_engine_ops(ops))
144 return NULL;
145
146 ret = malloc(sizeof(*ret));
147 memcpy(ret, ops, sizeof(*ret));
148 ret->data = NULL;
149
150 return ret;
151}
152
153void close_ioengine(struct thread_data *td)
154{
155 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
156
157 if (td->io_ops->cleanup)
158 td->io_ops->cleanup(td);
159
160 if (td->io_ops->dlhandle)
161 dlclose(td->io_ops->dlhandle);
162
163 free(td->io_ops);
164 td->io_ops = NULL;
165}
166
167int td_io_prep(struct thread_data *td, struct io_u *io_u)
168{
169 dprint_io_u(io_u, "prep");
170 fio_ro_check(td, io_u);
171
172 lock_file(td, io_u->file, io_u->ddir);
173
174 if (td->io_ops->prep) {
175 int ret = td->io_ops->prep(td, io_u);
176
177 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
178 if (ret)
179 unlock_file(td, io_u->file);
180 return ret;
181 }
182
183 return 0;
184}
185
186int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
187 struct timespec *t)
188{
189 int r = 0;
190
191 if (min > 0 && td->io_ops->commit) {
192 r = td->io_ops->commit(td);
193 if (r < 0)
194 goto out;
195 }
196
197 r = 0;
198 if (td->io_ops->getevents)
199 r = td->io_ops->getevents(td, min, max, t);
200out:
201 dprint(FD_IO, "getevents: %d\n", r);
202 return r;
203}
204
205int td_io_queue(struct thread_data *td, struct io_u *io_u)
206{
207 int ret;
208
209 dprint_io_u(io_u, "queue");
210 fio_ro_check(td, io_u);
211
212 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
213 io_u->flags |= IO_U_F_FLIGHT;
214
215 assert(io_u->file->flags & FIO_FILE_OPEN);
216
217 io_u->error = 0;
218 io_u->resid = 0;
219
220 if (td->io_ops->flags & FIO_SYNCIO) {
221 fio_gettime(&io_u->issue_time, NULL);
222 memcpy(&td->last_issue, &io_u->issue_time,
223 sizeof(struct timeval));
224
225 /*
226 * for a sync engine, set the timeout upfront
227 */
228 if (mtime_since(&td->timeout_end, &io_u->issue_time)
229 < IO_U_TIMEOUT)
230 io_u_set_timeout(td);
231 }
232
233 if (io_u->ddir != DDIR_SYNC)
234 td->io_issues[io_u->ddir]++;
235
236 ret = td->io_ops->queue(td, io_u);
237
238 unlock_file(td, io_u->file);
239
240 if (ret == FIO_Q_COMPLETED) {
241 if (io_u->ddir != DDIR_SYNC) {
242 io_u_mark_depth(td, 1);
243 td->ts.total_io_u[io_u->ddir]++;
244 }
245 } else if (ret == FIO_Q_QUEUED) {
246 int r;
247
248 if (io_u->ddir != DDIR_SYNC) {
249 td->io_u_queued++;
250 td->ts.total_io_u[io_u->ddir]++;
251 }
252
253 if (td->io_u_queued >= td->o.iodepth_batch) {
254 r = td_io_commit(td);
255 if (r < 0)
256 return r;
257 }
258 }
259
260 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
261 fio_gettime(&io_u->issue_time, NULL);
262 memcpy(&td->last_issue, &io_u->issue_time,
263 sizeof(struct timeval));
264
265 /*
266 * async engine, set the timeout here
267 */
268 if (ret == FIO_Q_QUEUED &&
269 (mtime_since(&td->timeout_end, &io_u->issue_time)
270 < IO_U_TIMEOUT)) {
271 io_u_set_timeout(td);
272 }
273 }
274
275 return ret;
276}
277
278int td_io_init(struct thread_data *td)
279{
280 int ret = 0;
281
282 if (td->io_ops->init) {
283 ret = td->io_ops->init(td);
284 if (ret && td->o.iodepth > 1) {
285 log_err("fio: io engine init failed. Perhaps try"
286 " reducing io depth?\n");
287 }
288 }
289
290 return ret;
291}
292
293int td_io_commit(struct thread_data *td)
294{
295 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
296
297 if (!td->cur_depth || !td->io_u_queued)
298 return 0;
299
300 io_u_mark_depth(td, td->io_u_queued);
301 td->io_u_queued = 0;
302
303 if (td->io_ops->commit)
304 return td->io_ops->commit(td);
305
306 return 0;
307}
308
309int td_io_open_file(struct thread_data *td, struct fio_file *f)
310{
311 if (td->io_ops->open_file(td, f)) {
312 if (td->error == EINVAL && td->o.odirect)
313 log_err("fio: destination does not support O_DIRECT\n");
314 if (td->error == EMFILE) {
315 log_err("fio: try reducing/setting openfiles (failed"
316 " at %u of %u)\n", td->nr_open_files,
317 td->o.nr_files);
318 }
319
320 return 1;
321 }
322
323 if (f->filetype == FIO_TYPE_PIPE) {
324 if (td_random(td)) {
325 log_err("fio: can't seek on pipes (no random io)\n");
326 goto err;
327 }
328 }
329
330 fio_file_reset(f);
331 f->flags |= FIO_FILE_OPEN;
332 f->flags &= ~FIO_FILE_CLOSING;
333
334 if (td->io_ops->flags & FIO_DISKLESSIO)
335 goto done;
336
337 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
338 goto err;
339
340 if (td->o.fadvise_hint &&
341 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
342 int flags;
343
344 if (td_random(td))
345 flags = POSIX_FADV_RANDOM;
346 else
347 flags = POSIX_FADV_SEQUENTIAL;
348
349 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
350 td_verror(td, errno, "fadvise");
351 goto err;
352 }
353 }
354
355 if (f->file_map)
356 memset(f->file_map, 0, f->num_maps * sizeof(long));
357
358done:
359 log_file(td, f, FIO_LOG_OPEN_FILE);
360 td->nr_open_files++;
361 get_file(f);
362 return 0;
363err:
364 if (td->io_ops->close_file)
365 td->io_ops->close_file(td, f);
366 return 1;
367}
368
369int td_io_close_file(struct thread_data *td, struct fio_file *f)
370{
371 if (!(f->flags & FIO_FILE_CLOSING))
372 log_file(td, f, FIO_LOG_CLOSE_FILE);
373
374 /*
375 * mark as closing, do real close when last io on it has completed
376 */
377 f->flags |= FIO_FILE_CLOSING;
378
379 unlock_file_all(td, f);
380
381 return put_file(td, f);
382}