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> |
| 6 | #include <assert.h> |
| 7 | |
| 8 | #include "fio.h" |
| 9 | #include "os.h" |
| 10 | |
| 11 | /* |
| 12 | * The ->file_map[] contains a map of blocks we have or have not done io |
| 13 | * to yet. Used to make sure we cover the entire range in a fair fashion. |
| 14 | */ |
| 15 | static int random_map_free(struct thread_data *td, struct fio_file *f, |
| 16 | unsigned long long block) |
| 17 | { |
| 18 | unsigned int idx = RAND_MAP_IDX(td, f, block); |
| 19 | unsigned int bit = RAND_MAP_BIT(td, f, block); |
| 20 | |
| 21 | return (f->file_map[idx] & (1UL << bit)) == 0; |
| 22 | } |
| 23 | |
| 24 | /* |
Jens Axboe | df41558 | 2006-10-20 11:41:03 +0200 | [diff] [blame] | 25 | * Mark a given offset as used in the map. |
| 26 | */ |
| 27 | static void mark_random_map(struct thread_data *td, struct fio_file *f, |
| 28 | struct io_u *io_u) |
| 29 | { |
Jens Axboe | afdbe58 | 2007-02-10 19:01:57 +0100 | [diff] [blame] | 30 | unsigned int min_bs = td->rw_min_bs; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 31 | unsigned long long block; |
| 32 | unsigned int blocks; |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 33 | unsigned int nr_blocks; |
Jens Axboe | df41558 | 2006-10-20 11:41:03 +0200 | [diff] [blame] | 34 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 35 | block = io_u->offset / (unsigned long long) min_bs; |
| 36 | blocks = 0; |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 37 | nr_blocks = (io_u->buflen + min_bs - 1) / min_bs; |
| 38 | |
| 39 | while (blocks < nr_blocks) { |
Jens Axboe | df41558 | 2006-10-20 11:41:03 +0200 | [diff] [blame] | 40 | unsigned int idx, bit; |
| 41 | |
| 42 | if (!random_map_free(td, f, block)) |
| 43 | break; |
| 44 | |
| 45 | idx = RAND_MAP_IDX(td, f, block); |
| 46 | bit = RAND_MAP_BIT(td, f, block); |
| 47 | |
| 48 | assert(idx < f->num_maps); |
| 49 | |
| 50 | f->file_map[idx] |= (1UL << bit); |
| 51 | block++; |
| 52 | blocks++; |
| 53 | } |
| 54 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 55 | if ((blocks * min_bs) < io_u->buflen) |
| 56 | io_u->buflen = blocks * min_bs; |
Jens Axboe | df41558 | 2006-10-20 11:41:03 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 60 | * Return the next free block in the map. |
| 61 | */ |
| 62 | static int get_next_free_block(struct thread_data *td, struct fio_file *f, |
| 63 | unsigned long long *b) |
| 64 | { |
| 65 | int i; |
| 66 | |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 67 | i = f->last_free_lookup; |
| 68 | *b = (i * BLOCKS_PER_MAP); |
Jens Axboe | ee88470 | 2006-12-20 10:58:07 +0100 | [diff] [blame] | 69 | while ((*b) * td->rw_min_bs < f->real_file_size) { |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 70 | if (f->file_map[i] != -1UL) { |
| 71 | *b += ffz(f->file_map[i]); |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 72 | f->last_free_lookup = i; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | *b += BLOCKS_PER_MAP; |
| 77 | i++; |
| 78 | } |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * For random io, generate a random new block and see if it's used. Repeat |
| 85 | * until we find a free one. For sequential io, just return the end of |
| 86 | * the last io issued. |
| 87 | */ |
| 88 | 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] | 89 | struct io_u *io_u) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 90 | { |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 91 | const int ddir = io_u->ddir; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 92 | unsigned long long b, rb; |
| 93 | long r; |
| 94 | |
| 95 | if (!td->sequential) { |
Jens Axboe | f697125 | 2006-11-15 10:00:31 +0100 | [diff] [blame] | 96 | unsigned long long max_blocks = f->file_size / td->min_bs[ddir]; |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 97 | int loops = 5; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 98 | |
| 99 | do { |
| 100 | r = os_random_long(&td->random_state); |
| 101 | b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0)); |
Jens Axboe | bb8895e | 2006-10-30 15:14:48 +0100 | [diff] [blame] | 102 | if (td->norandommap) |
| 103 | break; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 104 | rb = b + (f->file_offset / td->min_bs[ddir]); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 105 | loops--; |
| 106 | } while (!random_map_free(td, f, rb) && loops); |
| 107 | |
| 108 | if (!loops) { |
| 109 | if (get_next_free_block(td, f, &b)) |
| 110 | return 1; |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 111 | |
| 112 | b += f->file_offset / td->min_bs[ddir]; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 113 | } |
| 114 | } else |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 115 | b = f->last_pos / td->min_bs[ddir]; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 116 | |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 117 | io_u->offset = (b * td->min_bs[ddir]) + f->file_offset; |
| 118 | if (io_u->offset > f->real_file_size) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 119 | return 1; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 124 | static unsigned int get_next_buflen(struct thread_data *td, int ddir) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 125 | { |
| 126 | unsigned int buflen; |
| 127 | long r; |
| 128 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 129 | if (td->min_bs[ddir] == td->max_bs[ddir]) |
| 130 | buflen = td->min_bs[ddir]; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 131 | else { |
| 132 | r = os_random_long(&td->bsrange_state); |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 133 | 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] | 134 | if (!td->bs_unaligned) |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 135 | buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 138 | if (buflen > td->io_size - td->this_io_bytes[ddir]) { |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 139 | /* |
| 140 | * if using direct/raw io, we may not be able to |
| 141 | * shrink the size. so just fail it. |
| 142 | */ |
| 143 | if (td->io_ops->flags & FIO_RAWIO) |
| 144 | return 0; |
| 145 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 146 | buflen = td->io_size - td->this_io_bytes[ddir]; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return buflen; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Return the data direction for the next io_u. If the job is a |
| 154 | * mixed read/write workload, check the rwmix cycle and switch if |
| 155 | * necessary. |
| 156 | */ |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 157 | static enum fio_ddir get_rw_ddir(struct thread_data *td) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 158 | { |
| 159 | if (td_rw(td)) { |
| 160 | struct timeval now; |
| 161 | unsigned long elapsed; |
| 162 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 163 | fio_gettime(&now, NULL); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 164 | elapsed = mtime_since_now(&td->rwmix_switch); |
| 165 | |
| 166 | /* |
| 167 | * Check if it's time to seed a new data direction. |
| 168 | */ |
| 169 | if (elapsed >= td->rwmixcycle) { |
| 170 | unsigned int v; |
| 171 | long r; |
| 172 | |
| 173 | r = os_random_long(&td->rwmix_state); |
| 174 | v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0))); |
| 175 | if (v < td->rwmixread) |
| 176 | td->rwmix_ddir = DDIR_READ; |
| 177 | else |
| 178 | td->rwmix_ddir = DDIR_WRITE; |
| 179 | memcpy(&td->rwmix_switch, &now, sizeof(now)); |
| 180 | } |
| 181 | return td->rwmix_ddir; |
| 182 | } else if (td_read(td)) |
| 183 | return DDIR_READ; |
| 184 | else |
| 185 | return DDIR_WRITE; |
| 186 | } |
| 187 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 188 | void put_io_u(struct thread_data *td, struct io_u *io_u) |
| 189 | { |
| 190 | io_u->file = NULL; |
| 191 | list_del(&io_u->list); |
| 192 | list_add(&io_u->list, &td->io_u_freelist); |
| 193 | td->cur_depth--; |
| 194 | } |
| 195 | |
| 196 | static int fill_io_u(struct thread_data *td, struct fio_file *f, |
| 197 | struct io_u *io_u) |
| 198 | { |
| 199 | /* |
| 200 | * If using an iolog, grab next piece if any available. |
| 201 | */ |
| 202 | if (td->read_iolog) |
| 203 | return read_iolog_get(td, io_u); |
| 204 | |
| 205 | /* |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 206 | * see if it's time to sync |
| 207 | */ |
| 208 | if (td->fsync_blocks && !(td->io_blocks[DDIR_WRITE] % td->fsync_blocks) |
| 209 | && should_fsync(td)) { |
| 210 | io_u->ddir = DDIR_SYNC; |
| 211 | io_u->file = f; |
| 212 | return 0; |
| 213 | } |
| 214 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 215 | io_u->ddir = get_rw_ddir(td); |
| 216 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 217 | /* |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 218 | * No log, let the seq/rand engine retrieve the next buflen and |
| 219 | * position. |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 220 | */ |
Jens Axboe | c685b5b | 2007-02-10 20:02:28 +0100 | [diff] [blame] | 221 | io_u->buflen = get_next_buflen(td, io_u->ddir); |
| 222 | if (io_u->buflen) { |
| 223 | if (!get_next_offset(td, f, io_u)) { |
| 224 | /* |
| 225 | * mark entry before potentially trimming io_u |
| 226 | */ |
| 227 | if (!td->read_iolog && !td->sequential && |
| 228 | !td->norandommap) |
| 229 | mark_random_map(td, f, io_u); |
| 230 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 231 | /* |
| 232 | * If using a write iolog, store this entry. |
| 233 | */ |
Jens Axboe | 076efc7 | 2006-10-27 11:24:25 +0200 | [diff] [blame] | 234 | if (td->write_iolog_file) |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 235 | write_iolog_put(td, io_u); |
| 236 | |
| 237 | io_u->file = f; |
| 238 | return 0; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return 1; |
| 243 | } |
| 244 | |
Jens Axboe | 71619dc | 2007-01-13 23:56:33 +0100 | [diff] [blame] | 245 | static void io_u_mark_depth(struct thread_data *td) |
| 246 | { |
| 247 | int index = 0; |
| 248 | |
| 249 | switch (td->cur_depth) { |
| 250 | default: |
| 251 | index++; |
| 252 | case 32 ... 63: |
| 253 | index++; |
| 254 | case 16 ... 31: |
| 255 | index++; |
| 256 | case 8 ... 15: |
| 257 | index++; |
| 258 | case 4 ... 7: |
| 259 | index++; |
| 260 | case 2 ... 3: |
| 261 | index++; |
| 262 | case 1: |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | td->io_u_map[index]++; |
| 267 | td->total_io_u++; |
| 268 | } |
| 269 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 270 | struct io_u *__get_io_u(struct thread_data *td) |
| 271 | { |
| 272 | struct io_u *io_u = NULL; |
| 273 | |
| 274 | if (!queue_full(td)) { |
| 275 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); |
| 276 | |
Jens Axboe | 6040dab | 2006-10-24 19:38:15 +0200 | [diff] [blame] | 277 | io_u->buflen = 0; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 278 | io_u->error = 0; |
| 279 | io_u->resid = 0; |
| 280 | list_del(&io_u->list); |
| 281 | list_add(&io_u->list, &td->io_u_busylist); |
| 282 | td->cur_depth++; |
Jens Axboe | 71619dc | 2007-01-13 23:56:33 +0100 | [diff] [blame] | 283 | io_u_mark_depth(td); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return io_u; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Return an io_u to be processed. Gets a buflen and offset, sets direction, |
| 291 | * etc. The returned io_u is fully ready to be prepped and submitted. |
| 292 | */ |
| 293 | struct io_u *get_io_u(struct thread_data *td, struct fio_file *f) |
| 294 | { |
| 295 | struct io_u *io_u; |
| 296 | |
| 297 | io_u = __get_io_u(td); |
| 298 | if (!io_u) |
| 299 | return NULL; |
| 300 | |
| 301 | if (td->zone_bytes >= td->zone_size) { |
| 302 | td->zone_bytes = 0; |
| 303 | f->last_pos += td->zone_skip; |
| 304 | } |
| 305 | |
| 306 | if (fill_io_u(td, f, io_u)) { |
| 307 | put_io_u(td, io_u); |
| 308 | return NULL; |
| 309 | } |
| 310 | |
Jens Axboe | ee88470 | 2006-12-20 10:58:07 +0100 | [diff] [blame] | 311 | if (io_u->buflen + io_u->offset > f->real_file_size) { |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 312 | if (td->io_ops->flags & FIO_RAWIO) { |
| 313 | put_io_u(td, io_u); |
| 314 | return NULL; |
| 315 | } |
| 316 | |
Jens Axboe | ee88470 | 2006-12-20 10:58:07 +0100 | [diff] [blame] | 317 | io_u->buflen = f->real_file_size - io_u->offset; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 320 | if (io_u->ddir != DDIR_SYNC) { |
| 321 | if (!io_u->buflen) { |
| 322 | put_io_u(td, io_u); |
| 323 | return NULL; |
| 324 | } |
| 325 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 326 | f->last_pos += io_u->buflen; |
| 327 | |
| 328 | if (td->verify != VERIFY_NONE) |
| 329 | populate_verify_io_u(td, io_u); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 332 | if (td_io_prep(td, io_u)) { |
| 333 | put_io_u(td, io_u); |
| 334 | return NULL; |
| 335 | } |
| 336 | |
Jens Axboe | 165faf1 | 2007-02-07 11:30:37 +0100 | [diff] [blame] | 337 | /* |
| 338 | * Set io data pointers. |
| 339 | */ |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 340 | io_u->xfer_buf = io_u->buf; |
| 341 | io_u->xfer_buflen = io_u->buflen; |
Jens Axboe | 165faf1 | 2007-02-07 11:30:37 +0100 | [diff] [blame] | 342 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 343 | fio_gettime(&io_u->start_time, NULL); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 344 | return io_u; |
| 345 | } |
| 346 | |
| 347 | void io_completed(struct thread_data *td, struct io_u *io_u, |
| 348 | struct io_completion_data *icd) |
| 349 | { |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 350 | unsigned long msec; |
| 351 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 352 | if (io_u->ddir == DDIR_SYNC) { |
| 353 | td->last_was_sync = 1; |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | td->last_was_sync = 0; |
| 358 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 359 | if (!io_u->error) { |
| 360 | unsigned int bytes = io_u->buflen - io_u->resid; |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 361 | const enum fio_ddir idx = io_u->ddir; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 362 | |
| 363 | td->io_blocks[idx]++; |
| 364 | td->io_bytes[idx] += bytes; |
| 365 | td->zone_bytes += bytes; |
| 366 | td->this_io_bytes[idx] += bytes; |
| 367 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 368 | io_u->file->last_completed_pos = io_u->offset + io_u->buflen; |
| 369 | |
| 370 | msec = mtime_since(&io_u->issue_time, &icd->time); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 371 | |
| 372 | add_clat_sample(td, idx, msec); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 373 | add_bw_sample(td, idx, &icd->time); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 374 | |
| 375 | if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE) |
| 376 | log_io_piece(td, io_u); |
| 377 | |
| 378 | icd->bytes_done[idx] += bytes; |
| 379 | } else |
| 380 | icd->error = io_u->error; |
| 381 | } |
| 382 | |
| 383 | void ios_completed(struct thread_data *td, struct io_completion_data *icd) |
| 384 | { |
| 385 | struct io_u *io_u; |
| 386 | int i; |
| 387 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 388 | fio_gettime(&icd->time, NULL); |
| 389 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 390 | icd->error = 0; |
| 391 | icd->bytes_done[0] = icd->bytes_done[1] = 0; |
| 392 | |
| 393 | for (i = 0; i < icd->nr; i++) { |
| 394 | io_u = td->io_ops->event(td, i); |
| 395 | |
| 396 | io_completed(td, io_u, icd); |
| 397 | put_io_u(td, io_u); |
| 398 | } |
| 399 | } |