Merge branch 'fixes_1290' of https://github.com/rthardin/fio
[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 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
64 void 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
70 void 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
76 static 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
91 static 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
110 static 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
163 static 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
178 struct 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  */
224 void 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         }
238
239         td->io_ops = NULL;
240 }
241
242 void close_ioengine(struct thread_data *td)
243 {
244         dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
245
246         if (td->io_ops->cleanup) {
247                 td->io_ops->cleanup(td);
248                 td->io_ops_data = NULL;
249         }
250
251         free_ioengine(td);
252 }
253
254 int td_io_prep(struct thread_data *td, struct io_u *io_u)
255 {
256         dprint_io_u(io_u, "prep");
257         fio_ro_check(td, io_u);
258
259         lock_file(td, io_u->file, io_u->ddir);
260
261         if (td->io_ops->prep) {
262                 int ret = td->io_ops->prep(td, io_u);
263
264                 dprint(FD_IO, "prep: io_u %p: ret=%d\n", io_u, ret);
265
266                 if (ret)
267                         unlock_file(td, io_u->file);
268                 return ret;
269         }
270
271         return 0;
272 }
273
274 int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
275                     const struct timespec *t)
276 {
277         int r = 0;
278
279         /*
280          * For ioengine=rdma one side operation RDMA_WRITE or RDMA_READ,
281          * server side gets a message from the client
282          * side that the task is finished, and
283          * td->done is set to 1 after td_io_commit(). In this case,
284          * there is no need to reap complete event in server side.
285          */
286         if (td->done)
287                 return 0;
288
289         if (min > 0 && td->io_ops->commit) {
290                 r = td->io_ops->commit(td);
291                 if (r < 0)
292                         goto out;
293         }
294         if (max > td->cur_depth)
295                 max = td->cur_depth;
296         if (min > max)
297                 max = min;
298
299         r = 0;
300         if (max && td->io_ops->getevents)
301                 r = td->io_ops->getevents(td, min, max, t);
302 out:
303         if (r >= 0) {
304                 /*
305                  * Reflect that our submitted requests were retrieved with
306                  * whatever OS async calls are in the underlying engine.
307                  */
308                 td->io_u_in_flight -= r;
309                 io_u_mark_complete(td, r);
310         } else
311                 td_verror(td, r, "get_events");
312
313         dprint(FD_IO, "getevents: %d\n", r);
314         return r;
315 }
316
317 enum fio_q_status td_io_queue(struct thread_data *td, struct io_u *io_u)
318 {
319         const enum fio_ddir ddir = acct_ddir(io_u);
320         unsigned long long buflen = io_u->xfer_buflen;
321         enum fio_q_status ret;
322
323         dprint_io_u(io_u, "queue");
324         fio_ro_check(td, io_u);
325
326         assert((io_u->flags & IO_U_F_FLIGHT) == 0);
327         io_u_set(td, io_u, IO_U_F_FLIGHT);
328
329         /*
330          * If overlap checking was enabled in offload mode we
331          * can release this lock that was acquired when we
332          * started the overlap check because the IO_U_F_FLIGHT
333          * flag is now set
334          */
335         if (td_offload_overlap(td)) {
336                 int res = pthread_mutex_unlock(&overlap_check);
337                 assert(res == 0);
338         }
339
340         assert(fio_file_open(io_u->file));
341
342         /*
343          * If using a write iolog, store this entry.
344          */
345         log_io_u(td, io_u);
346
347         io_u->error = 0;
348         io_u->resid = 0;
349
350         if (td_ioengine_flagged(td, FIO_SYNCIO) ||
351                 (td_ioengine_flagged(td, FIO_ASYNCIO_SYNC_TRIM) && 
352                 io_u->ddir == DDIR_TRIM)) {
353                 if (fio_fill_issue_time(td))
354                         fio_gettime(&io_u->issue_time, NULL);
355
356                 /*
357                  * only used for iolog
358                  */
359                 if (td->o.read_iolog_file)
360                         memcpy(&td->last_issue, &io_u->issue_time,
361                                         sizeof(io_u->issue_time));
362         }
363
364
365         if (ddir_rw(ddir)) {
366                 if (!(io_u->flags & IO_U_F_VER_LIST)) {
367                         td->io_issues[ddir]++;
368                         td->io_issue_bytes[ddir] += buflen;
369                 }
370                 td->rate_io_issue_bytes[ddir] += buflen;
371         }
372
373         ret = td->io_ops->queue(td, io_u);
374         zbd_queue_io_u(td, io_u, ret);
375
376         unlock_file(td, io_u->file);
377
378         if (ret == FIO_Q_BUSY && ddir_rw(ddir)) {
379                 td->io_issues[ddir]--;
380                 td->io_issue_bytes[ddir] -= buflen;
381                 td->rate_io_issue_bytes[ddir] -= buflen;
382                 io_u_clear(td, io_u, IO_U_F_FLIGHT);
383         }
384
385         /*
386          * If an error was seen and the io engine didn't propagate it
387          * back to 'td', do so.
388          */
389         if (io_u->error && !td->error)
390                 td_verror(td, io_u->error, "td_io_queue");
391
392         /*
393          * Add warning for O_DIRECT so that users have an easier time
394          * spotting potentially bad alignment. If this triggers for the first
395          * IO, then it's likely an alignment problem or because the host fs
396          * does not support O_DIRECT
397          */
398         if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 &&
399             td->o.odirect) {
400
401                 log_info("fio: first direct IO errored. File system may not "
402                          "support direct IO, or iomem_align= is bad, or "
403                          "invalid block size. Try setting direct=0.\n");
404         }
405
406         if (zbd_unaligned_write(io_u->error) &&
407             td->io_issues[io_u->ddir & 1] == 1 &&
408             td->o.zone_mode != ZONE_MODE_ZBD) {
409                 log_info("fio: first I/O failed. If %s is a zoned block device, consider --zonemode=zbd\n",
410                          io_u->file->file_name);
411         }
412
413         if (!td->io_ops->commit) {
414                 io_u_mark_submit(td, 1);
415                 io_u_mark_complete(td, 1);
416         }
417
418         if (ret == FIO_Q_COMPLETED) {
419                 if (ddir_rw(io_u->ddir) ||
420                     (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING)) {
421                         io_u_mark_depth(td, 1);
422                         td->ts.total_io_u[io_u->ddir]++;
423                 }
424         } else if (ret == FIO_Q_QUEUED) {
425                 td->io_u_queued++;
426
427                 if (ddir_rw(io_u->ddir) ||
428                     (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING))
429                         td->ts.total_io_u[io_u->ddir]++;
430
431                 if (td->io_u_queued >= td->o.iodepth_batch)
432                         td_io_commit(td);
433         }
434
435         if (!td_ioengine_flagged(td, FIO_SYNCIO) &&
436                 (!td_ioengine_flagged(td, FIO_ASYNCIO_SYNC_TRIM) ||
437                  io_u->ddir != DDIR_TRIM)) {
438                 if (fio_fill_issue_time(td))
439                         fio_gettime(&io_u->issue_time, NULL);
440
441                 /*
442                  * only used for iolog
443                  */
444                 if (td->o.read_iolog_file)
445                         memcpy(&td->last_issue, &io_u->issue_time,
446                                         sizeof(io_u->issue_time));
447         }
448
449         return ret;
450 }
451
452 int td_io_init(struct thread_data *td)
453 {
454         int ret = 0;
455
456         if (td->io_ops->init) {
457                 ret = td->io_ops->init(td);
458                 if (ret)
459                         log_err("fio: io engine %s init failed.%s\n",
460                                 td->io_ops->name,
461                                 td->o.iodepth > 1 ?
462                                 " Perhaps try reducing io depth?" : "");
463                 else
464                         td->io_ops_init = 1;
465                 if (!td->error)
466                         td->error = ret;
467         }
468
469         return ret;
470 }
471
472 void td_io_commit(struct thread_data *td)
473 {
474         int ret;
475
476         dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
477
478         if (!td->cur_depth || !td->io_u_queued)
479                 return;
480
481         io_u_mark_depth(td, td->io_u_queued);
482
483         if (td->io_ops->commit) {
484                 ret = td->io_ops->commit(td);
485                 if (ret)
486                         td_verror(td, -ret, "io commit");
487         }
488
489         /*
490          * Reflect that events were submitted as async IO requests.
491          */
492         td->io_u_in_flight += td->io_u_queued;
493         td->io_u_queued = 0;
494 }
495
496 int td_io_open_file(struct thread_data *td, struct fio_file *f)
497 {
498         if (fio_file_closing(f)) {
499                 /*
500                  * Open translates to undo closing.
501                  */
502                 fio_file_clear_closing(f);
503                 get_file(f);
504                 return 0;
505         }
506         assert(!fio_file_open(f));
507         assert(f->fd == -1);
508         assert(td->io_ops->open_file);
509
510         if (td->io_ops->open_file(td, f)) {
511                 if (td->error == EINVAL && td->o.odirect)
512                         log_err("fio: destination does not support O_DIRECT\n");
513                 if (td->error == EMFILE) {
514                         log_err("fio: try reducing/setting openfiles (failed"
515                                 " at %u of %u)\n", td->nr_open_files,
516                                                         td->o.nr_files);
517                 }
518
519                 assert(f->fd == -1);
520                 assert(!fio_file_open(f));
521                 return 1;
522         }
523
524         fio_file_reset(td, f);
525         fio_file_set_open(f);
526         fio_file_clear_closing(f);
527         disk_util_inc(f->du);
528
529         td->nr_open_files++;
530         get_file(f);
531
532         if (f->filetype == FIO_TYPE_PIPE) {
533                 if (td_random(td)) {
534                         log_err("fio: can't seek on pipes (no random io)\n");
535                         goto err;
536                 }
537         }
538
539         if (td_ioengine_flagged(td, FIO_DISKLESSIO))
540                 goto done;
541
542         if (td->o.invalidate_cache && file_invalidate_cache(td, f))
543                 goto err;
544
545         if (td->o.fadvise_hint != F_ADV_NONE &&
546             (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
547                 int flags;
548
549                 if (td->o.fadvise_hint == F_ADV_TYPE) {
550                         if (td_random(td))
551                                 flags = POSIX_FADV_RANDOM;
552                         else
553                                 flags = POSIX_FADV_SEQUENTIAL;
554                 } else if (td->o.fadvise_hint == F_ADV_RANDOM)
555                         flags = POSIX_FADV_RANDOM;
556                 else if (td->o.fadvise_hint == F_ADV_SEQUENTIAL)
557                         flags = POSIX_FADV_SEQUENTIAL;
558                 else {
559                         log_err("fio: unknown fadvise type %d\n",
560                                                         td->o.fadvise_hint);
561                         flags = POSIX_FADV_NORMAL;
562                 }
563
564                 if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
565                         if (!fio_did_warn(FIO_WARN_FADVISE))
566                                 log_err("fio: fadvise hint failed\n");
567                 }
568         }
569 #ifdef FIO_HAVE_WRITE_HINT
570         if (fio_option_is_set(&td->o, write_hint) &&
571             (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
572                 uint64_t hint = td->o.write_hint;
573                 int cmd;
574
575                 /*
576                  * For direct IO, we just need/want to set the hint on
577                  * the file descriptor. For buffered IO, we need to set
578                  * it on the inode.
579                  */
580                 if (td->o.odirect)
581                         cmd = F_SET_FILE_RW_HINT;
582                 else
583                         cmd = F_SET_RW_HINT;
584
585                 if (fcntl(f->fd, cmd, &hint) < 0) {
586                         td_verror(td, errno, "fcntl write hint");
587                         goto err;
588                 }
589         }
590 #endif
591
592         if (td->o.odirect && !OS_O_DIRECT && fio_set_directio(td, f))
593                 goto err;
594
595 done:
596         log_file(td, f, FIO_LOG_OPEN_FILE);
597         return 0;
598 err:
599         disk_util_dec(f->du);
600         if (td->io_ops->close_file)
601                 td->io_ops->close_file(td, f);
602         return 1;
603 }
604
605 int td_io_close_file(struct thread_data *td, struct fio_file *f)
606 {
607         if (!fio_file_closing(f))
608                 log_file(td, f, FIO_LOG_CLOSE_FILE);
609
610         /*
611          * mark as closing, do real close when last io on it has completed
612          */
613         fio_file_set_closing(f);
614
615         return put_file(td, f);
616 }
617
618 int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
619 {
620         if (td->io_ops->unlink_file)
621                 return td->io_ops->unlink_file(td, f);
622         else {
623                 int ret;
624
625                 ret = unlink(f->file_name);
626                 if (ret < 0)
627                         return errno;
628
629                 return 0;
630         }
631 }
632
633 int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
634 {
635         if (!td->io_ops->get_file_size)
636                 return 0;
637
638         return td->io_ops->get_file_size(td, f);
639 }
640
641 #ifdef CONFIG_DYNAMIC_ENGINES
642 /* Load all dynamic engines in FIO_EXT_ENG_DIR for enghelp command */
643 static void
644 fio_load_dynamic_engines(struct thread_data *td)
645 {
646         DIR *dirhandle = NULL;
647         struct dirent *dirent = NULL;
648         char engine_path[PATH_MAX];
649
650         dirhandle = opendir(FIO_EXT_ENG_DIR);
651         if (!dirhandle)
652                 return;
653
654         while ((dirent = readdir(dirhandle)) != NULL) {
655                 if (!strcmp(dirent->d_name, ".") ||
656                     !strcmp(dirent->d_name, ".."))
657                         continue;
658
659                 sprintf(engine_path, "%s/%s", FIO_EXT_ENG_DIR, dirent->d_name);
660                 dlopen_ioengine(td, engine_path);
661         }
662
663         closedir(dirhandle);
664 }
665 #else
666 #define fio_load_dynamic_engines(td) do { } while (0)
667 #endif
668
669 int fio_show_ioengine_help(const char *engine)
670 {
671         struct flist_head *entry;
672         struct thread_data td;
673         struct ioengine_ops *io_ops;
674         char *sep;
675         int ret = 1;
676
677         memset(&td, 0, sizeof(struct thread_data));
678
679         if (!engine || !*engine) {
680                 log_info("Available IO engines:\n");
681                 fio_load_dynamic_engines(&td);
682                 flist_for_each(entry, &engine_list) {
683                         io_ops = flist_entry(entry, struct ioengine_ops, list);
684                         log_info("\t%s\n", io_ops->name);
685                 }
686                 return 0;
687         }
688         sep = strchr(engine, ',');
689         if (sep) {
690                 *sep = 0;
691                 sep++;
692         }
693
694         td.o.ioengine = (char *)engine;
695         td.io_ops = load_ioengine(&td);
696
697         if (!td.io_ops) {
698                 log_info("IO engine %s not found\n", engine);
699                 return 1;
700         }
701
702         if (td.io_ops->options)
703                 ret = show_cmd_help(td.io_ops->options, sep);
704         else
705                 log_info("IO engine %s has no options\n", td.io_ops->name);
706
707         free_ioengine(&td);
708         return ret;
709 }