Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <string.h> |
| 4 | #include <signal.h> |
| 5 | #include <time.h> |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 6 | |
| 7 | #include "fio.h" |
| 8 | #include "os.h" |
| 9 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 10 | struct io_completion_data { |
| 11 | int nr; /* input */ |
| 12 | endio_handler *handler; /* input */ |
| 13 | |
| 14 | int error; /* output */ |
| 15 | unsigned long bytes_done[2]; /* output */ |
| 16 | struct timeval time; /* output */ |
| 17 | }; |
| 18 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 19 | /* |
| 20 | * The ->file_map[] contains a map of blocks we have or have not done io |
| 21 | * to yet. Used to make sure we cover the entire range in a fair fashion. |
| 22 | */ |
| 23 | static int random_map_free(struct thread_data *td, struct fio_file *f, |
| 24 | unsigned long long block) |
| 25 | { |
| 26 | unsigned int idx = RAND_MAP_IDX(td, f, block); |
| 27 | unsigned int bit = RAND_MAP_BIT(td, f, block); |
| 28 | |
| 29 | return (f->file_map[idx] & (1UL << bit)) == 0; |
| 30 | } |
| 31 | |
| 32 | /* |
Jens Axboe | df41558 | 2006-10-20 11:41:03 +0200 | [diff] [blame] | 33 | * Mark a given offset as used in the map. |
| 34 | */ |
| 35 | static void mark_random_map(struct thread_data *td, struct fio_file *f, |
| 36 | struct io_u *io_u) |
| 37 | { |
Jens Axboe | afdbe58 | 2007-02-10 19:01:57 +0100 | [diff] [blame] | 38 | unsigned int min_bs = td->rw_min_bs; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 39 | unsigned long long block; |
| 40 | unsigned int blocks; |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 41 | unsigned int nr_blocks; |
Jens Axboe | df41558 | 2006-10-20 11:41:03 +0200 | [diff] [blame] | 42 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 43 | block = io_u->offset / (unsigned long long) min_bs; |
| 44 | blocks = 0; |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 45 | nr_blocks = (io_u->buflen + min_bs - 1) / min_bs; |
| 46 | |
| 47 | while (blocks < nr_blocks) { |
Jens Axboe | df41558 | 2006-10-20 11:41:03 +0200 | [diff] [blame] | 48 | unsigned int idx, bit; |
| 49 | |
| 50 | if (!random_map_free(td, f, block)) |
| 51 | break; |
| 52 | |
| 53 | idx = RAND_MAP_IDX(td, f, block); |
| 54 | bit = RAND_MAP_BIT(td, f, block); |
| 55 | |
Jens Axboe | 0032bf9 | 2007-02-13 17:39:56 +0100 | [diff] [blame] | 56 | fio_assert(td, idx < f->num_maps); |
Jens Axboe | df41558 | 2006-10-20 11:41:03 +0200 | [diff] [blame] | 57 | |
| 58 | f->file_map[idx] |= (1UL << bit); |
| 59 | block++; |
| 60 | blocks++; |
| 61 | } |
| 62 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 63 | if ((blocks * min_bs) < io_u->buflen) |
| 64 | io_u->buflen = blocks * min_bs; |
Jens Axboe | df41558 | 2006-10-20 11:41:03 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 68 | * Return the next free block in the map. |
| 69 | */ |
| 70 | static int get_next_free_block(struct thread_data *td, struct fio_file *f, |
| 71 | unsigned long long *b) |
| 72 | { |
| 73 | int i; |
| 74 | |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 75 | i = f->last_free_lookup; |
| 76 | *b = (i * BLOCKS_PER_MAP); |
Jens Axboe | ee88470 | 2006-12-20 10:58:07 +0100 | [diff] [blame] | 77 | while ((*b) * td->rw_min_bs < f->real_file_size) { |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 78 | if (f->file_map[i] != -1UL) { |
| 79 | *b += ffz(f->file_map[i]); |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 80 | f->last_free_lookup = i; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | *b += BLOCKS_PER_MAP; |
| 85 | i++; |
| 86 | } |
| 87 | |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * For random io, generate a random new block and see if it's used. Repeat |
| 93 | * until we find a free one. For sequential io, just return the end of |
| 94 | * the last io issued. |
| 95 | */ |
| 96 | static int get_next_offset(struct thread_data *td, struct fio_file *f, |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 97 | struct io_u *io_u) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 98 | { |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 99 | const int ddir = io_u->ddir; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 100 | unsigned long long b, rb; |
| 101 | long r; |
| 102 | |
| 103 | if (!td->sequential) { |
Jens Axboe | f697125 | 2006-11-15 10:00:31 +0100 | [diff] [blame] | 104 | unsigned long long max_blocks = f->file_size / td->min_bs[ddir]; |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 105 | int loops = 5; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 106 | |
| 107 | do { |
| 108 | r = os_random_long(&td->random_state); |
| 109 | b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0)); |
Jens Axboe | bb8895e | 2006-10-30 15:14:48 +0100 | [diff] [blame] | 110 | if (td->norandommap) |
| 111 | break; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 112 | rb = b + (f->file_offset / td->min_bs[ddir]); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 113 | loops--; |
| 114 | } while (!random_map_free(td, f, rb) && loops); |
| 115 | |
Jens Axboe | bca4ed4 | 2007-02-12 05:13:23 +0100 | [diff] [blame] | 116 | /* |
| 117 | * if we failed to retrieve a truly random offset within |
| 118 | * the loops assigned, see if there are free ones left at all |
| 119 | */ |
| 120 | if (!loops && get_next_free_block(td, f, &b)) |
| 121 | return 1; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 122 | } else |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 123 | b = f->last_pos / td->min_bs[ddir]; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 124 | |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 125 | io_u->offset = (b * td->min_bs[ddir]) + f->file_offset; |
Jens Axboe | bca4ed4 | 2007-02-12 05:13:23 +0100 | [diff] [blame] | 126 | if (io_u->offset >= f->real_file_size) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 127 | return 1; |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
Jens Axboe | bca4ed4 | 2007-02-12 05:13:23 +0100 | [diff] [blame] | 132 | static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f, |
| 133 | struct io_u *io_u) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 134 | { |
Jens Axboe | bca4ed4 | 2007-02-12 05:13:23 +0100 | [diff] [blame] | 135 | const int ddir = io_u->ddir; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 136 | unsigned int buflen; |
| 137 | long r; |
| 138 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 139 | if (td->min_bs[ddir] == td->max_bs[ddir]) |
| 140 | buflen = td->min_bs[ddir]; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 141 | else { |
| 142 | r = os_random_long(&td->bsrange_state); |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 143 | buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0)); |
Jens Axboe | 690adba | 2006-10-30 15:25:09 +0100 | [diff] [blame] | 144 | if (!td->bs_unaligned) |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 145 | buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Jens Axboe | bca4ed4 | 2007-02-12 05:13:23 +0100 | [diff] [blame] | 148 | while (buflen + io_u->offset > f->real_file_size) { |
Jens Axboe | bca4ed4 | 2007-02-12 05:13:23 +0100 | [diff] [blame] | 149 | if (buflen == td->min_bs[ddir]) |
| 150 | return 0; |
| 151 | |
| 152 | buflen = td->min_bs[ddir]; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | return buflen; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Return the data direction for the next io_u. If the job is a |
| 160 | * mixed read/write workload, check the rwmix cycle and switch if |
| 161 | * necessary. |
| 162 | */ |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 163 | static enum fio_ddir get_rw_ddir(struct thread_data *td) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 164 | { |
| 165 | if (td_rw(td)) { |
| 166 | struct timeval now; |
| 167 | unsigned long elapsed; |
| 168 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 169 | fio_gettime(&now, NULL); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 170 | elapsed = mtime_since_now(&td->rwmix_switch); |
| 171 | |
| 172 | /* |
| 173 | * Check if it's time to seed a new data direction. |
| 174 | */ |
| 175 | if (elapsed >= td->rwmixcycle) { |
| 176 | unsigned int v; |
| 177 | long r; |
| 178 | |
| 179 | r = os_random_long(&td->rwmix_state); |
| 180 | v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0))); |
| 181 | if (v < td->rwmixread) |
| 182 | td->rwmix_ddir = DDIR_READ; |
| 183 | else |
| 184 | td->rwmix_ddir = DDIR_WRITE; |
| 185 | memcpy(&td->rwmix_switch, &now, sizeof(now)); |
| 186 | } |
| 187 | return td->rwmix_ddir; |
| 188 | } else if (td_read(td)) |
| 189 | return DDIR_READ; |
| 190 | else |
| 191 | return DDIR_WRITE; |
| 192 | } |
| 193 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 194 | void put_io_u(struct thread_data *td, struct io_u *io_u) |
| 195 | { |
| 196 | io_u->file = NULL; |
| 197 | list_del(&io_u->list); |
| 198 | list_add(&io_u->list, &td->io_u_freelist); |
| 199 | td->cur_depth--; |
| 200 | } |
| 201 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 202 | void requeue_io_u(struct thread_data *td, struct io_u **io_u) |
| 203 | { |
| 204 | struct io_u *__io_u = *io_u; |
| 205 | |
| 206 | list_del(&__io_u->list); |
| 207 | list_add_tail(&__io_u->list, &td->io_u_requeues); |
| 208 | td->cur_depth--; |
| 209 | *io_u = NULL; |
| 210 | } |
| 211 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 212 | static int fill_io_u(struct thread_data *td, struct fio_file *f, |
| 213 | struct io_u *io_u) |
| 214 | { |
| 215 | /* |
| 216 | * If using an iolog, grab next piece if any available. |
| 217 | */ |
| 218 | if (td->read_iolog) |
| 219 | return read_iolog_get(td, io_u); |
| 220 | |
| 221 | /* |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 222 | * see if it's time to sync |
| 223 | */ |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 224 | if (td->fsync_blocks && !(td->io_issues[DDIR_WRITE] % td->fsync_blocks) |
| 225 | && td->io_issues[DDIR_WRITE] && should_fsync(td)) { |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 226 | io_u->ddir = DDIR_SYNC; |
| 227 | io_u->file = f; |
| 228 | return 0; |
| 229 | } |
| 230 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 231 | io_u->ddir = get_rw_ddir(td); |
| 232 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 233 | /* |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 234 | * No log, let the seq/rand engine retrieve the next buflen and |
| 235 | * position. |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 236 | */ |
Jens Axboe | bca4ed4 | 2007-02-12 05:13:23 +0100 | [diff] [blame] | 237 | if (get_next_offset(td, f, io_u)) |
| 238 | return 1; |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 239 | |
Jens Axboe | bca4ed4 | 2007-02-12 05:13:23 +0100 | [diff] [blame] | 240 | io_u->buflen = get_next_buflen(td, f, io_u); |
| 241 | if (!io_u->buflen) |
| 242 | return 1; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 243 | |
Jens Axboe | bca4ed4 | 2007-02-12 05:13:23 +0100 | [diff] [blame] | 244 | /* |
| 245 | * mark entry before potentially trimming io_u |
| 246 | */ |
| 247 | if (!td->read_iolog && !td->sequential && !td->norandommap) |
| 248 | mark_random_map(td, f, io_u); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 249 | |
Jens Axboe | bca4ed4 | 2007-02-12 05:13:23 +0100 | [diff] [blame] | 250 | /* |
| 251 | * If using a write iolog, store this entry. |
| 252 | */ |
| 253 | if (td->write_iolog_file) |
| 254 | write_iolog_put(td, io_u); |
| 255 | |
| 256 | io_u->file = f; |
| 257 | return 0; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Jens Axboe | 71619dc | 2007-01-13 23:56:33 +0100 | [diff] [blame] | 260 | static void io_u_mark_depth(struct thread_data *td) |
| 261 | { |
| 262 | int index = 0; |
| 263 | |
| 264 | switch (td->cur_depth) { |
| 265 | default: |
| 266 | index++; |
| 267 | case 32 ... 63: |
| 268 | index++; |
| 269 | case 16 ... 31: |
| 270 | index++; |
| 271 | case 8 ... 15: |
| 272 | index++; |
| 273 | case 4 ... 7: |
| 274 | index++; |
| 275 | case 2 ... 3: |
| 276 | index++; |
| 277 | case 1: |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | td->io_u_map[index]++; |
| 282 | td->total_io_u++; |
| 283 | } |
| 284 | |
Jens Axboe | ec11830 | 2007-02-17 04:38:20 +0100 | [diff] [blame] | 285 | static void io_u_mark_latency(struct thread_data *td, unsigned long msec) |
| 286 | { |
| 287 | int index = 0; |
| 288 | |
| 289 | switch (msec) { |
| 290 | default: |
| 291 | index++; |
| 292 | case 1024 ... 2047: |
| 293 | index++; |
| 294 | case 512 ... 1023: |
| 295 | index++; |
| 296 | case 256 ... 511: |
| 297 | index++; |
| 298 | case 128 ... 255: |
| 299 | index++; |
| 300 | case 64 ... 127: |
| 301 | index++; |
| 302 | case 32 ... 63: |
| 303 | index++; |
| 304 | case 16 ... 31: |
| 305 | index++; |
| 306 | case 8 ... 15: |
| 307 | index++; |
| 308 | case 4 ... 7: |
| 309 | index++; |
| 310 | case 2 ... 3: |
| 311 | index++; |
| 312 | case 0 ... 1: |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | td->io_u_lat[index]++; |
| 317 | } |
| 318 | |
Jens Axboe | 3d7c391 | 2007-02-19 13:16:12 +0100 | [diff] [blame] | 319 | static struct fio_file *get_next_file(struct thread_data *td) |
| 320 | { |
| 321 | unsigned int old_next_file = td->next_file; |
| 322 | struct fio_file *f; |
| 323 | |
| 324 | do { |
| 325 | f = &td->files[td->next_file]; |
| 326 | |
| 327 | td->next_file++; |
| 328 | if (td->next_file >= td->nr_files) |
| 329 | td->next_file = 0; |
| 330 | |
| 331 | if (f->fd != -1) |
| 332 | break; |
| 333 | |
| 334 | f = NULL; |
| 335 | } while (td->next_file != old_next_file); |
| 336 | |
| 337 | return f; |
| 338 | } |
| 339 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 340 | struct io_u *__get_io_u(struct thread_data *td) |
| 341 | { |
| 342 | struct io_u *io_u = NULL; |
| 343 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 344 | if (!list_empty(&td->io_u_requeues)) |
| 345 | io_u = list_entry(td->io_u_requeues.next, struct io_u, list); |
| 346 | else if (!queue_full(td)) { |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 347 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); |
| 348 | |
Jens Axboe | 6040dab | 2006-10-24 19:38:15 +0200 | [diff] [blame] | 349 | io_u->buflen = 0; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 350 | io_u->resid = 0; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 351 | io_u->file = NULL; |
| 352 | } |
| 353 | |
| 354 | if (io_u) { |
| 355 | io_u->error = 0; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 356 | list_del(&io_u->list); |
| 357 | list_add(&io_u->list, &td->io_u_busylist); |
| 358 | td->cur_depth++; |
Jens Axboe | 71619dc | 2007-01-13 23:56:33 +0100 | [diff] [blame] | 359 | io_u_mark_depth(td); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | return io_u; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Return an io_u to be processed. Gets a buflen and offset, sets direction, |
| 367 | * etc. The returned io_u is fully ready to be prepped and submitted. |
| 368 | */ |
Jens Axboe | 3d7c391 | 2007-02-19 13:16:12 +0100 | [diff] [blame] | 369 | struct io_u *get_io_u(struct thread_data *td) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 370 | { |
Jens Axboe | 3d7c391 | 2007-02-19 13:16:12 +0100 | [diff] [blame] | 371 | struct fio_file *f; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 372 | struct io_u *io_u; |
| 373 | |
| 374 | io_u = __get_io_u(td); |
| 375 | if (!io_u) |
| 376 | return NULL; |
| 377 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 378 | /* |
| 379 | * from a requeue, io_u already setup |
| 380 | */ |
| 381 | if (io_u->file) |
Jens Axboe | 77f392b | 2007-02-19 20:13:09 +0100 | [diff] [blame] | 382 | goto out; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 383 | |
Jens Axboe | 3d7c391 | 2007-02-19 13:16:12 +0100 | [diff] [blame] | 384 | f = get_next_file(td); |
| 385 | if (!f) { |
| 386 | put_io_u(td, io_u); |
| 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | io_u->file = f; |
| 391 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 392 | if (td->zone_bytes >= td->zone_size) { |
| 393 | td->zone_bytes = 0; |
| 394 | f->last_pos += td->zone_skip; |
| 395 | } |
| 396 | |
| 397 | if (fill_io_u(td, f, io_u)) { |
| 398 | put_io_u(td, io_u); |
| 399 | return NULL; |
| 400 | } |
| 401 | |
Jens Axboe | ee88470 | 2006-12-20 10:58:07 +0100 | [diff] [blame] | 402 | if (io_u->buflen + io_u->offset > f->real_file_size) { |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 403 | if (td->io_ops->flags & FIO_RAWIO) { |
| 404 | put_io_u(td, io_u); |
| 405 | return NULL; |
| 406 | } |
| 407 | |
Jens Axboe | ee88470 | 2006-12-20 10:58:07 +0100 | [diff] [blame] | 408 | io_u->buflen = f->real_file_size - io_u->offset; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 411 | if (io_u->ddir != DDIR_SYNC) { |
| 412 | if (!io_u->buflen) { |
| 413 | put_io_u(td, io_u); |
| 414 | return NULL; |
| 415 | } |
| 416 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 417 | f->last_pos = io_u->offset + io_u->buflen; |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 418 | |
| 419 | if (td->verify != VERIFY_NONE) |
| 420 | populate_verify_io_u(td, io_u); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Jens Axboe | 165faf1 | 2007-02-07 11:30:37 +0100 | [diff] [blame] | 423 | /* |
| 424 | * Set io data pointers. |
| 425 | */ |
Jens Axboe | 77f392b | 2007-02-19 20:13:09 +0100 | [diff] [blame] | 426 | out: |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 427 | io_u->xfer_buf = io_u->buf; |
| 428 | io_u->xfer_buflen = io_u->buflen; |
Jens Axboe | 165faf1 | 2007-02-07 11:30:37 +0100 | [diff] [blame] | 429 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 430 | if (td_io_prep(td, io_u)) { |
| 431 | put_io_u(td, io_u); |
| 432 | return NULL; |
| 433 | } |
| 434 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 435 | fio_gettime(&io_u->start_time, NULL); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 436 | return io_u; |
| 437 | } |
| 438 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 439 | static void io_completed(struct thread_data *td, struct io_u *io_u, |
| 440 | struct io_completion_data *icd) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 441 | { |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 442 | unsigned long msec; |
| 443 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 444 | if (io_u->ddir == DDIR_SYNC) { |
| 445 | td->last_was_sync = 1; |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | td->last_was_sync = 0; |
| 450 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 451 | if (!io_u->error) { |
| 452 | unsigned int bytes = io_u->buflen - io_u->resid; |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 453 | const enum fio_ddir idx = io_u->ddir; |
Jens Axboe | 3af6ef3 | 2007-02-18 06:57:43 +0100 | [diff] [blame] | 454 | int ret; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 455 | |
| 456 | td->io_blocks[idx]++; |
| 457 | td->io_bytes[idx] += bytes; |
| 458 | td->zone_bytes += bytes; |
| 459 | td->this_io_bytes[idx] += bytes; |
| 460 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 461 | io_u->file->last_completed_pos = io_u->offset + io_u->buflen; |
| 462 | |
| 463 | msec = mtime_since(&io_u->issue_time, &icd->time); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 464 | |
| 465 | add_clat_sample(td, idx, msec); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 466 | add_bw_sample(td, idx, &icd->time); |
Jens Axboe | ec11830 | 2007-02-17 04:38:20 +0100 | [diff] [blame] | 467 | io_u_mark_latency(td, msec); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 468 | |
| 469 | if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE) |
| 470 | log_io_piece(td, io_u); |
| 471 | |
| 472 | icd->bytes_done[idx] += bytes; |
Jens Axboe | 3af6ef3 | 2007-02-18 06:57:43 +0100 | [diff] [blame] | 473 | |
| 474 | if (icd->handler) { |
| 475 | ret = icd->handler(io_u); |
| 476 | if (ret && !icd->error) |
| 477 | icd->error = ret; |
| 478 | } |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 479 | } else |
| 480 | icd->error = io_u->error; |
| 481 | } |
| 482 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 483 | static void init_icd(struct io_completion_data *icd, endio_handler *handler, |
| 484 | int nr) |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 485 | { |
| 486 | fio_gettime(&icd->time, NULL); |
| 487 | |
Jens Axboe | 3af6ef3 | 2007-02-18 06:57:43 +0100 | [diff] [blame] | 488 | icd->handler = handler; |
| 489 | icd->nr = nr; |
| 490 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 491 | icd->error = 0; |
| 492 | icd->bytes_done[0] = icd->bytes_done[1] = 0; |
| 493 | } |
| 494 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 495 | static void ios_completed(struct thread_data *td, |
| 496 | struct io_completion_data *icd) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 497 | { |
| 498 | struct io_u *io_u; |
| 499 | int i; |
| 500 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 501 | for (i = 0; i < icd->nr; i++) { |
| 502 | io_u = td->io_ops->event(td, i); |
| 503 | |
| 504 | io_completed(td, io_u, icd); |
| 505 | put_io_u(td, io_u); |
| 506 | } |
| 507 | } |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 508 | |
Jens Axboe | e7e6cfb | 2007-02-20 10:58:34 +0100 | [diff] [blame^] | 509 | /* |
| 510 | * Complete a single io_u for the sync engines. |
| 511 | */ |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 512 | long io_u_sync_complete(struct thread_data *td, struct io_u *io_u, |
| 513 | endio_handler *handler) |
| 514 | { |
| 515 | struct io_completion_data icd; |
| 516 | |
| 517 | init_icd(&icd, handler, 1); |
| 518 | io_completed(td, io_u, &icd); |
| 519 | put_io_u(td, io_u); |
| 520 | |
| 521 | if (!icd.error) |
| 522 | return icd.bytes_done[0] + icd.bytes_done[1]; |
| 523 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 524 | return -1; |
| 525 | } |
| 526 | |
Jens Axboe | e7e6cfb | 2007-02-20 10:58:34 +0100 | [diff] [blame^] | 527 | /* |
| 528 | * Called to complete min_events number of io for the async engines. |
| 529 | */ |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 530 | long io_u_queued_complete(struct thread_data *td, int min_events, |
| 531 | endio_handler *handler) |
| 532 | |
| 533 | { |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 534 | struct io_completion_data icd; |
Jens Axboe | 00de55e | 2007-02-20 10:45:57 +0100 | [diff] [blame] | 535 | struct timespec *tvp = NULL; |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 536 | int ret; |
| 537 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 538 | if (min_events > 0) { |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 539 | ret = td_io_commit(td); |
| 540 | if (ret < 0) { |
| 541 | td_verror(td, -ret); |
| 542 | return ret; |
| 543 | } |
Jens Axboe | 00de55e | 2007-02-20 10:45:57 +0100 | [diff] [blame] | 544 | } else { |
| 545 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, }; |
| 546 | |
| 547 | tvp = &ts; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 548 | } |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 549 | |
Jens Axboe | 00de55e | 2007-02-20 10:45:57 +0100 | [diff] [blame] | 550 | ret = td_io_getevents(td, min_events, td->cur_depth, tvp); |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 551 | if (ret < 0) { |
| 552 | td_verror(td, -ret); |
| 553 | return ret; |
| 554 | } else if (!ret) |
| 555 | return ret; |
| 556 | |
| 557 | init_icd(&icd, handler, ret); |
| 558 | ios_completed(td, &icd); |
| 559 | if (!icd.error) |
| 560 | return icd.bytes_done[0] + icd.bytes_done[1]; |
| 561 | |
Jens Axboe | 9760102 | 2007-02-18 12:47:29 +0100 | [diff] [blame] | 562 | return -1; |
| 563 | } |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * Call when io_u is really queued, to update the submission latency. |
| 567 | */ |
| 568 | void io_u_queued(struct thread_data *td, struct io_u *io_u) |
| 569 | { |
| 570 | unsigned long slat_time; |
| 571 | |
| 572 | slat_time = mtime_since(&io_u->start_time, &io_u->issue_time); |
| 573 | add_slat_sample(td, io_u->ddir, slat_time); |
| 574 | } |