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 | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 21 | static LIST_HEAD(engine_list); |
| 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) { |
| 26 | log_err("bad ioops version %d (want %d)\n", ops->version, FIO_IOOPS_VERSION); |
| 27 | return 1; |
| 28 | } |
| 29 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 41 | if (!ops->event) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 42 | log_err("%s: no event handler\n", ops->name); |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 43 | return 1; |
| 44 | } |
| 45 | if (!ops->getevents) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 46 | log_err("%s: no getevents handler\n", ops->name); |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 47 | return 1; |
| 48 | } |
| 49 | if (!ops->queue) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 50 | log_err("%s: no queue handler\n", ops->name); |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 57 | void unregister_ioengine(struct ioengine_ops *ops) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 58 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 59 | dprint(FD_IO, "ioengine %s unregistered\n", ops->name); |
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 | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 66 | dprint(FD_IO, "ioengine %s registered\n", ops->name); |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 67 | INIT_LIST_HEAD(&ops->list); |
| 68 | list_add_tail(&ops->list, &engine_list); |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static struct ioengine_ops *find_ioengine(const char *name) |
| 72 | { |
| 73 | struct ioengine_ops *ops; |
| 74 | struct list_head *entry; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 75 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 76 | list_for_each(entry, &engine_list) { |
| 77 | ops = list_entry(entry, struct ioengine_ops, list); |
Jens Axboe | bc5b77a | 2007-02-28 09:32:54 +0100 | [diff] [blame] | 78 | if (!strcmp(name, ops->name)) |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 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 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 91 | dprint(FD_IO, "dload engine %s\n", engine_lib); |
| 92 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 93 | dlerror(); |
| 94 | dlhandle = dlopen(engine_lib, RTLD_LAZY); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 95 | if (!dlhandle) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 96 | td_vmsg(td, -1, dlerror(), "dlopen"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 97 | return NULL; |
| 98 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 99 | |
Jens Axboe | da51c05 | 2006-11-07 16:02:11 +0100 | [diff] [blame] | 100 | /* |
| 101 | * Unlike the included modules, external engines should have a |
| 102 | * non-static ioengine structure that we can reference. |
| 103 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 104 | ops = dlsym(dlhandle, "ioengine"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 105 | if (!ops) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 106 | td_vmsg(td, -1, dlerror(), "dlsym"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 107 | dlclose(dlhandle); |
| 108 | return NULL; |
| 109 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 110 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 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 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 120 | dprint(FD_IO, "load ioengine %s\n", name); |
| 121 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 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); |
Jens Axboe | b902ceb | 2006-10-09 16:11:45 +0200 | [diff] [blame] | 136 | return NULL; |
| 137 | } |
| 138 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 139 | /* |
| 140 | * Check that the required methods are there. |
| 141 | */ |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 142 | if (check_engine_ops(ops)) |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 143 | return NULL; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 144 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 145 | ret = malloc(sizeof(*ret)); |
| 146 | memcpy(ret, ops, sizeof(*ret)); |
| 147 | ret->data = NULL; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 148 | |
| 149 | return ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 152 | void close_ioengine(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 153 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 154 | dprint(FD_IO, "close ioengine %s\n", td->io_ops->name); |
| 155 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 156 | if (td->io_ops->cleanup) |
| 157 | td->io_ops->cleanup(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 158 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 159 | if (td->io_ops->dlhandle) |
| 160 | dlclose(td->io_ops->dlhandle); |
| 161 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 162 | free(td->io_ops); |
| 163 | td->io_ops = NULL; |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 164 | } |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 165 | |
| 166 | int td_io_prep(struct thread_data *td, struct io_u *io_u) |
| 167 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 168 | dprint_io_u(io_u, "prep"); |
Jens Axboe | 7101d9c | 2007-09-12 13:12:39 +0200 | [diff] [blame] | 169 | fio_ro_check(td, io_u); |
| 170 | |
Jens Axboe | 2ba1c29 | 2008-02-01 13:16:38 +0100 | [diff] [blame] | 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 | } |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Jens Axboe | e7d2e61 | 2007-12-11 10:49:39 +0100 | [diff] [blame] | 181 | 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] | 182 | struct timespec *t) |
| 183 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 184 | int r = 0; |
| 185 | |
Jens Axboe | face81b | 2007-02-26 10:40:03 +0100 | [diff] [blame] | 186 | if (min > 0 && td->io_ops->commit) { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 187 | r = td->io_ops->commit(td); |
Jens Axboe | face81b | 2007-02-26 10:40:03 +0100 | [diff] [blame] | 188 | if (r < 0) |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 189 | goto out; |
Jens Axboe | face81b | 2007-02-26 10:40:03 +0100 | [diff] [blame] | 190 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 191 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 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; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | int td_io_queue(struct thread_data *td, struct io_u *io_u) |
| 201 | { |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 202 | int ret; |
| 203 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 204 | dprint_io_u(io_u, "queue"); |
Jens Axboe | 7101d9c | 2007-09-12 13:12:39 +0200 | [diff] [blame] | 205 | fio_ro_check(td, io_u); |
| 206 | |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 207 | assert((io_u->flags & IO_U_F_FLIGHT) == 0); |
| 208 | io_u->flags |= IO_U_F_FLIGHT; |
| 209 | |
Jens Axboe | 3d7b485 | 2007-03-13 14:50:28 +0100 | [diff] [blame] | 210 | assert(io_u->file->flags & FIO_FILE_OPEN); |
| 211 | |
Jens Axboe | 1178680 | 2007-03-05 18:33:22 +0100 | [diff] [blame] | 212 | io_u->error = 0; |
| 213 | io_u->resid = 0; |
| 214 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 215 | if (td->io_ops->flags & FIO_SYNCIO) { |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 216 | fio_gettime(&io_u->issue_time, NULL); |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 217 | memcpy(&td->last_issue, &io_u->issue_time, sizeof(struct timeval)); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 218 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 226 | if (io_u->ddir != DDIR_SYNC) |
| 227 | td->io_issues[io_u->ddir]++; |
| 228 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 229 | ret = td->io_ops->queue(td, io_u); |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 230 | |
Jens Axboe | 163f849 | 2008-02-04 10:56:26 +0100 | [diff] [blame] | 231 | if (ret != FIO_Q_BUSY) |
| 232 | io_u_mark_depth(td, io_u); |
| 233 | |
Jens Axboe | eb7c8ae | 2007-02-26 14:45:12 +0100 | [diff] [blame] | 234 | if (ret == FIO_Q_QUEUED) { |
| 235 | int r; |
| 236 | |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 237 | td->io_u_queued++; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 238 | if (td->io_u_queued > td->o.iodepth_batch) { |
Jens Axboe | eb7c8ae | 2007-02-26 14:45:12 +0100 | [diff] [blame] | 239 | r = td_io_commit(td); |
| 240 | if (r < 0) |
| 241 | return r; |
| 242 | } |
| 243 | } |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 244 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 245 | if ((td->io_ops->flags & FIO_SYNCIO) == 0) { |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 246 | fio_gettime(&io_u->issue_time, NULL); |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 247 | memcpy(&td->last_issue, &io_u->issue_time, sizeof(struct timeval)); |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 248 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 249 | /* |
| 250 | * async engine, set the timeout here |
| 251 | */ |
| 252 | if (ret == FIO_Q_QUEUED && |
| 253 | mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT) |
| 254 | io_u_set_timeout(td); |
| 255 | } |
| 256 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 257 | return ret; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 258 | } |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 259 | |
| 260 | int td_io_init(struct thread_data *td) |
| 261 | { |
Jens Axboe | eeb1216 | 2007-03-20 10:47:45 +0100 | [diff] [blame] | 262 | int ret = 0; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 263 | |
Jens Axboe | eeb1216 | 2007-03-20 10:47:45 +0100 | [diff] [blame] | 264 | if (td->io_ops->init) { |
| 265 | ret = td->io_ops->init(td); |
| 266 | if (ret && td->o.iodepth > 1) |
Zach Brown | 9de6beb | 2007-09-19 08:42:17 +0200 | [diff] [blame] | 267 | log_err("fio: io engine init failed. Perhaps try reducing io depth?\n"); |
Jens Axboe | eeb1216 | 2007-03-20 10:47:45 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return ret; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 271 | } |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 272 | |
| 273 | int td_io_commit(struct thread_data *td) |
| 274 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 275 | dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth); |
| 276 | |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 277 | if (!td->cur_depth) |
| 278 | return 0; |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 279 | |
| 280 | td->io_u_queued = 0; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 281 | if (td->io_ops->commit) |
| 282 | return td->io_ops->commit(td); |
| 283 | |
| 284 | return 0; |
| 285 | } |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 286 | |
| 287 | int td_io_open_file(struct thread_data *td, struct fio_file *f) |
| 288 | { |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 289 | if (td->io_ops->open_file(td, f)) { |
| 290 | if (td->error == EINVAL && td->o.odirect) |
| 291 | log_err("fio: destination does not support O_DIRECT\n"); |
| 292 | if (td->error == EMFILE) |
| 293 | log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 294 | |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 295 | return 1; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 296 | } |
Jens Axboe | a978ba6 | 2007-03-08 14:29:03 +0100 | [diff] [blame] | 297 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 298 | if (f->filetype == FIO_TYPE_PIPE) { |
| 299 | if (td_random(td)) { |
| 300 | log_err("fio: can't seek on pipes (no random io)\n"); |
| 301 | goto err; |
| 302 | } |
| 303 | } |
| 304 | |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 305 | f->last_free_lookup = 0; |
| 306 | f->last_completed_pos = 0; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 307 | f->last_pos = f->file_offset; |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 308 | f->flags |= FIO_FILE_OPEN; |
| 309 | f->flags &= ~FIO_FILE_CLOSING; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 310 | |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 311 | if (td->io_ops->flags & FIO_DISKLESSIO) |
| 312 | goto done; |
| 313 | |
| 314 | if (td->o.invalidate_cache && file_invalidate_cache(td, f)) |
| 315 | goto err; |
| 316 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 317 | if (td->o.fadvise_hint && |
| 318 | (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) { |
| 319 | |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 320 | int flags; |
| 321 | |
| 322 | if (td_random(td)) |
| 323 | flags = POSIX_FADV_RANDOM; |
| 324 | else |
| 325 | flags = POSIX_FADV_SEQUENTIAL; |
| 326 | |
| 327 | if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) { |
| 328 | td_verror(td, errno, "fadvise"); |
| 329 | goto err; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (f->file_map) |
| 334 | memset(f->file_map, 0, f->num_maps * sizeof(long)); |
| 335 | |
| 336 | done: |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 337 | log_file(td, f, FIO_LOG_OPEN_FILE); |
Jens Axboe | 413d669 | 2007-03-27 15:38:21 +0200 | [diff] [blame] | 338 | td->nr_open_files++; |
| 339 | get_file(f); |
| 340 | return 0; |
| 341 | err: |
Jens Axboe | b284075 | 2007-04-12 12:57:27 +0200 | [diff] [blame] | 342 | if (td->io_ops->close_file) |
| 343 | td->io_ops->close_file(td, f); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 344 | return 1; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void td_io_close_file(struct thread_data *td, struct fio_file *f) |
| 348 | { |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 349 | if (!(f->flags & FIO_FILE_CLOSING)) |
| 350 | log_file(td, f, FIO_LOG_CLOSE_FILE); |
| 351 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 352 | /* |
| 353 | * mark as closing, do real close when last io on it has completed |
| 354 | */ |
| 355 | f->flags |= FIO_FILE_CLOSING; |
| 356 | |
| 357 | put_file(td, f); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 358 | } |