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