blob: a71357c67ddce9002b97f5d1afa4749a1e49100d [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
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>
Jens Axboe5c4e1db2006-06-07 14:17:08 +020015#include <string.h>
Jens Axboe2866c822006-10-09 15:57:48 +020016#include <dlfcn.h>
Jens Axboe8c16d842006-10-20 11:48:33 +020017
Jens Axboeebac4652005-12-08 15:25:21 +010018#include "fio.h"
19#include "os.h"
20
Jens Axboe5f350952006-11-07 15:20:59 +010021static LIST_HEAD(engine_list);
22
Jens Axboe8c16d842006-10-20 11:48:33 +020023static int check_engine_ops(struct ioengine_ops *ops)
24{
Jens Axboe5f350952006-11-07 15:20:59 +010025 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
Jens Axboe8c16d842006-10-20 11:48:33 +020030 /*
31 * cpu thread doesn't need to provide anything
32 */
33 if (ops->flags & FIO_CPUIO)
34 return 0;
35
36 if (!ops->event) {
37 log_err("%s: no event handler)\n", ops->name);
38 return 1;
39 }
40 if (!ops->getevents) {
41 log_err("%s: no getevents handler)\n", ops->name);
42 return 1;
43 }
44 if (!ops->queue) {
45 log_err("%s: no queue handler)\n", ops->name);
46 return 1;
47 }
48
49 return 0;
50}
51
Jens Axboe5f350952006-11-07 15:20:59 +010052void unregister_ioengine(struct ioengine_ops *ops)
Jens Axboeebac4652005-12-08 15:25:21 +010053{
Jens Axboe5f350952006-11-07 15:20:59 +010054 list_del(&ops->list);
55 INIT_LIST_HEAD(&ops->list);
56}
57
58int register_ioengine(struct ioengine_ops *ops)
59{
60 if (check_engine_ops(ops))
61 return 1;
62
63 INIT_LIST_HEAD(&ops->list);
64 list_add_tail(&ops->list, &engine_list);
65 return 0;
66}
67
68static struct ioengine_ops *find_ioengine(const char *name)
69{
70 struct ioengine_ops *ops;
71 struct list_head *entry;
72 char engine[16];
Jens Axboeebac4652005-12-08 15:25:21 +010073
Jens Axboe0bbab0e2006-11-02 09:18:36 +010074 strncpy(engine, name, sizeof(engine) - 1);
Jens Axboeebac4652005-12-08 15:25:21 +010075
Jens Axboe2866c822006-10-09 15:57:48 +020076 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
77 strcpy(engine, "libaio");
78
Jens Axboe5f350952006-11-07 15:20:59 +010079 list_for_each(entry, &engine_list) {
80 ops = list_entry(entry, struct ioengine_ops, list);
81 if (!strcmp(engine, ops->name))
82 return ops;
83 }
84
85 return NULL;
86}
87
88static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
89 const char *engine_lib)
90{
91 struct ioengine_ops *ops;
92 void *dlhandle;
93
Jens Axboe2866c822006-10-09 15:57:48 +020094 dlerror();
95 dlhandle = dlopen(engine_lib, RTLD_LAZY);
Jens Axboed4dbaaa2006-10-09 16:01:27 +020096 if (!dlhandle) {
97 td_vmsg(td, -1, dlerror());
98 return NULL;
99 }
Jens Axboe2866c822006-10-09 15:57:48 +0200100
101 ops = dlsym(dlhandle, "ioengine");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200102 if (!ops) {
103 td_vmsg(td, -1, dlerror());
104 dlclose(dlhandle);
105 return NULL;
106 }
Jens Axboe2866c822006-10-09 15:57:48 +0200107
Jens Axboe5f350952006-11-07 15:20:59 +0100108 ops->dlhandle = dlhandle;
109 return ops;
110}
111
112struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
113{
114 struct ioengine_ops *ops, *ret;
115 char engine[16];
116
117 strncpy(engine, name, sizeof(engine) - 1);
118
119 /*
120 * linux libaio has alias names, so convert to what we want
121 */
122 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
123 strcpy(engine, "libaio");
124
125 ops = find_ioengine(engine);
126 if (!ops)
127 ops = dlopen_ioengine(td, name);
128
129 if (!ops) {
130 log_err("fio: engine %s not loadable\n", name);
Jens Axboeb902ceb2006-10-09 16:11:45 +0200131 return NULL;
132 }
133
Jens Axboe8c16d842006-10-20 11:48:33 +0200134 /*
135 * Check that the required methods are there.
136 */
Jens Axboe5f350952006-11-07 15:20:59 +0100137 if (check_engine_ops(ops))
Jens Axboe8c16d842006-10-20 11:48:33 +0200138 return NULL;
Jens Axboe8c16d842006-10-20 11:48:33 +0200139
Jens Axboe84585002006-10-19 20:26:22 +0200140 ret = malloc(sizeof(*ret));
141 memcpy(ret, ops, sizeof(*ret));
142 ret->data = NULL;
Jens Axboe84585002006-10-19 20:26:22 +0200143
144 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100145}
146
Jens Axboe2866c822006-10-09 15:57:48 +0200147void close_ioengine(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100148{
Jens Axboe2866c822006-10-09 15:57:48 +0200149 if (td->io_ops->cleanup)
150 td->io_ops->cleanup(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100151
Jens Axboe5f350952006-11-07 15:20:59 +0100152 if (td->io_ops->dlhandle)
153 dlclose(td->io_ops->dlhandle);
154
Jens Axboe84585002006-10-19 20:26:22 +0200155 free(td->io_ops);
156 td->io_ops = NULL;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200157}
Jens Axboe10ba5352006-10-20 11:39:27 +0200158
159int td_io_prep(struct thread_data *td, struct io_u *io_u)
160{
161 if (td->io_ops->prep && td->io_ops->prep(td, io_u))
162 return 1;
163
164 return 0;
165}
166
Jens Axboe10ba5352006-10-20 11:39:27 +0200167int td_io_getevents(struct thread_data *td, int min, int max,
168 struct timespec *t)
169{
170 return td->io_ops->getevents(td, min, max, t);
171}
172
173int td_io_queue(struct thread_data *td, struct io_u *io_u)
174{
175 gettimeofday(&io_u->issue_time, NULL);
176
177 return td->io_ops->queue(td, io_u);
178}
Jens Axboe8c16d842006-10-20 11:48:33 +0200179
180int td_io_init(struct thread_data *td)
181{
182 if (td->io_ops->init)
183 return td->io_ops->init(td);
184
185 return 0;
186}