Merge branch 'security-token' of https://github.com/sfc-gh-rnarubin/fio
[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 <stdlib.h>
13#include <unistd.h>
14#include <string.h>
15#include <dlfcn.h>
16#include <fcntl.h>
17#include <assert.h>
18#include <sys/types.h>
19#include <dirent.h>
20#include <errno.h>
21
22#include "fio.h"
23#include "diskutil.h"
24#include "zbd.h"
25
26static FLIST_HEAD(engine_list);
27
28static inline bool async_ioengine_sync_trim(struct thread_data *td,
29 struct io_u *io_u)
30{
31 return td_ioengine_flagged(td, FIO_ASYNCIO_SYNC_TRIM) &&
32 io_u->ddir == DDIR_TRIM;
33}
34
35static bool check_engine_ops(struct thread_data *td, struct ioengine_ops *ops)
36{
37 if (ops->version != FIO_IOOPS_VERSION) {
38 log_err("bad ioops version %d (want %d)\n", ops->version,
39 FIO_IOOPS_VERSION);
40 return true;
41 }
42
43 if (!ops->queue) {
44 log_err("%s: no queue handler\n", ops->name);
45 return true;
46 }
47
48 /*
49 * sync engines only need a ->queue()
50 */
51 if (ops->flags & FIO_SYNCIO)
52 return false;
53
54 /*
55 * async engines aren't reliable with offload
56 */
57 if ((td->o.io_submit_mode == IO_MODE_OFFLOAD) &&
58 (ops->flags & FIO_NO_OFFLOAD)) {
59 log_err("%s: can't be used with offloaded submit. Use a sync "
60 "engine\n", ops->name);
61 return true;
62 }
63
64 if (!ops->event || !ops->getevents) {
65 log_err("%s: no event/getevents handler\n", ops->name);
66 return true;
67 }
68
69 return false;
70}
71
72void unregister_ioengine(struct ioengine_ops *ops)
73{
74 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
75 flist_del_init(&ops->list);
76}
77
78void register_ioengine(struct ioengine_ops *ops)
79{
80 dprint(FD_IO, "ioengine %s registered\n", ops->name);
81 flist_add_tail(&ops->list, &engine_list);
82}
83
84static struct ioengine_ops *find_ioengine(const char *name)
85{
86 struct ioengine_ops *ops;
87 struct flist_head *entry;
88
89 flist_for_each(entry, &engine_list) {
90 ops = flist_entry(entry, struct ioengine_ops, list);
91 if (!strcmp(name, ops->name))
92 return ops;
93 }
94
95 return NULL;
96}
97
98#ifdef CONFIG_DYNAMIC_ENGINES
99static void *dlopen_external(struct thread_data *td, const char *engine)
100{
101 char engine_path[PATH_MAX];
102 void *dlhandle;
103
104 sprintf(engine_path, "%s/fio-%s.so", FIO_EXT_ENG_DIR, engine);
105
106 dprint(FD_IO, "dlopen external %s\n", engine_path);
107 dlhandle = dlopen(engine_path, RTLD_LAZY);
108 if (!dlhandle)
109 log_info("Engine %s not found; Either name is invalid, was not built, or fio-engine-%s package is missing.\n",
110 engine, engine);
111
112 return dlhandle;
113}
114#else
115#define dlopen_external(td, engine) (NULL)
116#endif
117
118static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
119 const char *engine_lib)
120{
121 struct ioengine_ops *ops;
122 void *dlhandle;
123
124 if (!strncmp(engine_lib, "linuxaio", 8) ||
125 !strncmp(engine_lib, "aio", 3))
126 engine_lib = "libaio";
127
128 dprint(FD_IO, "dlopen engine %s\n", engine_lib);
129
130 dlerror();
131 dlhandle = dlopen(engine_lib, RTLD_LAZY);
132 if (!dlhandle) {
133 dlhandle = dlopen_external(td, engine_lib);
134 if (!dlhandle) {
135 td_vmsg(td, -1, dlerror(), "dlopen");
136 return NULL;
137 }
138 }
139
140 /*
141 * Unlike the included modules, external engines should have a
142 * non-static ioengine structure that we can reference.
143 */
144 ops = dlsym(dlhandle, engine_lib);
145 if (!ops)
146 ops = dlsym(dlhandle, "ioengine");
147
148 /*
149 * For some external engines (like C++ ones) it is not that trivial
150 * to provide a non-static ionengine structure that we can reference.
151 * Instead we call a method which allocates the required ioengine
152 * structure.
153 */
154 if (!ops) {
155 get_ioengine_t get_ioengine = dlsym(dlhandle, "get_ioengine");
156
157 if (get_ioengine)
158 get_ioengine(&ops);
159 }
160
161 if (!ops) {
162 td_vmsg(td, -1, dlerror(), "dlsym");
163 dlclose(dlhandle);
164 return NULL;
165 }
166
167 ops->dlhandle = dlhandle;
168 return ops;
169}
170
171static struct ioengine_ops *__load_ioengine(const char *engine)
172{
173 /*
174 * linux libaio has alias names, so convert to what we want
175 */
176 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3)) {
177 dprint(FD_IO, "converting ioengine name: %s -> libaio\n",
178 engine);
179 engine = "libaio";
180 }
181
182 dprint(FD_IO, "load ioengine %s\n", engine);
183 return find_ioengine(engine);
184}
185
186struct ioengine_ops *load_ioengine(struct thread_data *td)
187{
188 struct ioengine_ops *ops = NULL;
189 const char *name;
190
191 /*
192 * Use ->ioengine_so_path if an external ioengine path is specified.
193 * In this case, ->ioengine is "external" which also means the prefix
194 * for external ioengines "external:" is properly used.
195 */
196 name = td->o.ioengine_so_path ?: td->o.ioengine;
197
198 /*
199 * Try to load ->ioengine first, and if failed try to dlopen(3) either
200 * ->ioengine or ->ioengine_so_path. This is redundant for an external
201 * ioengine with prefix, and also leaves the possibility of unexpected
202 * behavior (e.g. if the "external" ioengine exists), but we do this
203 * so as not to break job files not using the prefix.
204 */
205 ops = __load_ioengine(td->o.ioengine);
206
207 /* We do re-dlopen existing handles, for reference counting */
208 if (!ops || ops->dlhandle)
209 ops = dlopen_ioengine(td, name);
210
211 /*
212 * If ops is NULL, we failed to load ->ioengine, and also failed to
213 * dlopen(3) either ->ioengine or ->ioengine_so_path as a path.
214 */
215 if (!ops) {
216 log_err("fio: engine %s not loadable\n", name);
217 return NULL;
218 }
219
220 /*
221 * Check that the required methods are there.
222 */
223 if (check_engine_ops(td, ops))
224 return NULL;
225
226 return ops;
227}
228
229/*
230 * For cleaning up an ioengine which never made it to init().
231 */
232void free_ioengine(struct thread_data *td)
233{
234 assert(td != NULL && td->io_ops != NULL);
235
236 dprint(FD_IO, "free ioengine %s\n", td->io_ops->name);
237
238 if (td->eo && td->io_ops->options) {
239 options_free(td->io_ops->options, td->eo);
240 free(td->eo);
241 td->eo = NULL;
242 }
243
244 if (td->io_ops->dlhandle) {
245 dprint(FD_IO, "dlclose ioengine %s\n", td->io_ops->name);
246 dlclose(td->io_ops->dlhandle);
247 }
248
249 td->io_ops = NULL;
250}
251
252void close_ioengine(struct thread_data *td)
253{
254 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
255
256 if (td->io_ops->cleanup) {
257 td->io_ops->cleanup(td);
258 td->io_ops_data = NULL;
259 }
260
261 free_ioengine(td);
262}
263
264int td_io_prep(struct thread_data *td, struct io_u *io_u)
265{
266 dprint_io_u(io_u, "prep");
267 fio_ro_check(td, io_u);
268
269 lock_file(td, io_u->file, io_u->ddir);
270
271 if (td->io_ops->prep) {
272 int ret = td->io_ops->prep(td, io_u);
273
274 dprint(FD_IO, "prep: io_u %p: ret=%d\n", io_u, ret);
275
276 if (ret)
277 unlock_file(td, io_u->file);
278 return ret;
279 }
280
281 return 0;
282}
283
284int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
285 const struct timespec *t)
286{
287 int r = 0;
288
289 /*
290 * For ioengine=rdma one side operation RDMA_WRITE or RDMA_READ,
291 * server side gets a message from the client
292 * side that the task is finished, and
293 * td->done is set to 1 after td_io_commit(). In this case,
294 * there is no need to reap complete event in server side.
295 */
296 if (td->done)
297 return 0;
298
299 if (min > 0 && td->io_ops->commit) {
300 r = td->io_ops->commit(td);
301 if (r < 0)
302 goto out;
303 }
304 if (max > td->cur_depth)
305 max = td->cur_depth;
306 if (min > max)
307 max = min;
308
309 r = 0;
310 if (max && td->io_ops->getevents)
311 r = td->io_ops->getevents(td, min, max, t);
312out:
313 if (r >= 0) {
314 /*
315 * Reflect that our submitted requests were retrieved with
316 * whatever OS async calls are in the underlying engine.
317 */
318 td->io_u_in_flight -= r;
319 io_u_mark_complete(td, r);
320 } else
321 td_verror(td, r, "get_events");
322
323 dprint(FD_IO, "getevents: %d\n", r);
324 return r;
325}
326
327enum fio_q_status td_io_queue(struct thread_data *td, struct io_u *io_u)
328{
329 const enum fio_ddir ddir = acct_ddir(io_u);
330 unsigned long long buflen = io_u->xfer_buflen;
331 enum fio_q_status ret;
332
333 dprint_io_u(io_u, "queue");
334 fio_ro_check(td, io_u);
335
336 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
337 io_u_set(td, io_u, IO_U_F_FLIGHT);
338
339 /*
340 * If overlap checking was enabled in offload mode we
341 * can release this lock that was acquired when we
342 * started the overlap check because the IO_U_F_FLIGHT
343 * flag is now set
344 */
345 if (td_offload_overlap(td)) {
346 int res;
347
348 res = pthread_mutex_unlock(&overlap_check);
349 if (fio_unlikely(res != 0)) {
350 log_err("failed to unlock overlap check mutex, err: %i:%s", errno, strerror(errno));
351 abort();
352 }
353 }
354
355 assert(fio_file_open(io_u->file));
356
357 /*
358 * If using a write iolog, store this entry.
359 */
360 log_io_u(td, io_u);
361
362 io_u->error = 0;
363 io_u->resid = 0;
364
365 if (td_ioengine_flagged(td, FIO_SYNCIO) ||
366 async_ioengine_sync_trim(td, io_u)) {
367 if (fio_fill_issue_time(td)) {
368 fio_gettime(&io_u->issue_time, NULL);
369
370 /*
371 * only used for iolog
372 */
373 if (td->o.read_iolog_file)
374 memcpy(&td->last_issue, &io_u->issue_time,
375 sizeof(io_u->issue_time));
376 }
377 }
378
379
380 if (ddir_rw(ddir)) {
381 if (!(io_u->flags & IO_U_F_VER_LIST)) {
382 td->io_issues[ddir]++;
383 td->io_issue_bytes[ddir] += buflen;
384 }
385 td->rate_io_issue_bytes[ddir] += buflen;
386 }
387
388 ret = td->io_ops->queue(td, io_u);
389 zbd_queue_io_u(td, io_u, &ret);
390
391 unlock_file(td, io_u->file);
392
393 if (ret == FIO_Q_BUSY) {
394 if (ddir_rw(ddir)) {
395 td->io_issues[ddir]--;
396 td->io_issue_bytes[ddir] -= buflen;
397 td->rate_io_issue_bytes[ddir] -= buflen;
398 }
399 io_u_clear(td, io_u, IO_U_F_FLIGHT);
400 }
401
402 /*
403 * If an error was seen and the io engine didn't propagate it
404 * back to 'td', do so.
405 */
406 if (io_u->error && !td->error)
407 td_verror(td, io_u->error, "td_io_queue");
408
409 /*
410 * Add warning for O_DIRECT so that users have an easier time
411 * spotting potentially bad alignment. If this triggers for the first
412 * IO, then it's likely an alignment problem or because the host fs
413 * does not support O_DIRECT
414 */
415 if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 &&
416 td->o.odirect) {
417
418 log_info("fio: first direct IO errored. File system may not "
419 "support direct IO, or iomem_align= is bad, or "
420 "invalid block size. Try setting direct=0.\n");
421 }
422
423 if (zbd_unaligned_write(io_u->error) &&
424 td->io_issues[io_u->ddir & 1] == 1 &&
425 td->o.zone_mode != ZONE_MODE_ZBD) {
426 log_info("fio: first I/O failed. If %s is a zoned block device, consider --zonemode=zbd\n",
427 io_u->file->file_name);
428 }
429
430 if (!td->io_ops->commit) {
431 io_u_mark_submit(td, 1);
432 io_u_mark_complete(td, 1);
433 }
434
435 if (ret == FIO_Q_COMPLETED) {
436 if (ddir_rw(io_u->ddir) ||
437 (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING)) {
438 io_u_mark_depth(td, 1);
439 td->ts.total_io_u[io_u->ddir]++;
440 }
441
442 td->last_ddir_issued = ddir;
443 } else if (ret == FIO_Q_QUEUED) {
444 td->io_u_queued++;
445
446 if (ddir_rw(io_u->ddir) ||
447 (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING))
448 td->ts.total_io_u[io_u->ddir]++;
449
450 if (td->io_u_queued >= td->o.iodepth_batch)
451 td_io_commit(td);
452
453 td->last_ddir_issued = ddir;
454 }
455
456 if (!td_ioengine_flagged(td, FIO_SYNCIO) &&
457 !async_ioengine_sync_trim(td, io_u)) {
458 if (fio_fill_issue_time(td) &&
459 !td_ioengine_flagged(td, FIO_ASYNCIO_SETS_ISSUE_TIME)) {
460 fio_gettime(&io_u->issue_time, NULL);
461
462 /*
463 * only used for iolog
464 */
465 if (td->o.read_iolog_file)
466 memcpy(&td->last_issue, &io_u->issue_time,
467 sizeof(io_u->issue_time));
468 }
469 }
470
471 return ret;
472}
473
474int td_io_init(struct thread_data *td)
475{
476 int ret = 0;
477
478 if (td->io_ops->init) {
479 ret = td->io_ops->init(td);
480 if (ret)
481 log_err("fio: io engine %s init failed.%s\n",
482 td->io_ops->name,
483 td->o.iodepth > 1 ?
484 " Perhaps try reducing io depth?" : "");
485 else
486 td->io_ops_init = 1;
487 if (!td->error)
488 td->error = ret;
489 }
490
491 return ret;
492}
493
494void td_io_commit(struct thread_data *td)
495{
496 int ret;
497
498 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
499
500 if (!td->cur_depth || !td->io_u_queued)
501 return;
502
503 io_u_mark_depth(td, td->io_u_queued);
504
505 if (td->io_ops->commit) {
506 ret = td->io_ops->commit(td);
507 if (ret)
508 td_verror(td, -ret, "io commit");
509 }
510
511 /*
512 * Reflect that events were submitted as async IO requests.
513 */
514 td->io_u_in_flight += td->io_u_queued;
515 td->io_u_queued = 0;
516}
517
518int td_io_open_file(struct thread_data *td, struct fio_file *f)
519{
520 if (fio_file_closing(f)) {
521 /*
522 * Open translates to undo closing.
523 */
524 fio_file_clear_closing(f);
525 get_file(f);
526 return 0;
527 }
528 assert(!fio_file_open(f));
529 assert(f->fd == -1);
530 assert(td->io_ops->open_file);
531
532 if (td->io_ops->open_file(td, f)) {
533 if (td->error == EINVAL && td->o.odirect)
534 log_err("fio: destination does not support O_DIRECT\n");
535 if (td->error == EMFILE) {
536 log_err("fio: try reducing/setting openfiles (failed"
537 " at %u of %u)\n", td->nr_open_files,
538 td->o.nr_files);
539 }
540
541 assert(f->fd == -1);
542 assert(!fio_file_open(f));
543 return 1;
544 }
545
546 fio_file_reset(td, f);
547 fio_file_set_open(f);
548 fio_file_clear_closing(f);
549 disk_util_inc(f->du);
550
551 td->nr_open_files++;
552 get_file(f);
553
554 if (f->filetype == FIO_TYPE_PIPE) {
555 if (td_random(td)) {
556 log_err("fio: can't seek on pipes (no random io)\n");
557 goto err;
558 }
559 }
560
561 if (td_ioengine_flagged(td, FIO_DISKLESSIO))
562 goto done;
563
564 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
565 goto err;
566
567 if (td->o.fadvise_hint != F_ADV_NONE &&
568 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
569 int flags;
570
571 if (td->o.fadvise_hint == F_ADV_TYPE) {
572 if (td_random(td))
573 flags = POSIX_FADV_RANDOM;
574 else
575 flags = POSIX_FADV_SEQUENTIAL;
576 } else if (td->o.fadvise_hint == F_ADV_RANDOM)
577 flags = POSIX_FADV_RANDOM;
578 else if (td->o.fadvise_hint == F_ADV_SEQUENTIAL)
579 flags = POSIX_FADV_SEQUENTIAL;
580#ifdef POSIX_FADV_NOREUSE
581 else if (td->o.fadvise_hint == F_ADV_NOREUSE)
582 flags = POSIX_FADV_NOREUSE;
583#endif
584 else {
585 log_err("fio: unknown fadvise type %d\n",
586 td->o.fadvise_hint);
587 flags = POSIX_FADV_NORMAL;
588 }
589
590 if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
591 if (!fio_did_warn(FIO_WARN_FADVISE))
592 log_err("fio: fadvise hint failed\n");
593 }
594 }
595#ifdef FIO_HAVE_WRITE_HINT
596 if (fio_option_is_set(&td->o, write_hint) &&
597 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
598 uint64_t hint = td->o.write_hint;
599 int res;
600
601 /*
602 * For direct IO, set the hint on the file descriptor if that is
603 * supported. Otherwise set it on the inode. For buffered IO, we
604 * need to set it on the inode.
605 */
606 if (td->o.odirect) {
607 res = fcntl(f->fd, F_SET_FILE_RW_HINT, &hint);
608 if (res < 0)
609 res = fcntl(f->fd, F_SET_RW_HINT, &hint);
610 } else {
611 res = fcntl(f->fd, F_SET_RW_HINT, &hint);
612 }
613 if (res < 0) {
614 td_verror(td, errno, "fcntl write hint");
615 goto err;
616 }
617 }
618#endif
619
620 if (td->o.odirect && !OS_O_DIRECT && fio_set_directio(td, f))
621 goto err;
622
623done:
624 log_file(td, f, FIO_LOG_OPEN_FILE);
625 return 0;
626err:
627 disk_util_dec(f->du);
628 if (td->io_ops->close_file)
629 td->io_ops->close_file(td, f);
630 return 1;
631}
632
633int td_io_close_file(struct thread_data *td, struct fio_file *f)
634{
635 if (!fio_file_closing(f))
636 log_file(td, f, FIO_LOG_CLOSE_FILE);
637
638 /*
639 * mark as closing, do real close when last io on it has completed
640 */
641 fio_file_set_closing(f);
642
643 return put_file(td, f);
644}
645
646int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
647{
648 if (td->io_ops->unlink_file)
649 return td->io_ops->unlink_file(td, f);
650 else {
651 int ret;
652
653 ret = unlink(f->file_name);
654 if (ret < 0)
655 return errno;
656
657 return 0;
658 }
659}
660
661int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
662{
663 if (!td->io_ops->get_file_size)
664 return 0;
665
666 return td->io_ops->get_file_size(td, f);
667}
668
669#ifdef CONFIG_DYNAMIC_ENGINES
670/* Load all dynamic engines in FIO_EXT_ENG_DIR for enghelp command */
671static void
672fio_load_dynamic_engines(struct thread_data *td)
673{
674 DIR *dirhandle = NULL;
675 struct dirent *dirent = NULL;
676 char engine_path[PATH_MAX];
677
678 dirhandle = opendir(FIO_EXT_ENG_DIR);
679 if (!dirhandle)
680 return;
681
682 while ((dirent = readdir(dirhandle)) != NULL) {
683 if (!strcmp(dirent->d_name, ".") ||
684 !strcmp(dirent->d_name, ".."))
685 continue;
686
687 sprintf(engine_path, "%s/%s", FIO_EXT_ENG_DIR, dirent->d_name);
688 dlopen_ioengine(td, engine_path);
689 }
690
691 closedir(dirhandle);
692}
693#else
694#define fio_load_dynamic_engines(td) do { } while (0)
695#endif
696
697int fio_show_ioengine_help(const char *engine)
698{
699 struct flist_head *entry;
700 struct thread_data td;
701 struct ioengine_ops *io_ops;
702 char *sep;
703 int ret = 1;
704
705 memset(&td, 0, sizeof(struct thread_data));
706
707 if (!engine || !*engine) {
708 log_info("Available IO engines:\n");
709 fio_load_dynamic_engines(&td);
710 flist_for_each(entry, &engine_list) {
711 io_ops = flist_entry(entry, struct ioengine_ops, list);
712 log_info("\t%s\n", io_ops->name);
713 }
714 return 0;
715 }
716 sep = strchr(engine, ',');
717 if (sep) {
718 *sep = 0;
719 sep++;
720 }
721
722 td.o.ioengine = (char *)engine;
723 td.io_ops = load_ioengine(&td);
724
725 if (!td.io_ops) {
726 log_info("IO engine %s not found\n", engine);
727 return 1;
728 }
729
730 if (td.io_ops->options)
731 ret = show_cmd_help(td.io_ops->options, sep);
732 else
733 log_info("IO engine %s has no options\n", td.io_ops->name);
734
735 free_ioengine(&td);
736 return ret;
737}