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