t/gen-rand: remove compile warning on 32-bit
[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 <fcntl.h>
18#include <assert.h>
19
20#include "fio.h"
21#include "diskutil.h"
22
23static FLIST_HEAD(engine_list);
24
25static int check_engine_ops(struct ioengine_ops *ops)
26{
27 if (ops->version != FIO_IOOPS_VERSION) {
28 log_err("bad ioops version %d (want %d)\n", ops->version,
29 FIO_IOOPS_VERSION);
30 return 1;
31 }
32
33 if (!ops->queue) {
34 log_err("%s: no queue handler\n", ops->name);
35 return 1;
36 }
37
38 /*
39 * sync engines only need a ->queue()
40 */
41 if (ops->flags & FIO_SYNCIO)
42 return 0;
43
44 if (!ops->event) {
45 log_err("%s: no event handler\n", ops->name);
46 return 1;
47 }
48 if (!ops->getevents) {
49 log_err("%s: no getevents handler\n", ops->name);
50 return 1;
51 }
52
53 return 0;
54}
55
56void unregister_ioengine(struct ioengine_ops *ops)
57{
58 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
59 flist_del(&ops->list);
60 INIT_FLIST_HEAD(&ops->list);
61}
62
63void register_ioengine(struct ioengine_ops *ops)
64{
65 dprint(FD_IO, "ioengine %s registered\n", ops->name);
66 INIT_FLIST_HEAD(&ops->list);
67 flist_add_tail(&ops->list, &engine_list);
68}
69
70static struct ioengine_ops *find_ioengine(const char *name)
71{
72 struct ioengine_ops *ops;
73 struct flist_head *entry;
74
75 flist_for_each(entry, &engine_list) {
76 ops = flist_entry(entry, struct ioengine_ops, list);
77 if (!strcmp(name, ops->name))
78 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
90 dprint(FD_IO, "dload engine %s\n", engine_lib);
91
92 dlerror();
93 dlhandle = dlopen(engine_lib, RTLD_LAZY);
94 if (!dlhandle) {
95 td_vmsg(td, -1, dlerror(), "dlopen");
96 return NULL;
97 }
98
99 /*
100 * Unlike the included modules, external engines should have a
101 * non-static ioengine structure that we can reference.
102 */
103 ops = dlsym(dlhandle, engine_lib);
104 if (!ops)
105 ops = dlsym(dlhandle, "ioengine");
106
107 /*
108 * For some external engines (like C++ ones) it is not that trivial
109 * to provide a non-static ionengine structure that we can reference.
110 * Instead we call a method which allocates the required ioengine
111 * structure.
112 */
113 if (!ops) {
114 get_ioengine_t get_ioengine = dlsym(dlhandle, "get_ioengine");
115
116 if (get_ioengine)
117 get_ioengine(&ops);
118 }
119
120 if (!ops) {
121 td_vmsg(td, -1, dlerror(), "dlsym");
122 dlclose(dlhandle);
123 return NULL;
124 }
125
126 ops->dlhandle = dlhandle;
127 return ops;
128}
129
130struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
131{
132 struct ioengine_ops *ops, *ret;
133 char engine[16];
134
135 dprint(FD_IO, "load ioengine %s\n", name);
136
137 strncpy(engine, name, sizeof(engine) - 1);
138
139 /*
140 * linux libaio has alias names, so convert to what we want
141 */
142 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
143 strcpy(engine, "libaio");
144
145 ops = find_ioengine(engine);
146 if (!ops)
147 ops = dlopen_ioengine(td, name);
148
149 if (!ops) {
150 log_err("fio: engine %s not loadable\n", name);
151 return NULL;
152 }
153
154 /*
155 * Check that the required methods are there.
156 */
157 if (check_engine_ops(ops))
158 return NULL;
159
160 ret = malloc(sizeof(*ret));
161 memcpy(ret, ops, sizeof(*ret));
162 ret->data = NULL;
163
164 return ret;
165}
166
167/*
168 * For cleaning up an ioengine which never made it to init().
169 */
170void free_ioengine(struct thread_data *td)
171{
172 dprint(FD_IO, "free ioengine %s\n", td->io_ops->name);
173
174 if (td->eo && td->io_ops->options) {
175 options_free(td->io_ops->options, td->eo);
176 free(td->eo);
177 td->eo = NULL;
178 }
179
180 if (td->io_ops->dlhandle)
181 dlclose(td->io_ops->dlhandle);
182
183 free(td->io_ops);
184 td->io_ops = NULL;
185}
186
187void close_ioengine(struct thread_data *td)
188{
189 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
190
191 if (td->io_ops->cleanup) {
192 td->io_ops->cleanup(td);
193 td->io_ops->data = NULL;
194 }
195
196 free_ioengine(td);
197}
198
199int td_io_prep(struct thread_data *td, struct io_u *io_u)
200{
201 dprint_io_u(io_u, "prep");
202 fio_ro_check(td, io_u);
203
204 lock_file(td, io_u->file, io_u->ddir);
205
206 if (td->io_ops->prep) {
207 int ret = td->io_ops->prep(td, io_u);
208
209 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
210 if (ret)
211 unlock_file(td, io_u->file);
212 return ret;
213 }
214
215 return 0;
216}
217
218int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
219 const struct timespec *t)
220{
221 int r = 0;
222
223 /*
224 * For ioengine=rdma one side operation RDMA_WRITE or RDMA_READ,
225 * server side gets a message from the client
226 * side that the task is finished, and
227 * td->done is set to 1 after td_io_commit(). In this case,
228 * there is no need to reap complete event in server side.
229 */
230 if (td->done)
231 return 0;
232
233 if (min > 0 && td->io_ops->commit) {
234 r = td->io_ops->commit(td);
235 if (r < 0)
236 goto out;
237 }
238 if (max > td->cur_depth)
239 max = td->cur_depth;
240 if (min > max)
241 max = min;
242
243 r = 0;
244 if (max && td->io_ops->getevents)
245 r = td->io_ops->getevents(td, min, max, t);
246out:
247 if (r >= 0) {
248 /*
249 * Reflect that our submitted requests were retrieved with
250 * whatever OS async calls are in the underlying engine.
251 */
252 td->io_u_in_flight -= r;
253 io_u_mark_complete(td, r);
254 } else
255 td_verror(td, r, "get_events");
256
257 dprint(FD_IO, "getevents: %d\n", r);
258 return r;
259}
260
261int td_io_queue(struct thread_data *td, struct io_u *io_u)
262{
263 const enum fio_ddir ddir = acct_ddir(io_u);
264 unsigned long buflen = io_u->xfer_buflen;
265 int ret;
266
267 dprint_io_u(io_u, "queue");
268 fio_ro_check(td, io_u);
269
270 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
271 io_u_set(io_u, IO_U_F_FLIGHT);
272
273 assert(fio_file_open(io_u->file));
274
275 /*
276 * If using a write iolog, store this entry.
277 */
278 log_io_u(td, io_u);
279
280 io_u->error = 0;
281 io_u->resid = 0;
282
283 if (td->io_ops->flags & FIO_SYNCIO) {
284 if (fio_fill_issue_time(td))
285 fio_gettime(&io_u->issue_time, NULL);
286
287 /*
288 * only used for iolog
289 */
290 if (td->o.read_iolog_file)
291 memcpy(&td->last_issue, &io_u->issue_time,
292 sizeof(struct timeval));
293 }
294
295 if (ddir_rw(ddir)) {
296 td->io_issues[ddir]++;
297 td->io_issue_bytes[ddir] += buflen;
298 td->rate_io_issue_bytes[ddir] += buflen;
299 }
300
301 ret = td->io_ops->queue(td, io_u);
302
303 unlock_file(td, io_u->file);
304
305 if (ret == FIO_Q_BUSY && ddir_rw(ddir)) {
306 td->io_issues[ddir]--;
307 td->io_issue_bytes[ddir] -= buflen;
308 td->rate_io_issue_bytes[ddir] -= buflen;
309 }
310
311 /*
312 * If an error was seen and the io engine didn't propagate it
313 * back to 'td', do so.
314 */
315 if (io_u->error && !td->error)
316 td_verror(td, io_u->error, "td_io_queue");
317
318 /*
319 * Add warning for O_DIRECT so that users have an easier time
320 * spotting potentially bad alignment. If this triggers for the first
321 * IO, then it's likely an alignment problem or because the host fs
322 * does not support O_DIRECT
323 */
324 if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 &&
325 td->o.odirect) {
326
327 log_info("fio: first direct IO errored. File system may not "
328 "support direct IO, or iomem_align= is bad. Try "
329 "setting direct=0.\n");
330 }
331
332 if (!td->io_ops->commit || io_u->ddir == DDIR_TRIM) {
333 io_u_mark_submit(td, 1);
334 io_u_mark_complete(td, 1);
335 }
336
337 if (ret == FIO_Q_COMPLETED) {
338 if (ddir_rw(io_u->ddir)) {
339 io_u_mark_depth(td, 1);
340 td->ts.total_io_u[io_u->ddir]++;
341 }
342 } else if (ret == FIO_Q_QUEUED) {
343 int r;
344
345 td->io_u_queued++;
346
347 if (ddir_rw(io_u->ddir))
348 td->ts.total_io_u[io_u->ddir]++;
349
350 if (td->io_u_queued >= td->o.iodepth_batch) {
351 r = td_io_commit(td);
352 if (r < 0)
353 return r;
354 }
355 }
356
357 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
358 if (fio_fill_issue_time(td))
359 fio_gettime(&io_u->issue_time, NULL);
360
361 /*
362 * only used for iolog
363 */
364 if (td->o.read_iolog_file)
365 memcpy(&td->last_issue, &io_u->issue_time,
366 sizeof(struct timeval));
367 }
368
369 return ret;
370}
371
372int td_io_init(struct thread_data *td)
373{
374 int ret = 0;
375
376 if (td->io_ops->init) {
377 ret = td->io_ops->init(td);
378 if (ret && td->o.iodepth > 1) {
379 log_err("fio: io engine init failed. Perhaps try"
380 " reducing io depth?\n");
381 }
382 if (!td->error)
383 td->error = ret;
384 }
385
386 if (!ret && (td->io_ops->flags & FIO_NOIO))
387 td->flags |= TD_F_NOIO;
388
389 return ret;
390}
391
392int td_io_commit(struct thread_data *td)
393{
394 int ret;
395
396 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
397
398 if (!td->cur_depth || !td->io_u_queued)
399 return 0;
400
401 io_u_mark_depth(td, td->io_u_queued);
402
403 if (td->io_ops->commit) {
404 ret = td->io_ops->commit(td);
405 if (ret)
406 td_verror(td, -ret, "io commit");
407 }
408
409 /*
410 * Reflect that events were submitted as async IO requests.
411 */
412 td->io_u_in_flight += td->io_u_queued;
413 td->io_u_queued = 0;
414
415 return 0;
416}
417
418int td_io_open_file(struct thread_data *td, struct fio_file *f)
419{
420 assert(!fio_file_open(f));
421 assert(f->fd == -1);
422
423 if (td->io_ops->open_file(td, f)) {
424 if (td->error == EINVAL && td->o.odirect)
425 log_err("fio: destination does not support O_DIRECT\n");
426 if (td->error == EMFILE) {
427 log_err("fio: try reducing/setting openfiles (failed"
428 " at %u of %u)\n", td->nr_open_files,
429 td->o.nr_files);
430 }
431
432 assert(f->fd == -1);
433 assert(!fio_file_open(f));
434 return 1;
435 }
436
437 fio_file_reset(td, f);
438 fio_file_set_open(f);
439 fio_file_clear_closing(f);
440 disk_util_inc(f->du);
441
442 td->nr_open_files++;
443 get_file(f);
444
445 if (f->filetype == FIO_TYPE_PIPE) {
446 if (td_random(td)) {
447 log_err("fio: can't seek on pipes (no random io)\n");
448 goto err;
449 }
450 }
451
452 if (td->io_ops->flags & FIO_DISKLESSIO)
453 goto done;
454
455 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
456 goto err;
457
458 if (td->o.fadvise_hint &&
459 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
460 int flags;
461
462 if (td_random(td))
463 flags = POSIX_FADV_RANDOM;
464 else
465 flags = POSIX_FADV_SEQUENTIAL;
466
467 if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
468 td_verror(td, errno, "fadvise");
469 goto err;
470 }
471 }
472#ifdef FIO_HAVE_STREAMID
473 if (td->o.fadvise_stream &&
474 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
475 off_t stream = td->o.fadvise_stream;
476
477 if (posix_fadvise(f->fd, stream, f->io_size, POSIX_FADV_STREAMID) < 0) {
478 td_verror(td, errno, "fadvise streamid");
479 goto err;
480 }
481 }
482#endif
483
484#ifdef FIO_OS_DIRECTIO
485 /*
486 * Some OS's have a distinct call to mark the file non-buffered,
487 * instead of using O_DIRECT (Solaris)
488 */
489 if (td->o.odirect) {
490 int ret = fio_set_odirect(f->fd);
491
492 if (ret) {
493 td_verror(td, ret, "fio_set_odirect");
494 log_err("fio: the file system does not seem to support direct IO\n");
495 goto err;
496 }
497 }
498#endif
499
500done:
501 log_file(td, f, FIO_LOG_OPEN_FILE);
502 return 0;
503err:
504 disk_util_dec(f->du);
505 if (td->io_ops->close_file)
506 td->io_ops->close_file(td, f);
507 return 1;
508}
509
510int td_io_close_file(struct thread_data *td, struct fio_file *f)
511{
512 if (!fio_file_closing(f))
513 log_file(td, f, FIO_LOG_CLOSE_FILE);
514
515 /*
516 * mark as closing, do real close when last io on it has completed
517 */
518 fio_file_set_closing(f);
519
520 disk_util_dec(f->du);
521
522 if (td->o.file_lock_mode != FILE_LOCK_NONE)
523 unlock_file_all(td, f);
524
525 return put_file(td, f);
526}
527
528int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
529{
530 if (td->io_ops->unlink_file)
531 return td->io_ops->unlink_file(td, f);
532 else
533 return unlink(f->file_name);
534}
535
536int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
537{
538 if (!td->io_ops->get_file_size)
539 return 0;
540
541 return td->io_ops->get_file_size(td, f);
542}
543
544static int do_sync_file_range(const struct thread_data *td,
545 struct fio_file *f)
546{
547 off64_t offset, nbytes;
548
549 offset = f->first_write;
550 nbytes = f->last_write - f->first_write;
551
552 if (!nbytes)
553 return 0;
554
555 return sync_file_range(f->fd, offset, nbytes, td->o.sync_file_range);
556}
557
558int do_io_u_sync(const struct thread_data *td, struct io_u *io_u)
559{
560 int ret;
561
562 if (io_u->ddir == DDIR_SYNC) {
563 ret = fsync(io_u->file->fd);
564 } else if (io_u->ddir == DDIR_DATASYNC) {
565#ifdef CONFIG_FDATASYNC
566 ret = fdatasync(io_u->file->fd);
567#else
568 ret = io_u->xfer_buflen;
569 io_u->error = EINVAL;
570#endif
571 } else if (io_u->ddir == DDIR_SYNC_FILE_RANGE)
572 ret = do_sync_file_range(td, io_u->file);
573 else {
574 ret = io_u->xfer_buflen;
575 io_u->error = EINVAL;
576 }
577
578 if (ret < 0)
579 io_u->error = errno;
580
581 return ret;
582}
583
584int do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
585{
586#ifndef FIO_HAVE_TRIM
587 io_u->error = EINVAL;
588 return 0;
589#else
590 struct fio_file *f = io_u->file;
591 int ret;
592
593 ret = os_trim(f->fd, io_u->offset, io_u->xfer_buflen);
594 if (!ret)
595 return io_u->xfer_buflen;
596
597 io_u->error = ret;
598 return 0;
599#endif
600}
601
602int fio_show_ioengine_help(const char *engine)
603{
604 struct flist_head *entry;
605 struct thread_data td;
606 char *sep;
607 int ret = 1;
608
609 if (!engine || !*engine) {
610 log_info("Available IO engines:\n");
611 flist_for_each(entry, &engine_list) {
612 td.io_ops = flist_entry(entry, struct ioengine_ops,
613 list);
614 log_info("\t%s\n", td.io_ops->name);
615 }
616 return 0;
617 }
618 sep = strchr(engine, ',');
619 if (sep) {
620 *sep = 0;
621 sep++;
622 }
623
624 memset(&td, 0, sizeof(td));
625
626 td.io_ops = load_ioengine(&td, engine);
627 if (!td.io_ops) {
628 log_info("IO engine %s not found\n", engine);
629 return 1;
630 }
631
632 if (td.io_ops->options)
633 ret = show_cmd_help(td.io_ops->options, sep);
634 else
635 log_info("IO engine %s has no options\n", td.io_ops->name);
636
637 free_ioengine(&td);
638
639 return ret;
640}