Commit | Line | Data |
---|---|---|
ebac4655 JA |
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> | |
5c4e1dbc | 15 | #include <string.h> |
2866c82d | 16 | #include <dlfcn.h> |
0c6e7517 | 17 | #include <assert.h> |
8c16d840 | 18 | |
ebac4655 JA |
19 | #include "fio.h" |
20 | #include "os.h" | |
21 | ||
5f350952 JA |
22 | static LIST_HEAD(engine_list); |
23 | ||
8c16d840 JA |
24 | static int check_engine_ops(struct ioengine_ops *ops) |
25 | { | |
5f350952 JA |
26 | if (ops->version != FIO_IOOPS_VERSION) { |
27 | log_err("bad ioops version %d (want %d)\n", ops->version, FIO_IOOPS_VERSION); | |
28 | return 1; | |
29 | } | |
30 | ||
8c16d840 JA |
31 | /* |
32 | * cpu thread doesn't need to provide anything | |
33 | */ | |
34 | if (ops->flags & FIO_CPUIO) | |
35 | return 0; | |
36 | ||
36167d82 JA |
37 | if (!ops->queue) { |
38 | log_err("%s: no queue handler\n", ops->name); | |
39 | return 1; | |
40 | } | |
41 | ||
42 | /* | |
43 | * sync engines only need a ->queue() | |
44 | */ | |
45 | if (ops->flags & FIO_SYNCIO) | |
46 | return 0; | |
47 | ||
8c16d840 | 48 | if (!ops->event) { |
36167d82 | 49 | log_err("%s: no event handler\n", ops->name); |
8c16d840 JA |
50 | return 1; |
51 | } | |
52 | if (!ops->getevents) { | |
36167d82 | 53 | log_err("%s: no getevents handler\n", ops->name); |
8c16d840 JA |
54 | return 1; |
55 | } | |
56 | if (!ops->queue) { | |
36167d82 | 57 | log_err("%s: no queue handler\n", ops->name); |
8c16d840 JA |
58 | return 1; |
59 | } | |
60 | ||
61 | return 0; | |
62 | } | |
63 | ||
5f350952 | 64 | void unregister_ioengine(struct ioengine_ops *ops) |
ebac4655 | 65 | { |
5f350952 JA |
66 | list_del(&ops->list); |
67 | INIT_LIST_HEAD(&ops->list); | |
68 | } | |
69 | ||
b2fdda43 | 70 | void register_ioengine(struct ioengine_ops *ops) |
5f350952 | 71 | { |
5f350952 JA |
72 | INIT_LIST_HEAD(&ops->list); |
73 | list_add_tail(&ops->list, &engine_list); | |
5f350952 JA |
74 | } |
75 | ||
76 | static struct ioengine_ops *find_ioengine(const char *name) | |
77 | { | |
78 | struct ioengine_ops *ops; | |
79 | struct list_head *entry; | |
ebac4655 | 80 | |
5f350952 JA |
81 | list_for_each(entry, &engine_list) { |
82 | ops = list_entry(entry, struct ioengine_ops, list); | |
bc5b77a8 | 83 | if (!strcmp(name, ops->name)) |
5f350952 JA |
84 | return ops; |
85 | } | |
86 | ||
87 | return NULL; | |
88 | } | |
89 | ||
90 | static struct ioengine_ops *dlopen_ioengine(struct thread_data *td, | |
91 | const char *engine_lib) | |
92 | { | |
93 | struct ioengine_ops *ops; | |
94 | void *dlhandle; | |
95 | ||
2866c82d JA |
96 | dlerror(); |
97 | dlhandle = dlopen(engine_lib, RTLD_LAZY); | |
d4dbaaa8 | 98 | if (!dlhandle) { |
e1161c32 | 99 | td_vmsg(td, -1, dlerror(), "dlopen"); |
d4dbaaa8 JA |
100 | return NULL; |
101 | } | |
8756e4d4 | 102 | |
da51c050 JA |
103 | /* |
104 | * Unlike the included modules, external engines should have a | |
105 | * non-static ioengine structure that we can reference. | |
106 | */ | |
2866c82d | 107 | ops = dlsym(dlhandle, "ioengine"); |
d4dbaaa8 | 108 | if (!ops) { |
e1161c32 | 109 | td_vmsg(td, -1, dlerror(), "dlsym"); |
d4dbaaa8 JA |
110 | dlclose(dlhandle); |
111 | return NULL; | |
112 | } | |
8756e4d4 | 113 | |
5f350952 JA |
114 | ops->dlhandle = dlhandle; |
115 | return ops; | |
116 | } | |
117 | ||
118 | struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name) | |
119 | { | |
120 | struct ioengine_ops *ops, *ret; | |
121 | char engine[16]; | |
122 | ||
123 | strncpy(engine, name, sizeof(engine) - 1); | |
124 | ||
125 | /* | |
126 | * linux libaio has alias names, so convert to what we want | |
127 | */ | |
128 | if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3)) | |
129 | strcpy(engine, "libaio"); | |
130 | ||
131 | ops = find_ioengine(engine); | |
132 | if (!ops) | |
133 | ops = dlopen_ioengine(td, name); | |
134 | ||
135 | if (!ops) { | |
136 | log_err("fio: engine %s not loadable\n", name); | |
b902ceb5 JA |
137 | return NULL; |
138 | } | |
139 | ||
8c16d840 JA |
140 | /* |
141 | * Check that the required methods are there. | |
142 | */ | |
5f350952 | 143 | if (check_engine_ops(ops)) |
8c16d840 | 144 | return NULL; |
8c16d840 | 145 | |
84585003 JA |
146 | ret = malloc(sizeof(*ret)); |
147 | memcpy(ret, ops, sizeof(*ret)); | |
148 | ret->data = NULL; | |
84585003 JA |
149 | |
150 | return ret; | |
8756e4d4 JA |
151 | } |
152 | ||
2866c82d | 153 | void close_ioengine(struct thread_data *td) |
8756e4d4 | 154 | { |
2866c82d JA |
155 | if (td->io_ops->cleanup) |
156 | td->io_ops->cleanup(td); | |
b990b5c0 | 157 | |
5f350952 JA |
158 | if (td->io_ops->dlhandle) |
159 | dlclose(td->io_ops->dlhandle); | |
160 | ||
19a98c41 JA |
161 | #if 0 |
162 | /* we can't do this for threads, so just leak it, it's exiting */ | |
84585003 | 163 | free(td->io_ops); |
19a98c41 | 164 | #endif |
84585003 | 165 | td->io_ops = NULL; |
b990b5c0 | 166 | } |
10ba535a JA |
167 | |
168 | int td_io_prep(struct thread_data *td, struct io_u *io_u) | |
169 | { | |
36167d82 JA |
170 | if (td->io_ops->prep) |
171 | return td->io_ops->prep(td, io_u); | |
10ba535a JA |
172 | |
173 | return 0; | |
174 | } | |
175 | ||
10ba535a JA |
176 | int td_io_getevents(struct thread_data *td, int min, int max, |
177 | struct timespec *t) | |
178 | { | |
face81b2 JA |
179 | if (min > 0 && td->io_ops->commit) { |
180 | int r = td->io_ops->commit(td); | |
181 | ||
182 | if (r < 0) | |
183 | return r; | |
184 | } | |
36167d82 JA |
185 | if (td->io_ops->getevents) |
186 | return td->io_ops->getevents(td, min, max, t); | |
187 | ||
188 | return 0; | |
10ba535a JA |
189 | } |
190 | ||
191 | int td_io_queue(struct thread_data *td, struct io_u *io_u) | |
192 | { | |
7e77dd02 JA |
193 | int ret; |
194 | ||
0c6e7517 JA |
195 | assert((io_u->flags & IO_U_F_FLIGHT) == 0); |
196 | io_u->flags |= IO_U_F_FLIGHT; | |
197 | ||
433afcb4 | 198 | if (td->io_ops->flags & FIO_SYNCIO) { |
5aeb77df | 199 | fio_gettime(&io_u->issue_time, NULL); |
10ba535a | 200 | |
433afcb4 JA |
201 | /* |
202 | * for a sync engine, set the timeout upfront | |
203 | */ | |
204 | if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT) | |
205 | io_u_set_timeout(td); | |
206 | } | |
207 | ||
755200a3 JA |
208 | if (io_u->ddir != DDIR_SYNC) |
209 | td->io_issues[io_u->ddir]++; | |
210 | ||
7e77dd02 | 211 | ret = td->io_ops->queue(td, io_u); |
5aeb77df | 212 | |
eb7c8ae2 JA |
213 | if (ret == FIO_Q_QUEUED) { |
214 | int r; | |
215 | ||
cb5ab512 | 216 | td->io_u_queued++; |
eb7c8ae2 JA |
217 | if (td->io_u_queued > td->iodepth_batch) { |
218 | r = td_io_commit(td); | |
219 | if (r < 0) | |
220 | return r; | |
221 | } | |
222 | } | |
cb5ab512 | 223 | |
433afcb4 | 224 | if ((td->io_ops->flags & FIO_SYNCIO) == 0) { |
5aeb77df JA |
225 | fio_gettime(&io_u->issue_time, NULL); |
226 | ||
433afcb4 JA |
227 | /* |
228 | * async engine, set the timeout here | |
229 | */ | |
230 | if (ret == FIO_Q_QUEUED && | |
231 | mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT) | |
232 | io_u_set_timeout(td); | |
233 | } | |
234 | ||
7e77dd02 | 235 | return ret; |
10ba535a | 236 | } |
8c16d840 JA |
237 | |
238 | int td_io_init(struct thread_data *td) | |
239 | { | |
240 | if (td->io_ops->init) | |
241 | return td->io_ops->init(td); | |
242 | ||
243 | return 0; | |
244 | } | |
755200a3 JA |
245 | |
246 | int td_io_commit(struct thread_data *td) | |
247 | { | |
e1161c32 JA |
248 | if (!td->cur_depth) |
249 | return 0; | |
cb5ab512 JA |
250 | |
251 | td->io_u_queued = 0; | |
755200a3 JA |
252 | if (td->io_ops->commit) |
253 | return td->io_ops->commit(td); | |
254 | ||
255 | return 0; | |
256 | } |