[PATCH] Split the io_u io handling out of fio.c
[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 "fio.h"
18 #include "os.h"
19
20 struct ioengine_ops *load_ioengine(struct thread_data *td, char *name)
21 {
22         char engine[16], engine_lib[256];
23         struct ioengine_ops *ops, *ret;
24         void *dlhandle;
25
26         strcpy(engine, name);
27
28         /*
29          * linux libaio has alias names, so convert to what we want
30          */
31         if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
32                 strcpy(engine, "libaio");
33
34         sprintf(engine_lib, "%s/lib/fio/fio-engine-%s.o", fio_inst_prefix, engine);
35         dlerror();
36         dlhandle = dlopen(engine_lib, RTLD_LAZY);
37         if (!dlhandle) {
38                 td_vmsg(td, -1, dlerror());
39                 return NULL;
40         }
41
42         ops = dlsym(dlhandle, "ioengine");
43         if (!ops) {
44                 td_vmsg(td, -1, dlerror());
45                 dlclose(dlhandle);
46                 return NULL;
47         }
48
49         if (ops->version != FIO_IOOPS_VERSION) {
50                 log_err("bad ioops version %d (want %d)\n", ops->version, FIO_IOOPS_VERSION);
51                 dlclose(dlhandle);
52                 return NULL;
53         }
54
55         ret = malloc(sizeof(*ret));
56         memcpy(ret, ops, sizeof(*ret));
57         ret->data = NULL;
58         ret->dlhandle = dlhandle;
59
60         return ret;
61 }
62
63 void close_ioengine(struct thread_data *td)
64 {
65         if (td->io_ops->cleanup)
66                 td->io_ops->cleanup(td);
67
68         dlclose(td->io_ops->dlhandle);
69         free(td->io_ops);
70         td->io_ops = NULL;
71 }
72
73 int td_io_prep(struct thread_data *td, struct io_u *io_u)
74 {
75         if (td->io_ops->prep && td->io_ops->prep(td, io_u))
76                 return 1;
77
78         return 0;
79 }
80
81 int td_io_sync(struct thread_data *td, struct fio_file *f)
82 {
83         if (td->io_ops->sync)
84                 return td->io_ops->sync(td, f);
85
86         return 0;
87 }
88
89 int td_io_getevents(struct thread_data *td, int min, int max,
90                     struct timespec *t)
91 {
92         return td->io_ops->getevents(td, min, max, t);
93 }
94
95 int td_io_queue(struct thread_data *td, struct io_u *io_u)
96 {
97         gettimeofday(&io_u->issue_time, NULL);
98
99         return td->io_ops->queue(td, io_u);
100 }