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