Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * fio - the flexible io tester |
| 3 | * |
| 4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> |
Jens Axboe | aae22ca | 2006-09-05 10:46:22 +0200 | [diff] [blame] | 5 | * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <string.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 25 | #include <signal.h> |
| 26 | #include <time.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 27 | #include <assert.h> |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 28 | #include <sys/stat.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <sys/ipc.h> |
| 31 | #include <sys/shm.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <sys/mman.h> |
| 34 | |
| 35 | #include "fio.h" |
| 36 | #include "os.h" |
| 37 | |
| 38 | #define MASK (4095) |
| 39 | |
| 40 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) |
| 41 | |
| 42 | int groupid = 0; |
| 43 | int thread_number = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 44 | int shm_id = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 45 | int temp_stall_ts; |
Jens Axboe | c1d5725 | 2006-10-09 19:56:04 +0200 | [diff] [blame] | 46 | char *fio_inst_prefix = _INST_PREFIX; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 47 | |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 48 | extern unsigned long long mlock_size; |
| 49 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 50 | #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 51 | |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 52 | static volatile int startup_sem; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 53 | |
| 54 | #define TERMINATE_ALL (-1) |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 55 | #define JOB_START_TIMEOUT (5 * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 56 | |
| 57 | static void terminate_threads(int group_id) |
| 58 | { |
| 59 | int i; |
| 60 | |
| 61 | for (i = 0; i < thread_number; i++) { |
| 62 | struct thread_data *td = &threads[i]; |
| 63 | |
| 64 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
| 65 | td->terminate = 1; |
| 66 | td->start_delay = 0; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static void sig_handler(int sig) |
| 72 | { |
| 73 | switch (sig) { |
| 74 | case SIGALRM: |
| 75 | update_io_ticks(); |
| 76 | disk_util_timer_arm(); |
| 77 | print_thread_status(); |
| 78 | break; |
| 79 | default: |
| 80 | printf("\nfio: terminating on signal\n"); |
| 81 | fflush(stdout); |
| 82 | terminate_threads(TERMINATE_ALL); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 87 | /* |
| 88 | * The ->file_map[] contains a map of blocks we have or have not done io |
| 89 | * to yet. Used to make sure we cover the entire range in a fair fashion. |
| 90 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 91 | static int random_map_free(struct thread_data *td, struct fio_file *f, |
| 92 | unsigned long long block) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 93 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 94 | unsigned int idx = RAND_MAP_IDX(td, f, block); |
| 95 | unsigned int bit = RAND_MAP_BIT(td, f, block); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 96 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 97 | return (f->file_map[idx] & (1UL << bit)) == 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 100 | /* |
| 101 | * Return the next free block in the map. |
| 102 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 103 | static int get_next_free_block(struct thread_data *td, struct fio_file *f, |
| 104 | unsigned long long *b) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 105 | { |
| 106 | int i; |
| 107 | |
| 108 | *b = 0; |
| 109 | i = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 110 | while ((*b) * td->min_bs < f->file_size) { |
| 111 | if (f->file_map[i] != -1UL) { |
| 112 | *b += ffz(f->file_map[i]); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | *b += BLOCKS_PER_MAP; |
| 117 | i++; |
| 118 | } |
| 119 | |
| 120 | return 1; |
| 121 | } |
| 122 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 123 | /* |
| 124 | * Mark a given offset as used in the map. |
| 125 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 126 | static void mark_random_map(struct thread_data *td, struct fio_file *f, |
| 127 | struct io_u *io_u) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 128 | { |
Jens Axboe | 200bc85 | 2006-06-01 16:10:12 +0200 | [diff] [blame] | 129 | unsigned long long block = io_u->offset / (unsigned long long) td->min_bs; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 130 | unsigned int blocks = 0; |
| 131 | |
| 132 | while (blocks < (io_u->buflen / td->min_bs)) { |
| 133 | unsigned int idx, bit; |
| 134 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 135 | if (!random_map_free(td, f, block)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 136 | break; |
| 137 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 138 | idx = RAND_MAP_IDX(td, f, block); |
| 139 | bit = RAND_MAP_BIT(td, f, block); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 140 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 141 | assert(idx < f->num_maps); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 142 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 143 | f->file_map[idx] |= (1UL << bit); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 144 | block++; |
| 145 | blocks++; |
| 146 | } |
| 147 | |
| 148 | if ((blocks * td->min_bs) < io_u->buflen) |
| 149 | io_u->buflen = blocks * td->min_bs; |
| 150 | } |
| 151 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 152 | /* |
| 153 | * For random io, generate a random new block and see if it's used. Repeat |
| 154 | * until we find a free one. For sequential io, just return the end of |
| 155 | * the last io issued. |
| 156 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 157 | static int get_next_offset(struct thread_data *td, struct fio_file *f, |
| 158 | unsigned long long *offset) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 159 | { |
| 160 | unsigned long long b, rb; |
| 161 | long r; |
| 162 | |
| 163 | if (!td->sequential) { |
Jens Axboe | 085227a | 2006-06-01 15:54:09 -0700 | [diff] [blame] | 164 | unsigned long long max_blocks = td->io_size / td->min_bs; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 165 | int loops = 50; |
| 166 | |
| 167 | do { |
Jens Axboe | 6dfd46b | 2006-06-07 13:57:06 +0200 | [diff] [blame] | 168 | r = os_random_long(&td->random_state); |
Jens Axboe | 085227a | 2006-06-01 15:54:09 -0700 | [diff] [blame] | 169 | b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0)); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 170 | rb = b + (f->file_offset / td->min_bs); |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 171 | loops--; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 172 | } while (!random_map_free(td, f, rb) && loops); |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 173 | |
| 174 | if (!loops) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 175 | if (get_next_free_block(td, f, &b)) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 176 | return 1; |
| 177 | } |
| 178 | } else |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 179 | b = f->last_pos / td->min_bs; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 180 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 181 | *offset = (b * td->min_bs) + f->file_offset; |
| 182 | if (*offset > f->file_size) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 183 | return 1; |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static unsigned int get_next_buflen(struct thread_data *td) |
| 189 | { |
| 190 | unsigned int buflen; |
| 191 | long r; |
| 192 | |
| 193 | if (td->min_bs == td->max_bs) |
| 194 | buflen = td->min_bs; |
| 195 | else { |
Jens Axboe | 6dfd46b | 2006-06-07 13:57:06 +0200 | [diff] [blame] | 196 | r = os_random_long(&td->bsrange_state); |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 197 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); |
| 198 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); |
| 199 | } |
| 200 | |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 201 | if (buflen > td->io_size - td->this_io_bytes[td->ddir]) { |
| 202 | /* |
| 203 | * if using direct/raw io, we may not be able to |
| 204 | * shrink the size. so just fail it. |
| 205 | */ |
| 206 | if (td->io_ops->flags & FIO_RAWIO) |
| 207 | return 0; |
| 208 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 209 | buflen = td->io_size - td->this_io_bytes[td->ddir]; |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 210 | } |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 211 | |
| 212 | return buflen; |
| 213 | } |
| 214 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 215 | /* |
| 216 | * Check if we are above the minimum rate given. |
| 217 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 218 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
| 219 | { |
| 220 | unsigned long spent; |
| 221 | unsigned long rate; |
| 222 | int ddir = td->ddir; |
| 223 | |
| 224 | /* |
| 225 | * allow a 2 second settle period in the beginning |
| 226 | */ |
| 227 | if (mtime_since(&td->start, now) < 2000) |
| 228 | return 0; |
| 229 | |
| 230 | /* |
| 231 | * if rate blocks is set, sample is running |
| 232 | */ |
| 233 | if (td->rate_bytes) { |
| 234 | spent = mtime_since(&td->lastrate, now); |
| 235 | if (spent < td->ratecycle) |
| 236 | return 0; |
| 237 | |
| 238 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; |
| 239 | if (rate < td->ratemin) { |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 240 | fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 241 | if (rate_quit) |
| 242 | terminate_threads(td->groupid); |
| 243 | return 1; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | td->rate_bytes = td->this_io_bytes[ddir]; |
| 248 | memcpy(&td->lastrate, now, sizeof(*now)); |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
| 253 | { |
| 254 | if (!td->timeout) |
| 255 | return 0; |
| 256 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) |
| 257 | return 1; |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 262 | /* |
| 263 | * Return the data direction for the next io_u. If the job is a |
| 264 | * mixed read/write workload, check the rwmix cycle and switch if |
| 265 | * necessary. |
| 266 | */ |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 267 | static int get_rw_ddir(struct thread_data *td) |
| 268 | { |
Jens Axboe | a6ccc7b | 2006-06-02 10:14:15 +0200 | [diff] [blame] | 269 | if (td_rw(td)) { |
| 270 | struct timeval now; |
| 271 | unsigned long elapsed; |
| 272 | |
| 273 | gettimeofday(&now, NULL); |
| 274 | elapsed = mtime_since_now(&td->rwmix_switch); |
| 275 | |
| 276 | /* |
| 277 | * Check if it's time to seed a new data direction. |
| 278 | */ |
| 279 | if (elapsed >= td->rwmixcycle) { |
Jens Axboe | c1ee2ca | 2006-06-07 22:42:37 +0200 | [diff] [blame] | 280 | int v; |
Jens Axboe | a6ccc7b | 2006-06-02 10:14:15 +0200 | [diff] [blame] | 281 | long r; |
| 282 | |
Jens Axboe | c1ee2ca | 2006-06-07 22:42:37 +0200 | [diff] [blame] | 283 | r = os_random_long(&td->rwmix_state); |
| 284 | v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0))); |
Jens Axboe | a6ccc7b | 2006-06-02 10:14:15 +0200 | [diff] [blame] | 285 | if (v < td->rwmixread) |
| 286 | td->rwmix_ddir = DDIR_READ; |
| 287 | else |
| 288 | td->rwmix_ddir = DDIR_WRITE; |
| 289 | memcpy(&td->rwmix_switch, &now, sizeof(now)); |
| 290 | } |
| 291 | return td->rwmix_ddir; |
| 292 | } else if (td_read(td)) |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 293 | return DDIR_READ; |
| 294 | else |
| 295 | return DDIR_WRITE; |
| 296 | } |
| 297 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 298 | static int td_io_prep(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 299 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 300 | if (td->io_ops->prep && td->io_ops->prep(td, io_u)) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 301 | return 1; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 306 | void put_io_u(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 307 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 308 | io_u->file = NULL; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 309 | list_del(&io_u->list); |
| 310 | list_add(&io_u->list, &td->io_u_freelist); |
| 311 | td->cur_depth--; |
| 312 | } |
| 313 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 314 | static int fill_io_u(struct thread_data *td, struct fio_file *f, |
| 315 | struct io_u *io_u) |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 316 | { |
| 317 | /* |
| 318 | * If using an iolog, grab next piece if any available. |
| 319 | */ |
| 320 | if (td->read_iolog) |
| 321 | return read_iolog_get(td, io_u); |
| 322 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 323 | /* |
| 324 | * No log, let the seq/rand engine retrieve the next position. |
| 325 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 326 | if (!get_next_offset(td, f, &io_u->offset)) { |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 327 | io_u->buflen = get_next_buflen(td); |
| 328 | |
| 329 | if (io_u->buflen) { |
| 330 | io_u->ddir = get_rw_ddir(td); |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * If using a write iolog, store this entry. |
| 334 | */ |
| 335 | if (td->write_iolog) |
| 336 | write_iolog_put(td, io_u); |
| 337 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 338 | io_u->file = f; |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return 1; |
| 344 | } |
| 345 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 346 | #define queue_full(td) list_empty(&(td)->io_u_freelist) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 347 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 348 | struct io_u *__get_io_u(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 349 | { |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 350 | struct io_u *io_u = NULL; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 351 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 352 | if (!queue_full(td)) { |
| 353 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 354 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 355 | io_u->error = 0; |
| 356 | io_u->resid = 0; |
| 357 | list_del(&io_u->list); |
| 358 | list_add(&io_u->list, &td->io_u_busylist); |
| 359 | td->cur_depth++; |
| 360 | } |
| 361 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 362 | return io_u; |
| 363 | } |
| 364 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 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 | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 369 | static struct io_u *get_io_u(struct thread_data *td, struct fio_file *f) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 370 | { |
| 371 | struct io_u *io_u; |
| 372 | |
| 373 | io_u = __get_io_u(td); |
| 374 | if (!io_u) |
| 375 | return NULL; |
| 376 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 377 | if (td->zone_bytes >= td->zone_size) { |
| 378 | td->zone_bytes = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 379 | f->last_pos += td->zone_skip; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 382 | if (fill_io_u(td, f, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 383 | put_io_u(td, io_u); |
| 384 | return NULL; |
| 385 | } |
| 386 | |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 387 | if (io_u->buflen + io_u->offset > f->file_size) { |
| 388 | if (td->io_ops->flags & FIO_RAWIO) { |
| 389 | put_io_u(td, io_u); |
| 390 | return NULL; |
| 391 | } |
| 392 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 393 | io_u->buflen = f->file_size - io_u->offset; |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 394 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 395 | |
| 396 | if (!io_u->buflen) { |
| 397 | put_io_u(td, io_u); |
| 398 | return NULL; |
| 399 | } |
| 400 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 401 | if (!td->read_iolog && !td->sequential) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 402 | mark_random_map(td, f, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 403 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 404 | f->last_pos += io_u->buflen; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 405 | |
| 406 | if (td->verify != VERIFY_NONE) |
Jens Axboe | e29d1b7 | 2006-10-18 15:43:15 +0200 | [diff] [blame^] | 407 | populate_verify_io_u(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 408 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 409 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 410 | put_io_u(td, io_u); |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | gettimeofday(&io_u->start_time, NULL); |
| 415 | return io_u; |
| 416 | } |
| 417 | |
| 418 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
| 419 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 420 | td->runstate = runstate; |
| 421 | } |
| 422 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 423 | static struct fio_file *get_next_file(struct thread_data *td) |
| 424 | { |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 425 | int old_next_file = td->next_file; |
| 426 | struct fio_file *f; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 427 | |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 428 | do { |
| 429 | f = &td->files[td->next_file]; |
| 430 | |
| 431 | td->next_file++; |
| 432 | if (td->next_file >= td->nr_files) |
| 433 | td->next_file = 0; |
| 434 | |
| 435 | if (f->fd != -1) |
| 436 | break; |
| 437 | |
| 438 | f = NULL; |
| 439 | } while (td->next_file != old_next_file); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 440 | |
| 441 | return f; |
| 442 | } |
| 443 | |
| 444 | static int td_io_sync(struct thread_data *td, struct fio_file *f) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 445 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 446 | if (td->io_ops->sync) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 447 | return td->io_ops->sync(td, f); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 452 | static int td_io_getevents(struct thread_data *td, int min, int max, |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 453 | struct timespec *t) |
| 454 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 455 | return td->io_ops->getevents(td, min, max, t); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 458 | static int td_io_queue(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 459 | { |
| 460 | gettimeofday(&io_u->issue_time, NULL); |
| 461 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 462 | return td->io_ops->queue(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) |
| 466 | |
| 467 | static void io_completed(struct thread_data *td, struct io_u *io_u, |
| 468 | struct io_completion_data *icd) |
| 469 | { |
| 470 | struct timeval e; |
| 471 | unsigned long msec; |
| 472 | |
| 473 | gettimeofday(&e, NULL); |
| 474 | |
| 475 | if (!io_u->error) { |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 476 | unsigned int bytes = io_u->buflen - io_u->resid; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 477 | const int idx = io_u->ddir; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 478 | |
| 479 | td->io_blocks[idx]++; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 480 | td->io_bytes[idx] += bytes; |
| 481 | td->zone_bytes += bytes; |
| 482 | td->this_io_bytes[idx] += bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 483 | |
| 484 | msec = mtime_since(&io_u->issue_time, &e); |
| 485 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 486 | add_clat_sample(td, idx, msec); |
| 487 | add_bw_sample(td, idx); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 488 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 489 | if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 490 | log_io_piece(td, io_u); |
| 491 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 492 | icd->bytes_done[idx] += bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 493 | } else |
| 494 | icd->error = io_u->error; |
| 495 | } |
| 496 | |
| 497 | static void ios_completed(struct thread_data *td,struct io_completion_data *icd) |
| 498 | { |
| 499 | struct io_u *io_u; |
| 500 | int i; |
| 501 | |
| 502 | icd->error = 0; |
| 503 | icd->bytes_done[0] = icd->bytes_done[1] = 0; |
| 504 | |
| 505 | for (i = 0; i < icd->nr; i++) { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 506 | io_u = td->io_ops->event(td, i); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 507 | |
| 508 | io_completed(td, io_u, icd); |
| 509 | put_io_u(td, io_u); |
| 510 | } |
| 511 | } |
| 512 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 513 | /* |
| 514 | * When job exits, we can cancel the in-flight IO if we are using async |
| 515 | * io. Attempt to do so. |
| 516 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 517 | static void cleanup_pending_aio(struct thread_data *td) |
| 518 | { |
| 519 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 520 | struct list_head *entry, *n; |
| 521 | struct io_completion_data icd; |
| 522 | struct io_u *io_u; |
| 523 | int r; |
| 524 | |
| 525 | /* |
| 526 | * get immediately available events, if any |
| 527 | */ |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 528 | r = td_io_getevents(td, 0, td->cur_depth, &ts); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 529 | if (r > 0) { |
| 530 | icd.nr = r; |
| 531 | ios_completed(td, &icd); |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * now cancel remaining active events |
| 536 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 537 | if (td->io_ops->cancel) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 538 | list_for_each_safe(entry, n, &td->io_u_busylist) { |
| 539 | io_u = list_entry(entry, struct io_u, list); |
| 540 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 541 | r = td->io_ops->cancel(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 542 | if (!r) |
| 543 | put_io_u(td, io_u); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | if (td->cur_depth) { |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 548 | r = td_io_getevents(td, td->cur_depth, td->cur_depth, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 549 | if (r > 0) { |
| 550 | icd.nr = r; |
| 551 | ios_completed(td, &icd); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | static int do_io_u_verify(struct thread_data *td, struct io_u **io_u) |
| 557 | { |
| 558 | struct io_u *v_io_u = *io_u; |
| 559 | int ret = 0; |
| 560 | |
| 561 | if (v_io_u) { |
| 562 | ret = verify_io_u(v_io_u); |
| 563 | put_io_u(td, v_io_u); |
| 564 | *io_u = NULL; |
| 565 | } |
| 566 | |
| 567 | return ret; |
| 568 | } |
| 569 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 570 | /* |
| 571 | * The main verify engine. Runs over the writes we previusly submitted, |
| 572 | * reads the blocks back in, and checks the crc/md5 of the data. |
| 573 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 574 | static void do_verify(struct thread_data *td) |
| 575 | { |
| 576 | struct timeval t; |
| 577 | struct io_u *io_u, *v_io_u = NULL; |
| 578 | struct io_completion_data icd; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 579 | struct fio_file *f; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 580 | int ret; |
| 581 | |
| 582 | td_set_runstate(td, TD_VERIFYING); |
| 583 | |
| 584 | do { |
| 585 | if (td->terminate) |
| 586 | break; |
| 587 | |
| 588 | gettimeofday(&t, NULL); |
| 589 | if (runtime_exceeded(td, &t)) |
| 590 | break; |
| 591 | |
| 592 | io_u = __get_io_u(td); |
| 593 | if (!io_u) |
| 594 | break; |
| 595 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 596 | if (get_next_verify(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 597 | put_io_u(td, io_u); |
| 598 | break; |
| 599 | } |
| 600 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 601 | f = get_next_file(td); |
| 602 | if (!f) |
| 603 | break; |
| 604 | |
| 605 | io_u->file = f; |
| 606 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 607 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 608 | put_io_u(td, io_u); |
| 609 | break; |
| 610 | } |
| 611 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 612 | ret = td_io_queue(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 613 | if (ret) { |
| 614 | put_io_u(td, io_u); |
| 615 | td_verror(td, ret); |
| 616 | break; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * we have one pending to verify, do that while |
| 621 | * we are doing io on the next one |
| 622 | */ |
| 623 | if (do_io_u_verify(td, &v_io_u)) |
| 624 | break; |
| 625 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 626 | ret = td_io_getevents(td, 1, 1, NULL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 627 | if (ret != 1) { |
| 628 | if (ret < 0) |
| 629 | td_verror(td, ret); |
| 630 | break; |
| 631 | } |
| 632 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 633 | v_io_u = td->io_ops->event(td, 0); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 634 | icd.nr = 1; |
| 635 | icd.error = 0; |
| 636 | io_completed(td, v_io_u, &icd); |
| 637 | |
| 638 | if (icd.error) { |
| 639 | td_verror(td, icd.error); |
| 640 | put_io_u(td, v_io_u); |
| 641 | v_io_u = NULL; |
| 642 | break; |
| 643 | } |
| 644 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 645 | /* |
| 646 | * if we can't submit more io, we need to verify now |
| 647 | */ |
| 648 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) |
| 649 | break; |
| 650 | |
| 651 | } while (1); |
| 652 | |
| 653 | do_io_u_verify(td, &v_io_u); |
| 654 | |
| 655 | if (td->cur_depth) |
| 656 | cleanup_pending_aio(td); |
| 657 | |
| 658 | td_set_runstate(td, TD_RUNNING); |
| 659 | } |
| 660 | |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 661 | /* |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 662 | * Not really an io thread, all it does is burn CPU cycles in the specified |
| 663 | * manner. |
| 664 | */ |
| 665 | static void do_cpuio(struct thread_data *td) |
| 666 | { |
| 667 | struct timeval e; |
| 668 | int split = 100 / td->cpuload; |
| 669 | int i = 0; |
| 670 | |
| 671 | while (!td->terminate) { |
| 672 | gettimeofday(&e, NULL); |
| 673 | |
| 674 | if (runtime_exceeded(td, &e)) |
| 675 | break; |
| 676 | |
| 677 | if (!(i % split)) |
| 678 | __usec_sleep(10000); |
| 679 | else |
| 680 | usec_sleep(td, 10000); |
| 681 | |
| 682 | i++; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 687 | * Main IO worker function. It retrieves io_u's to process and queues |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 688 | * and reaps them, checking for rate and errors along the way. |
| 689 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 690 | static void do_io(struct thread_data *td) |
| 691 | { |
| 692 | struct io_completion_data icd; |
| 693 | struct timeval s, e; |
| 694 | unsigned long usec; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 695 | struct fio_file *f; |
| 696 | int i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 697 | |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 698 | td_set_runstate(td, TD_RUNNING); |
| 699 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 700 | while (td->this_io_bytes[td->ddir] < td->io_size) { |
| 701 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 702 | struct timespec *timeout; |
| 703 | int ret, min_evts = 0; |
| 704 | struct io_u *io_u; |
| 705 | |
| 706 | if (td->terminate) |
| 707 | break; |
| 708 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 709 | f = get_next_file(td); |
| 710 | if (!f) |
| 711 | break; |
| 712 | |
| 713 | io_u = get_io_u(td, f); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 714 | if (!io_u) |
| 715 | break; |
| 716 | |
| 717 | memcpy(&s, &io_u->start_time, sizeof(s)); |
| 718 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 719 | ret = td_io_queue(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 720 | if (ret) { |
| 721 | put_io_u(td, io_u); |
| 722 | td_verror(td, ret); |
| 723 | break; |
| 724 | } |
| 725 | |
| 726 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); |
| 727 | |
| 728 | if (td->cur_depth < td->iodepth) { |
| 729 | timeout = &ts; |
| 730 | min_evts = 0; |
| 731 | } else { |
| 732 | timeout = NULL; |
| 733 | min_evts = 1; |
| 734 | } |
| 735 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 736 | ret = td_io_getevents(td, min_evts, td->cur_depth, timeout); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 737 | if (ret < 0) { |
| 738 | td_verror(td, ret); |
| 739 | break; |
| 740 | } else if (!ret) |
| 741 | continue; |
| 742 | |
| 743 | icd.nr = ret; |
| 744 | ios_completed(td, &icd); |
| 745 | if (icd.error) { |
| 746 | td_verror(td, icd.error); |
| 747 | break; |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * the rate is batched for now, it should work for batches |
| 752 | * of completions except the very first one which may look |
| 753 | * a little bursty |
| 754 | */ |
| 755 | gettimeofday(&e, NULL); |
| 756 | usec = utime_since(&s, &e); |
| 757 | |
| 758 | rate_throttle(td, usec, icd.bytes_done[td->ddir]); |
| 759 | |
| 760 | if (check_min_rate(td, &e)) { |
| 761 | td_verror(td, ENOMEM); |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | if (runtime_exceeded(td, &e)) |
| 766 | break; |
| 767 | |
| 768 | if (td->thinktime) |
| 769 | usec_sleep(td, td->thinktime); |
| 770 | |
| 771 | if (should_fsync(td) && td->fsync_blocks && |
| 772 | (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 773 | td_io_sync(td, f); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | if (td->cur_depth) |
| 777 | cleanup_pending_aio(td); |
| 778 | |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 779 | if (should_fsync(td) && td->end_fsync) { |
| 780 | td_set_runstate(td, TD_FSYNCING); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 781 | for_each_file(td, f, i) |
| 782 | td_io_sync(td, f); |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 783 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 784 | } |
| 785 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 786 | static int td_io_init(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 787 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 788 | if (td->io_ops->init) |
| 789 | return td->io_ops->init(td); |
| 790 | |
| 791 | return 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | static void cleanup_io_u(struct thread_data *td) |
| 795 | { |
| 796 | struct list_head *entry, *n; |
| 797 | struct io_u *io_u; |
| 798 | |
| 799 | list_for_each_safe(entry, n, &td->io_u_freelist) { |
| 800 | io_u = list_entry(entry, struct io_u, list); |
| 801 | |
| 802 | list_del(&io_u->list); |
| 803 | free(io_u); |
| 804 | } |
| 805 | |
| 806 | if (td->mem_type == MEM_MALLOC) |
| 807 | free(td->orig_buffer); |
| 808 | else if (td->mem_type == MEM_SHM) { |
| 809 | struct shmid_ds sbuf; |
| 810 | |
| 811 | shmdt(td->orig_buffer); |
| 812 | shmctl(td->shm_id, IPC_RMID, &sbuf); |
| 813 | } else if (td->mem_type == MEM_MMAP) |
| 814 | munmap(td->orig_buffer, td->orig_buffer_size); |
| 815 | else |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 816 | log_err("Bad memory type %d\n", td->mem_type); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 817 | |
| 818 | td->orig_buffer = NULL; |
| 819 | } |
| 820 | |
| 821 | static int init_io_u(struct thread_data *td) |
| 822 | { |
| 823 | struct io_u *io_u; |
| 824 | int i, max_units; |
| 825 | char *p; |
| 826 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 827 | if (td->io_ops->flags & FIO_CPUIO) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 828 | return 0; |
| 829 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 830 | if (td->io_ops->flags & FIO_SYNCIO) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 831 | max_units = 1; |
| 832 | else |
| 833 | max_units = td->iodepth; |
| 834 | |
| 835 | td->orig_buffer_size = td->max_bs * max_units + MASK; |
| 836 | |
| 837 | if (td->mem_type == MEM_MALLOC) |
| 838 | td->orig_buffer = malloc(td->orig_buffer_size); |
| 839 | else if (td->mem_type == MEM_SHM) { |
| 840 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600); |
| 841 | if (td->shm_id < 0) { |
| 842 | td_verror(td, errno); |
| 843 | perror("shmget"); |
| 844 | return 1; |
| 845 | } |
| 846 | |
| 847 | td->orig_buffer = shmat(td->shm_id, NULL, 0); |
| 848 | if (td->orig_buffer == (void *) -1) { |
| 849 | td_verror(td, errno); |
| 850 | perror("shmat"); |
| 851 | td->orig_buffer = NULL; |
| 852 | return 1; |
| 853 | } |
| 854 | } else if (td->mem_type == MEM_MMAP) { |
| 855 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
| 856 | if (td->orig_buffer == MAP_FAILED) { |
| 857 | td_verror(td, errno); |
| 858 | perror("mmap"); |
| 859 | td->orig_buffer = NULL; |
| 860 | return 1; |
| 861 | } |
| 862 | } |
| 863 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 864 | p = ALIGN(td->orig_buffer); |
| 865 | for (i = 0; i < max_units; i++) { |
| 866 | io_u = malloc(sizeof(*io_u)); |
| 867 | memset(io_u, 0, sizeof(*io_u)); |
| 868 | INIT_LIST_HEAD(&io_u->list); |
| 869 | |
| 870 | io_u->buf = p + td->max_bs * i; |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 871 | io_u->index = i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 872 | list_add(&io_u->list, &td->io_u_freelist); |
| 873 | } |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 878 | static int switch_ioscheduler(struct thread_data *td) |
| 879 | { |
| 880 | char tmp[256], tmp2[128]; |
| 881 | FILE *f; |
| 882 | int ret; |
| 883 | |
| 884 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); |
| 885 | |
| 886 | f = fopen(tmp, "r+"); |
| 887 | if (!f) { |
| 888 | td_verror(td, errno); |
| 889 | return 1; |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | * Set io scheduler. |
| 894 | */ |
| 895 | ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f); |
| 896 | if (ferror(f) || ret != 1) { |
| 897 | td_verror(td, errno); |
| 898 | fclose(f); |
| 899 | return 1; |
| 900 | } |
| 901 | |
| 902 | rewind(f); |
| 903 | |
| 904 | /* |
| 905 | * Read back and check that the selected scheduler is now the default. |
| 906 | */ |
| 907 | ret = fread(tmp, 1, sizeof(tmp), f); |
| 908 | if (ferror(f) || ret < 0) { |
| 909 | td_verror(td, errno); |
| 910 | fclose(f); |
| 911 | return 1; |
| 912 | } |
| 913 | |
| 914 | sprintf(tmp2, "[%s]", td->ioscheduler); |
| 915 | if (!strstr(tmp, tmp2)) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 916 | log_err("fio: io scheduler %s not found\n", td->ioscheduler); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 917 | td_verror(td, EINVAL); |
| 918 | fclose(f); |
| 919 | return 1; |
| 920 | } |
| 921 | |
| 922 | fclose(f); |
| 923 | return 0; |
| 924 | } |
| 925 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 926 | static void clear_io_state(struct thread_data *td) |
| 927 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 928 | struct fio_file *f; |
| 929 | int i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 930 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 931 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
| 932 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 933 | td->zone_bytes = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 934 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 935 | for_each_file(td, f, i) { |
| 936 | f->last_pos = 0; |
| 937 | if (td->io_ops->flags & FIO_SYNCIO) |
| 938 | lseek(f->fd, SEEK_SET, 0); |
| 939 | |
| 940 | if (f->file_map) |
| 941 | memset(f->file_map, 0, f->num_maps * sizeof(long)); |
| 942 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 943 | } |
| 944 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 945 | /* |
| 946 | * Entry point for the thread based jobs. The process based jobs end up |
| 947 | * here as well, after a little setup. |
| 948 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 949 | static void *thread_main(void *data) |
| 950 | { |
| 951 | struct thread_data *td = data; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 952 | |
| 953 | if (!td->use_thread) |
| 954 | setsid(); |
| 955 | |
| 956 | td->pid = getpid(); |
| 957 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 958 | INIT_LIST_HEAD(&td->io_u_freelist); |
| 959 | INIT_LIST_HEAD(&td->io_u_busylist); |
| 960 | INIT_LIST_HEAD(&td->io_hist_list); |
| 961 | INIT_LIST_HEAD(&td->io_log_list); |
| 962 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 963 | if (init_io_u(td)) |
| 964 | goto err; |
| 965 | |
| 966 | if (fio_setaffinity(td) == -1) { |
| 967 | td_verror(td, errno); |
| 968 | goto err; |
| 969 | } |
| 970 | |
Jens Axboe | 45bee28 | 2006-10-18 15:26:44 +0200 | [diff] [blame] | 971 | if (td_io_init(td)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 972 | goto err; |
| 973 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 974 | if (init_iolog(td)) |
| 975 | goto err; |
| 976 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 977 | if (td->ioprio) { |
| 978 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
| 979 | td_verror(td, errno); |
| 980 | goto err; |
| 981 | } |
| 982 | } |
| 983 | |
Jens Axboe | 1056eaa | 2006-07-13 10:33:45 -0700 | [diff] [blame] | 984 | if (nice(td->nice) == -1) { |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 985 | td_verror(td, errno); |
| 986 | goto err; |
| 987 | } |
| 988 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 989 | if (init_random_state(td)) |
| 990 | goto err; |
| 991 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 992 | if (td->ioscheduler && switch_ioscheduler(td)) |
| 993 | goto err; |
| 994 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 995 | td_set_runstate(td, TD_INITIALIZED); |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 996 | fio_sem_up(&startup_sem); |
| 997 | fio_sem_down(&td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 998 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 999 | if (!td->create_serialize && setup_files(td)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1000 | goto err; |
| 1001 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1002 | gettimeofday(&td->epoch, NULL); |
| 1003 | |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 1004 | if (td->exec_prerun) |
| 1005 | system(td->exec_prerun); |
| 1006 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1007 | while (td->loops--) { |
| 1008 | getrusage(RUSAGE_SELF, &td->ru_start); |
| 1009 | gettimeofday(&td->start, NULL); |
| 1010 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); |
| 1011 | |
| 1012 | if (td->ratemin) |
| 1013 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); |
| 1014 | |
| 1015 | clear_io_state(td); |
| 1016 | prune_io_piece_log(td); |
| 1017 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1018 | if (td->io_ops->flags & FIO_CPUIO) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1019 | do_cpuio(td); |
| 1020 | else |
| 1021 | do_io(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1022 | |
| 1023 | td->runtime[td->ddir] += mtime_since_now(&td->start); |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1024 | if (td_rw(td) && td->io_bytes[td->ddir ^ 1]) |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1025 | td->runtime[td->ddir ^ 1] = td->runtime[td->ddir]; |
| 1026 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1027 | update_rusage_stat(td); |
| 1028 | |
| 1029 | if (td->error || td->terminate) |
| 1030 | break; |
| 1031 | |
| 1032 | if (td->verify == VERIFY_NONE) |
| 1033 | continue; |
| 1034 | |
| 1035 | clear_io_state(td); |
| 1036 | gettimeofday(&td->start, NULL); |
| 1037 | |
| 1038 | do_verify(td); |
| 1039 | |
| 1040 | td->runtime[DDIR_READ] += mtime_since_now(&td->start); |
| 1041 | |
| 1042 | if (td->error || td->terminate) |
| 1043 | break; |
| 1044 | } |
| 1045 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1046 | if (td->bw_log) |
| 1047 | finish_log(td, td->bw_log, "bw"); |
| 1048 | if (td->slat_log) |
| 1049 | finish_log(td, td->slat_log, "slat"); |
| 1050 | if (td->clat_log) |
| 1051 | finish_log(td, td->clat_log, "clat"); |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 1052 | if (td->write_iolog) |
| 1053 | write_iolog_close(td); |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 1054 | if (td->exec_postrun) |
| 1055 | system(td->exec_postrun); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1056 | |
| 1057 | if (exitall_on_terminate) |
| 1058 | terminate_threads(td->groupid); |
| 1059 | |
| 1060 | err: |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1061 | close_files(td); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1062 | close_ioengine(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1063 | cleanup_io_u(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1064 | td_set_runstate(td, TD_EXITED); |
| 1065 | return NULL; |
| 1066 | |
| 1067 | } |
| 1068 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1069 | /* |
| 1070 | * We cannot pass the td data into a forked process, so attach the td and |
| 1071 | * pass it to the thread worker. |
| 1072 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1073 | static void *fork_main(int shmid, int offset) |
| 1074 | { |
| 1075 | struct thread_data *td; |
| 1076 | void *data; |
| 1077 | |
| 1078 | data = shmat(shmid, NULL, 0); |
| 1079 | if (data == (void *) -1) { |
| 1080 | perror("shmat"); |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | |
| 1084 | td = data + offset * sizeof(struct thread_data); |
| 1085 | thread_main(td); |
| 1086 | shmdt(data); |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1090 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1091 | * Run over the job map and reap the threads that have exited, if any. |
| 1092 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1093 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
| 1094 | { |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1095 | int i, cputhreads; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1096 | |
| 1097 | /* |
| 1098 | * reap exited threads (TD_EXITED -> TD_REAPED) |
| 1099 | */ |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1100 | for (i = 0, cputhreads = 0; i < thread_number; i++) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1101 | struct thread_data *td = &threads[i]; |
| 1102 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1103 | if (td->io_ops->flags & FIO_CPUIO) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1104 | cputhreads++; |
| 1105 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1106 | if (td->runstate != TD_EXITED) |
| 1107 | continue; |
| 1108 | |
| 1109 | td_set_runstate(td, TD_REAPED); |
| 1110 | |
| 1111 | if (td->use_thread) { |
| 1112 | long ret; |
| 1113 | |
| 1114 | if (pthread_join(td->thread, (void *) &ret)) |
| 1115 | perror("thread_join"); |
| 1116 | } else |
| 1117 | waitpid(td->pid, NULL, 0); |
| 1118 | |
| 1119 | (*nr_running)--; |
| 1120 | (*m_rate) -= td->ratemin; |
| 1121 | (*t_rate) -= td->rate; |
| 1122 | } |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1123 | |
| 1124 | if (*nr_running == cputhreads) |
| 1125 | terminate_threads(TERMINATE_ALL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1128 | static void fio_unpin_memory(void *pinned) |
| 1129 | { |
| 1130 | if (pinned) { |
| 1131 | if (munlock(pinned, mlock_size) < 0) |
| 1132 | perror("munlock"); |
| 1133 | munmap(pinned, mlock_size); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | static void *fio_pin_memory(void) |
| 1138 | { |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 1139 | unsigned long long phys_mem; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1140 | void *ptr; |
| 1141 | |
| 1142 | if (!mlock_size) |
| 1143 | return NULL; |
| 1144 | |
| 1145 | /* |
| 1146 | * Don't allow mlock of more than real_mem-128MB |
| 1147 | */ |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 1148 | phys_mem = os_phys_mem(); |
| 1149 | if (phys_mem) { |
| 1150 | if ((mlock_size + 128 * 1024 * 1024) > phys_mem) { |
| 1151 | mlock_size = phys_mem - 128 * 1024 * 1024; |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 1152 | fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20); |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
| 1157 | if (!ptr) { |
| 1158 | perror("malloc locked mem"); |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | if (mlock(ptr, mlock_size) < 0) { |
| 1162 | munmap(ptr, mlock_size); |
| 1163 | perror("mlock"); |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | |
| 1167 | return ptr; |
| 1168 | } |
| 1169 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1170 | /* |
| 1171 | * Main function for kicking off and reaping jobs, as needed. |
| 1172 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1173 | static void run_threads(void) |
| 1174 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1175 | struct thread_data *td; |
| 1176 | unsigned long spent; |
| 1177 | int i, todo, nr_running, m_rate, t_rate, nr_started; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1178 | void *mlocked_mem; |
| 1179 | |
| 1180 | mlocked_mem = fio_pin_memory(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1181 | |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 1182 | if (!terse_output) { |
| 1183 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); |
| 1184 | fflush(stdout); |
| 1185 | } |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 1186 | |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame] | 1187 | signal(SIGINT, sig_handler); |
| 1188 | signal(SIGALRM, sig_handler); |
| 1189 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1190 | todo = thread_number; |
| 1191 | nr_running = 0; |
| 1192 | nr_started = 0; |
| 1193 | m_rate = t_rate = 0; |
| 1194 | |
| 1195 | for (i = 0; i < thread_number; i++) { |
| 1196 | td = &threads[i]; |
| 1197 | |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 1198 | print_status_init(td->thread_number - 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1199 | |
| 1200 | init_disk_util(td); |
| 1201 | |
| 1202 | if (!td->create_serialize) |
| 1203 | continue; |
| 1204 | |
| 1205 | /* |
| 1206 | * do file setup here so it happens sequentially, |
| 1207 | * we don't want X number of threads getting their |
| 1208 | * client data interspersed on disk |
| 1209 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1210 | if (setup_files(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1211 | td_set_runstate(td, TD_REAPED); |
| 1212 | todo--; |
| 1213 | } |
| 1214 | } |
| 1215 | |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 1216 | time_init(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1217 | |
| 1218 | while (todo) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1219 | struct thread_data *map[MAX_JOBS]; |
| 1220 | struct timeval this_start; |
| 1221 | int this_jobs = 0, left; |
| 1222 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1223 | /* |
| 1224 | * create threads (TD_NOT_CREATED -> TD_CREATED) |
| 1225 | */ |
| 1226 | for (i = 0; i < thread_number; i++) { |
| 1227 | td = &threads[i]; |
| 1228 | |
| 1229 | if (td->runstate != TD_NOT_CREATED) |
| 1230 | continue; |
| 1231 | |
| 1232 | /* |
| 1233 | * never got a chance to start, killed by other |
| 1234 | * thread for some reason |
| 1235 | */ |
| 1236 | if (td->terminate) { |
| 1237 | todo--; |
| 1238 | continue; |
| 1239 | } |
| 1240 | |
| 1241 | if (td->start_delay) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 1242 | spent = mtime_since_genesis(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1243 | |
| 1244 | if (td->start_delay * 1000 > spent) |
| 1245 | continue; |
| 1246 | } |
| 1247 | |
| 1248 | if (td->stonewall && (nr_started || nr_running)) |
| 1249 | break; |
| 1250 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1251 | /* |
| 1252 | * Set state to created. Thread will transition |
| 1253 | * to TD_INITIALIZED when it's done setting up. |
| 1254 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1255 | td_set_runstate(td, TD_CREATED); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1256 | map[this_jobs++] = td; |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1257 | fio_sem_init(&startup_sem, 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1258 | nr_started++; |
| 1259 | |
| 1260 | if (td->use_thread) { |
| 1261 | if (pthread_create(&td->thread, NULL, thread_main, td)) { |
| 1262 | perror("thread_create"); |
| 1263 | nr_started--; |
| 1264 | } |
| 1265 | } else { |
| 1266 | if (fork()) |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1267 | fio_sem_down(&startup_sem); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1268 | else { |
| 1269 | fork_main(shm_id, i); |
| 1270 | exit(0); |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | /* |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1276 | * Wait for the started threads to transition to |
| 1277 | * TD_INITIALIZED. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1278 | */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1279 | gettimeofday(&this_start, NULL); |
| 1280 | left = this_jobs; |
| 1281 | while (left) { |
| 1282 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) |
| 1283 | break; |
| 1284 | |
| 1285 | usleep(100000); |
| 1286 | |
| 1287 | for (i = 0; i < this_jobs; i++) { |
| 1288 | td = map[i]; |
| 1289 | if (!td) |
| 1290 | continue; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1291 | if (td->runstate == TD_INITIALIZED) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1292 | map[i] = NULL; |
| 1293 | left--; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1294 | } else if (td->runstate >= TD_EXITED) { |
| 1295 | map[i] = NULL; |
| 1296 | left--; |
| 1297 | todo--; |
| 1298 | nr_running++; /* work-around... */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | if (left) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 1304 | log_err("fio: %d jobs failed to start\n", left); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1305 | for (i = 0; i < this_jobs; i++) { |
| 1306 | td = map[i]; |
| 1307 | if (!td) |
| 1308 | continue; |
| 1309 | kill(td->pid, SIGTERM); |
| 1310 | } |
| 1311 | break; |
| 1312 | } |
| 1313 | |
| 1314 | /* |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1315 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1316 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1317 | for (i = 0; i < thread_number; i++) { |
| 1318 | td = &threads[i]; |
| 1319 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1320 | if (td->runstate != TD_INITIALIZED) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1321 | continue; |
| 1322 | |
| 1323 | td_set_runstate(td, TD_RUNNING); |
| 1324 | nr_running++; |
| 1325 | nr_started--; |
| 1326 | m_rate += td->ratemin; |
| 1327 | t_rate += td->rate; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1328 | todo--; |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1329 | fio_sem_up(&td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1333 | |
| 1334 | if (todo) |
| 1335 | usleep(100000); |
| 1336 | } |
| 1337 | |
| 1338 | while (nr_running) { |
| 1339 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1340 | usleep(10000); |
| 1341 | } |
| 1342 | |
| 1343 | update_io_ticks(); |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1344 | fio_unpin_memory(mlocked_mem); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1345 | } |
| 1346 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1347 | int main(int argc, char *argv[]) |
| 1348 | { |
| 1349 | if (parse_options(argc, argv)) |
| 1350 | return 1; |
| 1351 | |
| 1352 | if (!thread_number) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 1353 | log_err("Nothing to do\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1354 | return 1; |
| 1355 | } |
| 1356 | |
| 1357 | disk_util_timer_arm(); |
| 1358 | |
| 1359 | run_threads(); |
| 1360 | show_run_stats(); |
| 1361 | |
| 1362 | return 0; |
| 1363 | } |