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> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | #include <signal.h> |
| 28 | #include <time.h> |
| 29 | #include <math.h> |
| 30 | #include <assert.h> |
| 31 | #include <dirent.h> |
| 32 | #include <libgen.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/wait.h> |
| 36 | #include <sys/ipc.h> |
| 37 | #include <sys/shm.h> |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <sys/mman.h> |
| 40 | |
| 41 | #include "fio.h" |
| 42 | #include "os.h" |
| 43 | |
| 44 | #define MASK (4095) |
| 45 | |
| 46 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) |
| 47 | |
| 48 | int groupid = 0; |
| 49 | int thread_number = 0; |
| 50 | static char run_str[MAX_JOBS + 1]; |
| 51 | int shm_id = 0; |
| 52 | static LIST_HEAD(disk_list); |
| 53 | static struct itimerval itimer; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 54 | static struct timeval genesis; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 55 | |
| 56 | static void update_io_ticks(void); |
| 57 | static void disk_util_timer_arm(void); |
| 58 | static void print_thread_status(void); |
| 59 | |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 60 | extern unsigned long long mlock_size; |
| 61 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 62 | /* |
| 63 | * thread life cycle |
| 64 | */ |
| 65 | enum { |
| 66 | TD_NOT_CREATED = 0, |
| 67 | TD_CREATED, |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 68 | TD_INITIALIZED, |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 69 | TD_RUNNING, |
| 70 | TD_VERIFYING, |
| 71 | TD_EXITED, |
| 72 | TD_REAPED, |
| 73 | }; |
| 74 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 75 | #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] | 76 | |
| 77 | static sem_t startup_sem; |
| 78 | |
| 79 | #define TERMINATE_ALL (-1) |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 80 | #define JOB_START_TIMEOUT (5 * 1000) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 81 | |
| 82 | static void terminate_threads(int group_id) |
| 83 | { |
| 84 | int i; |
| 85 | |
| 86 | for (i = 0; i < thread_number; i++) { |
| 87 | struct thread_data *td = &threads[i]; |
| 88 | |
| 89 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { |
| 90 | td->terminate = 1; |
| 91 | td->start_delay = 0; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | static void sig_handler(int sig) |
| 97 | { |
| 98 | switch (sig) { |
| 99 | case SIGALRM: |
| 100 | update_io_ticks(); |
| 101 | disk_util_timer_arm(); |
| 102 | print_thread_status(); |
| 103 | break; |
| 104 | default: |
| 105 | printf("\nfio: terminating on signal\n"); |
| 106 | fflush(stdout); |
| 107 | terminate_threads(TERMINATE_ALL); |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static unsigned long utime_since(struct timeval *s, struct timeval *e) |
| 113 | { |
| 114 | double sec, usec; |
| 115 | |
| 116 | sec = e->tv_sec - s->tv_sec; |
| 117 | usec = e->tv_usec - s->tv_usec; |
| 118 | if (sec > 0 && usec < 0) { |
| 119 | sec--; |
| 120 | usec += 1000000; |
| 121 | } |
| 122 | |
| 123 | sec *= (double) 1000000; |
| 124 | |
| 125 | return sec + usec; |
| 126 | } |
| 127 | |
| 128 | static unsigned long utime_since_now(struct timeval *s) |
| 129 | { |
| 130 | struct timeval t; |
| 131 | |
| 132 | gettimeofday(&t, NULL); |
| 133 | return utime_since(s, &t); |
| 134 | } |
| 135 | |
| 136 | static unsigned long mtime_since(struct timeval *s, struct timeval *e) |
| 137 | { |
| 138 | double sec, usec; |
| 139 | |
| 140 | sec = e->tv_sec - s->tv_sec; |
| 141 | usec = e->tv_usec - s->tv_usec; |
| 142 | if (sec > 0 && usec < 0) { |
| 143 | sec--; |
| 144 | usec += 1000000; |
| 145 | } |
| 146 | |
| 147 | sec *= (double) 1000; |
| 148 | usec /= (double) 1000; |
| 149 | |
| 150 | return sec + usec; |
| 151 | } |
| 152 | |
| 153 | static unsigned long mtime_since_now(struct timeval *s) |
| 154 | { |
| 155 | struct timeval t; |
| 156 | |
| 157 | gettimeofday(&t, NULL); |
| 158 | return mtime_since(s, &t); |
| 159 | } |
| 160 | |
| 161 | static inline unsigned long msec_now(struct timeval *s) |
| 162 | { |
| 163 | return s->tv_sec * 1000 + s->tv_usec / 1000; |
| 164 | } |
| 165 | |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 166 | static unsigned long time_since_now(struct timeval *s) |
| 167 | { |
| 168 | return mtime_since_now(s) / 1000; |
| 169 | } |
| 170 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 171 | static int random_map_free(struct thread_data *td, unsigned long long block) |
| 172 | { |
| 173 | unsigned int idx = RAND_MAP_IDX(td, block); |
| 174 | unsigned int bit = RAND_MAP_BIT(td, block); |
| 175 | |
| 176 | return (td->file_map[idx] & (1UL << bit)) == 0; |
| 177 | } |
| 178 | |
| 179 | static int get_next_free_block(struct thread_data *td, unsigned long long *b) |
| 180 | { |
| 181 | int i; |
| 182 | |
| 183 | *b = 0; |
| 184 | i = 0; |
| 185 | while ((*b) * td->min_bs < td->io_size) { |
| 186 | if (td->file_map[i] != -1UL) { |
| 187 | *b += ffz(td->file_map[i]); |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | *b += BLOCKS_PER_MAP; |
| 192 | i++; |
| 193 | } |
| 194 | |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | static void mark_random_map(struct thread_data *td, struct io_u *io_u) |
| 199 | { |
Jens Axboe | 200bc85 | 2006-06-01 16:10:12 +0200 | [diff] [blame] | 200 | 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] | 201 | unsigned int blocks = 0; |
| 202 | |
| 203 | while (blocks < (io_u->buflen / td->min_bs)) { |
| 204 | unsigned int idx, bit; |
| 205 | |
| 206 | if (!random_map_free(td, block)) |
| 207 | break; |
| 208 | |
| 209 | idx = RAND_MAP_IDX(td, block); |
| 210 | bit = RAND_MAP_BIT(td, block); |
| 211 | |
| 212 | assert(idx < td->num_maps); |
| 213 | |
| 214 | td->file_map[idx] |= (1UL << bit); |
| 215 | block++; |
| 216 | blocks++; |
| 217 | } |
| 218 | |
| 219 | if ((blocks * td->min_bs) < io_u->buflen) |
| 220 | io_u->buflen = blocks * td->min_bs; |
| 221 | } |
| 222 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 223 | static inline void add_stat_sample(struct io_stat *is, unsigned long val) |
| 224 | { |
| 225 | if (val > is->max_val) |
| 226 | is->max_val = val; |
| 227 | if (val < is->min_val) |
| 228 | is->min_val = val; |
| 229 | |
| 230 | is->val += val; |
| 231 | is->val_sq += val * val; |
| 232 | is->samples++; |
| 233 | } |
| 234 | |
| 235 | static void add_log_sample(struct thread_data *td, struct io_log *iolog, |
| 236 | unsigned long val, int ddir) |
| 237 | { |
| 238 | if (iolog->nr_samples == iolog->max_samples) { |
| 239 | int new_size = sizeof(struct io_sample) * iolog->max_samples*2; |
| 240 | |
| 241 | iolog->log = realloc(iolog->log, new_size); |
| 242 | iolog->max_samples <<= 1; |
| 243 | } |
| 244 | |
| 245 | iolog->log[iolog->nr_samples].val = val; |
| 246 | iolog->log[iolog->nr_samples].time = mtime_since_now(&td->epoch); |
| 247 | iolog->log[iolog->nr_samples].ddir = ddir; |
| 248 | iolog->nr_samples++; |
| 249 | } |
| 250 | |
| 251 | static void add_clat_sample(struct thread_data *td, int ddir,unsigned long msec) |
| 252 | { |
| 253 | add_stat_sample(&td->clat_stat[ddir], msec); |
| 254 | |
| 255 | if (td->clat_log) |
| 256 | add_log_sample(td, td->clat_log, msec, ddir); |
| 257 | } |
| 258 | |
| 259 | static void add_slat_sample(struct thread_data *td, int ddir,unsigned long msec) |
| 260 | { |
| 261 | add_stat_sample(&td->slat_stat[ddir], msec); |
| 262 | |
| 263 | if (td->slat_log) |
| 264 | add_log_sample(td, td->slat_log, msec, ddir); |
| 265 | } |
| 266 | |
| 267 | static void add_bw_sample(struct thread_data *td, int ddir) |
| 268 | { |
| 269 | unsigned long spent = mtime_since_now(&td->stat_sample_time[ddir]); |
| 270 | unsigned long rate; |
| 271 | |
| 272 | if (spent < td->bw_avg_time) |
| 273 | return; |
| 274 | |
| 275 | rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent; |
| 276 | add_stat_sample(&td->bw_stat[ddir], rate); |
| 277 | |
| 278 | if (td->bw_log) |
| 279 | add_log_sample(td, td->bw_log, rate, ddir); |
| 280 | |
| 281 | gettimeofday(&td->stat_sample_time[ddir], NULL); |
| 282 | td->stat_io_bytes[ddir] = td->this_io_bytes[ddir]; |
| 283 | } |
| 284 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 285 | static int get_next_offset(struct thread_data *td, unsigned long long *offset) |
| 286 | { |
| 287 | unsigned long long b, rb; |
| 288 | long r; |
| 289 | |
| 290 | if (!td->sequential) { |
Jens Axboe | 085227a | 2006-06-01 15:54:09 -0700 | [diff] [blame] | 291 | unsigned long long max_blocks = td->io_size / td->min_bs; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 292 | int loops = 50; |
| 293 | |
| 294 | do { |
| 295 | lrand48_r(&td->random_state, &r); |
Jens Axboe | 085227a | 2006-06-01 15:54:09 -0700 | [diff] [blame] | 296 | b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0)); |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 297 | rb = b + (td->file_offset / td->min_bs); |
| 298 | loops--; |
| 299 | } while (!random_map_free(td, rb) && loops); |
| 300 | |
| 301 | if (!loops) { |
| 302 | if (get_next_free_block(td, &b)) |
| 303 | return 1; |
| 304 | } |
| 305 | } else |
| 306 | b = td->last_pos / td->min_bs; |
| 307 | |
| 308 | *offset = (b * td->min_bs) + td->file_offset; |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 309 | if (*offset > td->real_file_size) |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 310 | return 1; |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static unsigned int get_next_buflen(struct thread_data *td) |
| 316 | { |
| 317 | unsigned int buflen; |
| 318 | long r; |
| 319 | |
| 320 | if (td->min_bs == td->max_bs) |
| 321 | buflen = td->min_bs; |
| 322 | else { |
| 323 | lrand48_r(&td->bsrange_state, &r); |
| 324 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); |
| 325 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); |
| 326 | } |
| 327 | |
| 328 | if (buflen > td->io_size - td->this_io_bytes[td->ddir]) |
| 329 | buflen = td->io_size - td->this_io_bytes[td->ddir]; |
| 330 | |
| 331 | return buflen; |
| 332 | } |
| 333 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 334 | /* |
| 335 | * busy looping version for the last few usec |
| 336 | */ |
| 337 | static void __usec_sleep(unsigned int usec) |
| 338 | { |
| 339 | struct timeval start; |
| 340 | |
| 341 | gettimeofday(&start, NULL); |
| 342 | while (utime_since_now(&start) < usec) |
| 343 | nop; |
| 344 | } |
| 345 | |
| 346 | static void usec_sleep(struct thread_data *td, unsigned long usec) |
| 347 | { |
| 348 | struct timespec req, rem; |
| 349 | |
| 350 | req.tv_sec = usec / 1000000; |
| 351 | req.tv_nsec = usec * 1000 - req.tv_sec * 1000000; |
| 352 | |
| 353 | do { |
| 354 | if (usec < 5000) { |
| 355 | __usec_sleep(usec); |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | rem.tv_sec = rem.tv_nsec = 0; |
| 360 | if (nanosleep(&req, &rem) < 0) |
| 361 | break; |
| 362 | |
| 363 | if ((rem.tv_sec + rem.tv_nsec) == 0) |
| 364 | break; |
| 365 | |
| 366 | req.tv_nsec = rem.tv_nsec; |
| 367 | req.tv_sec = rem.tv_sec; |
| 368 | |
| 369 | usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000; |
| 370 | } while (!td->terminate); |
| 371 | } |
| 372 | |
| 373 | static void rate_throttle(struct thread_data *td, unsigned long time_spent, |
| 374 | unsigned int bytes) |
| 375 | { |
| 376 | unsigned long usec_cycle; |
| 377 | |
| 378 | if (!td->rate) |
| 379 | return; |
| 380 | |
| 381 | usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs); |
| 382 | |
| 383 | if (time_spent < usec_cycle) { |
| 384 | unsigned long s = usec_cycle - time_spent; |
| 385 | |
| 386 | td->rate_pending_usleep += s; |
| 387 | if (td->rate_pending_usleep >= 100000) { |
| 388 | usec_sleep(td, td->rate_pending_usleep); |
| 389 | td->rate_pending_usleep = 0; |
| 390 | } |
| 391 | } else { |
| 392 | long overtime = time_spent - usec_cycle; |
| 393 | |
| 394 | td->rate_pending_usleep -= overtime; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
| 399 | { |
| 400 | unsigned long spent; |
| 401 | unsigned long rate; |
| 402 | int ddir = td->ddir; |
| 403 | |
| 404 | /* |
| 405 | * allow a 2 second settle period in the beginning |
| 406 | */ |
| 407 | if (mtime_since(&td->start, now) < 2000) |
| 408 | return 0; |
| 409 | |
| 410 | /* |
| 411 | * if rate blocks is set, sample is running |
| 412 | */ |
| 413 | if (td->rate_bytes) { |
| 414 | spent = mtime_since(&td->lastrate, now); |
| 415 | if (spent < td->ratecycle) |
| 416 | return 0; |
| 417 | |
| 418 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; |
| 419 | if (rate < td->ratemin) { |
| 420 | printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate); |
| 421 | if (rate_quit) |
| 422 | terminate_threads(td->groupid); |
| 423 | return 1; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | td->rate_bytes = td->this_io_bytes[ddir]; |
| 428 | memcpy(&td->lastrate, now, sizeof(*now)); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
| 433 | { |
| 434 | if (!td->timeout) |
| 435 | return 0; |
| 436 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) |
| 437 | return 1; |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static void fill_random_bytes(struct thread_data *td, |
| 443 | unsigned char *p, unsigned int len) |
| 444 | { |
| 445 | unsigned int todo; |
| 446 | double r; |
| 447 | |
| 448 | while (len) { |
| 449 | drand48_r(&td->verify_state, &r); |
| 450 | |
| 451 | /* |
| 452 | * lrand48_r seems to be broken and only fill the bottom |
| 453 | * 32-bits, even on 64-bit archs with 64-bit longs |
| 454 | */ |
| 455 | todo = sizeof(r); |
| 456 | if (todo > len) |
| 457 | todo = len; |
| 458 | |
| 459 | memcpy(p, &r, todo); |
| 460 | |
| 461 | len -= todo; |
| 462 | p += todo; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static void hexdump(void *buffer, int len) |
| 467 | { |
| 468 | unsigned char *p = buffer; |
| 469 | int i; |
| 470 | |
| 471 | for (i = 0; i < len; i++) |
| 472 | printf("%02x", p[i]); |
| 473 | printf("\n"); |
| 474 | } |
| 475 | |
| 476 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u) |
| 477 | { |
| 478 | unsigned char *p = (unsigned char *) io_u->buf; |
| 479 | unsigned long c; |
| 480 | int ret; |
| 481 | |
| 482 | p += sizeof(*hdr); |
| 483 | c = crc32(p, hdr->len - sizeof(*hdr)); |
| 484 | ret = c != hdr->crc32; |
| 485 | |
| 486 | if (ret) { |
| 487 | fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
| 488 | fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c); |
| 489 | } |
| 490 | |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u) |
| 495 | { |
| 496 | unsigned char *p = (unsigned char *) io_u->buf; |
| 497 | struct md5_ctx md5_ctx; |
| 498 | int ret; |
| 499 | |
| 500 | memset(&md5_ctx, 0, sizeof(md5_ctx)); |
| 501 | p += sizeof(*hdr); |
| 502 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); |
| 503 | |
| 504 | ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); |
| 505 | if (ret) { |
| 506 | fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); |
| 507 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); |
| 508 | hexdump(md5_ctx.hash, sizeof(md5_ctx.hash)); |
| 509 | } |
| 510 | |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | static int verify_io_u(struct io_u *io_u) |
| 515 | { |
| 516 | struct verify_header *hdr = (struct verify_header *) io_u->buf; |
| 517 | int ret; |
| 518 | |
| 519 | if (hdr->fio_magic != FIO_HDR_MAGIC) |
| 520 | return 1; |
| 521 | |
| 522 | if (hdr->verify_type == VERIFY_MD5) |
| 523 | ret = verify_io_u_md5(hdr, io_u); |
| 524 | else if (hdr->verify_type == VERIFY_CRC32) |
| 525 | ret = verify_io_u_crc32(hdr, io_u); |
| 526 | else { |
| 527 | fprintf(stderr, "Bad verify type %d\n", hdr->verify_type); |
| 528 | ret = 1; |
| 529 | } |
| 530 | |
| 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) |
| 535 | { |
| 536 | hdr->crc32 = crc32(p, len); |
| 537 | } |
| 538 | |
| 539 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) |
| 540 | { |
| 541 | struct md5_ctx md5_ctx; |
| 542 | |
| 543 | memset(&md5_ctx, 0, sizeof(md5_ctx)); |
| 544 | md5_update(&md5_ctx, p, len); |
| 545 | memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); |
| 546 | } |
| 547 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 548 | static int get_rw_ddir(struct thread_data *td) |
| 549 | { |
Jens Axboe | a6ccc7b | 2006-06-02 10:14:15 +0200 | [diff] [blame] | 550 | if (td_rw(td)) { |
| 551 | struct timeval now; |
| 552 | unsigned long elapsed; |
| 553 | |
| 554 | gettimeofday(&now, NULL); |
| 555 | elapsed = mtime_since_now(&td->rwmix_switch); |
| 556 | |
| 557 | /* |
| 558 | * Check if it's time to seed a new data direction. |
| 559 | */ |
| 560 | if (elapsed >= td->rwmixcycle) { |
| 561 | unsigned long v; |
| 562 | long r; |
| 563 | |
| 564 | lrand48_r(&td->random_state, &r); |
| 565 | v = 100UL * r / (unsigned long) (RAND_MAX + 1.0); |
| 566 | if (v < td->rwmixread) |
| 567 | td->rwmix_ddir = DDIR_READ; |
| 568 | else |
| 569 | td->rwmix_ddir = DDIR_WRITE; |
| 570 | memcpy(&td->rwmix_switch, &now, sizeof(now)); |
| 571 | } |
| 572 | return td->rwmix_ddir; |
| 573 | } else if (td_read(td)) |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 574 | return DDIR_READ; |
| 575 | else |
| 576 | return DDIR_WRITE; |
| 577 | } |
| 578 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 579 | /* |
| 580 | * fill body of io_u->buf with random data and add a header with the |
| 581 | * (eg) sha1sum of that data. |
| 582 | */ |
| 583 | static void populate_io_u(struct thread_data *td, struct io_u *io_u) |
| 584 | { |
| 585 | unsigned char *p = (unsigned char *) io_u->buf; |
| 586 | struct verify_header hdr; |
| 587 | |
| 588 | hdr.fio_magic = FIO_HDR_MAGIC; |
| 589 | hdr.len = io_u->buflen; |
| 590 | p += sizeof(hdr); |
| 591 | fill_random_bytes(td, p, io_u->buflen - sizeof(hdr)); |
| 592 | |
| 593 | if (td->verify == VERIFY_MD5) { |
| 594 | fill_md5(&hdr, p, io_u->buflen - sizeof(hdr)); |
| 595 | hdr.verify_type = VERIFY_MD5; |
| 596 | } else { |
| 597 | fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr)); |
| 598 | hdr.verify_type = VERIFY_CRC32; |
| 599 | } |
| 600 | |
| 601 | memcpy(io_u->buf, &hdr, sizeof(hdr)); |
| 602 | } |
| 603 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 604 | 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] | 605 | { |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 606 | if (td->io_prep && td->io_prep(td, io_u)) |
| 607 | return 1; |
| 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 612 | 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] | 613 | { |
| 614 | list_del(&io_u->list); |
| 615 | list_add(&io_u->list, &td->io_u_freelist); |
| 616 | td->cur_depth--; |
| 617 | } |
| 618 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 619 | static void write_iolog_put(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 620 | { |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 621 | fprintf(td->iolog_f, "%d,%llu,%u\n", io_u->ddir, io_u->offset, io_u->buflen); |
| 622 | } |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 623 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 624 | static int read_iolog_get(struct thread_data *td, struct io_u *io_u) |
| 625 | { |
| 626 | struct io_piece *ipo; |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 627 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 628 | if (!list_empty(&td->io_log_list)) { |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 629 | ipo = list_entry(td->io_log_list.next, struct io_piece, list); |
| 630 | list_del(&ipo->list); |
| 631 | io_u->offset = ipo->offset; |
| 632 | io_u->buflen = ipo->len; |
| 633 | io_u->ddir = ipo->ddir; |
| 634 | free(ipo); |
| 635 | return 0; |
| 636 | } |
| 637 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 638 | return 1; |
| 639 | } |
| 640 | |
| 641 | static int fill_io_u(struct thread_data *td, struct io_u *io_u) |
| 642 | { |
| 643 | /* |
| 644 | * If using an iolog, grab next piece if any available. |
| 645 | */ |
| 646 | if (td->read_iolog) |
| 647 | return read_iolog_get(td, io_u); |
| 648 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 649 | /* |
| 650 | * No log, let the seq/rand engine retrieve the next position. |
| 651 | */ |
| 652 | if (!get_next_offset(td, &io_u->offset)) { |
| 653 | io_u->buflen = get_next_buflen(td); |
| 654 | |
| 655 | if (io_u->buflen) { |
| 656 | io_u->ddir = get_rw_ddir(td); |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 657 | |
| 658 | /* |
| 659 | * If using a write iolog, store this entry. |
| 660 | */ |
| 661 | if (td->write_iolog) |
| 662 | write_iolog_put(td, io_u); |
| 663 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 664 | return 0; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | return 1; |
| 669 | } |
| 670 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 671 | #define queue_full(td) (list_empty(&(td)->io_u_freelist)) |
| 672 | |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 673 | struct io_u *__get_io_u(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 674 | { |
| 675 | struct io_u *io_u; |
| 676 | |
| 677 | if (queue_full(td)) |
| 678 | return NULL; |
| 679 | |
| 680 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); |
| 681 | io_u->error = 0; |
| 682 | io_u->resid = 0; |
| 683 | list_del(&io_u->list); |
| 684 | list_add(&io_u->list, &td->io_u_busylist); |
| 685 | td->cur_depth++; |
| 686 | return io_u; |
| 687 | } |
| 688 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 689 | static struct io_u *get_io_u(struct thread_data *td) |
| 690 | { |
| 691 | struct io_u *io_u; |
| 692 | |
| 693 | io_u = __get_io_u(td); |
| 694 | if (!io_u) |
| 695 | return NULL; |
| 696 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 697 | if (td->zone_bytes >= td->zone_size) { |
| 698 | td->zone_bytes = 0; |
| 699 | td->last_pos += td->zone_skip; |
| 700 | } |
| 701 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 702 | if (fill_io_u(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 703 | put_io_u(td, io_u); |
| 704 | return NULL; |
| 705 | } |
| 706 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 707 | if (io_u->buflen + io_u->offset > td->real_file_size) |
| 708 | io_u->buflen = td->real_file_size - io_u->offset; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 709 | |
| 710 | if (!io_u->buflen) { |
| 711 | put_io_u(td, io_u); |
| 712 | return NULL; |
| 713 | } |
| 714 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 715 | if (!td->read_iolog && !td->sequential) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 716 | mark_random_map(td, io_u); |
| 717 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 718 | td->last_pos += io_u->buflen; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 719 | |
| 720 | if (td->verify != VERIFY_NONE) |
| 721 | populate_io_u(td, io_u); |
| 722 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 723 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 724 | put_io_u(td, io_u); |
| 725 | return NULL; |
| 726 | } |
| 727 | |
| 728 | gettimeofday(&io_u->start_time, NULL); |
| 729 | return io_u; |
| 730 | } |
| 731 | |
| 732 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
| 733 | { |
| 734 | td->old_runstate = td->runstate; |
| 735 | td->runstate = runstate; |
| 736 | } |
| 737 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 738 | 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] | 739 | { |
| 740 | struct io_piece *ipo; |
| 741 | |
| 742 | if (list_empty(&td->io_hist_list)) |
| 743 | return 1; |
| 744 | |
| 745 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); |
| 746 | list_del(&ipo->list); |
| 747 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 748 | io_u->offset = ipo->offset; |
| 749 | io_u->buflen = ipo->len; |
| 750 | io_u->ddir = DDIR_READ; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 751 | free(ipo); |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static void prune_io_piece_log(struct thread_data *td) |
| 756 | { |
| 757 | struct io_piece *ipo; |
| 758 | |
| 759 | while (!list_empty(&td->io_hist_list)) { |
| 760 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); |
| 761 | |
| 762 | list_del(&ipo->list); |
| 763 | free(ipo); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * log a succesful write, so we can unwind the log for verify |
| 769 | */ |
| 770 | static void log_io_piece(struct thread_data *td, struct io_u *io_u) |
| 771 | { |
| 772 | struct io_piece *ipo = malloc(sizeof(struct io_piece)); |
| 773 | struct list_head *entry; |
| 774 | |
| 775 | INIT_LIST_HEAD(&ipo->list); |
| 776 | ipo->offset = io_u->offset; |
| 777 | ipo->len = io_u->buflen; |
| 778 | |
| 779 | /* |
| 780 | * for random io where the writes extend the file, it will typically |
| 781 | * be laid out with the block scattered as written. it's faster to |
| 782 | * read them in in that order again, so don't sort |
| 783 | */ |
| 784 | if (td->sequential || !td->overwrite) { |
| 785 | list_add_tail(&ipo->list, &td->io_hist_list); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | * for random io, sort the list so verify will run faster |
| 791 | */ |
| 792 | entry = &td->io_hist_list; |
| 793 | while ((entry = entry->prev) != &td->io_hist_list) { |
| 794 | struct io_piece *__ipo = list_entry(entry, struct io_piece, list); |
| 795 | |
| 796 | if (__ipo->offset < ipo->offset) |
| 797 | break; |
| 798 | } |
| 799 | |
| 800 | list_add(&ipo->list, entry); |
| 801 | } |
| 802 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 803 | static void write_iolog_close(struct thread_data *td) |
| 804 | { |
| 805 | fflush(td->iolog_f); |
| 806 | fclose(td->iolog_f); |
| 807 | free(td->iolog_buf); |
| 808 | } |
| 809 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 810 | static int init_iolog(struct thread_data *td) |
| 811 | { |
| 812 | unsigned long long offset; |
| 813 | unsigned int bytes; |
| 814 | char *str, *p; |
| 815 | FILE *f; |
| 816 | int rw, i, reads, writes; |
| 817 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 818 | if (!td->read_iolog && !td->write_iolog) |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 819 | return 0; |
| 820 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 821 | if (td->read_iolog) |
| 822 | f = fopen(td->iolog_file, "r"); |
| 823 | else |
| 824 | f = fopen(td->iolog_file, "w"); |
| 825 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 826 | if (!f) { |
| 827 | perror("fopen iolog"); |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 828 | printf("file %s, %d/%d\n", td->iolog_file, td->read_iolog, td->write_iolog); |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 829 | return 1; |
| 830 | } |
| 831 | |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 832 | /* |
| 833 | * That's it for writing, setup a log buffer and we're done. |
| 834 | */ |
| 835 | if (td->write_iolog) { |
| 836 | td->iolog_f = f; |
| 837 | td->iolog_buf = malloc(8192); |
| 838 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | /* |
| 843 | * Read in the read iolog and store it, reuse the infrastructure |
| 844 | * for doing verifications. |
| 845 | */ |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 846 | str = malloc(4096); |
| 847 | reads = writes = i = 0; |
| 848 | while ((p = fgets(str, 4096, f)) != NULL) { |
| 849 | struct io_piece *ipo; |
| 850 | |
| 851 | if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) { |
| 852 | fprintf(stderr, "bad iolog: %s\n", p); |
| 853 | continue; |
| 854 | } |
| 855 | if (rw == DDIR_READ) |
| 856 | reads++; |
| 857 | else if (rw == DDIR_WRITE) |
| 858 | writes++; |
| 859 | else { |
| 860 | fprintf(stderr, "bad ddir: %d\n", rw); |
| 861 | continue; |
| 862 | } |
| 863 | |
| 864 | ipo = malloc(sizeof(*ipo)); |
| 865 | INIT_LIST_HEAD(&ipo->list); |
| 866 | ipo->offset = offset; |
| 867 | ipo->len = bytes; |
| 868 | if (bytes > td->max_bs) |
| 869 | td->max_bs = bytes; |
| 870 | ipo->ddir = rw; |
| 871 | list_add_tail(&ipo->list, &td->io_log_list); |
| 872 | i++; |
| 873 | } |
| 874 | |
| 875 | free(str); |
| 876 | fclose(f); |
| 877 | |
| 878 | if (!i) |
| 879 | return 1; |
| 880 | |
| 881 | if (reads && !writes) |
| 882 | td->ddir = DDIR_READ; |
| 883 | else if (!reads && writes) |
| 884 | td->ddir = DDIR_READ; |
| 885 | else |
| 886 | td->iomix = 1; |
| 887 | |
| 888 | return 0; |
| 889 | } |
| 890 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 891 | static int sync_td(struct thread_data *td) |
| 892 | { |
| 893 | if (td->io_sync) |
| 894 | return td->io_sync(td); |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | static int io_u_getevents(struct thread_data *td, int min, int max, |
| 900 | struct timespec *t) |
| 901 | { |
| 902 | return td->io_getevents(td, min, max, t); |
| 903 | } |
| 904 | |
| 905 | static int io_u_queue(struct thread_data *td, struct io_u *io_u) |
| 906 | { |
| 907 | gettimeofday(&io_u->issue_time, NULL); |
| 908 | |
| 909 | return td->io_queue(td, io_u); |
| 910 | } |
| 911 | |
| 912 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) |
| 913 | |
| 914 | static void io_completed(struct thread_data *td, struct io_u *io_u, |
| 915 | struct io_completion_data *icd) |
| 916 | { |
| 917 | struct timeval e; |
| 918 | unsigned long msec; |
| 919 | |
| 920 | gettimeofday(&e, NULL); |
| 921 | |
| 922 | if (!io_u->error) { |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 923 | unsigned int bytes = io_u->buflen - io_u->resid; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 924 | const int idx = io_u->ddir; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 925 | |
| 926 | td->io_blocks[idx]++; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 927 | td->io_bytes[idx] += bytes; |
| 928 | td->zone_bytes += bytes; |
| 929 | td->this_io_bytes[idx] += bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 930 | |
| 931 | msec = mtime_since(&io_u->issue_time, &e); |
| 932 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 933 | add_clat_sample(td, idx, msec); |
| 934 | add_bw_sample(td, idx); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 935 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 936 | if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 937 | log_io_piece(td, io_u); |
| 938 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 939 | icd->bytes_done[idx] += bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 940 | } else |
| 941 | icd->error = io_u->error; |
| 942 | } |
| 943 | |
| 944 | static void ios_completed(struct thread_data *td,struct io_completion_data *icd) |
| 945 | { |
| 946 | struct io_u *io_u; |
| 947 | int i; |
| 948 | |
| 949 | icd->error = 0; |
| 950 | icd->bytes_done[0] = icd->bytes_done[1] = 0; |
| 951 | |
| 952 | for (i = 0; i < icd->nr; i++) { |
| 953 | io_u = td->io_event(td, i); |
| 954 | |
| 955 | io_completed(td, io_u, icd); |
| 956 | put_io_u(td, io_u); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | static void cleanup_pending_aio(struct thread_data *td) |
| 961 | { |
| 962 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 963 | struct list_head *entry, *n; |
| 964 | struct io_completion_data icd; |
| 965 | struct io_u *io_u; |
| 966 | int r; |
| 967 | |
| 968 | /* |
| 969 | * get immediately available events, if any |
| 970 | */ |
| 971 | r = io_u_getevents(td, 0, td->cur_depth, &ts); |
| 972 | if (r > 0) { |
| 973 | icd.nr = r; |
| 974 | ios_completed(td, &icd); |
| 975 | } |
| 976 | |
| 977 | /* |
| 978 | * now cancel remaining active events |
| 979 | */ |
| 980 | if (td->io_cancel) { |
| 981 | list_for_each_safe(entry, n, &td->io_u_busylist) { |
| 982 | io_u = list_entry(entry, struct io_u, list); |
| 983 | |
| 984 | r = td->io_cancel(td, io_u); |
| 985 | if (!r) |
| 986 | put_io_u(td, io_u); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | if (td->cur_depth) { |
| 991 | r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL); |
| 992 | if (r > 0) { |
| 993 | icd.nr = r; |
| 994 | ios_completed(td, &icd); |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | static int do_io_u_verify(struct thread_data *td, struct io_u **io_u) |
| 1000 | { |
| 1001 | struct io_u *v_io_u = *io_u; |
| 1002 | int ret = 0; |
| 1003 | |
| 1004 | if (v_io_u) { |
| 1005 | ret = verify_io_u(v_io_u); |
| 1006 | put_io_u(td, v_io_u); |
| 1007 | *io_u = NULL; |
| 1008 | } |
| 1009 | |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
| 1013 | static void do_verify(struct thread_data *td) |
| 1014 | { |
| 1015 | struct timeval t; |
| 1016 | struct io_u *io_u, *v_io_u = NULL; |
| 1017 | struct io_completion_data icd; |
| 1018 | int ret; |
| 1019 | |
| 1020 | td_set_runstate(td, TD_VERIFYING); |
| 1021 | |
| 1022 | do { |
| 1023 | if (td->terminate) |
| 1024 | break; |
| 1025 | |
| 1026 | gettimeofday(&t, NULL); |
| 1027 | if (runtime_exceeded(td, &t)) |
| 1028 | break; |
| 1029 | |
| 1030 | io_u = __get_io_u(td); |
| 1031 | if (!io_u) |
| 1032 | break; |
| 1033 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1034 | if (get_next_verify(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1035 | put_io_u(td, io_u); |
| 1036 | break; |
| 1037 | } |
| 1038 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1039 | if (td_io_prep(td, io_u)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1040 | put_io_u(td, io_u); |
| 1041 | break; |
| 1042 | } |
| 1043 | |
| 1044 | ret = io_u_queue(td, io_u); |
| 1045 | if (ret) { |
| 1046 | put_io_u(td, io_u); |
| 1047 | td_verror(td, ret); |
| 1048 | break; |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * we have one pending to verify, do that while |
| 1053 | * we are doing io on the next one |
| 1054 | */ |
| 1055 | if (do_io_u_verify(td, &v_io_u)) |
| 1056 | break; |
| 1057 | |
| 1058 | ret = io_u_getevents(td, 1, 1, NULL); |
| 1059 | if (ret != 1) { |
| 1060 | if (ret < 0) |
| 1061 | td_verror(td, ret); |
| 1062 | break; |
| 1063 | } |
| 1064 | |
| 1065 | v_io_u = td->io_event(td, 0); |
| 1066 | icd.nr = 1; |
| 1067 | icd.error = 0; |
| 1068 | io_completed(td, v_io_u, &icd); |
| 1069 | |
| 1070 | if (icd.error) { |
| 1071 | td_verror(td, icd.error); |
| 1072 | put_io_u(td, v_io_u); |
| 1073 | v_io_u = NULL; |
| 1074 | break; |
| 1075 | } |
| 1076 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1077 | /* |
| 1078 | * if we can't submit more io, we need to verify now |
| 1079 | */ |
| 1080 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) |
| 1081 | break; |
| 1082 | |
| 1083 | } while (1); |
| 1084 | |
| 1085 | do_io_u_verify(td, &v_io_u); |
| 1086 | |
| 1087 | if (td->cur_depth) |
| 1088 | cleanup_pending_aio(td); |
| 1089 | |
| 1090 | td_set_runstate(td, TD_RUNNING); |
| 1091 | } |
| 1092 | |
| 1093 | static void do_io(struct thread_data *td) |
| 1094 | { |
| 1095 | struct io_completion_data icd; |
| 1096 | struct timeval s, e; |
| 1097 | unsigned long usec; |
| 1098 | |
| 1099 | while (td->this_io_bytes[td->ddir] < td->io_size) { |
| 1100 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
| 1101 | struct timespec *timeout; |
| 1102 | int ret, min_evts = 0; |
| 1103 | struct io_u *io_u; |
| 1104 | |
| 1105 | if (td->terminate) |
| 1106 | break; |
| 1107 | |
| 1108 | io_u = get_io_u(td); |
| 1109 | if (!io_u) |
| 1110 | break; |
| 1111 | |
| 1112 | memcpy(&s, &io_u->start_time, sizeof(s)); |
| 1113 | |
| 1114 | ret = io_u_queue(td, io_u); |
| 1115 | if (ret) { |
| 1116 | put_io_u(td, io_u); |
| 1117 | td_verror(td, ret); |
| 1118 | break; |
| 1119 | } |
| 1120 | |
| 1121 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); |
| 1122 | |
| 1123 | if (td->cur_depth < td->iodepth) { |
| 1124 | timeout = &ts; |
| 1125 | min_evts = 0; |
| 1126 | } else { |
| 1127 | timeout = NULL; |
| 1128 | min_evts = 1; |
| 1129 | } |
| 1130 | |
| 1131 | ret = io_u_getevents(td, min_evts, td->cur_depth, timeout); |
| 1132 | if (ret < 0) { |
| 1133 | td_verror(td, ret); |
| 1134 | break; |
| 1135 | } else if (!ret) |
| 1136 | continue; |
| 1137 | |
| 1138 | icd.nr = ret; |
| 1139 | ios_completed(td, &icd); |
| 1140 | if (icd.error) { |
| 1141 | td_verror(td, icd.error); |
| 1142 | break; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * the rate is batched for now, it should work for batches |
| 1147 | * of completions except the very first one which may look |
| 1148 | * a little bursty |
| 1149 | */ |
| 1150 | gettimeofday(&e, NULL); |
| 1151 | usec = utime_since(&s, &e); |
| 1152 | |
| 1153 | rate_throttle(td, usec, icd.bytes_done[td->ddir]); |
| 1154 | |
| 1155 | if (check_min_rate(td, &e)) { |
| 1156 | td_verror(td, ENOMEM); |
| 1157 | break; |
| 1158 | } |
| 1159 | |
| 1160 | if (runtime_exceeded(td, &e)) |
| 1161 | break; |
| 1162 | |
| 1163 | if (td->thinktime) |
| 1164 | usec_sleep(td, td->thinktime); |
| 1165 | |
| 1166 | if (should_fsync(td) && td->fsync_blocks && |
| 1167 | (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0) |
| 1168 | sync_td(td); |
| 1169 | } |
| 1170 | |
| 1171 | if (td->cur_depth) |
| 1172 | cleanup_pending_aio(td); |
| 1173 | |
Jens Axboe | fc1a471 | 2006-05-30 13:04:05 +0200 | [diff] [blame] | 1174 | if (should_fsync(td) && td->end_fsync) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1175 | sync_td(td); |
| 1176 | } |
| 1177 | |
| 1178 | static void cleanup_io(struct thread_data *td) |
| 1179 | { |
| 1180 | if (td->io_cleanup) |
| 1181 | td->io_cleanup(td); |
| 1182 | } |
| 1183 | |
| 1184 | static int init_io(struct thread_data *td) |
| 1185 | { |
| 1186 | if (td->io_engine == FIO_SYNCIO) |
| 1187 | return fio_syncio_init(td); |
| 1188 | else if (td->io_engine == FIO_MMAPIO) |
| 1189 | return fio_mmapio_init(td); |
| 1190 | else if (td->io_engine == FIO_LIBAIO) |
| 1191 | return fio_libaio_init(td); |
| 1192 | else if (td->io_engine == FIO_POSIXAIO) |
| 1193 | return fio_posixaio_init(td); |
| 1194 | else if (td->io_engine == FIO_SGIO) |
| 1195 | return fio_sgio_init(td); |
Jens Axboe | 8756e4d | 2006-05-27 20:24:53 +0200 | [diff] [blame] | 1196 | else if (td->io_engine == FIO_SPLICEIO) |
| 1197 | return fio_spliceio_init(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1198 | else { |
| 1199 | fprintf(stderr, "bad io_engine %d\n", td->io_engine); |
| 1200 | return 1; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | static void cleanup_io_u(struct thread_data *td) |
| 1205 | { |
| 1206 | struct list_head *entry, *n; |
| 1207 | struct io_u *io_u; |
| 1208 | |
| 1209 | list_for_each_safe(entry, n, &td->io_u_freelist) { |
| 1210 | io_u = list_entry(entry, struct io_u, list); |
| 1211 | |
| 1212 | list_del(&io_u->list); |
| 1213 | free(io_u); |
| 1214 | } |
| 1215 | |
| 1216 | if (td->mem_type == MEM_MALLOC) |
| 1217 | free(td->orig_buffer); |
| 1218 | else if (td->mem_type == MEM_SHM) { |
| 1219 | struct shmid_ds sbuf; |
| 1220 | |
| 1221 | shmdt(td->orig_buffer); |
| 1222 | shmctl(td->shm_id, IPC_RMID, &sbuf); |
| 1223 | } else if (td->mem_type == MEM_MMAP) |
| 1224 | munmap(td->orig_buffer, td->orig_buffer_size); |
| 1225 | else |
| 1226 | fprintf(stderr, "Bad memory type %d\n", td->mem_type); |
| 1227 | |
| 1228 | td->orig_buffer = NULL; |
| 1229 | } |
| 1230 | |
| 1231 | static int init_io_u(struct thread_data *td) |
| 1232 | { |
| 1233 | struct io_u *io_u; |
| 1234 | int i, max_units; |
| 1235 | char *p; |
| 1236 | |
| 1237 | if (td->io_engine & FIO_SYNCIO) |
| 1238 | max_units = 1; |
| 1239 | else |
| 1240 | max_units = td->iodepth; |
| 1241 | |
| 1242 | td->orig_buffer_size = td->max_bs * max_units + MASK; |
| 1243 | |
| 1244 | if (td->mem_type == MEM_MALLOC) |
| 1245 | td->orig_buffer = malloc(td->orig_buffer_size); |
| 1246 | else if (td->mem_type == MEM_SHM) { |
| 1247 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600); |
| 1248 | if (td->shm_id < 0) { |
| 1249 | td_verror(td, errno); |
| 1250 | perror("shmget"); |
| 1251 | return 1; |
| 1252 | } |
| 1253 | |
| 1254 | td->orig_buffer = shmat(td->shm_id, NULL, 0); |
| 1255 | if (td->orig_buffer == (void *) -1) { |
| 1256 | td_verror(td, errno); |
| 1257 | perror("shmat"); |
| 1258 | td->orig_buffer = NULL; |
| 1259 | return 1; |
| 1260 | } |
| 1261 | } else if (td->mem_type == MEM_MMAP) { |
| 1262 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
| 1263 | if (td->orig_buffer == MAP_FAILED) { |
| 1264 | td_verror(td, errno); |
| 1265 | perror("mmap"); |
| 1266 | td->orig_buffer = NULL; |
| 1267 | return 1; |
| 1268 | } |
| 1269 | } |
| 1270 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1271 | p = ALIGN(td->orig_buffer); |
| 1272 | for (i = 0; i < max_units; i++) { |
| 1273 | io_u = malloc(sizeof(*io_u)); |
| 1274 | memset(io_u, 0, sizeof(*io_u)); |
| 1275 | INIT_LIST_HEAD(&io_u->list); |
| 1276 | |
| 1277 | io_u->buf = p + td->max_bs * i; |
Jens Axboe | b1ff340 | 2006-02-17 11:35:58 +0100 | [diff] [blame] | 1278 | io_u->index = i; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1279 | list_add(&io_u->list, &td->io_u_freelist); |
| 1280 | } |
| 1281 | |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | static int create_file(struct thread_data *td, unsigned long long size, |
| 1286 | int extend) |
| 1287 | { |
| 1288 | unsigned long long left; |
| 1289 | unsigned int bs; |
| 1290 | int r, oflags; |
| 1291 | char *b; |
| 1292 | |
| 1293 | /* |
| 1294 | * unless specifically asked for overwrite, let normal io extend it |
| 1295 | */ |
| 1296 | if (td_write(td) && !td->overwrite) |
| 1297 | return 0; |
| 1298 | |
| 1299 | if (!size) { |
| 1300 | fprintf(stderr, "Need size for create\n"); |
| 1301 | td_verror(td, EINVAL); |
| 1302 | return 1; |
| 1303 | } |
| 1304 | |
| 1305 | if (!extend) { |
| 1306 | oflags = O_CREAT | O_TRUNC; |
| 1307 | printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20); |
| 1308 | } else { |
| 1309 | oflags = O_APPEND; |
| 1310 | printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20); |
| 1311 | } |
| 1312 | |
| 1313 | td->fd = open(td->file_name, O_WRONLY | oflags, 0644); |
| 1314 | if (td->fd < 0) { |
| 1315 | td_verror(td, errno); |
| 1316 | return 1; |
| 1317 | } |
| 1318 | |
| 1319 | if (!extend && ftruncate(td->fd, td->file_size) == -1) { |
| 1320 | td_verror(td, errno); |
| 1321 | return 1; |
| 1322 | } |
| 1323 | |
| 1324 | td->io_size = td->file_size; |
| 1325 | b = malloc(td->max_bs); |
| 1326 | memset(b, 0, td->max_bs); |
| 1327 | |
| 1328 | left = size; |
| 1329 | while (left && !td->terminate) { |
| 1330 | bs = td->max_bs; |
| 1331 | if (bs > left) |
| 1332 | bs = left; |
| 1333 | |
| 1334 | r = write(td->fd, b, bs); |
| 1335 | |
| 1336 | if (r == (int) bs) { |
| 1337 | left -= bs; |
| 1338 | continue; |
| 1339 | } else { |
| 1340 | if (r < 0) |
| 1341 | td_verror(td, errno); |
| 1342 | else |
| 1343 | td_verror(td, EIO); |
| 1344 | |
| 1345 | break; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | if (td->terminate) |
| 1350 | unlink(td->file_name); |
| 1351 | else if (td->create_fsync) |
| 1352 | fsync(td->fd); |
| 1353 | |
| 1354 | close(td->fd); |
| 1355 | td->fd = -1; |
| 1356 | free(b); |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
| 1360 | static int file_size(struct thread_data *td) |
| 1361 | { |
| 1362 | struct stat st; |
| 1363 | |
| 1364 | if (fstat(td->fd, &st) == -1) { |
| 1365 | td_verror(td, errno); |
| 1366 | return 1; |
| 1367 | } |
| 1368 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1369 | td->real_file_size = st.st_size; |
| 1370 | |
| 1371 | if (!td->file_size || td->file_size > td->real_file_size) |
| 1372 | td->file_size = td->real_file_size; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1373 | |
Jens Axboe | e8e387c | 2006-05-05 11:11:22 +0200 | [diff] [blame] | 1374 | td->file_size -= td->file_offset; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | static int bdev_size(struct thread_data *td) |
| 1379 | { |
Jens Axboe | 9104f87 | 2005-12-28 23:50:45 +0100 | [diff] [blame] | 1380 | unsigned long long bytes; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1381 | int r; |
| 1382 | |
| 1383 | r = blockdev_size(td->fd, &bytes); |
| 1384 | if (r) { |
| 1385 | td_verror(td, r); |
| 1386 | return 1; |
| 1387 | } |
| 1388 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1389 | td->real_file_size = bytes; |
| 1390 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1391 | /* |
| 1392 | * no extend possibilities, so limit size to device size if too large |
| 1393 | */ |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1394 | if (!td->file_size || td->file_size > td->real_file_size) |
| 1395 | td->file_size = td->real_file_size; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1396 | |
Jens Axboe | e8e387c | 2006-05-05 11:11:22 +0200 | [diff] [blame] | 1397 | td->file_size -= td->file_offset; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | static int get_file_size(struct thread_data *td) |
| 1402 | { |
Jens Axboe | 0af7b54 | 2006-02-17 10:10:12 +0100 | [diff] [blame] | 1403 | int ret = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1404 | |
| 1405 | if (td->filetype == FIO_TYPE_FILE) |
| 1406 | ret = file_size(td); |
Jens Axboe | 0af7b54 | 2006-02-17 10:10:12 +0100 | [diff] [blame] | 1407 | else if (td->filetype == FIO_TYPE_BD) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1408 | ret = bdev_size(td); |
Jens Axboe | 0af7b54 | 2006-02-17 10:10:12 +0100 | [diff] [blame] | 1409 | else |
| 1410 | td->real_file_size = -1; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1411 | |
| 1412 | if (ret) |
| 1413 | return ret; |
| 1414 | |
Jens Axboe | e8e387c | 2006-05-05 11:11:22 +0200 | [diff] [blame] | 1415 | if (td->file_offset > td->real_file_size) { |
| 1416 | fprintf(stderr, "Client%d: offset extends end (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->real_file_size); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1417 | return 1; |
| 1418 | } |
| 1419 | |
Jens Axboe | 838a3cd | 2006-01-20 12:38:29 +0100 | [diff] [blame] | 1420 | td->io_size = td->file_size; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1421 | if (td->io_size == 0) { |
| 1422 | fprintf(stderr, "Client%d: no io blocks\n", td->thread_number); |
| 1423 | td_verror(td, EINVAL); |
| 1424 | return 1; |
| 1425 | } |
| 1426 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 1427 | if (!td->zone_size) |
| 1428 | td->zone_size = td->io_size; |
| 1429 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1430 | td->total_io_size = td->io_size * td->loops; |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | static int setup_file_mmap(struct thread_data *td) |
| 1435 | { |
| 1436 | int flags; |
| 1437 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1438 | if (td_rw(td)) |
| 1439 | flags = PROT_READ | PROT_WRITE; |
| 1440 | else if (td_write(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1441 | flags = PROT_WRITE; |
| 1442 | |
| 1443 | if (td->verify != VERIFY_NONE) |
| 1444 | flags |= PROT_READ; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1445 | } else |
| 1446 | flags = PROT_READ; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1447 | |
| 1448 | td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset); |
| 1449 | if (td->mmap == MAP_FAILED) { |
| 1450 | td->mmap = NULL; |
| 1451 | td_verror(td, errno); |
| 1452 | return 1; |
| 1453 | } |
| 1454 | |
| 1455 | if (td->invalidate_cache) { |
| 1456 | if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) { |
| 1457 | td_verror(td, errno); |
| 1458 | return 1; |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | if (td->sequential) { |
| 1463 | if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) { |
| 1464 | td_verror(td, errno); |
| 1465 | return 1; |
| 1466 | } |
| 1467 | } else { |
| 1468 | if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) { |
| 1469 | td_verror(td, errno); |
| 1470 | return 1; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
| 1477 | static int setup_file_plain(struct thread_data *td) |
| 1478 | { |
| 1479 | if (td->invalidate_cache) { |
| 1480 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) { |
| 1481 | td_verror(td, errno); |
| 1482 | return 1; |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | if (td->sequential) { |
| 1487 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) { |
| 1488 | td_verror(td, errno); |
| 1489 | return 1; |
| 1490 | } |
| 1491 | } else { |
| 1492 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) { |
| 1493 | td_verror(td, errno); |
| 1494 | return 1; |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | static int setup_file(struct thread_data *td) |
| 1502 | { |
| 1503 | struct stat st; |
| 1504 | int flags = 0; |
| 1505 | |
| 1506 | if (stat(td->file_name, &st) == -1) { |
| 1507 | if (errno != ENOENT) { |
| 1508 | td_verror(td, errno); |
| 1509 | return 1; |
| 1510 | } |
| 1511 | if (!td->create_file) { |
| 1512 | td_verror(td, ENOENT); |
| 1513 | return 1; |
| 1514 | } |
| 1515 | if (create_file(td, td->file_size, 0)) |
| 1516 | return 1; |
| 1517 | } else if (td->filetype == FIO_TYPE_FILE) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 1518 | if (st.st_size < (off_t) td->file_size) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1519 | if (create_file(td, td->file_size - st.st_size, 1)) |
| 1520 | return 1; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | if (td->odirect) |
| 1525 | flags |= O_DIRECT; |
| 1526 | |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1527 | if (td_write(td) || td_rw(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1528 | if (td->filetype == FIO_TYPE_FILE) { |
| 1529 | if (!td->overwrite) |
| 1530 | flags |= O_TRUNC; |
| 1531 | |
| 1532 | flags |= O_CREAT; |
| 1533 | } |
| 1534 | if (td->sync_io) |
| 1535 | flags |= O_SYNC; |
| 1536 | |
| 1537 | flags |= O_RDWR; |
| 1538 | |
| 1539 | td->fd = open(td->file_name, flags, 0600); |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1540 | } else { |
| 1541 | if (td->filetype == FIO_TYPE_CHAR) |
| 1542 | flags |= O_RDWR; |
| 1543 | else |
| 1544 | flags |= O_RDONLY; |
| 1545 | |
| 1546 | td->fd = open(td->file_name, flags); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | if (td->fd == -1) { |
| 1550 | td_verror(td, errno); |
| 1551 | return 1; |
| 1552 | } |
| 1553 | |
| 1554 | if (get_file_size(td)) |
| 1555 | return 1; |
| 1556 | |
| 1557 | if (td->io_engine != FIO_MMAPIO) |
| 1558 | return setup_file_plain(td); |
| 1559 | else |
| 1560 | return setup_file_mmap(td); |
| 1561 | } |
| 1562 | |
| 1563 | static int check_dev_match(dev_t dev, char *path) |
| 1564 | { |
| 1565 | unsigned int major, minor; |
| 1566 | char line[256], *p; |
| 1567 | FILE *f; |
| 1568 | |
| 1569 | f = fopen(path, "r"); |
| 1570 | if (!f) { |
| 1571 | perror("open path"); |
| 1572 | return 1; |
| 1573 | } |
| 1574 | |
| 1575 | p = fgets(line, sizeof(line), f); |
| 1576 | if (!p) { |
| 1577 | fclose(f); |
| 1578 | return 1; |
| 1579 | } |
| 1580 | |
| 1581 | if (sscanf(p, "%u:%u", &major, &minor) != 2) { |
| 1582 | fclose(f); |
| 1583 | return 1; |
| 1584 | } |
| 1585 | |
| 1586 | if (((major << 8) | minor) == dev) { |
| 1587 | fclose(f); |
| 1588 | return 0; |
| 1589 | } |
| 1590 | |
| 1591 | fclose(f); |
| 1592 | return 1; |
| 1593 | } |
| 1594 | |
| 1595 | static int find_block_dir(dev_t dev, char *path) |
| 1596 | { |
| 1597 | struct dirent *dir; |
| 1598 | struct stat st; |
| 1599 | int found = 0; |
| 1600 | DIR *D; |
| 1601 | |
| 1602 | D = opendir(path); |
| 1603 | if (!D) |
| 1604 | return 0; |
| 1605 | |
| 1606 | while ((dir = readdir(D)) != NULL) { |
| 1607 | char full_path[256]; |
| 1608 | |
| 1609 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 1610 | continue; |
| 1611 | if (!strcmp(dir->d_name, "device")) |
| 1612 | continue; |
| 1613 | |
| 1614 | sprintf(full_path, "%s/%s", path, dir->d_name); |
| 1615 | |
| 1616 | if (!strcmp(dir->d_name, "dev")) { |
| 1617 | if (!check_dev_match(dev, full_path)) { |
| 1618 | found = 1; |
| 1619 | break; |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | if (stat(full_path, &st) == -1) { |
| 1624 | perror("stat"); |
| 1625 | break; |
| 1626 | } |
| 1627 | |
| 1628 | if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) |
| 1629 | continue; |
| 1630 | |
| 1631 | found = find_block_dir(dev, full_path); |
| 1632 | if (found) { |
| 1633 | strcpy(path, full_path); |
| 1634 | break; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | closedir(D); |
| 1639 | return found; |
| 1640 | } |
| 1641 | |
| 1642 | static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus) |
| 1643 | { |
| 1644 | unsigned in_flight; |
| 1645 | char line[256]; |
| 1646 | FILE *f; |
| 1647 | char *p; |
| 1648 | |
| 1649 | f = fopen(du->path, "r"); |
| 1650 | if (!f) |
| 1651 | return 1; |
| 1652 | |
| 1653 | p = fgets(line, sizeof(line), f); |
| 1654 | if (!p) { |
| 1655 | fclose(f); |
| 1656 | return 1; |
| 1657 | } |
| 1658 | |
| 1659 | if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) { |
| 1660 | fclose(f); |
| 1661 | return 1; |
| 1662 | } |
| 1663 | |
| 1664 | fclose(f); |
| 1665 | return 0; |
| 1666 | } |
| 1667 | |
| 1668 | static void update_io_tick_disk(struct disk_util *du) |
| 1669 | { |
| 1670 | struct disk_util_stat __dus, *dus, *ldus; |
| 1671 | struct timeval t; |
| 1672 | |
| 1673 | if (get_io_ticks(du, &__dus)) |
| 1674 | return; |
| 1675 | |
| 1676 | dus = &du->dus; |
| 1677 | ldus = &du->last_dus; |
| 1678 | |
| 1679 | dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]); |
| 1680 | dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]); |
| 1681 | dus->ios[0] += (__dus.ios[0] - ldus->ios[0]); |
| 1682 | dus->ios[1] += (__dus.ios[1] - ldus->ios[1]); |
| 1683 | dus->merges[0] += (__dus.merges[0] - ldus->merges[0]); |
| 1684 | dus->merges[1] += (__dus.merges[1] - ldus->merges[1]); |
| 1685 | dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]); |
| 1686 | dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]); |
| 1687 | dus->io_ticks += (__dus.io_ticks - ldus->io_ticks); |
| 1688 | dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue); |
| 1689 | |
| 1690 | gettimeofday(&t, NULL); |
| 1691 | du->msec += mtime_since(&du->time, &t); |
| 1692 | memcpy(&du->time, &t, sizeof(t)); |
| 1693 | memcpy(ldus, &__dus, sizeof(__dus)); |
| 1694 | } |
| 1695 | |
| 1696 | static void update_io_ticks(void) |
| 1697 | { |
| 1698 | struct list_head *entry; |
| 1699 | struct disk_util *du; |
| 1700 | |
| 1701 | list_for_each(entry, &disk_list) { |
| 1702 | du = list_entry(entry, struct disk_util, list); |
| 1703 | update_io_tick_disk(du); |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | static int disk_util_exists(dev_t dev) |
| 1708 | { |
| 1709 | struct list_head *entry; |
| 1710 | struct disk_util *du; |
| 1711 | |
| 1712 | list_for_each(entry, &disk_list) { |
| 1713 | du = list_entry(entry, struct disk_util, list); |
| 1714 | |
| 1715 | if (du->dev == dev) |
| 1716 | return 1; |
| 1717 | } |
| 1718 | |
| 1719 | return 0; |
| 1720 | } |
| 1721 | |
| 1722 | static void disk_util_add(dev_t dev, char *path) |
| 1723 | { |
| 1724 | struct disk_util *du = malloc(sizeof(*du)); |
| 1725 | |
| 1726 | memset(du, 0, sizeof(*du)); |
| 1727 | INIT_LIST_HEAD(&du->list); |
| 1728 | sprintf(du->path, "%s/stat", path); |
| 1729 | du->name = strdup(basename(path)); |
| 1730 | du->dev = dev; |
| 1731 | |
| 1732 | gettimeofday(&du->time, NULL); |
| 1733 | get_io_ticks(du, &du->last_dus); |
| 1734 | |
| 1735 | list_add_tail(&du->list, &disk_list); |
| 1736 | } |
| 1737 | |
| 1738 | static void init_disk_util(struct thread_data *td) |
| 1739 | { |
| 1740 | struct stat st; |
| 1741 | char foo[256], tmp[256]; |
| 1742 | dev_t dev; |
| 1743 | char *p; |
| 1744 | |
| 1745 | if (!td->do_disk_util) |
| 1746 | return; |
| 1747 | |
| 1748 | if (!stat(td->file_name, &st)) { |
| 1749 | if (S_ISBLK(st.st_mode)) |
| 1750 | dev = st.st_rdev; |
| 1751 | else |
| 1752 | dev = st.st_dev; |
| 1753 | } else { |
| 1754 | /* |
| 1755 | * must be a file, open "." in that path |
| 1756 | */ |
| 1757 | strcpy(foo, td->file_name); |
| 1758 | p = dirname(foo); |
| 1759 | if (stat(p, &st)) { |
| 1760 | perror("disk util stat"); |
| 1761 | return; |
| 1762 | } |
| 1763 | |
| 1764 | dev = st.st_dev; |
| 1765 | } |
| 1766 | |
| 1767 | if (disk_util_exists(dev)) |
| 1768 | return; |
| 1769 | |
| 1770 | sprintf(foo, "/sys/block"); |
| 1771 | if (!find_block_dir(dev, foo)) |
| 1772 | return; |
| 1773 | |
| 1774 | /* |
Jens Axboe | d07f439 | 2006-05-31 20:43:01 +0200 | [diff] [blame] | 1775 | * If there's a ../queue/ directory there, we are inside a partition. |
| 1776 | * Check if that is the case and jump back. For loop/md/dm etc we |
| 1777 | * are already in the right spot. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1778 | */ |
Jens Axboe | d07f439 | 2006-05-31 20:43:01 +0200 | [diff] [blame] | 1779 | sprintf(tmp, "%s/../queue", foo); |
| 1780 | if (!stat(tmp, &st)) { |
| 1781 | p = dirname(foo); |
| 1782 | sprintf(tmp, "%s/queue", p); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1783 | if (stat(tmp, &st)) { |
Jens Axboe | d07f439 | 2006-05-31 20:43:01 +0200 | [diff] [blame] | 1784 | fprintf(stderr, "unknown sysfs layout\n"); |
| 1785 | return; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1786 | } |
Jens Axboe | d07f439 | 2006-05-31 20:43:01 +0200 | [diff] [blame] | 1787 | sprintf(foo, "%s", p); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | disk_util_add(dev, foo); |
| 1791 | } |
| 1792 | |
| 1793 | static void disk_util_timer_arm(void) |
| 1794 | { |
| 1795 | itimer.it_value.tv_sec = 0; |
| 1796 | itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000; |
| 1797 | setitimer(ITIMER_REAL, &itimer, NULL); |
| 1798 | } |
| 1799 | |
| 1800 | static void clear_io_state(struct thread_data *td) |
| 1801 | { |
| 1802 | if (td->io_engine == FIO_SYNCIO) |
| 1803 | lseek(td->fd, SEEK_SET, 0); |
| 1804 | |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 1805 | td->last_pos = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1806 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; |
| 1807 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; |
Jens Axboe | 20dc95c | 2005-12-09 10:29:35 +0100 | [diff] [blame] | 1808 | td->zone_bytes = 0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1809 | |
| 1810 | if (td->file_map) |
| 1811 | memset(td->file_map, 0, td->num_maps * sizeof(long)); |
| 1812 | } |
| 1813 | |
| 1814 | static void update_rusage_stat(struct thread_data *td) |
| 1815 | { |
| 1816 | if (!(td->runtime[0] + td->runtime[1])) |
| 1817 | return; |
| 1818 | |
| 1819 | getrusage(RUSAGE_SELF, &td->ru_end); |
| 1820 | |
| 1821 | td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime); |
| 1822 | td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime); |
| 1823 | td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw); |
| 1824 | |
| 1825 | |
| 1826 | memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end)); |
| 1827 | } |
| 1828 | |
| 1829 | static void *thread_main(void *data) |
| 1830 | { |
| 1831 | struct thread_data *td = data; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1832 | |
| 1833 | if (!td->use_thread) |
| 1834 | setsid(); |
| 1835 | |
| 1836 | td->pid = getpid(); |
| 1837 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1838 | INIT_LIST_HEAD(&td->io_u_freelist); |
| 1839 | INIT_LIST_HEAD(&td->io_u_busylist); |
| 1840 | INIT_LIST_HEAD(&td->io_hist_list); |
| 1841 | INIT_LIST_HEAD(&td->io_log_list); |
| 1842 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1843 | if (init_io_u(td)) |
| 1844 | goto err; |
| 1845 | |
| 1846 | if (fio_setaffinity(td) == -1) { |
| 1847 | td_verror(td, errno); |
| 1848 | goto err; |
| 1849 | } |
| 1850 | |
| 1851 | if (init_io(td)) |
| 1852 | goto err; |
| 1853 | |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1854 | if (init_iolog(td)) |
| 1855 | goto err; |
| 1856 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1857 | if (td->ioprio) { |
| 1858 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
| 1859 | td_verror(td, errno); |
| 1860 | goto err; |
| 1861 | } |
| 1862 | } |
| 1863 | |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 1864 | if (nice(td->nice) < 0) { |
| 1865 | td_verror(td, errno); |
| 1866 | goto err; |
| 1867 | } |
| 1868 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 1869 | if (init_random_state(td)) |
| 1870 | goto err; |
| 1871 | |
| 1872 | td_set_runstate(td, TD_INITIALIZED); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1873 | sem_post(&startup_sem); |
| 1874 | sem_wait(&td->mutex); |
| 1875 | |
| 1876 | if (!td->create_serialize && setup_file(td)) |
| 1877 | goto err; |
| 1878 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1879 | gettimeofday(&td->epoch, NULL); |
| 1880 | |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame^] | 1881 | if (td->exec_prerun) |
| 1882 | system(td->exec_prerun); |
| 1883 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1884 | while (td->loops--) { |
| 1885 | getrusage(RUSAGE_SELF, &td->ru_start); |
| 1886 | gettimeofday(&td->start, NULL); |
| 1887 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); |
| 1888 | |
| 1889 | if (td->ratemin) |
| 1890 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); |
| 1891 | |
| 1892 | clear_io_state(td); |
| 1893 | prune_io_piece_log(td); |
| 1894 | |
| 1895 | do_io(td); |
| 1896 | |
| 1897 | td->runtime[td->ddir] += mtime_since_now(&td->start); |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 1898 | if (td_rw(td) && td->io_bytes[td->ddir ^ 1]) |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 1899 | td->runtime[td->ddir ^ 1] = td->runtime[td->ddir]; |
| 1900 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1901 | update_rusage_stat(td); |
| 1902 | |
| 1903 | if (td->error || td->terminate) |
| 1904 | break; |
| 1905 | |
| 1906 | if (td->verify == VERIFY_NONE) |
| 1907 | continue; |
| 1908 | |
| 1909 | clear_io_state(td); |
| 1910 | gettimeofday(&td->start, NULL); |
| 1911 | |
| 1912 | do_verify(td); |
| 1913 | |
| 1914 | td->runtime[DDIR_READ] += mtime_since_now(&td->start); |
| 1915 | |
| 1916 | if (td->error || td->terminate) |
| 1917 | break; |
| 1918 | } |
| 1919 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1920 | if (td->bw_log) |
| 1921 | finish_log(td, td->bw_log, "bw"); |
| 1922 | if (td->slat_log) |
| 1923 | finish_log(td, td->slat_log, "slat"); |
| 1924 | if (td->clat_log) |
| 1925 | finish_log(td, td->clat_log, "clat"); |
Jens Axboe | 843a741 | 2006-06-01 21:14:21 -0700 | [diff] [blame] | 1926 | if (td->write_iolog) |
| 1927 | write_iolog_close(td); |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame^] | 1928 | if (td->exec_postrun) |
| 1929 | system(td->exec_postrun); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1930 | |
| 1931 | if (exitall_on_terminate) |
| 1932 | terminate_threads(td->groupid); |
| 1933 | |
| 1934 | err: |
| 1935 | if (td->fd != -1) { |
| 1936 | close(td->fd); |
| 1937 | td->fd = -1; |
| 1938 | } |
| 1939 | if (td->mmap) |
| 1940 | munmap(td->mmap, td->file_size); |
Jens Axboe | ef899b6 | 2006-06-06 09:31:00 +0200 | [diff] [blame] | 1941 | if (td->directory) |
| 1942 | free(td->directory); |
| 1943 | if (td->iolog_file) |
| 1944 | free(td->iolog_file); |
Jens Axboe | 4e0ba8a | 2006-06-06 09:36:28 +0200 | [diff] [blame^] | 1945 | if (td->exec_prerun) |
| 1946 | free(td->exec_prerun); |
| 1947 | if (td->exec_postrun) |
| 1948 | free(td->exec_postrun); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1949 | cleanup_io(td); |
| 1950 | cleanup_io_u(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1951 | td_set_runstate(td, TD_EXITED); |
| 1952 | return NULL; |
| 1953 | |
| 1954 | } |
| 1955 | |
| 1956 | static void *fork_main(int shmid, int offset) |
| 1957 | { |
| 1958 | struct thread_data *td; |
| 1959 | void *data; |
| 1960 | |
| 1961 | data = shmat(shmid, NULL, 0); |
| 1962 | if (data == (void *) -1) { |
| 1963 | perror("shmat"); |
| 1964 | return NULL; |
| 1965 | } |
| 1966 | |
| 1967 | td = data + offset * sizeof(struct thread_data); |
| 1968 | thread_main(td); |
| 1969 | shmdt(data); |
| 1970 | return NULL; |
| 1971 | } |
| 1972 | |
| 1973 | static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max, |
| 1974 | double *mean, double *dev) |
| 1975 | { |
| 1976 | double n; |
| 1977 | |
| 1978 | if (is->samples == 0) |
| 1979 | return 0; |
| 1980 | |
| 1981 | *min = is->min_val; |
| 1982 | *max = is->max_val; |
| 1983 | |
| 1984 | n = (double) is->samples; |
| 1985 | *mean = (double) is->val / n; |
| 1986 | *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1)); |
| 1987 | if (!(*min + *max) && !(*mean + *dev)) |
| 1988 | return 0; |
| 1989 | |
| 1990 | return 1; |
| 1991 | } |
| 1992 | |
| 1993 | static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs, |
| 1994 | int ddir) |
| 1995 | { |
| 1996 | char *ddir_str[] = { "read ", "write" }; |
Jens Axboe | 200bc85 | 2006-06-01 16:10:12 +0200 | [diff] [blame] | 1997 | unsigned long min, max; |
| 1998 | unsigned long long bw; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1999 | double mean, dev; |
| 2000 | |
| 2001 | if (!td->runtime[ddir]) |
| 2002 | return; |
| 2003 | |
| 2004 | bw = td->io_bytes[ddir] / td->runtime[ddir]; |
Jens Axboe | 200bc85 | 2006-06-01 16:10:12 +0200 | [diff] [blame] | 2005 | printf(" %s: io=%6lluMiB, bw=%6lluKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2006 | |
| 2007 | if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev)) |
| 2008 | printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); |
| 2009 | |
| 2010 | if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev)) |
| 2011 | printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); |
| 2012 | |
| 2013 | if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) { |
| 2014 | double p_of_agg; |
| 2015 | |
| 2016 | p_of_agg = mean * 100 / (double) rs->agg[ddir]; |
| 2017 | printf(" bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, dev=%5.02f\n", min, max, p_of_agg, mean, dev); |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | static void show_thread_status(struct thread_data *td, |
| 2022 | struct group_run_stats *rs) |
| 2023 | { |
| 2024 | double usr_cpu, sys_cpu; |
| 2025 | |
| 2026 | if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error) |
| 2027 | return; |
| 2028 | |
| 2029 | printf("Client%d (groupid=%d): err=%2d:\n", td->thread_number, td->groupid, td->error); |
| 2030 | |
| 2031 | show_ddir_status(td, rs, td->ddir); |
Jens Axboe | aea47d4 | 2006-05-26 19:27:29 +0200 | [diff] [blame] | 2032 | if (td->io_bytes[td->ddir ^ 1]) |
| 2033 | show_ddir_status(td, rs, td->ddir ^ 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2034 | |
| 2035 | if (td->runtime[0] + td->runtime[1]) { |
| 2036 | double runt = td->runtime[0] + td->runtime[1]; |
| 2037 | |
| 2038 | usr_cpu = (double) td->usr_time * 100 / runt; |
| 2039 | sys_cpu = (double) td->sys_time * 100 / runt; |
| 2040 | } else { |
| 2041 | usr_cpu = 0; |
| 2042 | sys_cpu = 0; |
| 2043 | } |
| 2044 | |
| 2045 | printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx); |
| 2046 | } |
| 2047 | |
| 2048 | static void check_str_update(struct thread_data *td) |
| 2049 | { |
| 2050 | char c = run_str[td->thread_number - 1]; |
| 2051 | |
| 2052 | if (td->runstate == td->old_runstate) |
| 2053 | return; |
| 2054 | |
| 2055 | switch (td->runstate) { |
| 2056 | case TD_REAPED: |
| 2057 | c = '_'; |
| 2058 | break; |
| 2059 | case TD_EXITED: |
| 2060 | c = 'E'; |
| 2061 | break; |
| 2062 | case TD_RUNNING: |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 2063 | if (td_rw(td)) { |
| 2064 | if (td->sequential) |
| 2065 | c = 'M'; |
| 2066 | else |
| 2067 | c = 'm'; |
| 2068 | } else if (td_read(td)) { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2069 | if (td->sequential) |
| 2070 | c = 'R'; |
| 2071 | else |
| 2072 | c = 'r'; |
| 2073 | } else { |
| 2074 | if (td->sequential) |
| 2075 | c = 'W'; |
| 2076 | else |
| 2077 | c = 'w'; |
| 2078 | } |
| 2079 | break; |
| 2080 | case TD_VERIFYING: |
| 2081 | c = 'V'; |
| 2082 | break; |
| 2083 | case TD_CREATED: |
| 2084 | c = 'C'; |
| 2085 | break; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2086 | case TD_INITIALIZED: |
| 2087 | c = 'I'; |
| 2088 | break; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2089 | case TD_NOT_CREATED: |
| 2090 | c = 'P'; |
| 2091 | break; |
| 2092 | default: |
| 2093 | printf("state %d\n", td->runstate); |
| 2094 | } |
| 2095 | |
| 2096 | run_str[td->thread_number - 1] = c; |
| 2097 | td->old_runstate = td->runstate; |
| 2098 | } |
| 2099 | |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2100 | static void eta_to_str(char *str, int eta_sec) |
| 2101 | { |
| 2102 | unsigned int d, h, m, s; |
| 2103 | static int always_d, always_h; |
| 2104 | |
| 2105 | d = h = m = s = 0; |
| 2106 | |
| 2107 | s = eta_sec % 60; |
| 2108 | eta_sec /= 60; |
| 2109 | m = eta_sec % 60; |
| 2110 | eta_sec /= 60; |
| 2111 | h = eta_sec % 24; |
| 2112 | eta_sec /= 24; |
| 2113 | d = eta_sec; |
| 2114 | |
| 2115 | if (d || always_d) { |
| 2116 | always_d = 1; |
| 2117 | str += sprintf(str, "%02dd:", d); |
| 2118 | } |
| 2119 | if (h || always_h) { |
| 2120 | always_h = 1; |
| 2121 | str += sprintf(str, "%02dh:", h); |
| 2122 | } |
| 2123 | |
| 2124 | str += sprintf(str, "%02dm:", m); |
| 2125 | str += sprintf(str, "%02ds", s); |
| 2126 | } |
| 2127 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2128 | static int thread_eta(struct thread_data *td, unsigned long elapsed) |
| 2129 | { |
| 2130 | unsigned long long bytes_total, bytes_done; |
| 2131 | unsigned int eta_sec = 0; |
| 2132 | |
| 2133 | bytes_total = td->total_io_size; |
Jens Axboe | 3d60d1e | 2006-05-25 06:31:06 +0200 | [diff] [blame] | 2134 | |
| 2135 | /* |
| 2136 | * if writing, bytes_total will be twice the size. If mixing, |
| 2137 | * assume a 50/50 split and thus bytes_total will be 50% larger. |
| 2138 | */ |
| 2139 | if (td->verify) { |
| 2140 | if (td_rw(td)) |
| 2141 | bytes_total = bytes_total * 3 / 2; |
| 2142 | else |
| 2143 | bytes_total <<= 1; |
| 2144 | } |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2145 | if (td->zone_size && td->zone_skip) |
| 2146 | bytes_total /= (td->zone_skip / td->zone_size); |
| 2147 | |
| 2148 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) { |
| 2149 | double perc; |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 2150 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2151 | bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]; |
| 2152 | perc = (double) bytes_done / (double) bytes_total; |
| 2153 | if (perc > 1.0) |
| 2154 | perc = 1.0; |
| 2155 | |
| 2156 | eta_sec = (elapsed * (1.0 / perc)) - elapsed; |
| 2157 | |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 2158 | if (td->timeout && eta_sec > (td->timeout - elapsed)) |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2159 | eta_sec = td->timeout - elapsed; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2160 | } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED |
| 2161 | || td->runstate == TD_INITIALIZED) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2162 | int t_eta = 0, r_eta = 0; |
| 2163 | |
| 2164 | /* |
| 2165 | * We can only guess - assume it'll run the full timeout |
| 2166 | * if given, otherwise assume it'll run at the specified rate. |
| 2167 | */ |
| 2168 | if (td->timeout) |
| 2169 | t_eta = td->timeout + td->start_delay - elapsed; |
| 2170 | if (td->rate) { |
| 2171 | r_eta = (bytes_total / 1024) / td->rate; |
| 2172 | r_eta += td->start_delay - elapsed; |
| 2173 | } |
| 2174 | |
| 2175 | if (r_eta && t_eta) |
| 2176 | eta_sec = min(r_eta, t_eta); |
| 2177 | else if (r_eta) |
| 2178 | eta_sec = r_eta; |
| 2179 | else if (t_eta) |
| 2180 | eta_sec = t_eta; |
| 2181 | else |
| 2182 | eta_sec = INT_MAX; |
| 2183 | } else { |
| 2184 | /* |
| 2185 | * thread is already done |
| 2186 | */ |
| 2187 | eta_sec = 0; |
| 2188 | } |
| 2189 | |
| 2190 | return eta_sec; |
| 2191 | } |
| 2192 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2193 | static void print_thread_status(void) |
| 2194 | { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2195 | unsigned long elapsed = time_since_now(&genesis); |
| 2196 | int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2197 | char eta_str[32]; |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2198 | double perc = 0.0; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2199 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2200 | eta_secs = malloc(thread_number * sizeof(int)); |
| 2201 | memset(eta_secs, 0, thread_number * sizeof(int)); |
| 2202 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2203 | nr_running = t_rate = m_rate = 0; |
| 2204 | for (i = 0; i < thread_number; i++) { |
| 2205 | struct thread_data *td = &threads[i]; |
| 2206 | |
| 2207 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){ |
| 2208 | nr_running++; |
| 2209 | t_rate += td->rate; |
| 2210 | m_rate += td->ratemin; |
| 2211 | } |
| 2212 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2213 | if (elapsed >= 3) |
| 2214 | eta_secs[i] = thread_eta(td, elapsed); |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 2215 | else |
| 2216 | eta_secs[i] = INT_MAX; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2217 | |
| 2218 | check_str_update(td); |
| 2219 | } |
| 2220 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2221 | if (exitall_on_terminate) |
| 2222 | eta_sec = INT_MAX; |
| 2223 | else |
| 2224 | eta_sec = 0; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2225 | |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2226 | for (i = 0; i < thread_number; i++) { |
| 2227 | if (exitall_on_terminate) { |
| 2228 | if (eta_secs[i] < eta_sec) |
| 2229 | eta_sec = eta_secs[i]; |
| 2230 | } else { |
| 2231 | if (eta_secs[i] > eta_sec) |
| 2232 | eta_sec = eta_secs[i]; |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2233 | } |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2234 | } |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2235 | |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 2236 | if (eta_sec != INT_MAX && elapsed) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2237 | perc = (double) elapsed / (double) (elapsed + eta_sec); |
| 2238 | eta_to_str(eta_str, eta_sec); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2239 | } |
| 2240 | |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2241 | printf("Threads now running (%d)", nr_running); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2242 | if (m_rate || t_rate) |
| 2243 | printf(", commitrate %d/%dKiB/sec", t_rate, m_rate); |
Jens Axboe | 8b611c3 | 2006-05-25 01:46:59 +0200 | [diff] [blame] | 2244 | if (eta_sec != INT_MAX) { |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2245 | perc *= 100.0; |
| 2246 | printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str); |
| 2247 | } |
Jens Axboe | 5289b84 | 2005-12-08 20:47:28 +0100 | [diff] [blame] | 2248 | printf("\r"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2249 | fflush(stdout); |
Jens Axboe | 6a0106a | 2006-05-05 10:34:43 +0200 | [diff] [blame] | 2250 | free(eta_secs); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
| 2254 | { |
| 2255 | int i; |
| 2256 | |
| 2257 | /* |
| 2258 | * reap exited threads (TD_EXITED -> TD_REAPED) |
| 2259 | */ |
| 2260 | for (i = 0; i < thread_number; i++) { |
| 2261 | struct thread_data *td = &threads[i]; |
| 2262 | |
| 2263 | if (td->runstate != TD_EXITED) |
| 2264 | continue; |
| 2265 | |
| 2266 | td_set_runstate(td, TD_REAPED); |
| 2267 | |
| 2268 | if (td->use_thread) { |
| 2269 | long ret; |
| 2270 | |
| 2271 | if (pthread_join(td->thread, (void *) &ret)) |
| 2272 | perror("thread_join"); |
| 2273 | } else |
| 2274 | waitpid(td->pid, NULL, 0); |
| 2275 | |
| 2276 | (*nr_running)--; |
| 2277 | (*m_rate) -= td->ratemin; |
| 2278 | (*t_rate) -= td->rate; |
| 2279 | } |
| 2280 | } |
| 2281 | |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 2282 | static void fio_unpin_memory(void *pinned) |
| 2283 | { |
| 2284 | if (pinned) { |
| 2285 | if (munlock(pinned, mlock_size) < 0) |
| 2286 | perror("munlock"); |
| 2287 | munmap(pinned, mlock_size); |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | static void *fio_pin_memory(void) |
| 2292 | { |
| 2293 | long pagesize, pages; |
| 2294 | void *ptr; |
| 2295 | |
| 2296 | if (!mlock_size) |
| 2297 | return NULL; |
| 2298 | |
| 2299 | /* |
| 2300 | * Don't allow mlock of more than real_mem-128MB |
| 2301 | */ |
| 2302 | pagesize = sysconf(_SC_PAGESIZE); |
| 2303 | pages = sysconf(_SC_PHYS_PAGES); |
| 2304 | if (pages != -1 && pagesize != -1) { |
| 2305 | unsigned long long real_mem = pages * pagesize; |
| 2306 | |
| 2307 | if ((mlock_size + 128 * 1024 * 1024) > real_mem) { |
| 2308 | mlock_size = real_mem - 128 * 1024 * 1024; |
| 2309 | printf("fio: limiting mlocked memory to %lluMiB\n", |
| 2310 | mlock_size >> 20); |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
| 2315 | if (!ptr) { |
| 2316 | perror("malloc locked mem"); |
| 2317 | return NULL; |
| 2318 | } |
| 2319 | if (mlock(ptr, mlock_size) < 0) { |
| 2320 | munmap(ptr, mlock_size); |
| 2321 | perror("mlock"); |
| 2322 | return NULL; |
| 2323 | } |
| 2324 | |
| 2325 | return ptr; |
| 2326 | } |
| 2327 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2328 | static void run_threads(void) |
| 2329 | { |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2330 | struct thread_data *td; |
| 2331 | unsigned long spent; |
| 2332 | int i, todo, nr_running, m_rate, t_rate, nr_started; |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 2333 | void *mlocked_mem; |
| 2334 | |
| 2335 | mlocked_mem = fio_pin_memory(); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2336 | |
| 2337 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 2338 | fflush(stdout); |
Jens Axboe | c04f7ec | 2006-05-31 10:13:16 +0200 | [diff] [blame] | 2339 | |
Jens Axboe | 4efa970 | 2006-05-31 11:56:49 +0200 | [diff] [blame] | 2340 | signal(SIGINT, sig_handler); |
| 2341 | signal(SIGALRM, sig_handler); |
| 2342 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2343 | todo = thread_number; |
| 2344 | nr_running = 0; |
| 2345 | nr_started = 0; |
| 2346 | m_rate = t_rate = 0; |
| 2347 | |
| 2348 | for (i = 0; i < thread_number; i++) { |
| 2349 | td = &threads[i]; |
| 2350 | |
| 2351 | run_str[td->thread_number - 1] = 'P'; |
| 2352 | |
| 2353 | init_disk_util(td); |
| 2354 | |
| 2355 | if (!td->create_serialize) |
| 2356 | continue; |
| 2357 | |
| 2358 | /* |
| 2359 | * do file setup here so it happens sequentially, |
| 2360 | * we don't want X number of threads getting their |
| 2361 | * client data interspersed on disk |
| 2362 | */ |
| 2363 | if (setup_file(td)) { |
| 2364 | td_set_runstate(td, TD_REAPED); |
| 2365 | todo--; |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | gettimeofday(&genesis, NULL); |
| 2370 | |
| 2371 | while (todo) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2372 | struct thread_data *map[MAX_JOBS]; |
| 2373 | struct timeval this_start; |
| 2374 | int this_jobs = 0, left; |
| 2375 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2376 | /* |
| 2377 | * create threads (TD_NOT_CREATED -> TD_CREATED) |
| 2378 | */ |
| 2379 | for (i = 0; i < thread_number; i++) { |
| 2380 | td = &threads[i]; |
| 2381 | |
| 2382 | if (td->runstate != TD_NOT_CREATED) |
| 2383 | continue; |
| 2384 | |
| 2385 | /* |
| 2386 | * never got a chance to start, killed by other |
| 2387 | * thread for some reason |
| 2388 | */ |
| 2389 | if (td->terminate) { |
| 2390 | todo--; |
| 2391 | continue; |
| 2392 | } |
| 2393 | |
| 2394 | if (td->start_delay) { |
| 2395 | spent = mtime_since_now(&genesis); |
| 2396 | |
| 2397 | if (td->start_delay * 1000 > spent) |
| 2398 | continue; |
| 2399 | } |
| 2400 | |
| 2401 | if (td->stonewall && (nr_started || nr_running)) |
| 2402 | break; |
| 2403 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2404 | /* |
| 2405 | * Set state to created. Thread will transition |
| 2406 | * to TD_INITIALIZED when it's done setting up. |
| 2407 | */ |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2408 | td_set_runstate(td, TD_CREATED); |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2409 | map[this_jobs++] = td; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2410 | sem_init(&startup_sem, 0, 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2411 | nr_started++; |
| 2412 | |
| 2413 | if (td->use_thread) { |
| 2414 | if (pthread_create(&td->thread, NULL, thread_main, td)) { |
| 2415 | perror("thread_create"); |
| 2416 | nr_started--; |
| 2417 | } |
| 2418 | } else { |
| 2419 | if (fork()) |
| 2420 | sem_wait(&startup_sem); |
| 2421 | else { |
| 2422 | fork_main(shm_id, i); |
| 2423 | exit(0); |
| 2424 | } |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | /* |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2429 | * Wait for the started threads to transition to |
| 2430 | * TD_INITIALIZED. |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2431 | */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2432 | printf("fio: Waiting for threads to initialize...\n"); |
| 2433 | gettimeofday(&this_start, NULL); |
| 2434 | left = this_jobs; |
| 2435 | while (left) { |
| 2436 | if (mtime_since_now(&this_start) > JOB_START_TIMEOUT) |
| 2437 | break; |
| 2438 | |
| 2439 | usleep(100000); |
| 2440 | |
| 2441 | for (i = 0; i < this_jobs; i++) { |
| 2442 | td = map[i]; |
| 2443 | if (!td) |
| 2444 | continue; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 2445 | if (td->runstate == TD_INITIALIZED) { |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2446 | map[i] = NULL; |
| 2447 | left--; |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 2448 | } else if (td->runstate >= TD_EXITED) { |
| 2449 | map[i] = NULL; |
| 2450 | left--; |
| 2451 | todo--; |
| 2452 | nr_running++; /* work-around... */ |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2453 | } |
| 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | if (left) { |
| 2458 | fprintf(stderr, "fio: %d jobs failed to start\n", left); |
| 2459 | for (i = 0; i < this_jobs; i++) { |
| 2460 | td = map[i]; |
| 2461 | if (!td) |
| 2462 | continue; |
| 2463 | kill(td->pid, SIGTERM); |
| 2464 | } |
| 2465 | break; |
| 2466 | } |
| 2467 | |
| 2468 | /* |
Jens Axboe | b6f4d88 | 2006-06-02 10:32:51 +0200 | [diff] [blame] | 2469 | * start created threads (TD_INITIALIZED -> TD_RUNNING). |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2470 | */ |
| 2471 | printf("fio: Go for launch\n"); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2472 | for (i = 0; i < thread_number; i++) { |
| 2473 | td = &threads[i]; |
| 2474 | |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2475 | if (td->runstate != TD_INITIALIZED) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2476 | continue; |
| 2477 | |
| 2478 | td_set_runstate(td, TD_RUNNING); |
| 2479 | nr_running++; |
| 2480 | nr_started--; |
| 2481 | m_rate += td->ratemin; |
| 2482 | t_rate += td->rate; |
Jens Axboe | 7515484 | 2006-06-01 13:56:09 +0200 | [diff] [blame] | 2483 | todo--; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2484 | sem_post(&td->mutex); |
| 2485 | } |
| 2486 | |
| 2487 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 2488 | |
| 2489 | if (todo) |
| 2490 | usleep(100000); |
| 2491 | } |
| 2492 | |
| 2493 | while (nr_running) { |
| 2494 | reap_threads(&nr_running, &t_rate, &m_rate); |
| 2495 | usleep(10000); |
| 2496 | } |
| 2497 | |
| 2498 | update_io_ticks(); |
Jens Axboe | fcb6ade | 2006-05-31 12:14:35 +0200 | [diff] [blame] | 2499 | fio_unpin_memory(mlocked_mem); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | static void show_group_stats(struct group_run_stats *rs, int id) |
| 2503 | { |
| 2504 | printf("\nRun status group %d (all jobs):\n", id); |
| 2505 | |
| 2506 | if (rs->max_run[DDIR_READ]) |
Jens Axboe | 9104f87 | 2005-12-28 23:50:45 +0100 | [diff] [blame] | 2507 | printf(" READ: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_mb[0], rs->agg[0], rs->min_bw[0], rs->max_bw[0], rs->min_run[0], rs->max_run[0]); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2508 | if (rs->max_run[DDIR_WRITE]) |
Jens Axboe | 9104f87 | 2005-12-28 23:50:45 +0100 | [diff] [blame] | 2509 | printf(" WRITE: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_mb[1], rs->agg[1], rs->min_bw[1], rs->max_bw[1], rs->min_run[1], rs->max_run[1]); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | static void show_disk_util(void) |
| 2513 | { |
| 2514 | struct disk_util_stat *dus; |
| 2515 | struct list_head *entry; |
| 2516 | struct disk_util *du; |
| 2517 | double util; |
| 2518 | |
| 2519 | printf("\nDisk stats (read/write):\n"); |
| 2520 | |
| 2521 | list_for_each(entry, &disk_list) { |
| 2522 | du = list_entry(entry, struct disk_util, list); |
| 2523 | dus = &du->dus; |
| 2524 | |
| 2525 | util = (double) 100 * du->dus.io_ticks / (double) du->msec; |
| 2526 | if (util > 100.0) |
| 2527 | util = 100.0; |
| 2528 | |
| 2529 | printf(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util); |
| 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | static void show_run_stats(void) |
| 2534 | { |
| 2535 | struct group_run_stats *runstats, *rs; |
| 2536 | struct thread_data *td; |
| 2537 | int i; |
| 2538 | |
| 2539 | runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1)); |
| 2540 | |
| 2541 | for (i = 0; i < groupid + 1; i++) { |
| 2542 | rs = &runstats[i]; |
| 2543 | |
| 2544 | memset(rs, 0, sizeof(*rs)); |
| 2545 | rs->min_bw[0] = rs->min_run[0] = ~0UL; |
| 2546 | rs->min_bw[1] = rs->min_run[1] = ~0UL; |
| 2547 | } |
| 2548 | |
| 2549 | for (i = 0; i < thread_number; i++) { |
Jens Axboe | 200bc85 | 2006-06-01 16:10:12 +0200 | [diff] [blame] | 2550 | unsigned long long rbw, wbw; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2551 | |
| 2552 | td = &threads[i]; |
| 2553 | |
| 2554 | if (td->error) { |
| 2555 | printf("Client%d: %s\n", td->thread_number, td->verror); |
| 2556 | continue; |
| 2557 | } |
| 2558 | |
| 2559 | rs = &runstats[td->groupid]; |
| 2560 | |
| 2561 | if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0]) |
| 2562 | rs->min_run[0] = td->runtime[0]; |
| 2563 | if (td->runtime[0] > rs->max_run[0]) |
| 2564 | rs->max_run[0] = td->runtime[0]; |
| 2565 | if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1]) |
| 2566 | rs->min_run[1] = td->runtime[1]; |
| 2567 | if (td->runtime[1] > rs->max_run[1]) |
| 2568 | rs->max_run[1] = td->runtime[1]; |
| 2569 | |
| 2570 | rbw = wbw = 0; |
| 2571 | if (td->runtime[0]) |
Jens Axboe | 200bc85 | 2006-06-01 16:10:12 +0200 | [diff] [blame] | 2572 | rbw = td->io_bytes[0] / (unsigned long long) td->runtime[0]; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2573 | if (td->runtime[1]) |
Jens Axboe | 200bc85 | 2006-06-01 16:10:12 +0200 | [diff] [blame] | 2574 | wbw = td->io_bytes[1] / (unsigned long long) td->runtime[1]; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 2575 | |
| 2576 | if (rbw < rs->min_bw[0]) |
| 2577 | rs->min_bw[0] = rbw; |
| 2578 | if (wbw < rs->min_bw[1]) |
| 2579 | rs->min_bw[1] = wbw; |
| 2580 | if (rbw > rs->max_bw[0]) |
| 2581 | rs->max_bw[0] = rbw; |
| 2582 | if (wbw > rs->max_bw[1]) |
| 2583 | rs->max_bw[1] = wbw; |
| 2584 | |
| 2585 | rs->io_mb[0] += td->io_bytes[0] >> 20; |
| 2586 | rs->io_mb[1] += td->io_bytes[1] >> 20; |
| 2587 | } |
| 2588 | |
| 2589 | for (i = 0; i < groupid + 1; i++) { |
| 2590 | rs = &runstats[i]; |
| 2591 | |
| 2592 | if (rs->max_run[0]) |
| 2593 | rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0]; |
| 2594 | if (rs->max_run[1]) |
| 2595 | rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1]; |
| 2596 | } |
| 2597 | |
| 2598 | /* |
| 2599 | * don't overwrite last signal output |
| 2600 | */ |
| 2601 | printf("\n"); |
| 2602 | |
| 2603 | for (i = 0; i < thread_number; i++) { |
| 2604 | td = &threads[i]; |
| 2605 | rs = &runstats[td->groupid]; |
| 2606 | |
| 2607 | show_thread_status(td, rs); |
| 2608 | } |
| 2609 | |
| 2610 | for (i = 0; i < groupid + 1; i++) |
| 2611 | show_group_stats(&runstats[i], i); |
| 2612 | |
| 2613 | show_disk_util(); |
| 2614 | } |
| 2615 | |
| 2616 | int main(int argc, char *argv[]) |
| 2617 | { |
| 2618 | if (parse_options(argc, argv)) |
| 2619 | return 1; |
| 2620 | |
| 2621 | if (!thread_number) { |
| 2622 | printf("Nothing to do\n"); |
| 2623 | return 1; |
| 2624 | } |
| 2625 | |
| 2626 | disk_util_timer_arm(); |
| 2627 | |
| 2628 | run_threads(); |
| 2629 | show_run_stats(); |
| 2630 | |
| 2631 | return 0; |
| 2632 | } |