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