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