CPU burn engine fix
[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 <assert.h>
18
19 #include "fio.h"
20
21 static LIST_HEAD(engine_list);
22
23 static 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         if (!ops->queue) {
31                 log_err("%s: no queue handler\n", ops->name);
32                 return 1;
33         }
34
35         /*
36          * sync engines only need a ->queue()
37          */
38         if (ops->flags & FIO_SYNCIO)
39                 return 0;
40         
41         if (!ops->event) {
42                 log_err("%s: no event handler\n", ops->name);
43                 return 1;
44         }
45         if (!ops->getevents) {
46                 log_err("%s: no getevents handler\n", ops->name);
47                 return 1;
48         }
49         if (!ops->queue) {
50                 log_err("%s: no queue handler\n", ops->name);
51                 return 1;
52         }
53                 
54         return 0;
55 }
56
57 void unregister_ioengine(struct ioengine_ops *ops)
58 {
59         dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
60         list_del(&ops->list);
61         INIT_LIST_HEAD(&ops->list);
62 }
63
64 void register_ioengine(struct ioengine_ops *ops)
65 {
66         dprint(FD_IO, "ioengine %s registered\n", ops->name);
67         INIT_LIST_HEAD(&ops->list);
68         list_add_tail(&ops->list, &engine_list);
69 }
70
71 static struct ioengine_ops *find_ioengine(const char *name)
72 {
73         struct ioengine_ops *ops;
74         struct list_head *entry;
75
76         list_for_each(entry, &engine_list) {
77                 ops = list_entry(entry, struct ioengine_ops, list);
78                 if (!strcmp(name, ops->name))
79                         return ops;
80         }
81
82         return NULL;
83 }
84
85 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
86                                             const char *engine_lib)
87 {
88         struct ioengine_ops *ops;
89         void *dlhandle;
90
91         dprint(FD_IO, "dload engine %s\n", engine_lib);
92
93         dlerror();
94         dlhandle = dlopen(engine_lib, RTLD_LAZY);
95         if (!dlhandle) {
96                 td_vmsg(td, -1, dlerror(), "dlopen");
97                 return NULL;
98         }
99
100         /*
101          * Unlike the included modules, external engines should have a
102          * non-static ioengine structure that we can reference.
103          */
104         ops = dlsym(dlhandle, "ioengine");
105         if (!ops) {
106                 td_vmsg(td, -1, dlerror(), "dlsym");
107                 dlclose(dlhandle);
108                 return NULL;
109         }
110
111         ops->dlhandle = dlhandle;
112         return ops;
113 }
114
115 struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
116 {
117         struct ioengine_ops *ops, *ret;
118         char engine[16];
119
120         dprint(FD_IO, "load ioengine %s\n", name);
121
122         strncpy(engine, name, sizeof(engine) - 1);
123
124         /*
125          * linux libaio has alias names, so convert to what we want
126          */
127         if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
128                 strcpy(engine, "libaio");
129
130         ops = find_ioengine(engine);
131         if (!ops)
132                 ops = dlopen_ioengine(td, name);
133
134         if (!ops) {
135                 log_err("fio: engine %s not loadable\n", name);
136                 return NULL;
137         }
138
139         /*
140          * Check that the required methods are there.
141          */
142         if (check_engine_ops(ops))
143                 return NULL;
144
145         ret = malloc(sizeof(*ret));
146         memcpy(ret, ops, sizeof(*ret));
147         ret->data = NULL;
148
149         return ret;
150 }
151
152 void close_ioengine(struct thread_data *td)
153 {
154         dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
155
156         if (td->io_ops->cleanup)
157                 td->io_ops->cleanup(td);
158
159         if (td->io_ops->dlhandle)
160                 dlclose(td->io_ops->dlhandle);
161
162         free(td->io_ops);
163         td->io_ops = NULL;
164 }
165
166 int td_io_prep(struct thread_data *td, struct io_u *io_u)
167 {
168         dprint_io_u(io_u, "prep");
169         fio_ro_check(td, io_u);
170
171         if (td->io_ops->prep) {
172                 int ret = td->io_ops->prep(td, io_u);
173
174                 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
175                 return ret;
176         }
177
178         return 0;
179 }
180
181 int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
182                     struct timespec *t)
183 {
184         int r = 0;
185
186         if (min > 0 && td->io_ops->commit) {
187                 r = td->io_ops->commit(td);
188                 if (r < 0)
189                         goto out;
190         }
191
192         r = 0;
193         if (td->io_ops->getevents)
194                 r = td->io_ops->getevents(td, min, max, t);
195 out:
196         dprint(FD_IO, "getevents: %d\n", r);
197         return r;
198 }
199
200 int td_io_queue(struct thread_data *td, struct io_u *io_u)
201 {
202         int ret;
203
204         dprint_io_u(io_u, "queue");
205         fio_ro_check(td, io_u);
206
207         assert((io_u->flags & IO_U_F_FLIGHT) == 0);
208         io_u->flags |= IO_U_F_FLIGHT;
209
210         assert(io_u->file->flags & FIO_FILE_OPEN);
211
212         io_u->error = 0;
213         io_u->resid = 0;
214
215         if (td->io_ops->flags & FIO_SYNCIO) {
216                 fio_gettime(&io_u->issue_time, NULL);
217                 memcpy(&td->last_issue, &io_u->issue_time, sizeof(struct timeval));
218
219                 /*
220                  * for a sync engine, set the timeout upfront
221                  */
222                 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
223                         io_u_set_timeout(td);
224         }
225
226         if (io_u->ddir != DDIR_SYNC)
227                 td->io_issues[io_u->ddir]++;
228
229         io_u_mark_depth(td, io_u);
230
231         ret = td->io_ops->queue(td, io_u);
232
233         if (ret == FIO_Q_QUEUED) {
234                 int r;
235
236                 td->io_u_queued++;
237                 if (td->io_u_queued > td->o.iodepth_batch) {
238                         r = td_io_commit(td);
239                         if (r < 0)
240                                 return r;
241                 }
242         }
243
244         if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
245                 fio_gettime(&io_u->issue_time, NULL);
246                 memcpy(&td->last_issue, &io_u->issue_time, sizeof(struct timeval));
247
248                 /*
249                  * async engine, set the timeout here
250                  */
251                 if (ret == FIO_Q_QUEUED &&
252                     mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
253                         io_u_set_timeout(td);
254         }
255
256         return ret;
257 }
258
259 int td_io_init(struct thread_data *td)
260 {
261         int ret = 0;
262
263         if (td->io_ops->init) {
264                 ret = td->io_ops->init(td);
265                 if (ret && td->o.iodepth > 1)
266                         log_err("fio: io engine init failed. Perhaps try reducing io depth?\n");
267         }
268
269         return ret;
270 }
271
272 int td_io_commit(struct thread_data *td)
273 {
274         dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
275
276         if (!td->cur_depth)
277                 return 0;
278
279         td->io_u_queued = 0;
280         if (td->io_ops->commit)
281                 return td->io_ops->commit(td);
282
283         return 0;
284 }
285
286 int td_io_open_file(struct thread_data *td, struct fio_file *f)
287 {
288         if (td->io_ops->open_file(td, f)) {
289                 if (td->error == EINVAL && td->o.odirect)
290                         log_err("fio: destination does not support O_DIRECT\n");
291                 if (td->error == EMFILE)
292                         log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
293
294                 return 1;
295         }
296
297         if (f->filetype == FIO_TYPE_PIPE) {
298                 if (td_random(td)) {
299                         log_err("fio: can't seek on pipes (no random io)\n");
300                         goto err;
301                 }
302         }
303
304         f->last_free_lookup = 0;
305         f->last_completed_pos = 0;
306         f->last_pos = f->file_offset;
307         f->flags |= FIO_FILE_OPEN;
308         f->flags &= ~FIO_FILE_CLOSING;
309
310         if (td->io_ops->flags & FIO_DISKLESSIO)
311                 goto done;
312
313         if (td->o.invalidate_cache && file_invalidate_cache(td, f))
314                 goto err;
315
316         if (td->o.fadvise_hint &&
317             (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
318                 
319                 int flags;
320
321                 if (td_random(td))
322                         flags = POSIX_FADV_RANDOM;
323                 else
324                         flags = POSIX_FADV_SEQUENTIAL;
325
326                 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
327                         td_verror(td, errno, "fadvise");
328                         goto err;
329                 }
330         }
331
332         if (f->file_map)
333                 memset(f->file_map, 0, f->num_maps * sizeof(long));
334
335 done:
336         log_file(td, f, FIO_LOG_OPEN_FILE);
337         td->nr_open_files++;
338         get_file(f);
339         return 0;
340 err:
341         if (td->io_ops->close_file)
342                 td->io_ops->close_file(td, f);
343         return 1;
344 }
345
346 void td_io_close_file(struct thread_data *td, struct fio_file *f)
347 {
348         if (!(f->flags & FIO_FILE_CLOSING))
349                 log_file(td, f, FIO_LOG_CLOSE_FILE);
350
351         /*
352          * mark as closing, do real close when last io on it has completed
353          */
354         f->flags |= FIO_FILE_CLOSING;
355
356         put_file(td, f);
357 }