Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 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> |
Jens Axboe | 5c4e1db | 2006-06-07 14:17:08 +0200 | [diff] [blame] | 15 | #include <string.h> |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 16 | #include <dlfcn.h> |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 17 | #include <assert.h> |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 18 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 19 | #include "fio.h" |
| 20 | #include "os.h" |
| 21 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 22 | static LIST_HEAD(engine_list); |
| 23 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 24 | static int check_engine_ops(struct ioengine_ops *ops) |
| 25 | { |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 31 | if (!ops->queue) { |
| 32 | log_err("%s: no queue handler\n", ops->name); |
| 33 | return 1; |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * sync engines only need a ->queue() |
| 38 | */ |
| 39 | if (ops->flags & FIO_SYNCIO) |
| 40 | return 0; |
| 41 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 42 | if (!ops->event) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 43 | log_err("%s: no event handler\n", ops->name); |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 44 | return 1; |
| 45 | } |
| 46 | if (!ops->getevents) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 47 | log_err("%s: no getevents handler\n", ops->name); |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 48 | return 1; |
| 49 | } |
| 50 | if (!ops->queue) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 51 | log_err("%s: no queue handler\n", ops->name); |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 58 | void unregister_ioengine(struct ioengine_ops *ops) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 59 | { |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 60 | list_del(&ops->list); |
| 61 | INIT_LIST_HEAD(&ops->list); |
| 62 | } |
| 63 | |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 64 | void register_ioengine(struct ioengine_ops *ops) |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 65 | { |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 66 | INIT_LIST_HEAD(&ops->list); |
| 67 | list_add_tail(&ops->list, &engine_list); |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static struct ioengine_ops *find_ioengine(const char *name) |
| 71 | { |
| 72 | struct ioengine_ops *ops; |
| 73 | struct list_head *entry; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 74 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 75 | list_for_each(entry, &engine_list) { |
| 76 | ops = list_entry(entry, struct ioengine_ops, list); |
Jens Axboe | bc5b77a | 2007-02-28 09:32:54 +0100 | [diff] [blame] | 77 | if (!strcmp(name, ops->name)) |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 78 | return ops; |
| 79 | } |
| 80 | |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | static struct ioengine_ops *dlopen_ioengine(struct thread_data *td, |
| 85 | const char *engine_lib) |
| 86 | { |
| 87 | struct ioengine_ops *ops; |
| 88 | void *dlhandle; |
| 89 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 90 | dlerror(); |
| 91 | dlhandle = dlopen(engine_lib, RTLD_LAZY); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 92 | if (!dlhandle) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 93 | td_vmsg(td, -1, dlerror(), "dlopen"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 94 | return NULL; |
| 95 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 96 | |
Jens Axboe | da51c05 | 2006-11-07 16:02:11 +0100 | [diff] [blame] | 97 | /* |
| 98 | * Unlike the included modules, external engines should have a |
| 99 | * non-static ioengine structure that we can reference. |
| 100 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 101 | ops = dlsym(dlhandle, "ioengine"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 102 | if (!ops) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 103 | td_vmsg(td, -1, dlerror(), "dlsym"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 104 | dlclose(dlhandle); |
| 105 | return NULL; |
| 106 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 107 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 108 | ops->dlhandle = dlhandle; |
| 109 | return ops; |
| 110 | } |
| 111 | |
| 112 | struct 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 Axboe | b902ceb | 2006-10-09 16:11:45 +0200 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
| 133 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 134 | /* |
| 135 | * Check that the required methods are there. |
| 136 | */ |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 137 | if (check_engine_ops(ops)) |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 138 | return NULL; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 139 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 140 | ret = malloc(sizeof(*ret)); |
| 141 | memcpy(ret, ops, sizeof(*ret)); |
| 142 | ret->data = NULL; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 143 | |
| 144 | return ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 147 | void close_ioengine(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 148 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 149 | if (td->io_ops->cleanup) |
| 150 | td->io_ops->cleanup(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 151 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 152 | if (td->io_ops->dlhandle) |
| 153 | dlclose(td->io_ops->dlhandle); |
| 154 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 155 | free(td->io_ops); |
| 156 | td->io_ops = NULL; |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 157 | } |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 158 | |
| 159 | int td_io_prep(struct thread_data *td, struct io_u *io_u) |
| 160 | { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 161 | if (td->io_ops->prep) |
| 162 | return td->io_ops->prep(td, io_u); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 167 | int td_io_getevents(struct thread_data *td, int min, int max, |
| 168 | struct timespec *t) |
| 169 | { |
Jens Axboe | face81b | 2007-02-26 10:40:03 +0100 | [diff] [blame] | 170 | if (min > 0 && td->io_ops->commit) { |
| 171 | int r = td->io_ops->commit(td); |
| 172 | |
| 173 | if (r < 0) |
| 174 | return r; |
| 175 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 176 | if (td->io_ops->getevents) |
| 177 | return td->io_ops->getevents(td, min, max, t); |
| 178 | |
| 179 | return 0; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | int td_io_queue(struct thread_data *td, struct io_u *io_u) |
| 183 | { |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 184 | int ret; |
| 185 | |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 186 | assert((io_u->flags & IO_U_F_FLIGHT) == 0); |
| 187 | io_u->flags |= IO_U_F_FLIGHT; |
| 188 | |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 189 | assert(io_u->file->flags & FIO_FILE_OPEN); |
| 190 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 191 | io_u->error = 0; |
| 192 | io_u->resid = 0; |
| 193 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 194 | if (td->io_ops->flags & FIO_SYNCIO) { |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 195 | fio_gettime(&io_u->issue_time, NULL); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 196 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 197 | /* |
| 198 | * for a sync engine, set the timeout upfront |
| 199 | */ |
| 200 | if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT) |
| 201 | io_u_set_timeout(td); |
| 202 | } |
| 203 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 204 | if (io_u->ddir != DDIR_SYNC) |
| 205 | td->io_issues[io_u->ddir]++; |
| 206 | |
Jens Axboe | b360506 | 2007-03-12 14:19:47 +0100 | [diff] [blame] | 207 | io_u_mark_depth(td, io_u); |
| 208 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 209 | ret = td->io_ops->queue(td, io_u); |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 210 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 211 | if (ret == FIO_Q_QUEUED || ret == FIO_Q_COMPLETED) |
| 212 | get_file(io_u->file); |
| 213 | |
Jens Axboe | eb7c8ae | 2007-02-26 14:45:12 +0100 | [diff] [blame] | 214 | if (ret == FIO_Q_QUEUED) { |
| 215 | int r; |
| 216 | |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 217 | td->io_u_queued++; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 218 | if (td->io_u_queued > td->o.iodepth_batch) { |
Jens Axboe | eb7c8ae | 2007-02-26 14:45:12 +0100 | [diff] [blame] | 219 | r = td_io_commit(td); |
| 220 | if (r < 0) |
| 221 | return r; |
| 222 | } |
| 223 | } |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 224 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 225 | if ((td->io_ops->flags & FIO_SYNCIO) == 0) { |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 226 | fio_gettime(&io_u->issue_time, NULL); |
| 227 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 228 | /* |
| 229 | * async engine, set the timeout here |
| 230 | */ |
| 231 | if (ret == FIO_Q_QUEUED && |
| 232 | mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT) |
| 233 | io_u_set_timeout(td); |
| 234 | } |
| 235 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 236 | return ret; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 237 | } |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 238 | |
| 239 | int td_io_init(struct thread_data *td) |
| 240 | { |
Jens Axboe | eeb1216 | 2007-03-20 10:47:45 +0100 | [diff] [blame^] | 241 | int ret = 0; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 242 | |
Jens Axboe | eeb1216 | 2007-03-20 10:47:45 +0100 | [diff] [blame^] | 243 | if (td->io_ops->init) { |
| 244 | ret = td->io_ops->init(td); |
| 245 | if (ret && td->o.iodepth > 1) |
| 246 | log_err("fio: io engine init failed. Perhaps try reducing io dpeth?\n"); |
| 247 | } |
| 248 | |
| 249 | return ret; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 250 | } |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 251 | |
| 252 | int td_io_commit(struct thread_data *td) |
| 253 | { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 254 | if (!td->cur_depth) |
| 255 | return 0; |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 256 | |
| 257 | td->io_u_queued = 0; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 258 | if (td->io_ops->commit) |
| 259 | return td->io_ops->commit(td); |
| 260 | |
| 261 | return 0; |
| 262 | } |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 263 | |
| 264 | int td_io_open_file(struct thread_data *td, struct fio_file *f) |
| 265 | { |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 266 | if (td->io_ops->open_file(td, f)) |
| 267 | return 1; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 268 | |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 269 | f->last_free_lookup = 0; |
| 270 | f->last_completed_pos = 0; |
| 271 | f->last_pos = 0; |
Jens Axboe | f11bd94 | 2007-03-13 10:16:34 +0100 | [diff] [blame] | 272 | f->flags |= FIO_FILE_OPEN; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 273 | f->flags &= ~FIO_FILE_CLOSING; |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 274 | |
| 275 | if (f->file_map) |
| 276 | memset(f->file_map, 0, f->num_maps * sizeof(long)); |
| 277 | |
| 278 | td->nr_open_files++; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 279 | get_file(f); |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 280 | return 0; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void td_io_close_file(struct thread_data *td, struct fio_file *f) |
| 284 | { |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 285 | /* |
| 286 | * mark as closing, do real close when last io on it has completed |
| 287 | */ |
| 288 | f->flags |= FIO_FILE_CLOSING; |
| 289 | |
| 290 | put_file(td, f); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 291 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 292 | |