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