Improve submission latency calculation
[fio.git] / ioengines.c
... / ...
CommitLineData
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
18#include "fio.h"
19#include "os.h"
20
21static LIST_HEAD(engine_list);
22
23static int check_engine_ops(struct ioengine_ops *ops)
24{
25 if (ops->version != FIO_IOOPS_VERSION) {
26 log_err("bad ioops version %d (want %d)\n", ops->version, FIO_IOOPS_VERSION);
27 return 1;
28 }
29
30 /*
31 * cpu thread doesn't need to provide anything
32 */
33 if (ops->flags & FIO_CPUIO)
34 return 0;
35
36 if (!ops->queue) {
37 log_err("%s: no queue handler\n", ops->name);
38 return 1;
39 }
40
41 /*
42 * sync engines only need a ->queue()
43 */
44 if (ops->flags & FIO_SYNCIO)
45 return 0;
46
47 if (!ops->event) {
48 log_err("%s: no event handler\n", ops->name);
49 return 1;
50 }
51 if (!ops->getevents) {
52 log_err("%s: no getevents handler\n", ops->name);
53 return 1;
54 }
55 if (!ops->queue) {
56 log_err("%s: no queue handler\n", ops->name);
57 return 1;
58 }
59
60 return 0;
61}
62
63void unregister_ioengine(struct ioengine_ops *ops)
64{
65 list_del(&ops->list);
66 INIT_LIST_HEAD(&ops->list);
67}
68
69int register_ioengine(struct ioengine_ops *ops)
70{
71 INIT_LIST_HEAD(&ops->list);
72 list_add_tail(&ops->list, &engine_list);
73 return 0;
74}
75
76static struct ioengine_ops *find_ioengine(const char *name)
77{
78 struct ioengine_ops *ops;
79 struct list_head *entry;
80 char engine[16];
81
82 strncpy(engine, name, sizeof(engine) - 1);
83
84 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
85 strcpy(engine, "libaio");
86
87 list_for_each(entry, &engine_list) {
88 ops = list_entry(entry, struct ioengine_ops, list);
89 if (!strcmp(engine, ops->name))
90 return ops;
91 }
92
93 return NULL;
94}
95
96static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
97 const char *engine_lib)
98{
99 struct ioengine_ops *ops;
100 void *dlhandle;
101
102 dlerror();
103 dlhandle = dlopen(engine_lib, RTLD_LAZY);
104 if (!dlhandle) {
105 td_vmsg(td, -1, dlerror());
106 return NULL;
107 }
108
109 /*
110 * Unlike the included modules, external engines should have a
111 * non-static ioengine structure that we can reference.
112 */
113 ops = dlsym(dlhandle, "ioengine");
114 if (!ops) {
115 td_vmsg(td, -1, dlerror());
116 dlclose(dlhandle);
117 return NULL;
118 }
119
120 ops->dlhandle = dlhandle;
121 return ops;
122}
123
124struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
125{
126 struct ioengine_ops *ops, *ret;
127 char engine[16];
128
129 strncpy(engine, name, sizeof(engine) - 1);
130
131 /*
132 * linux libaio has alias names, so convert to what we want
133 */
134 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
135 strcpy(engine, "libaio");
136
137 ops = find_ioengine(engine);
138 if (!ops)
139 ops = dlopen_ioengine(td, name);
140
141 if (!ops) {
142 log_err("fio: engine %s not loadable\n", name);
143 return NULL;
144 }
145
146 /*
147 * Check that the required methods are there.
148 */
149 if (check_engine_ops(ops))
150 return NULL;
151
152 ret = malloc(sizeof(*ret));
153 memcpy(ret, ops, sizeof(*ret));
154 ret->data = NULL;
155
156 return ret;
157}
158
159void close_ioengine(struct thread_data *td)
160{
161 if (td->io_ops->cleanup)
162 td->io_ops->cleanup(td);
163
164 if (td->io_ops->dlhandle)
165 dlclose(td->io_ops->dlhandle);
166
167 free(td->io_ops);
168 td->io_ops = NULL;
169}
170
171int td_io_prep(struct thread_data *td, struct io_u *io_u)
172{
173 if (td->io_ops->prep)
174 return td->io_ops->prep(td, io_u);
175
176 return 0;
177}
178
179int td_io_getevents(struct thread_data *td, int min, int max,
180 struct timespec *t)
181{
182 if (td->io_ops->getevents)
183 return td->io_ops->getevents(td, min, max, t);
184
185 return 0;
186}
187
188int td_io_queue(struct thread_data *td, struct io_u *io_u)
189{
190 int ret;
191
192
193 if (io_u->ddir != DDIR_SYNC)
194 td->io_issues[io_u->ddir]++;
195
196 ret = td->io_ops->queue(td, io_u);
197 fio_gettime(&io_u->issue_time, NULL);
198 return ret;
199}
200
201int td_io_init(struct thread_data *td)
202{
203 if (td->io_ops->init)
204 return td->io_ops->init(td);
205
206 return 0;
207}
208
209int td_io_commit(struct thread_data *td)
210{
211 if (td->io_ops->commit)
212 return td->io_ops->commit(td);
213
214 return 0;
215}