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