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; |
| 44 | static char run_str[MAX_JOBS + 1]; |
| 45 | int shm_id = 0; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 46 | static struct timeval genesis; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 47 | int temp_stall_ts; |
Jens Axboe | c1d5725 | 2006-10-09 19:56:04 +0200 | [diff] [blame] | 48 | char *fio_inst_prefix = _INST_PREFIX; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 49 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 50 | static void print_thread_status(void); |
| 51 | |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 52 | extern unsigned long long mlock_size; |
| 53 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 54 | /* |
Jens Axboe | 7980911 | 2006-06-09 10:14:54 +0200 | [diff] [blame] | 55 | * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it |
| 56 | * will never back again. It may cycle between running/verififying/fsyncing. |
| 57 | * Once the thread reaches TD_EXITED, it is just waiting for the core to |
| 58 | * reap it. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 59 | */ |
| 60 | enum { |
| 61 | TD_NOT_CREATED = 0, |
| 62 | TD_CREATED, |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 63 | TD_INITIALIZED, |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 64 | TD_RUNNING, |
| 65 | TD_VERIFYING, |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 66 | TD_FSYNCING, |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 67 | TD_EXITED, |
| 68 | TD_REAPED, |
| 69 | }; |
| 70 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 71 | #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] | 72 | |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 73 | static volatile int startup_sem; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 74 | |
| 75 | #define TERMINATE_ALL (-1) |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 76 | #define JOB_START_TIMEOUT (5 * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 77 | |
| 78 | static void terminate_threads(int group_id) |
| 79 | { |
| 80 | int i; |
| 81 | |
| 82 | for (i = 0; i < thread_number; i++) { |
| 83 | struct thread_data *td = &threads[i]; |
| 84 | |
| 85 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
| 86 | td->terminate = 1; |
| 87 | td->start_delay = 0; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static void sig_handler(int sig) |
| 93 | { |
| 94 | switch (sig) { |
| 95 | case SIGALRM: |
| 96 | update_io_ticks(); |
| 97 | disk_util_timer_arm(); |
| 98 | print_thread_status(); |
| 99 | break; |
| 100 | default: |
| 101 | printf("\nfio: terminating on signal\n"); |
| 102 | fflush(stdout); |
| 103 | terminate_threads(TERMINATE_ALL); |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 108 | /* |
| 109 | * The ->file_map[] contains a map of blocks we have or have not done io |
| 110 | * to yet. Used to make sure we cover the entire range in a fair fashion. |
| 111 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 112 | static int random_map_free(struct thread_data *td, struct fio_file *f, |
| 113 | unsigned long long block) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 114 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 115 | unsigned int idx = RAND_MAP_IDX(td, f, block); |
| 116 | unsigned int bit = RAND_MAP_BIT(td, f, block); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 117 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 118 | return (f->file_map[idx] & (1UL << bit)) == 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 121 | /* |
| 122 | * Return the next free block in the map. |
| 123 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 124 | static int get_next_free_block(struct thread_data *td, struct fio_file *f, |
| 125 | unsigned long long *b) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 126 | { |
| 127 | int i; |
| 128 | |
| 129 | *b = 0; |
| 130 | i = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 131 | while ((*b) * td->min_bs < f->file_size) { |
| 132 | if (f->file_map[i] != -1UL) { |
| 133 | *b += ffz(f->file_map[i]); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | *b += BLOCKS_PER_MAP; |
| 138 | i++; |
| 139 | } |
| 140 | |
| 141 | return 1; |
| 142 | } |
| 143 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 144 | /* |
| 145 | * Mark a given offset as used in the map. |
| 146 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 147 | static void mark_random_map(struct thread_data *td, struct fio_file *f, |
| 148 | struct io_u *io_u) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 149 | { |
Jens Axboe | 200bc85 | 2006-06-01 16:10:12 +0200 | [diff] [blame] | 150 | 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] | 151 | unsigned int blocks = 0; |
| 152 | |
| 153 | while (blocks < (io_u->buflen / td->min_bs)) { |
| 154 | unsigned int idx, bit; |
| 155 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 156 | if (!random_map_free(td, f, block)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 157 | break; |
| 158 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 159 | idx = RAND_MAP_IDX(td, f, block); |
| 160 | bit = RAND_MAP_BIT(td, f, block); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 161 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 162 | assert(idx < f->num_maps); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 163 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 164 | f->file_map[idx] |= (1UL << bit); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 165 | block++; |
| 166 | blocks++; |
| 167 | } |
| 168 | |
| 169 | if ((blocks * td->min_bs) < io_u->buflen) |
| 170 | io_u->buflen = blocks * td->min_bs; |
| 171 | } |
| 172 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 173 | /* |
| 174 | * For random io, generate a random new block and see if it's used. Repeat |
| 175 | * until we find a free one. For sequential io, just return the end of |
| 176 | * the last io issued. |
| 177 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 178 | static int get_next_offset(struct thread_data *td, struct fio_file *f, |
| 179 | unsigned long long *offset) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 180 | { |
| 181 | unsigned long long b, rb; |
| 182 | long r; |
| 183 | |
| 184 | if (!td->sequential) { |
Jens Axboe | 085227a | 2006-06-01 15:54:09 -0700 | [diff] [blame] | 185 | unsigned long long max_blocks = td->io_size / td->min_bs; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 186 | int loops = 50; |
| 187 | |
| 188 | do { |
Jens Axboe | 6dfd46b | 2006-06-07 13:57:06 +0200 | [diff] [blame] | 189 | r = os_random_long(&td->random_state); |
Jens Axboe | 085227a | 2006-06-01 15:54:09 -0700 | [diff] [blame] | 190 | b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0)); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 191 | rb = b + (f->file_offset / td->min_bs); |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 192 | loops--; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 193 | } while (!random_map_free(td, f, rb) && loops); |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 194 | |
| 195 | if (!loops) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 196 | if (get_next_free_block(td, f, &b)) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 197 | return 1; |
| 198 | } |
| 199 | } else |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 200 | b = f->last_pos / td->min_bs; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 201 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 202 | *offset = (b * td->min_bs) + f->file_offset; |
| 203 | if (*offset > f->file_size) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 204 | return 1; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static unsigned int get_next_buflen(struct thread_data *td) |
| 210 | { |
| 211 | unsigned int buflen; |
| 212 | long r; |
| 213 | |
| 214 | if (td->min_bs == td->max_bs) |
| 215 | buflen = td->min_bs; |
| 216 | else { |
Jens Axboe | 6dfd46b | 2006-06-07 13:57:06 +0200 | [diff] [blame] | 217 | r = os_random_long(&td->bsrange_state); |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 218 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); |
| 219 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); |
| 220 | } |
| 221 | |
| 222 | if (buflen > td->io_size - td->this_io_bytes[td->ddir]) |
| 223 | buflen = td->io_size - td->this_io_bytes[td->ddir]; |
| 224 | |
| 225 | return buflen; |
| 226 | } |
| 227 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 228 | /* |
| 229 | * Check if we are above the minimum rate given. |
| 230 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 231 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
| 232 | { |
| 233 | unsigned long spent; |
| 234 | unsigned long rate; |
| 235 | int ddir = td->ddir; |
| 236 | |
| 237 | /* |
| 238 | * allow a 2 second settle period in the beginning |
| 239 | */ |
| 240 | if (mtime_since(&td->start, now) < 2000) |
| 241 | return 0; |
| 242 | |
| 243 | /* |
| 244 | * if rate blocks is set, sample is running |
| 245 | */ |
| 246 | if (td->rate_bytes) { |
| 247 | spent = mtime_since(&td->lastrate, now); |
| 248 | if (spent < td->ratecycle) |
| 249 | return 0; |
| 250 | |
| 251 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; |
| 252 | if (rate < td->ratemin) { |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 253 | 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] | 254 | if (rate_quit) |
| 255 | terminate_threads(td->groupid); |
| 256 | return 1; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | td->rate_bytes = td->this_io_bytes[ddir]; |
| 261 | memcpy(&td->lastrate, now, sizeof(*now)); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
| 266 | { |
| 267 | if (!td->timeout) |
| 268 | return 0; |
| 269 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) |
| 270 | return 1; |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static void fill_random_bytes(struct thread_data *td, |
| 276 | unsigned char *p, unsigned int len) |
| 277 | { |
| 278 | unsigned int todo; |
| 279 | double r; |
| 280 | |
| 281 | while (len) { |
Jens Axboe | 6dfd46b | 2006-06-07 13:57:06 +0200 | [diff] [blame] | 282 | r = os_random_double(&td->verify_state); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * lrand48_r seems to be broken and only fill the bottom |
| 286 | * 32-bits, even on 64-bit archs with 64-bit longs |
| 287 | */ |
| 288 | todo = sizeof(r); |
| 289 | if (todo > len) |
| 290 | todo = len; |
| 291 | |
| 292 | memcpy(p, &r, todo); |
| 293 | |
| 294 | len -= todo; |
| 295 | p += todo; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static void hexdump(void *buffer, int len) |
| 300 | { |
| 301 | unsigned char *p = buffer; |
| 302 | int i; |
| 303 | |
| 304 | for (i = 0; i < len; i++) |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 305 | fprintf(f_out, "%02x", p[i]); |
| 306 | fprintf(f_out, "\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u) |
| 310 | { |
| 311 | unsigned char *p = (unsigned char *) io_u->buf; |
| 312 | unsigned long c; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 313 | |
| 314 | p += sizeof(*hdr); |
| 315 | c = crc32(p, hdr->len - sizeof(*hdr)); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 316 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 317 | if (c != hdr->crc32) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 318 | log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
| 319 | log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c); |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 320 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 323 | return 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u) |
| 327 | { |
| 328 | unsigned char *p = (unsigned char *) io_u->buf; |
| 329 | struct md5_ctx md5_ctx; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 330 | |
| 331 | memset(&md5_ctx, 0, sizeof(md5_ctx)); |
| 332 | p += sizeof(*hdr); |
| 333 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); |
| 334 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 335 | if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 336 | log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 337 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); |
| 338 | hexdump(md5_ctx.hash, sizeof(md5_ctx.hash)); |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 339 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 342 | return 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static int verify_io_u(struct io_u *io_u) |
| 346 | { |
| 347 | struct verify_header *hdr = (struct verify_header *) io_u->buf; |
| 348 | int ret; |
| 349 | |
| 350 | if (hdr->fio_magic != FIO_HDR_MAGIC) |
| 351 | return 1; |
| 352 | |
| 353 | if (hdr->verify_type == VERIFY_MD5) |
| 354 | ret = verify_io_u_md5(hdr, io_u); |
| 355 | else if (hdr->verify_type == VERIFY_CRC32) |
| 356 | ret = verify_io_u_crc32(hdr, io_u); |
| 357 | else { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 358 | log_err("Bad verify type %d\n", hdr->verify_type); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 359 | ret = 1; |
| 360 | } |
| 361 | |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
| 366 | { |
| 367 | hdr->crc32 = crc32(p, len); |
| 368 | } |
| 369 | |
| 370 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
| 371 | { |
| 372 | struct md5_ctx md5_ctx; |
| 373 | |
| 374 | memset(&md5_ctx, 0, sizeof(md5_ctx)); |
| 375 | md5_update(&md5_ctx, p, len); |
| 376 | memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); |
| 377 | } |
| 378 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 379 | /* |
| 380 | * Return the data direction for the next io_u. If the job is a |
| 381 | * mixed read/write workload, check the rwmix cycle and switch if |
| 382 | * necessary. |
| 383 | */ |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 384 | static int get_rw_ddir(struct thread_data *td) |
| 385 | { |
Jens Axboe | a6ccc7b | 2006-06-02 10:14:15 +0200 | [diff] [blame] | 386 | if (td_rw(td)) { |
| 387 | struct timeval now; |
| 388 | unsigned long elapsed; |
| 389 | |
| 390 | gettimeofday(&now, NULL); |
| 391 | elapsed = mtime_since_now(&td->rwmix_switch); |
| 392 | |
| 393 | /* |
| 394 | * Check if it's time to seed a new data direction. |
| 395 | */ |
| 396 | if (elapsed >= td->rwmixcycle) { |
Jens Axboe | c1ee2ca | 2006-06-07 22:42:37 +0200 | [diff] [blame] | 397 | int v; |
Jens Axboe | a6ccc7b | 2006-06-02 10:14:15 +0200 | [diff] [blame] | 398 | long r; |
| 399 | |
Jens Axboe | c1ee2ca | 2006-06-07 22:42:37 +0200 | [diff] [blame] | 400 | r = os_random_long(&td->rwmix_state); |
| 401 | v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0))); |
Jens Axboe | a6ccc7b | 2006-06-02 10:14:15 +0200 | [diff] [blame] | 402 | if (v < td->rwmixread) |
| 403 | td->rwmix_ddir = DDIR_READ; |
| 404 | else |
| 405 | td->rwmix_ddir = DDIR_WRITE; |
| 406 | memcpy(&td->rwmix_switch, &now, sizeof(now)); |
| 407 | } |
| 408 | return td->rwmix_ddir; |
| 409 | } else if (td_read(td)) |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 410 | return DDIR_READ; |
| 411 | else |
| 412 | return DDIR_WRITE; |
| 413 | } |
| 414 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 415 | /* |
| 416 | * fill body of io_u->buf with random data and add a header with the |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 417 | * crc32 or md5 sum of that data. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 418 | */ |
| 419 | static void populate_io_u(struct thread_data *td, struct io_u *io_u) |
| 420 | { |
| 421 | unsigned char *p = (unsigned char *) io_u->buf; |
| 422 | struct verify_header hdr; |
| 423 | |
| 424 | hdr.fio_magic = FIO_HDR_MAGIC; |
| 425 | hdr.len = io_u->buflen; |
| 426 | p += sizeof(hdr); |
| 427 | fill_random_bytes(td, p, io_u->buflen - sizeof(hdr)); |
| 428 | |
| 429 | if (td->verify == VERIFY_MD5) { |
| 430 | fill_md5(&hdr, p, io_u->buflen - sizeof(hdr)); |
| 431 | hdr.verify_type = VERIFY_MD5; |
| 432 | } else { |
| 433 | fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr)); |
| 434 | hdr.verify_type = VERIFY_CRC32; |
| 435 | } |
| 436 | |
| 437 | memcpy(io_u->buf, &hdr, sizeof(hdr)); |
| 438 | } |
| 439 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 440 | 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] | 441 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 442 | if (td->io_ops->prep && td->io_ops->prep(td, io_u)) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 443 | return 1; |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 448 | 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] | 449 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 450 | io_u->file = NULL; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 451 | list_del(&io_u->list); |
| 452 | list_add(&io_u->list, &td->io_u_freelist); |
| 453 | td->cur_depth--; |
| 454 | } |
| 455 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 456 | static int fill_io_u(struct thread_data *td, struct fio_file *f, |
| 457 | struct io_u *io_u) |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 458 | { |
| 459 | /* |
| 460 | * If using an iolog, grab next piece if any available. |
| 461 | */ |
| 462 | if (td->read_iolog) |
| 463 | return read_iolog_get(td, io_u); |
| 464 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 465 | /* |
| 466 | * No log, let the seq/rand engine retrieve the next position. |
| 467 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 468 | if (!get_next_offset(td, f, &io_u->offset)) { |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 469 | io_u->buflen = get_next_buflen(td); |
| 470 | |
| 471 | if (io_u->buflen) { |
| 472 | io_u->ddir = get_rw_ddir(td); |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | * If using a write iolog, store this entry. |
| 476 | */ |
| 477 | if (td->write_iolog) |
| 478 | write_iolog_put(td, io_u); |
| 479 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 480 | io_u->file = f; |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return 1; |
| 486 | } |
| 487 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 488 | #define queue_full(td) list_empty(&(td)->io_u_freelist) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 489 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 490 | struct io_u *__get_io_u(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 491 | { |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 492 | struct io_u *io_u = NULL; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 493 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 494 | if (!queue_full(td)) { |
| 495 | 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] | 496 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 497 | io_u->error = 0; |
| 498 | io_u->resid = 0; |
| 499 | list_del(&io_u->list); |
| 500 | list_add(&io_u->list, &td->io_u_busylist); |
| 501 | td->cur_depth++; |
| 502 | } |
| 503 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 504 | return io_u; |
| 505 | } |
| 506 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 507 | /* |
| 508 | * Return an io_u to be processed. Gets a buflen and offset, sets direction, |
| 509 | * etc. The returned io_u is fully ready to be prepped and submitted. |
| 510 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 511 | 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] | 512 | { |
| 513 | struct io_u *io_u; |
| 514 | |
| 515 | io_u = __get_io_u(td); |
| 516 | if (!io_u) |
| 517 | return NULL; |
| 518 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 519 | if (td->zone_bytes >= td->zone_size) { |
| 520 | td->zone_bytes = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 521 | f->last_pos += td->zone_skip; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 524 | if (fill_io_u(td, f, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 525 | put_io_u(td, io_u); |
| 526 | return NULL; |
| 527 | } |
| 528 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 529 | if (io_u->buflen + io_u->offset > f->file_size) |
| 530 | io_u->buflen = f->file_size - io_u->offset; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 531 | |
| 532 | if (!io_u->buflen) { |
| 533 | put_io_u(td, io_u); |
| 534 | return NULL; |
| 535 | } |
| 536 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 537 | if (!td->read_iolog && !td->sequential) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 538 | mark_random_map(td, f, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 539 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 540 | f->last_pos += io_u->buflen; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 541 | |
| 542 | if (td->verify != VERIFY_NONE) |
| 543 | populate_io_u(td, io_u); |
| 544 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 545 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 546 | put_io_u(td, io_u); |
| 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | gettimeofday(&io_u->start_time, NULL); |
| 551 | return io_u; |
| 552 | } |
| 553 | |
| 554 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
| 555 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 556 | td->runstate = runstate; |
| 557 | } |
| 558 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 559 | static int get_next_verify(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 560 | { |
| 561 | struct io_piece *ipo; |
| 562 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 563 | if (!list_empty(&td->io_hist_list)) { |
| 564 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 565 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 566 | list_del(&ipo->list); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 567 | |
Jens Axboe | 22f78b3 | 2006-06-07 11:30:07 +0200 | [diff] [blame] | 568 | io_u->offset = ipo->offset; |
| 569 | io_u->buflen = ipo->len; |
| 570 | io_u->ddir = DDIR_READ; |
| 571 | free(ipo); |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | return 1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 578 | static struct fio_file *get_next_file(struct thread_data *td) |
| 579 | { |
| 580 | struct fio_file *f = &td->files[td->next_file]; |
| 581 | |
| 582 | td->next_file++; |
| 583 | if (td->next_file >= td->nr_files) |
| 584 | td->next_file = 0; |
| 585 | |
| 586 | return f; |
| 587 | } |
| 588 | |
| 589 | 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] | 590 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 591 | if (td->io_ops->sync) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 592 | return td->io_ops->sync(td, f); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | static int io_u_getevents(struct thread_data *td, int min, int max, |
| 598 | struct timespec *t) |
| 599 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 600 | return td->io_ops->getevents(td, min, max, t); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static int io_u_queue(struct thread_data *td, struct io_u *io_u) |
| 604 | { |
| 605 | gettimeofday(&io_u->issue_time, NULL); |
| 606 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 607 | return td->io_ops->queue(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) |
| 611 | |
| 612 | static void io_completed(struct thread_data *td, struct io_u *io_u, |
| 613 | struct io_completion_data *icd) |
| 614 | { |
| 615 | struct timeval e; |
| 616 | unsigned long msec; |
| 617 | |
| 618 | gettimeofday(&e, NULL); |
| 619 | |
| 620 | if (!io_u->error) { |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 621 | unsigned int bytes = io_u->buflen - io_u->resid; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 622 | const int idx = io_u->ddir; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 623 | |
| 624 | td->io_blocks[idx]++; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 625 | td->io_bytes[idx] += bytes; |
| 626 | td->zone_bytes += bytes; |
| 627 | td->this_io_bytes[idx] += bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 628 | |
| 629 | msec = mtime_since(&io_u->issue_time, &e); |
| 630 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 631 | add_clat_sample(td, idx, msec); |
| 632 | add_bw_sample(td, idx); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 633 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 634 | if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 635 | log_io_piece(td, io_u); |
| 636 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 637 | icd->bytes_done[idx] += bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 638 | } else |
| 639 | icd->error = io_u->error; |
| 640 | } |
| 641 | |
| 642 | static void ios_completed(struct thread_data *td,struct io_completion_data *icd) |
| 643 | { |
| 644 | struct io_u *io_u; |
| 645 | int i; |
| 646 | |
| 647 | icd->error = 0; |
| 648 | icd->bytes_done[0] = icd->bytes_done[1] = 0; |
| 649 | |
| 650 | for (i = 0; i < icd->nr; i++) { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 651 | io_u = td->io_ops->event(td, i); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 652 | |
| 653 | io_completed(td, io_u, icd); |
| 654 | put_io_u(td, io_u); |
| 655 | } |
| 656 | } |
| 657 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 658 | /* |
| 659 | * When job exits, we can cancel the in-flight IO if we are using async |
| 660 | * io. Attempt to do so. |
| 661 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 662 | static void cleanup_pending_aio(struct thread_data *td) |
| 663 | { |
| 664 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 665 | struct list_head *entry, *n; |
| 666 | struct io_completion_data icd; |
| 667 | struct io_u *io_u; |
| 668 | int r; |
| 669 | |
| 670 | /* |
| 671 | * get immediately available events, if any |
| 672 | */ |
| 673 | r = io_u_getevents(td, 0, td->cur_depth, &ts); |
| 674 | if (r > 0) { |
| 675 | icd.nr = r; |
| 676 | ios_completed(td, &icd); |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * now cancel remaining active events |
| 681 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 682 | if (td->io_ops->cancel) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 683 | list_for_each_safe(entry, n, &td->io_u_busylist) { |
| 684 | io_u = list_entry(entry, struct io_u, list); |
| 685 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 686 | r = td->io_ops->cancel(td, io_u); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 687 | if (!r) |
| 688 | put_io_u(td, io_u); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | if (td->cur_depth) { |
| 693 | r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL); |
| 694 | if (r > 0) { |
| 695 | icd.nr = r; |
| 696 | ios_completed(td, &icd); |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | static int do_io_u_verify(struct thread_data *td, struct io_u **io_u) |
| 702 | { |
| 703 | struct io_u *v_io_u = *io_u; |
| 704 | int ret = 0; |
| 705 | |
| 706 | if (v_io_u) { |
| 707 | ret = verify_io_u(v_io_u); |
| 708 | put_io_u(td, v_io_u); |
| 709 | *io_u = NULL; |
| 710 | } |
| 711 | |
| 712 | return ret; |
| 713 | } |
| 714 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 715 | /* |
| 716 | * The main verify engine. Runs over the writes we previusly submitted, |
| 717 | * reads the blocks back in, and checks the crc/md5 of the data. |
| 718 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 719 | static void do_verify(struct thread_data *td) |
| 720 | { |
| 721 | struct timeval t; |
| 722 | struct io_u *io_u, *v_io_u = NULL; |
| 723 | struct io_completion_data icd; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 724 | struct fio_file *f; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 725 | int ret; |
| 726 | |
| 727 | td_set_runstate(td, TD_VERIFYING); |
| 728 | |
| 729 | do { |
| 730 | if (td->terminate) |
| 731 | break; |
| 732 | |
| 733 | gettimeofday(&t, NULL); |
| 734 | if (runtime_exceeded(td, &t)) |
| 735 | break; |
| 736 | |
| 737 | io_u = __get_io_u(td); |
| 738 | if (!io_u) |
| 739 | break; |
| 740 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 741 | if (get_next_verify(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 742 | put_io_u(td, io_u); |
| 743 | break; |
| 744 | } |
| 745 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 746 | f = get_next_file(td); |
| 747 | if (!f) |
| 748 | break; |
| 749 | |
| 750 | io_u->file = f; |
| 751 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 752 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 753 | put_io_u(td, io_u); |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | ret = io_u_queue(td, io_u); |
| 758 | if (ret) { |
| 759 | put_io_u(td, io_u); |
| 760 | td_verror(td, ret); |
| 761 | break; |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * we have one pending to verify, do that while |
| 766 | * we are doing io on the next one |
| 767 | */ |
| 768 | if (do_io_u_verify(td, &v_io_u)) |
| 769 | break; |
| 770 | |
| 771 | ret = io_u_getevents(td, 1, 1, NULL); |
| 772 | if (ret != 1) { |
| 773 | if (ret < 0) |
| 774 | td_verror(td, ret); |
| 775 | break; |
| 776 | } |
| 777 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 778 | v_io_u = td->io_ops->event(td, 0); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 779 | icd.nr = 1; |
| 780 | icd.error = 0; |
| 781 | io_completed(td, v_io_u, &icd); |
| 782 | |
| 783 | if (icd.error) { |
| 784 | td_verror(td, icd.error); |
| 785 | put_io_u(td, v_io_u); |
| 786 | v_io_u = NULL; |
| 787 | break; |
| 788 | } |
| 789 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 790 | /* |
| 791 | * if we can't submit more io, we need to verify now |
| 792 | */ |
| 793 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) |
| 794 | break; |
| 795 | |
| 796 | } while (1); |
| 797 | |
| 798 | do_io_u_verify(td, &v_io_u); |
| 799 | |
| 800 | if (td->cur_depth) |
| 801 | cleanup_pending_aio(td); |
| 802 | |
| 803 | td_set_runstate(td, TD_RUNNING); |
| 804 | } |
| 805 | |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 806 | /* |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 807 | * Not really an io thread, all it does is burn CPU cycles in the specified |
| 808 | * manner. |
| 809 | */ |
| 810 | static void do_cpuio(struct thread_data *td) |
| 811 | { |
| 812 | struct timeval e; |
| 813 | int split = 100 / td->cpuload; |
| 814 | int i = 0; |
| 815 | |
| 816 | while (!td->terminate) { |
| 817 | gettimeofday(&e, NULL); |
| 818 | |
| 819 | if (runtime_exceeded(td, &e)) |
| 820 | break; |
| 821 | |
| 822 | if (!(i % split)) |
| 823 | __usec_sleep(10000); |
| 824 | else |
| 825 | usec_sleep(td, 10000); |
| 826 | |
| 827 | i++; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /* |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 832 | * 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] | 833 | * and reaps them, checking for rate and errors along the way. |
| 834 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 835 | static void do_io(struct thread_data *td) |
| 836 | { |
| 837 | struct io_completion_data icd; |
| 838 | struct timeval s, e; |
| 839 | unsigned long usec; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 840 | struct fio_file *f; |
| 841 | int i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 842 | |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 843 | td_set_runstate(td, TD_RUNNING); |
| 844 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 845 | while (td->this_io_bytes[td->ddir] < td->io_size) { |
| 846 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 847 | struct timespec *timeout; |
| 848 | int ret, min_evts = 0; |
| 849 | struct io_u *io_u; |
| 850 | |
| 851 | if (td->terminate) |
| 852 | break; |
| 853 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 854 | f = get_next_file(td); |
| 855 | if (!f) |
| 856 | break; |
| 857 | |
| 858 | io_u = get_io_u(td, f); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 859 | if (!io_u) |
| 860 | break; |
| 861 | |
| 862 | memcpy(&s, &io_u->start_time, sizeof(s)); |
| 863 | |
| 864 | ret = io_u_queue(td, io_u); |
| 865 | if (ret) { |
| 866 | put_io_u(td, io_u); |
| 867 | td_verror(td, ret); |
| 868 | break; |
| 869 | } |
| 870 | |
| 871 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); |
| 872 | |
| 873 | if (td->cur_depth < td->iodepth) { |
| 874 | timeout = &ts; |
| 875 | min_evts = 0; |
| 876 | } else { |
| 877 | timeout = NULL; |
| 878 | min_evts = 1; |
| 879 | } |
| 880 | |
| 881 | ret = io_u_getevents(td, min_evts, td->cur_depth, timeout); |
| 882 | if (ret < 0) { |
| 883 | td_verror(td, ret); |
| 884 | break; |
| 885 | } else if (!ret) |
| 886 | continue; |
| 887 | |
| 888 | icd.nr = ret; |
| 889 | ios_completed(td, &icd); |
| 890 | if (icd.error) { |
| 891 | td_verror(td, icd.error); |
| 892 | break; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * the rate is batched for now, it should work for batches |
| 897 | * of completions except the very first one which may look |
| 898 | * a little bursty |
| 899 | */ |
| 900 | gettimeofday(&e, NULL); |
| 901 | usec = utime_since(&s, &e); |
| 902 | |
| 903 | rate_throttle(td, usec, icd.bytes_done[td->ddir]); |
| 904 | |
| 905 | if (check_min_rate(td, &e)) { |
| 906 | td_verror(td, ENOMEM); |
| 907 | break; |
| 908 | } |
| 909 | |
| 910 | if (runtime_exceeded(td, &e)) |
| 911 | break; |
| 912 | |
| 913 | if (td->thinktime) |
| 914 | usec_sleep(td, td->thinktime); |
| 915 | |
| 916 | if (should_fsync(td) && td->fsync_blocks && |
| 917 | (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 918 | td_io_sync(td, f); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | if (td->cur_depth) |
| 922 | cleanup_pending_aio(td); |
| 923 | |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 924 | if (should_fsync(td) && td->end_fsync) { |
| 925 | td_set_runstate(td, TD_FSYNCING); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 926 | for_each_file(td, f, i) |
| 927 | td_io_sync(td, f); |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 928 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 929 | } |
| 930 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 931 | static int init_io(struct thread_data *td) |
| 932 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 933 | if (td->io_ops->init) |
| 934 | return td->io_ops->init(td); |
| 935 | |
| 936 | return 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | static void cleanup_io_u(struct thread_data *td) |
| 940 | { |
| 941 | struct list_head *entry, *n; |
| 942 | struct io_u *io_u; |
| 943 | |
| 944 | list_for_each_safe(entry, n, &td->io_u_freelist) { |
| 945 | io_u = list_entry(entry, struct io_u, list); |
| 946 | |
| 947 | list_del(&io_u->list); |
| 948 | free(io_u); |
| 949 | } |
| 950 | |
| 951 | if (td->mem_type == MEM_MALLOC) |
| 952 | free(td->orig_buffer); |
| 953 | else if (td->mem_type == MEM_SHM) { |
| 954 | struct shmid_ds sbuf; |
| 955 | |
| 956 | shmdt(td->orig_buffer); |
| 957 | shmctl(td->shm_id, IPC_RMID, &sbuf); |
| 958 | } else if (td->mem_type == MEM_MMAP) |
| 959 | munmap(td->orig_buffer, td->orig_buffer_size); |
| 960 | else |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 961 | log_err("Bad memory type %d\n", td->mem_type); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 962 | |
| 963 | td->orig_buffer = NULL; |
| 964 | } |
| 965 | |
| 966 | static int init_io_u(struct thread_data *td) |
| 967 | { |
| 968 | struct io_u *io_u; |
| 969 | int i, max_units; |
| 970 | char *p; |
| 971 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 972 | if (td->io_ops->flags & FIO_CPUIO) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 973 | return 0; |
| 974 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 975 | if (td->io_ops->flags & FIO_SYNCIO) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 976 | max_units = 1; |
| 977 | else |
| 978 | max_units = td->iodepth; |
| 979 | |
| 980 | td->orig_buffer_size = td->max_bs * max_units + MASK; |
| 981 | |
| 982 | if (td->mem_type == MEM_MALLOC) |
| 983 | td->orig_buffer = malloc(td->orig_buffer_size); |
| 984 | else if (td->mem_type == MEM_SHM) { |
| 985 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600); |
| 986 | if (td->shm_id < 0) { |
| 987 | td_verror(td, errno); |
| 988 | perror("shmget"); |
| 989 | return 1; |
| 990 | } |
| 991 | |
| 992 | td->orig_buffer = shmat(td->shm_id, NULL, 0); |
| 993 | if (td->orig_buffer == (void *) -1) { |
| 994 | td_verror(td, errno); |
| 995 | perror("shmat"); |
| 996 | td->orig_buffer = NULL; |
| 997 | return 1; |
| 998 | } |
| 999 | } else if (td->mem_type == MEM_MMAP) { |
| 1000 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
| 1001 | if (td->orig_buffer == MAP_FAILED) { |
| 1002 | td_verror(td, errno); |
| 1003 | perror("mmap"); |
| 1004 | td->orig_buffer = NULL; |
| 1005 | return 1; |
| 1006 | } |
| 1007 | } |
| 1008 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1009 | p = ALIGN(td->orig_buffer); |
| 1010 | for (i = 0; i < max_units; i++) { |
| 1011 | io_u = malloc(sizeof(*io_u)); |
| 1012 | memset(io_u, 0, sizeof(*io_u)); |
| 1013 | INIT_LIST_HEAD(&io_u->list); |
| 1014 | |
| 1015 | io_u->buf = p + td->max_bs * i; |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 1016 | io_u->index = i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1017 | list_add(&io_u->list, &td->io_u_freelist); |
| 1018 | } |
| 1019 | |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 1023 | static int switch_ioscheduler(struct thread_data *td) |
| 1024 | { |
| 1025 | char tmp[256], tmp2[128]; |
| 1026 | FILE *f; |
| 1027 | int ret; |
| 1028 | |
| 1029 | sprintf(tmp, "%s/queue/scheduler", td->sysfs_root); |
| 1030 | |
| 1031 | f = fopen(tmp, "r+"); |
| 1032 | if (!f) { |
| 1033 | td_verror(td, errno); |
| 1034 | return 1; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Set io scheduler. |
| 1039 | */ |
| 1040 | ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f); |
| 1041 | if (ferror(f) || ret != 1) { |
| 1042 | td_verror(td, errno); |
| 1043 | fclose(f); |
| 1044 | return 1; |
| 1045 | } |
| 1046 | |
| 1047 | rewind(f); |
| 1048 | |
| 1049 | /* |
| 1050 | * Read back and check that the selected scheduler is now the default. |
| 1051 | */ |
| 1052 | ret = fread(tmp, 1, sizeof(tmp), f); |
| 1053 | if (ferror(f) || ret < 0) { |
| 1054 | td_verror(td, errno); |
| 1055 | fclose(f); |
| 1056 | return 1; |
| 1057 | } |
| 1058 | |
| 1059 | sprintf(tmp2, "[%s]", td->ioscheduler); |
| 1060 | if (!strstr(tmp, tmp2)) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 1061 | log_err("fio: io scheduler %s not found\n", td->ioscheduler); |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 1062 | td_verror(td, EINVAL); |
| 1063 | fclose(f); |
| 1064 | return 1; |
| 1065 | } |
| 1066 | |
| 1067 | fclose(f); |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1071 | static void clear_io_state(struct thread_data *td) |
| 1072 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 1073 | struct fio_file *f; |
| 1074 | int i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1075 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1076 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
| 1077 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 1078 | td->zone_bytes = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1079 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 1080 | for_each_file(td, f, i) { |
| 1081 | f->last_pos = 0; |
| 1082 | if (td->io_ops->flags & FIO_SYNCIO) |
| 1083 | lseek(f->fd, SEEK_SET, 0); |
| 1084 | |
| 1085 | if (f->file_map) |
| 1086 | memset(f->file_map, 0, f->num_maps * sizeof(long)); |
| 1087 | } |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1088 | } |
| 1089 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1090 | /* |
| 1091 | * Entry point for the thread based jobs. The process based jobs end up |
| 1092 | * here as well, after a little setup. |
| 1093 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1094 | static void *thread_main(void *data) |
| 1095 | { |
| 1096 | struct thread_data *td = data; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1097 | |
| 1098 | if (!td->use_thread) |
| 1099 | setsid(); |
| 1100 | |
| 1101 | td->pid = getpid(); |
| 1102 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1103 | INIT_LIST_HEAD(&td->io_u_freelist); |
| 1104 | INIT_LIST_HEAD(&td->io_u_busylist); |
| 1105 | INIT_LIST_HEAD(&td->io_hist_list); |
| 1106 | INIT_LIST_HEAD(&td->io_log_list); |
| 1107 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1108 | if (init_io_u(td)) |
| 1109 | goto err; |
| 1110 | |
| 1111 | if (fio_setaffinity(td) == -1) { |
| 1112 | td_verror(td, errno); |
| 1113 | goto err; |
| 1114 | } |
| 1115 | |
| 1116 | if (init_io(td)) |
| 1117 | goto err; |
| 1118 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1119 | if (init_iolog(td)) |
| 1120 | goto err; |
| 1121 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1122 | if (td->ioprio) { |
| 1123 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
| 1124 | td_verror(td, errno); |
| 1125 | goto err; |
| 1126 | } |
| 1127 | } |
| 1128 | |
Jens Axboe | 1056eaa | 2006-07-13 10:33:45 -0700 | [diff] [blame] | 1129 | if (nice(td->nice) == -1) { |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1130 | td_verror(td, errno); |
| 1131 | goto err; |
| 1132 | } |
| 1133 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1134 | if (init_random_state(td)) |
| 1135 | goto err; |
| 1136 | |
Jens Axboe | da86774 | 2006-06-06 10:39:10 -0700 | [diff] [blame] | 1137 | if (td->ioscheduler && switch_ioscheduler(td)) |
| 1138 | goto err; |
| 1139 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1140 | td_set_runstate(td, TD_INITIALIZED); |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1141 | fio_sem_up(&startup_sem); |
| 1142 | fio_sem_down(&td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1143 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 1144 | if (!td->create_serialize && setup_files(td)) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1145 | goto err; |
| 1146 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1147 | gettimeofday(&td->epoch, NULL); |
| 1148 | |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 1149 | if (td->exec_prerun) |
| 1150 | system(td->exec_prerun); |
| 1151 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1152 | while (td->loops--) { |
| 1153 | getrusage(RUSAGE_SELF, &td->ru_start); |
| 1154 | gettimeofday(&td->start, NULL); |
| 1155 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); |
| 1156 | |
| 1157 | if (td->ratemin) |
| 1158 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); |
| 1159 | |
| 1160 | clear_io_state(td); |
| 1161 | prune_io_piece_log(td); |
| 1162 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1163 | if (td->io_ops->flags & FIO_CPUIO) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1164 | do_cpuio(td); |
| 1165 | else |
| 1166 | do_io(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1167 | |
| 1168 | td->runtime[td->ddir] += mtime_since_now(&td->start); |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1169 | if (td_rw(td) && td->io_bytes[td->ddir ^ 1]) |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1170 | td->runtime[td->ddir ^ 1] = td->runtime[td->ddir]; |
| 1171 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1172 | update_rusage_stat(td); |
| 1173 | |
| 1174 | if (td->error || td->terminate) |
| 1175 | break; |
| 1176 | |
| 1177 | if (td->verify == VERIFY_NONE) |
| 1178 | continue; |
| 1179 | |
| 1180 | clear_io_state(td); |
| 1181 | gettimeofday(&td->start, NULL); |
| 1182 | |
| 1183 | do_verify(td); |
| 1184 | |
| 1185 | td->runtime[DDIR_READ] += mtime_since_now(&td->start); |
| 1186 | |
| 1187 | if (td->error || td->terminate) |
| 1188 | break; |
| 1189 | } |
| 1190 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1191 | if (td->bw_log) |
| 1192 | finish_log(td, td->bw_log, "bw"); |
| 1193 | if (td->slat_log) |
| 1194 | finish_log(td, td->slat_log, "slat"); |
| 1195 | if (td->clat_log) |
| 1196 | finish_log(td, td->clat_log, "clat"); |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 1197 | if (td->write_iolog) |
| 1198 | write_iolog_close(td); |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame] | 1199 | if (td->exec_postrun) |
| 1200 | system(td->exec_postrun); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1201 | |
| 1202 | if (exitall_on_terminate) |
| 1203 | terminate_threads(td->groupid); |
| 1204 | |
| 1205 | err: |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 1206 | close_files(td); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1207 | close_ioengine(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1208 | cleanup_io_u(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1209 | td_set_runstate(td, TD_EXITED); |
| 1210 | return NULL; |
| 1211 | |
| 1212 | } |
| 1213 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1214 | /* |
| 1215 | * We cannot pass the td data into a forked process, so attach the td and |
| 1216 | * pass it to the thread worker. |
| 1217 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1218 | static void *fork_main(int shmid, int offset) |
| 1219 | { |
| 1220 | struct thread_data *td; |
| 1221 | void *data; |
| 1222 | |
| 1223 | data = shmat(shmid, NULL, 0); |
| 1224 | if (data == (void *) -1) { |
| 1225 | perror("shmat"); |
| 1226 | return NULL; |
| 1227 | } |
| 1228 | |
| 1229 | td = data + offset * sizeof(struct thread_data); |
| 1230 | thread_main(td); |
| 1231 | shmdt(data); |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1235 | /* |
| 1236 | * Sets the status of the 'td' in the printed status map. |
| 1237 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1238 | static void check_str_update(struct thread_data *td) |
| 1239 | { |
| 1240 | char c = run_str[td->thread_number - 1]; |
| 1241 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1242 | switch (td->runstate) { |
| 1243 | case TD_REAPED: |
| 1244 | c = '_'; |
| 1245 | break; |
| 1246 | case TD_EXITED: |
| 1247 | c = 'E'; |
| 1248 | break; |
| 1249 | case TD_RUNNING: |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1250 | if (td_rw(td)) { |
| 1251 | if (td->sequential) |
| 1252 | c = 'M'; |
| 1253 | else |
| 1254 | c = 'm'; |
| 1255 | } else if (td_read(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1256 | if (td->sequential) |
| 1257 | c = 'R'; |
| 1258 | else |
| 1259 | c = 'r'; |
| 1260 | } else { |
| 1261 | if (td->sequential) |
| 1262 | c = 'W'; |
| 1263 | else |
| 1264 | c = 'w'; |
| 1265 | } |
| 1266 | break; |
| 1267 | case TD_VERIFYING: |
| 1268 | c = 'V'; |
| 1269 | break; |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 1270 | case TD_FSYNCING: |
| 1271 | c = 'F'; |
| 1272 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1273 | case TD_CREATED: |
| 1274 | c = 'C'; |
| 1275 | break; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1276 | case TD_INITIALIZED: |
| 1277 | c = 'I'; |
| 1278 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1279 | case TD_NOT_CREATED: |
| 1280 | c = 'P'; |
| 1281 | break; |
| 1282 | default: |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 1283 | log_err("state %d\n", td->runstate); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | run_str[td->thread_number - 1] = c; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1287 | } |
| 1288 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1289 | /* |
| 1290 | * Convert seconds to a printable string. |
| 1291 | */ |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1292 | static void eta_to_str(char *str, int eta_sec) |
| 1293 | { |
| 1294 | unsigned int d, h, m, s; |
| 1295 | static int always_d, always_h; |
| 1296 | |
| 1297 | d = h = m = s = 0; |
| 1298 | |
| 1299 | s = eta_sec % 60; |
| 1300 | eta_sec /= 60; |
| 1301 | m = eta_sec % 60; |
| 1302 | eta_sec /= 60; |
| 1303 | h = eta_sec % 24; |
| 1304 | eta_sec /= 24; |
| 1305 | d = eta_sec; |
| 1306 | |
| 1307 | if (d || always_d) { |
| 1308 | always_d = 1; |
| 1309 | str += sprintf(str, "%02dd:", d); |
| 1310 | } |
| 1311 | if (h || always_h) { |
| 1312 | always_h = 1; |
| 1313 | str += sprintf(str, "%02dh:", h); |
| 1314 | } |
| 1315 | |
| 1316 | str += sprintf(str, "%02dm:", m); |
| 1317 | str += sprintf(str, "%02ds", s); |
| 1318 | } |
| 1319 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1320 | /* |
| 1321 | * Best effort calculation of the estimated pending runtime of a job. |
| 1322 | */ |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1323 | static int thread_eta(struct thread_data *td, unsigned long elapsed) |
| 1324 | { |
| 1325 | unsigned long long bytes_total, bytes_done; |
| 1326 | unsigned int eta_sec = 0; |
| 1327 | |
| 1328 | bytes_total = td->total_io_size; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1329 | |
| 1330 | /* |
| 1331 | * if writing, bytes_total will be twice the size. If mixing, |
| 1332 | * assume a 50/50 split and thus bytes_total will be 50% larger. |
| 1333 | */ |
| 1334 | if (td->verify) { |
| 1335 | if (td_rw(td)) |
| 1336 | bytes_total = bytes_total * 3 / 2; |
| 1337 | else |
| 1338 | bytes_total <<= 1; |
| 1339 | } |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1340 | if (td->zone_size && td->zone_skip) |
| 1341 | bytes_total /= (td->zone_skip / td->zone_size); |
| 1342 | |
| 1343 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) { |
| 1344 | double perc; |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 1345 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1346 | bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]; |
| 1347 | perc = (double) bytes_done / (double) bytes_total; |
| 1348 | if (perc > 1.0) |
| 1349 | perc = 1.0; |
| 1350 | |
| 1351 | eta_sec = (elapsed * (1.0 / perc)) - elapsed; |
| 1352 | |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 1353 | if (td->timeout && eta_sec > (td->timeout - elapsed)) |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1354 | eta_sec = td->timeout - elapsed; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1355 | } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED |
| 1356 | || td->runstate == TD_INITIALIZED) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1357 | int t_eta = 0, r_eta = 0; |
| 1358 | |
| 1359 | /* |
| 1360 | * We can only guess - assume it'll run the full timeout |
| 1361 | * if given, otherwise assume it'll run at the specified rate. |
| 1362 | */ |
| 1363 | if (td->timeout) |
| 1364 | t_eta = td->timeout + td->start_delay - elapsed; |
| 1365 | if (td->rate) { |
| 1366 | r_eta = (bytes_total / 1024) / td->rate; |
| 1367 | r_eta += td->start_delay - elapsed; |
| 1368 | } |
| 1369 | |
| 1370 | if (r_eta && t_eta) |
| 1371 | eta_sec = min(r_eta, t_eta); |
| 1372 | else if (r_eta) |
| 1373 | eta_sec = r_eta; |
| 1374 | else if (t_eta) |
| 1375 | eta_sec = t_eta; |
| 1376 | else |
Jens Axboe | 972cfd2 | 2006-06-09 11:08:56 +0200 | [diff] [blame] | 1377 | eta_sec = 0; |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1378 | } else { |
| 1379 | /* |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 1380 | * thread is already done or waiting for fsync |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1381 | */ |
| 1382 | eta_sec = 0; |
| 1383 | } |
| 1384 | |
| 1385 | return eta_sec; |
| 1386 | } |
| 1387 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1388 | /* |
| 1389 | * Print status of the jobs we know about. This includes rate estimates, |
| 1390 | * ETA, thread state, etc. |
| 1391 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1392 | static void print_thread_status(void) |
| 1393 | { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1394 | unsigned long elapsed = time_since_now(&genesis); |
Jens Axboe | 71a751c | 2006-06-08 14:48:47 +0200 | [diff] [blame] | 1395 | int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1396 | char eta_str[32]; |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1397 | double perc = 0.0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1398 | |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 1399 | if (temp_stall_ts || terse_output) |
Jens Axboe | 88c6ed8 | 2006-06-09 11:28:10 +0200 | [diff] [blame] | 1400 | return; |
| 1401 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1402 | eta_secs = malloc(thread_number * sizeof(int)); |
| 1403 | memset(eta_secs, 0, thread_number * sizeof(int)); |
| 1404 | |
Jens Axboe | 71a751c | 2006-06-08 14:48:47 +0200 | [diff] [blame] | 1405 | nr_pending = nr_running = t_rate = m_rate = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1406 | for (i = 0; i < thread_number; i++) { |
| 1407 | struct thread_data *td = &threads[i]; |
| 1408 | |
Jens Axboe | 5853e5a | 2006-06-08 14:41:05 +0200 | [diff] [blame] | 1409 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING|| |
| 1410 | td->runstate == TD_FSYNCING) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1411 | nr_running++; |
| 1412 | t_rate += td->rate; |
| 1413 | m_rate += td->ratemin; |
Jens Axboe | 71a751c | 2006-06-08 14:48:47 +0200 | [diff] [blame] | 1414 | } else if (td->runstate < TD_RUNNING) |
| 1415 | nr_pending++; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1416 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1417 | if (elapsed >= 3) |
| 1418 | eta_secs[i] = thread_eta(td, elapsed); |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 1419 | else |
| 1420 | eta_secs[i] = INT_MAX; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1421 | |
| 1422 | check_str_update(td); |
| 1423 | } |
| 1424 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1425 | if (exitall_on_terminate) |
| 1426 | eta_sec = INT_MAX; |
| 1427 | else |
| 1428 | eta_sec = 0; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1429 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1430 | for (i = 0; i < thread_number; i++) { |
| 1431 | if (exitall_on_terminate) { |
| 1432 | if (eta_secs[i] < eta_sec) |
| 1433 | eta_sec = eta_secs[i]; |
| 1434 | } else { |
| 1435 | if (eta_secs[i] > eta_sec) |
| 1436 | eta_sec = eta_secs[i]; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1437 | } |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1438 | } |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1439 | |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 1440 | if (eta_sec != INT_MAX && elapsed) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1441 | perc = (double) elapsed / (double) (elapsed + eta_sec); |
| 1442 | eta_to_str(eta_str, eta_sec); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1443 | } |
| 1444 | |
Jens Axboe | 71a751c | 2006-06-08 14:48:47 +0200 | [diff] [blame] | 1445 | if (!nr_running && !nr_pending) |
Jens Axboe | f44c1d4 | 2006-06-08 14:46:24 +0200 | [diff] [blame] | 1446 | return; |
| 1447 | |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 1448 | printf("Threads running: %d", nr_running); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1449 | if (m_rate || t_rate) |
| 1450 | printf(", commitrate %d/%dKiB/sec", t_rate, m_rate); |
Jens Axboe | 3052145 | 2006-07-28 15:05:34 +0200 | [diff] [blame] | 1451 | if (eta_sec != INT_MAX && nr_running) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1452 | perc *= 100.0; |
| 1453 | printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str); |
| 1454 | } |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 1455 | printf("\r"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1456 | fflush(stdout); |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1457 | free(eta_secs); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1458 | } |
| 1459 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1460 | /* |
| 1461 | * Run over the job map and reap the threads that have exited, if any. |
| 1462 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1463 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
| 1464 | { |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1465 | int i, cputhreads; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1466 | |
| 1467 | /* |
| 1468 | * reap exited threads (TD_EXITED -> TD_REAPED) |
| 1469 | */ |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1470 | for (i = 0, cputhreads = 0; i < thread_number; i++) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1471 | struct thread_data *td = &threads[i]; |
| 1472 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1473 | if (td->io_ops->flags & FIO_CPUIO) |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1474 | cputhreads++; |
| 1475 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1476 | if (td->runstate != TD_EXITED) |
| 1477 | continue; |
| 1478 | |
| 1479 | td_set_runstate(td, TD_REAPED); |
| 1480 | |
| 1481 | if (td->use_thread) { |
| 1482 | long ret; |
| 1483 | |
| 1484 | if (pthread_join(td->thread, (void *) &ret)) |
| 1485 | perror("thread_join"); |
| 1486 | } else |
| 1487 | waitpid(td->pid, NULL, 0); |
| 1488 | |
| 1489 | (*nr_running)--; |
| 1490 | (*m_rate) -= td->ratemin; |
| 1491 | (*t_rate) -= td->rate; |
| 1492 | } |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 1493 | |
| 1494 | if (*nr_running == cputhreads) |
| 1495 | terminate_threads(TERMINATE_ALL); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1496 | } |
| 1497 | |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1498 | static void fio_unpin_memory(void *pinned) |
| 1499 | { |
| 1500 | if (pinned) { |
| 1501 | if (munlock(pinned, mlock_size) < 0) |
| 1502 | perror("munlock"); |
| 1503 | munmap(pinned, mlock_size); |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | static void *fio_pin_memory(void) |
| 1508 | { |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 1509 | unsigned long long phys_mem; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1510 | void *ptr; |
| 1511 | |
| 1512 | if (!mlock_size) |
| 1513 | return NULL; |
| 1514 | |
| 1515 | /* |
| 1516 | * Don't allow mlock of more than real_mem-128MB |
| 1517 | */ |
Jens Axboe | 32cd46a | 2006-06-07 13:40:40 +0200 | [diff] [blame] | 1518 | phys_mem = os_phys_mem(); |
| 1519 | if (phys_mem) { |
| 1520 | if ((mlock_size + 128 * 1024 * 1024) > phys_mem) { |
| 1521 | mlock_size = phys_mem - 128 * 1024 * 1024; |
Jens Axboe | eb8bbf4 | 2006-06-08 21:40:11 +0200 | [diff] [blame] | 1522 | 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] | 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
| 1527 | if (!ptr) { |
| 1528 | perror("malloc locked mem"); |
| 1529 | return NULL; |
| 1530 | } |
| 1531 | if (mlock(ptr, mlock_size) < 0) { |
| 1532 | munmap(ptr, mlock_size); |
| 1533 | perror("mlock"); |
| 1534 | return NULL; |
| 1535 | } |
| 1536 | |
| 1537 | return ptr; |
| 1538 | } |
| 1539 | |
Jens Axboe | 906c8d7 | 2006-06-13 09:37:56 +0200 | [diff] [blame] | 1540 | /* |
| 1541 | * Main function for kicking off and reaping jobs, as needed. |
| 1542 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1543 | static void run_threads(void) |
| 1544 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1545 | struct thread_data *td; |
| 1546 | unsigned long spent; |
| 1547 | int i, todo, nr_running, m_rate, t_rate, nr_started; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1548 | void *mlocked_mem; |
| 1549 | |
| 1550 | mlocked_mem = fio_pin_memory(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1551 | |
Jens Axboe | c6ae0a5 | 2006-06-12 11:04:44 +0200 | [diff] [blame] | 1552 | if (!terse_output) { |
| 1553 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); |
| 1554 | fflush(stdout); |
| 1555 | } |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 1556 | |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame] | 1557 | signal(SIGINT, sig_handler); |
| 1558 | signal(SIGALRM, sig_handler); |
| 1559 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1560 | todo = thread_number; |
| 1561 | nr_running = 0; |
| 1562 | nr_started = 0; |
| 1563 | m_rate = t_rate = 0; |
| 1564 | |
| 1565 | for (i = 0; i < thread_number; i++) { |
| 1566 | td = &threads[i]; |
| 1567 | |
| 1568 | run_str[td->thread_number - 1] = 'P'; |
| 1569 | |
| 1570 | init_disk_util(td); |
| 1571 | |
| 1572 | if (!td->create_serialize) |
| 1573 | continue; |
| 1574 | |
| 1575 | /* |
| 1576 | * do file setup here so it happens sequentially, |
| 1577 | * we don't want X number of threads getting their |
| 1578 | * client data interspersed on disk |
| 1579 | */ |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame^] | 1580 | if (setup_files(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1581 | td_set_runstate(td, TD_REAPED); |
| 1582 | todo--; |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | gettimeofday(&genesis, NULL); |
| 1587 | |
| 1588 | while (todo) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1589 | struct thread_data *map[MAX_JOBS]; |
| 1590 | struct timeval this_start; |
| 1591 | int this_jobs = 0, left; |
| 1592 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1593 | /* |
| 1594 | * create threads (TD_NOT_CREATED -> TD_CREATED) |
| 1595 | */ |
| 1596 | for (i = 0; i < thread_number; i++) { |
| 1597 | td = &threads[i]; |
| 1598 | |
| 1599 | if (td->runstate != TD_NOT_CREATED) |
| 1600 | continue; |
| 1601 | |
| 1602 | /* |
| 1603 | * never got a chance to start, killed by other |
| 1604 | * thread for some reason |
| 1605 | */ |
| 1606 | if (td->terminate) { |
| 1607 | todo--; |
| 1608 | continue; |
| 1609 | } |
| 1610 | |
| 1611 | if (td->start_delay) { |
| 1612 | spent = mtime_since_now(&genesis); |
| 1613 | |
| 1614 | if (td->start_delay * 1000 > spent) |
| 1615 | continue; |
| 1616 | } |
| 1617 | |
| 1618 | if (td->stonewall && (nr_started || nr_running)) |
| 1619 | break; |
| 1620 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1621 | /* |
| 1622 | * Set state to created. Thread will transition |
| 1623 | * to TD_INITIALIZED when it's done setting up. |
| 1624 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1625 | td_set_runstate(td, TD_CREATED); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1626 | map[this_jobs++] = td; |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1627 | fio_sem_init(&startup_sem, 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1628 | nr_started++; |
| 1629 | |
| 1630 | if (td->use_thread) { |
| 1631 | if (pthread_create(&td->thread, NULL, thread_main, td)) { |
| 1632 | perror("thread_create"); |
| 1633 | nr_started--; |
| 1634 | } |
| 1635 | } else { |
| 1636 | if (fork()) |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1637 | fio_sem_down(&startup_sem); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1638 | else { |
| 1639 | fork_main(shm_id, i); |
| 1640 | exit(0); |
| 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | /* |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1646 | * Wait for the started threads to transition to |
| 1647 | * TD_INITIALIZED. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1648 | */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1649 | gettimeofday(&this_start, NULL); |
| 1650 | left = this_jobs; |
| 1651 | while (left) { |
| 1652 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) |
| 1653 | break; |
| 1654 | |
| 1655 | usleep(100000); |
| 1656 | |
| 1657 | for (i = 0; i < this_jobs; i++) { |
| 1658 | td = map[i]; |
| 1659 | if (!td) |
| 1660 | continue; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1661 | if (td->runstate == TD_INITIALIZED) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1662 | map[i] = NULL; |
| 1663 | left--; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1664 | } else if (td->runstate >= TD_EXITED) { |
| 1665 | map[i] = NULL; |
| 1666 | left--; |
| 1667 | todo--; |
| 1668 | nr_running++; /* work-around... */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | if (left) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 1674 | log_err("fio: %d jobs failed to start\n", left); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1675 | for (i = 0; i < this_jobs; i++) { |
| 1676 | td = map[i]; |
| 1677 | if (!td) |
| 1678 | continue; |
| 1679 | kill(td->pid, SIGTERM); |
| 1680 | } |
| 1681 | break; |
| 1682 | } |
| 1683 | |
| 1684 | /* |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1685 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1686 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1687 | for (i = 0; i < thread_number; i++) { |
| 1688 | td = &threads[i]; |
| 1689 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1690 | if (td->runstate != TD_INITIALIZED) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1691 | continue; |
| 1692 | |
| 1693 | td_set_runstate(td, TD_RUNNING); |
| 1694 | nr_running++; |
| 1695 | nr_started--; |
| 1696 | m_rate += td->ratemin; |
| 1697 | t_rate += td->rate; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1698 | todo--; |
Jens Axboe | bbfd6b0 | 2006-06-07 19:42:54 +0200 | [diff] [blame] | 1699 | fio_sem_up(&td->mutex); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1703 | |
| 1704 | if (todo) |
| 1705 | usleep(100000); |
| 1706 | } |
| 1707 | |
| 1708 | while (nr_running) { |
| 1709 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 1710 | usleep(10000); |
| 1711 | } |
| 1712 | |
| 1713 | update_io_ticks(); |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 1714 | fio_unpin_memory(mlocked_mem); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1715 | } |
| 1716 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1717 | int main(int argc, char *argv[]) |
| 1718 | { |
| 1719 | if (parse_options(argc, argv)) |
| 1720 | return 1; |
| 1721 | |
| 1722 | if (!thread_number) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 1723 | log_err("Nothing to do\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1724 | return 1; |
| 1725 | } |
| 1726 | |
| 1727 | disk_util_timer_arm(); |
| 1728 | |
| 1729 | run_threads(); |
| 1730 | show_run_stats(); |
| 1731 | |
| 1732 | return 0; |
| 1733 | } |