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