Only memcpy last issue time when using iolog replay
[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 FLIST_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 flist_del(&ops->list);
62 INIT_FLIST_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_FLIST_HEAD(&ops->list);
69 flist_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 flist_head *entry;
76
77 flist_for_each(entry, &engine_list) {
78 ops = flist_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 td->io_ops->data = NULL;
160 }
161
162 if (td->io_ops->dlhandle)
163 dlclose(td->io_ops->dlhandle);
164
165 free(td->io_ops);
166 td->io_ops = NULL;
167}
168
169int td_io_prep(struct thread_data *td, struct io_u *io_u)
170{
171 dprint_io_u(io_u, "prep");
172 fio_ro_check(td, io_u);
173
174 lock_file(td, io_u->file, io_u->ddir);
175
176 if (td->io_ops->prep) {
177 int ret = td->io_ops->prep(td, io_u);
178
179 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
180 if (ret)
181 unlock_file(td, io_u->file);
182 return ret;
183 }
184
185 return 0;
186}
187
188int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
189 struct timespec *t)
190{
191 int r = 0;
192
193 if (min > 0 && td->io_ops->commit) {
194 r = td->io_ops->commit(td);
195 if (r < 0)
196 goto out;
197 }
198 if (max > td->cur_depth)
199 max = td->cur_depth;
200 if (min > max)
201 max = min;
202
203 r = 0;
204 if (max && td->io_ops->getevents)
205 r = td->io_ops->getevents(td, min, max, t);
206out:
207 if (r >= 0)
208 io_u_mark_complete(td, r);
209 dprint(FD_IO, "getevents: %d\n", r);
210 return r;
211}
212
213int td_io_queue(struct thread_data *td, struct io_u *io_u)
214{
215 int ret;
216
217 dprint_io_u(io_u, "queue");
218 fio_ro_check(td, io_u);
219
220 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
221 io_u->flags |= IO_U_F_FLIGHT;
222
223 assert(io_u->file->flags & FIO_FILE_OPEN);
224
225 io_u->error = 0;
226 io_u->resid = 0;
227
228 if (td->io_ops->flags & FIO_SYNCIO) {
229 fio_gettime(&io_u->issue_time, NULL);
230
231 /*
232 * only used for iolog
233 */
234 if (td->o.read_iolog_file)
235 memcpy(&td->last_issue, &io_u->issue_time,
236 sizeof(struct timeval));
237
238 /*
239 * for a sync engine, set the timeout upfront
240 */
241 if (mtime_since(&td->timeout_end, &io_u->issue_time)
242 < IO_U_TIMEOUT)
243 io_u_set_timeout(td);
244 }
245
246 if (io_u->ddir != DDIR_SYNC)
247 td->io_issues[io_u->ddir]++;
248
249 ret = td->io_ops->queue(td, io_u);
250
251 unlock_file(td, io_u->file);
252
253 if (!td->io_ops->commit) {
254 io_u_mark_submit(td, 1);
255 io_u_mark_complete(td, 1);
256 }
257
258 if (ret == FIO_Q_COMPLETED) {
259 if (io_u->ddir != DDIR_SYNC) {
260 io_u_mark_depth(td, 1);
261 td->ts.total_io_u[io_u->ddir]++;
262 }
263 } else if (ret == FIO_Q_QUEUED) {
264 int r;
265
266 if (io_u->ddir != DDIR_SYNC) {
267 td->io_u_queued++;
268 td->ts.total_io_u[io_u->ddir]++;
269 }
270
271 if (td->io_u_queued >= td->o.iodepth_batch) {
272 r = td_io_commit(td);
273 if (r < 0)
274 return r;
275 }
276 }
277
278 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
279 fio_gettime(&io_u->issue_time, NULL);
280
281 /*
282 * only used for iolog
283 */
284 if (td->o.read_iolog_file)
285 memcpy(&td->last_issue, &io_u->issue_time,
286 sizeof(struct timeval));
287
288 /*
289 * async engine, set the timeout here
290 */
291 if (ret == FIO_Q_QUEUED &&
292 (mtime_since(&td->timeout_end, &io_u->issue_time)
293 < IO_U_TIMEOUT)) {
294 io_u_set_timeout(td);
295 }
296 }
297
298 return ret;
299}
300
301int td_io_init(struct thread_data *td)
302{
303 int ret = 0;
304
305 if (td->io_ops->init) {
306 ret = td->io_ops->init(td);
307 if (ret && td->o.iodepth > 1) {
308 log_err("fio: io engine init failed. Perhaps try"
309 " reducing io depth?\n");
310 }
311 }
312
313 return ret;
314}
315
316int td_io_commit(struct thread_data *td)
317{
318 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
319
320 if (!td->cur_depth || !td->io_u_queued)
321 return 0;
322
323 io_u_mark_depth(td, td->io_u_queued);
324 td->io_u_queued = 0;
325
326 if (td->io_ops->commit)
327 return td->io_ops->commit(td);
328
329 return 0;
330}
331
332int td_io_open_file(struct thread_data *td, struct fio_file *f)
333{
334 if (td->io_ops->open_file(td, f)) {
335 if (td->error == EINVAL && td->o.odirect)
336 log_err("fio: destination does not support O_DIRECT\n");
337 if (td->error == EMFILE) {
338 log_err("fio: try reducing/setting openfiles (failed"
339 " at %u of %u)\n", td->nr_open_files,
340 td->o.nr_files);
341 }
342
343 return 1;
344 }
345
346 fio_file_reset(f);
347 f->flags |= FIO_FILE_OPEN;
348 f->flags &= ~FIO_FILE_CLOSING;
349
350 td->nr_open_files++;
351 get_file(f);
352
353 if (f->filetype == FIO_TYPE_PIPE) {
354 if (td_random(td)) {
355 log_err("fio: can't seek on pipes (no random io)\n");
356 goto err;
357 }
358 }
359
360 if (td->io_ops->flags & FIO_DISKLESSIO)
361 goto done;
362
363 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
364 goto err;
365
366 if (td->o.fadvise_hint &&
367 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
368 int flags;
369
370 if (td_random(td))
371 flags = POSIX_FADV_RANDOM;
372 else
373 flags = POSIX_FADV_SEQUENTIAL;
374
375 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
376 td_verror(td, errno, "fadvise");
377 goto err;
378 }
379 }
380
381 if (f->file_map)
382 memset(f->file_map, 0, f->num_maps * sizeof(int));
383
384#ifdef FIO_OS_DIRECTIO
385 /*
386 * Some OS's have a distinct call to mark the file non-buffered,
387 * instead of using O_DIRECT (Solaris)
388 */
389 if (td->o.odirect) {
390 int ret = fio_set_odirect(f->fd);
391
392 if (ret) {
393 td_verror(td, ret, "fio_set_odirect");
394 goto err;
395 }
396 }
397#endif
398
399done:
400 log_file(td, f, FIO_LOG_OPEN_FILE);
401 return 0;
402err:
403 if (td->io_ops->close_file)
404 td->io_ops->close_file(td, f);
405 return 1;
406}
407
408int td_io_close_file(struct thread_data *td, struct fio_file *f)
409{
410 if (!(f->flags & FIO_FILE_CLOSING))
411 log_file(td, f, FIO_LOG_CLOSE_FILE);
412
413 /*
414 * mark as closing, do real close when last io on it has completed
415 */
416 f->flags |= FIO_FILE_CLOSING;
417
418 unlock_file_all(td, f);
419
420 return put_file(td, f);
421}