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" |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 20 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 21 | static FLIST_HEAD(engine_list); |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 22 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 23 | static int check_engine_ops(struct ioengine_ops *ops) |
| 24 | { |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 25 | if (ops->version != FIO_IOOPS_VERSION) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 26 | log_err("bad ioops version %d (want %d)\n", ops->version, |
| 27 | FIO_IOOPS_VERSION); |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 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; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 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 | } |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 54 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 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 | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 60 | dprint(FD_IO, "ioengine %s unregistered\n", ops->name); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 61 | flist_del(&ops->list); |
| 62 | INIT_FLIST_HEAD(&ops->list); |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 65 | void register_ioengine(struct ioengine_ops *ops) |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 66 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 67 | dprint(FD_IO, "ioengine %s registered\n", ops->name); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 68 | INIT_FLIST_HEAD(&ops->list); |
| 69 | flist_add_tail(&ops->list, &engine_list); |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static struct ioengine_ops *find_ioengine(const char *name) |
| 73 | { |
| 74 | struct ioengine_ops *ops; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 75 | struct flist_head *entry; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 76 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 77 | flist_for_each(entry, &engine_list) { |
| 78 | ops = flist_entry(entry, struct ioengine_ops, list); |
Jens Axboe | bc5b77a | 2007-02-28 09:32:54 +0100 | [diff] [blame] | 79 | if (!strcmp(name, ops->name)) |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 80 | return ops; |
| 81 | } |
| 82 | |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | static struct ioengine_ops *dlopen_ioengine(struct thread_data *td, |
| 87 | const char *engine_lib) |
| 88 | { |
| 89 | struct ioengine_ops *ops; |
| 90 | void *dlhandle; |
| 91 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 92 | dprint(FD_IO, "dload engine %s\n", engine_lib); |
| 93 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 94 | dlerror(); |
| 95 | dlhandle = dlopen(engine_lib, RTLD_LAZY); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 96 | if (!dlhandle) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 97 | td_vmsg(td, -1, dlerror(), "dlopen"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 98 | return NULL; |
| 99 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 100 | |
Jens Axboe | da51c05 | 2006-11-07 16:02:11 +0100 | [diff] [blame] | 101 | /* |
| 102 | * Unlike the included modules, external engines should have a |
| 103 | * non-static ioengine structure that we can reference. |
| 104 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 105 | ops = dlsym(dlhandle, "ioengine"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 106 | if (!ops) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 107 | td_vmsg(td, -1, dlerror(), "dlsym"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 108 | dlclose(dlhandle); |
| 109 | return NULL; |
| 110 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 111 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 112 | ops->dlhandle = dlhandle; |
| 113 | return ops; |
| 114 | } |
| 115 | |
| 116 | struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name) |
| 117 | { |
| 118 | struct ioengine_ops *ops, *ret; |
| 119 | char engine[16]; |
| 120 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 121 | dprint(FD_IO, "load ioengine %s\n", name); |
| 122 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 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); |
Jens Axboe | b902ceb | 2006-10-09 16:11:45 +0200 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
| 139 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 140 | /* |
| 141 | * Check that the required methods are there. |
| 142 | */ |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 143 | if (check_engine_ops(ops)) |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 144 | return NULL; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 145 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 146 | ret = malloc(sizeof(*ret)); |
| 147 | memcpy(ret, ops, sizeof(*ret)); |
| 148 | ret->data = NULL; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 149 | |
| 150 | return ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 153 | void close_ioengine(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 154 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 155 | dprint(FD_IO, "close ioengine %s\n", td->io_ops->name); |
| 156 | |
Jens Axboe | 2992b05 | 2008-05-30 22:07:12 +0200 | [diff] [blame] | 157 | if (td->io_ops->cleanup) { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 158 | td->io_ops->cleanup(td); |
Jens Axboe | 2992b05 | 2008-05-30 22:07:12 +0200 | [diff] [blame] | 159 | td->io_ops->data = NULL; |
| 160 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 161 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 162 | if (td->io_ops->dlhandle) |
| 163 | dlclose(td->io_ops->dlhandle); |
| 164 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 165 | free(td->io_ops); |
| 166 | td->io_ops = NULL; |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 167 | } |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 168 | |
| 169 | int td_io_prep(struct thread_data *td, struct io_u *io_u) |
| 170 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 171 | dprint_io_u(io_u, "prep"); |
Jens Axboe | 7101d9c | 2007-09-12 13:12:39 +0200 | [diff] [blame] | 172 | fio_ro_check(td, io_u); |
| 173 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 174 | lock_file(td, io_u->file, io_u->ddir); |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 175 | |
Jens Axboe | 2ba1c29 | 2008-02-01 13:16:38 +0100 | [diff] [blame] | 176 | if (td->io_ops->prep) { |
| 177 | int ret = td->io_ops->prep(td, io_u); |
| 178 | |
| 179 | dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret); |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 180 | if (ret) |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 181 | unlock_file(td, io_u->file); |
Jens Axboe | 2ba1c29 | 2008-02-01 13:16:38 +0100 | [diff] [blame] | 182 | return ret; |
| 183 | } |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Jens Axboe | e7d2e61 | 2007-12-11 10:49:39 +0100 | [diff] [blame] | 188 | int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max, |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 189 | struct timespec *t) |
| 190 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 191 | int r = 0; |
| 192 | |
Jens Axboe | face81b | 2007-02-26 10:40:03 +0100 | [diff] [blame] | 193 | if (min > 0 && td->io_ops->commit) { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 194 | r = td->io_ops->commit(td); |
Jens Axboe | face81b | 2007-02-26 10:40:03 +0100 | [diff] [blame] | 195 | if (r < 0) |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 196 | goto out; |
Jens Axboe | face81b | 2007-02-26 10:40:03 +0100 | [diff] [blame] | 197 | } |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 198 | if (max > td->cur_depth) |
| 199 | max = td->cur_depth; |
| 200 | if (min > max) |
| 201 | max = min; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 202 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 203 | r = 0; |
Jens Axboe | 4950421 | 2008-06-05 09:03:30 +0200 | [diff] [blame] | 204 | if (max && td->io_ops->getevents) |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 205 | r = td->io_ops->getevents(td, min, max, t); |
| 206 | out: |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 207 | if (r >= 0) |
| 208 | io_u_mark_complete(td, r); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 209 | dprint(FD_IO, "getevents: %d\n", r); |
| 210 | return r; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | int td_io_queue(struct thread_data *td, struct io_u *io_u) |
| 214 | { |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 215 | int ret; |
| 216 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 217 | dprint_io_u(io_u, "queue"); |
Jens Axboe | 7101d9c | 2007-09-12 13:12:39 +0200 | [diff] [blame] | 218 | fio_ro_check(td, io_u); |
| 219 | |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 220 | assert((io_u->flags & IO_U_F_FLIGHT) == 0); |
| 221 | io_u->flags |= IO_U_F_FLIGHT; |
| 222 | |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 223 | assert(io_u->file->flags & FIO_FILE_OPEN); |
| 224 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 225 | io_u->error = 0; |
| 226 | io_u->resid = 0; |
| 227 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 228 | if (td->io_ops->flags & FIO_SYNCIO) { |
Jens Axboe | 12d9d84 | 2008-10-16 21:26:10 +0200 | [diff] [blame] | 229 | if (fio_fill_issue_time(td)) |
Jens Axboe | 9520ebb | 2008-10-16 21:03:27 +0200 | [diff] [blame] | 230 | fio_gettime(&io_u->issue_time, NULL); |
Jens Axboe | d0c1532 | 2008-10-16 20:33:51 +0200 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * only used for iolog |
| 234 | */ |
| 235 | if (td->o.read_iolog_file) |
| 236 | memcpy(&td->last_issue, &io_u->issue_time, |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 237 | sizeof(struct timeval)); |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 240 | if (io_u->ddir != DDIR_SYNC) |
| 241 | td->io_issues[io_u->ddir]++; |
| 242 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 243 | ret = td->io_ops->queue(td, io_u); |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 244 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 245 | unlock_file(td, io_u->file); |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 246 | |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 247 | if (!td->io_ops->commit) { |
| 248 | io_u_mark_submit(td, 1); |
| 249 | io_u_mark_complete(td, 1); |
| 250 | } |
| 251 | |
Jens Axboe | d800575 | 2008-05-15 09:49:09 +0200 | [diff] [blame] | 252 | if (ret == FIO_Q_COMPLETED) { |
| 253 | if (io_u->ddir != DDIR_SYNC) { |
| 254 | io_u_mark_depth(td, 1); |
| 255 | td->ts.total_io_u[io_u->ddir]++; |
| 256 | } |
| 257 | } else if (ret == FIO_Q_QUEUED) { |
Jens Axboe | eb7c8ae | 2007-02-26 14:45:12 +0100 | [diff] [blame] | 258 | int r; |
| 259 | |
Jens Axboe | d800575 | 2008-05-15 09:49:09 +0200 | [diff] [blame] | 260 | if (io_u->ddir != DDIR_SYNC) { |
| 261 | td->io_u_queued++; |
| 262 | td->ts.total_io_u[io_u->ddir]++; |
| 263 | } |
| 264 | |
| 265 | if (td->io_u_queued >= td->o.iodepth_batch) { |
Jens Axboe | eb7c8ae | 2007-02-26 14:45:12 +0100 | [diff] [blame] | 266 | r = td_io_commit(td); |
| 267 | if (r < 0) |
| 268 | return r; |
| 269 | } |
| 270 | } |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 271 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 272 | if ((td->io_ops->flags & FIO_SYNCIO) == 0) { |
Jens Axboe | 12d9d84 | 2008-10-16 21:26:10 +0200 | [diff] [blame] | 273 | if (fio_fill_issue_time(td)) |
Jens Axboe | 9520ebb | 2008-10-16 21:03:27 +0200 | [diff] [blame] | 274 | fio_gettime(&io_u->issue_time, NULL); |
Jens Axboe | d0c1532 | 2008-10-16 20:33:51 +0200 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * only used for iolog |
| 278 | */ |
| 279 | if (td->o.read_iolog_file) |
| 280 | memcpy(&td->last_issue, &io_u->issue_time, |
| 281 | sizeof(struct timeval)); |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 284 | return ret; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 285 | } |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 286 | |
| 287 | int td_io_init(struct thread_data *td) |
| 288 | { |
Jens Axboe | eeb1216 | 2007-03-20 10:47:45 +0100 | [diff] [blame] | 289 | int ret = 0; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 290 | |
Jens Axboe | eeb1216 | 2007-03-20 10:47:45 +0100 | [diff] [blame] | 291 | if (td->io_ops->init) { |
| 292 | ret = td->io_ops->init(td); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 293 | if (ret && td->o.iodepth > 1) { |
| 294 | log_err("fio: io engine init failed. Perhaps try" |
| 295 | " reducing io depth?\n"); |
| 296 | } |
Jens Axboe | eeb1216 | 2007-03-20 10:47:45 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | return ret; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 300 | } |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 301 | |
| 302 | int td_io_commit(struct thread_data *td) |
| 303 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 304 | dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth); |
| 305 | |
Jens Axboe | d800575 | 2008-05-15 09:49:09 +0200 | [diff] [blame] | 306 | if (!td->cur_depth || !td->io_u_queued) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 307 | return 0; |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 308 | |
Jens Axboe | d800575 | 2008-05-15 09:49:09 +0200 | [diff] [blame] | 309 | io_u_mark_depth(td, td->io_u_queued); |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 310 | td->io_u_queued = 0; |
Jens Axboe | d800575 | 2008-05-15 09:49:09 +0200 | [diff] [blame] | 311 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 312 | if (td->io_ops->commit) |
| 313 | return td->io_ops->commit(td); |
| 314 | |
| 315 | return 0; |
| 316 | } |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 317 | |
| 318 | int td_io_open_file(struct thread_data *td, struct fio_file *f) |
| 319 | { |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 320 | if (td->io_ops->open_file(td, f)) { |
| 321 | if (td->error == EINVAL && td->o.odirect) |
| 322 | log_err("fio: destination does not support O_DIRECT\n"); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 323 | if (td->error == EMFILE) { |
| 324 | log_err("fio: try reducing/setting openfiles (failed" |
| 325 | " at %u of %u)\n", td->nr_open_files, |
| 326 | td->o.nr_files); |
| 327 | } |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 328 | |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 329 | return 1; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 330 | } |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 331 | |
Jens Axboe | d5707a3 | 2008-06-04 15:13:02 +0200 | [diff] [blame] | 332 | fio_file_reset(f); |
| 333 | f->flags |= FIO_FILE_OPEN; |
| 334 | f->flags &= ~FIO_FILE_CLOSING; |
| 335 | |
| 336 | td->nr_open_files++; |
| 337 | get_file(f); |
| 338 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 339 | if (f->filetype == FIO_TYPE_PIPE) { |
| 340 | if (td_random(td)) { |
| 341 | log_err("fio: can't seek on pipes (no random io)\n"); |
| 342 | goto err; |
| 343 | } |
| 344 | } |
| 345 | |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 346 | if (td->io_ops->flags & FIO_DISKLESSIO) |
| 347 | goto done; |
| 348 | |
| 349 | if (td->o.invalidate_cache && file_invalidate_cache(td, f)) |
| 350 | goto err; |
| 351 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 352 | if (td->o.fadvise_hint && |
| 353 | (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) { |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 354 | int flags; |
| 355 | |
| 356 | if (td_random(td)) |
| 357 | flags = POSIX_FADV_RANDOM; |
| 358 | else |
| 359 | flags = POSIX_FADV_SEQUENTIAL; |
| 360 | |
| 361 | if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) { |
| 362 | td_verror(td, errno, "fadvise"); |
| 363 | goto err; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (f->file_map) |
Jens Axboe | de60566 | 2008-05-31 00:21:12 +0200 | [diff] [blame] | 368 | memset(f->file_map, 0, f->num_maps * sizeof(int)); |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 369 | |
Jens Axboe | e116f2b | 2008-06-04 15:14:24 +0200 | [diff] [blame] | 370 | #ifdef FIO_OS_DIRECTIO |
| 371 | /* |
| 372 | * Some OS's have a distinct call to mark the file non-buffered, |
| 373 | * instead of using O_DIRECT (Solaris) |
| 374 | */ |
| 375 | if (td->o.odirect) { |
| 376 | int ret = fio_set_odirect(f->fd); |
| 377 | |
| 378 | if (ret) { |
| 379 | td_verror(td, ret, "fio_set_odirect"); |
| 380 | goto err; |
| 381 | } |
| 382 | } |
| 383 | #endif |
| 384 | |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 385 | done: |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 386 | log_file(td, f, FIO_LOG_OPEN_FILE); |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 387 | return 0; |
| 388 | err: |
Jens Axboe | b284075 | 2007-04-12 12:57:27 +0200 | [diff] [blame] | 389 | if (td->io_ops->close_file) |
| 390 | td->io_ops->close_file(td, f); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 391 | return 1; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 394 | int td_io_close_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 395 | { |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 396 | if (!(f->flags & FIO_FILE_CLOSING)) |
| 397 | log_file(td, f, FIO_LOG_CLOSE_FILE); |
| 398 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 399 | /* |
| 400 | * mark as closing, do real close when last io on it has completed |
| 401 | */ |
| 402 | f->flags |= FIO_FILE_CLOSING; |
| 403 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 404 | unlock_file_all(td, f); |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 405 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 406 | return put_file(td, f); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 407 | } |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 408 | |
| 409 | int td_io_get_file_size(struct thread_data *td, struct fio_file *f) |
| 410 | { |
| 411 | if (!td->io_ops->get_file_size) |
| 412 | return 0; |
| 413 | |
| 414 | return td->io_ops->get_file_size(td, f); |
| 415 | } |