init.c revision 8c029376bbe6d8a6bcb01c3ef7d545720aca3868
1/* 2 * This file contains job initialization and setup functions. 3 */ 4#include <stdio.h> 5#include <stdlib.h> 6#include <unistd.h> 7#include <fcntl.h> 8#include <ctype.h> 9#include <string.h> 10#include <errno.h> 11#include <sys/ipc.h> 12#include <sys/shm.h> 13#include <sys/types.h> 14#include <sys/stat.h> 15 16#include "fio.h" 17#include "parse.h" 18#include "smalloc.h" 19#include "filehash.h" 20#include "verify.h" 21#include "profile.h" 22 23#include "lib/getopt.h" 24 25static char fio_version_string[] = "fio 1.58"; 26 27#define FIO_RANDSEED (0xb1899bedUL) 28 29static char **ini_file; 30static int max_jobs = FIO_MAX_JOBS; 31static int dump_cmdline; 32static int def_timeout; 33 34static struct thread_data def_thread; 35struct thread_data *threads = NULL; 36 37int exitall_on_terminate = 0; 38int terse_output = 0; 39int eta_print; 40unsigned long long mlock_size = 0; 41FILE *f_out = NULL; 42FILE *f_err = NULL; 43char **job_sections = NULL; 44int nr_job_sections = 0; 45char *exec_profile = NULL; 46int warnings_fatal = 0; 47int terse_version = 2; 48 49int write_bw_log = 0; 50int read_only = 0; 51 52static int write_lat_log; 53 54static int prev_group_jobs; 55 56unsigned long fio_debug = 0; 57unsigned int fio_debug_jobno = -1; 58unsigned int *fio_debug_jobp = NULL; 59 60static char cmd_optstr[256]; 61 62/* 63 * Command line options. These will contain the above, plus a few 64 * extra that only pertain to fio itself and not jobs. 65 */ 66static struct option l_opts[FIO_NR_OPTIONS] = { 67 { 68 .name = (char *) "output", 69 .has_arg = required_argument, 70 .val = 'o', 71 }, 72 { 73 .name = (char *) "timeout", 74 .has_arg = required_argument, 75 .val = 't', 76 }, 77 { 78 .name = (char *) "latency-log", 79 .has_arg = required_argument, 80 .val = 'l', 81 }, 82 { 83 .name = (char *) "bandwidth-log", 84 .has_arg = required_argument, 85 .val = 'b', 86 }, 87 { 88 .name = (char *) "minimal", 89 .has_arg = optional_argument, 90 .val = 'm', 91 }, 92 { 93 .name = (char *) "version", 94 .has_arg = no_argument, 95 .val = 'v', 96 }, 97 { 98 .name = (char *) "help", 99 .has_arg = no_argument, 100 .val = 'h', 101 }, 102 { 103 .name = (char *) "cmdhelp", 104 .has_arg = optional_argument, 105 .val = 'c', 106 }, 107 { 108 .name = (char *) "showcmd", 109 .has_arg = no_argument, 110 .val = 's', 111 }, 112 { 113 .name = (char *) "readonly", 114 .has_arg = no_argument, 115 .val = 'r', 116 }, 117 { 118 .name = (char *) "eta", 119 .has_arg = required_argument, 120 .val = 'e', 121 }, 122 { 123 .name = (char *) "debug", 124 .has_arg = required_argument, 125 .val = 'd', 126 }, 127 { 128 .name = (char *) "section", 129 .has_arg = required_argument, 130 .val = 'x', 131 }, 132 { 133 .name = (char *) "alloc-size", 134 .has_arg = required_argument, 135 .val = 'a', 136 }, 137 { 138 .name = (char *) "profile", 139 .has_arg = required_argument, 140 .val = 'p', 141 }, 142 { 143 .name = (char *) "warnings-fatal", 144 .has_arg = no_argument, 145 .val = 'w', 146 }, 147 { 148 .name = (char *) "max-jobs", 149 .has_arg = required_argument, 150 .val = 'j', 151 }, 152 { 153 .name = (char *) "terse-version", 154 .has_arg = required_argument, 155 .val = 'V', 156 }, 157 { 158 .name = NULL, 159 }, 160}; 161 162FILE *get_f_out() 163{ 164 return f_out; 165} 166 167FILE *get_f_err() 168{ 169 return f_err; 170} 171 172/* 173 * Return a free job structure. 174 */ 175static struct thread_data *get_new_job(int global, struct thread_data *parent) 176{ 177 struct thread_data *td; 178 179 if (global) 180 return &def_thread; 181 if (thread_number >= max_jobs) { 182 log_err("error: maximum number of jobs (%d) reached.\n", 183 max_jobs); 184 return NULL; 185 } 186 187 td = &threads[thread_number++]; 188 *td = *parent; 189 190 td->o.uid = td->o.gid = -1U; 191 192 dup_files(td, parent); 193 options_mem_dupe(td); 194 195 profile_add_hooks(td); 196 197 td->thread_number = thread_number; 198 return td; 199} 200 201static void put_job(struct thread_data *td) 202{ 203 if (td == &def_thread) 204 return; 205 206 profile_td_exit(td); 207 208 if (td->error) 209 log_info("fio: %s\n", td->verror); 210 211 memset(&threads[td->thread_number - 1], 0, sizeof(*td)); 212 thread_number--; 213} 214 215static int __setup_rate(struct thread_data *td, enum fio_ddir ddir) 216{ 217 unsigned int bs = td->o.min_bs[ddir]; 218 unsigned long long bytes_per_sec; 219 220 assert(ddir_rw(ddir)); 221 222 if (td->o.rate[ddir]) 223 bytes_per_sec = td->o.rate[ddir]; 224 else 225 bytes_per_sec = td->o.rate_iops[ddir] * bs; 226 227 if (!bytes_per_sec) { 228 log_err("rate lower than supported\n"); 229 return -1; 230 } 231 232 td->rate_nsec_cycle[ddir] = 1000000000ULL / bytes_per_sec; 233 td->rate_pending_usleep[ddir] = 0; 234 return 0; 235} 236 237static int setup_rate(struct thread_data *td) 238{ 239 int ret = 0; 240 241 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ]) 242 ret = __setup_rate(td, DDIR_READ); 243 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE]) 244 ret |= __setup_rate(td, DDIR_WRITE); 245 246 return ret; 247} 248 249static int fixed_block_size(struct thread_options *o) 250{ 251 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] && 252 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] && 253 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE]; 254} 255 256/* 257 * Lazy way of fixing up options that depend on each other. We could also 258 * define option callback handlers, but this is easier. 259 */ 260static int fixup_options(struct thread_data *td) 261{ 262 struct thread_options *o = &td->o; 263 int ret = 0; 264 265#ifndef FIO_HAVE_PSHARED_MUTEX 266 if (!o->use_thread) { 267 log_info("fio: this platform does not support process shared" 268 " mutexes, forcing use of threads. Use the 'thread'" 269 " option to get rid of this warning.\n"); 270 o->use_thread = 1; 271 ret = warnings_fatal; 272 } 273#endif 274 275 if (o->write_iolog_file && o->read_iolog_file) { 276 log_err("fio: read iolog overrides write_iolog\n"); 277 free(o->write_iolog_file); 278 o->write_iolog_file = NULL; 279 ret = warnings_fatal; 280 } 281 282 /* 283 * only really works for sequential io for now, and with 1 file 284 */ 285 if (o->zone_size && td_random(td) && o->open_files == 1) 286 o->zone_size = 0; 287 288 /* 289 * Reads can do overwrites, we always need to pre-create the file 290 */ 291 if (td_read(td) || td_rw(td)) 292 o->overwrite = 1; 293 294 if (!o->min_bs[DDIR_READ]) 295 o->min_bs[DDIR_READ] = o->bs[DDIR_READ]; 296 if (!o->max_bs[DDIR_READ]) 297 o->max_bs[DDIR_READ] = o->bs[DDIR_READ]; 298 if (!o->min_bs[DDIR_WRITE]) 299 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE]; 300 if (!o->max_bs[DDIR_WRITE]) 301 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE]; 302 303 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]); 304 305 /* 306 * For random IO, allow blockalign offset other than min_bs. 307 */ 308 if (!o->ba[DDIR_READ] || !td_random(td)) 309 o->ba[DDIR_READ] = o->min_bs[DDIR_READ]; 310 if (!o->ba[DDIR_WRITE] || !td_random(td)) 311 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE]; 312 313 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] || 314 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) && 315 !o->norandommap) { 316 log_err("fio: Any use of blockalign= turns off randommap\n"); 317 o->norandommap = 1; 318 ret = warnings_fatal; 319 } 320 321 if (!o->file_size_high) 322 o->file_size_high = o->file_size_low; 323 324 if (o->norandommap && o->verify != VERIFY_NONE 325 && !fixed_block_size(o)) { 326 log_err("fio: norandommap given for variable block sizes, " 327 "verify disabled\n"); 328 o->verify = VERIFY_NONE; 329 ret = warnings_fatal; 330 } 331 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO)) 332 log_err("fio: bs_unaligned may not work with raw io\n"); 333 334 /* 335 * thinktime_spin must be less than thinktime 336 */ 337 if (o->thinktime_spin > o->thinktime) 338 o->thinktime_spin = o->thinktime; 339 340 /* 341 * The low water mark cannot be bigger than the iodepth 342 */ 343 if (o->iodepth_low > o->iodepth || !o->iodepth_low) { 344 /* 345 * syslet work around - if the workload is sequential, 346 * we want to let the queue drain all the way down to 347 * avoid seeking between async threads 348 */ 349 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td)) 350 o->iodepth_low = 1; 351 else 352 o->iodepth_low = o->iodepth; 353 } 354 355 /* 356 * If batch number isn't set, default to the same as iodepth 357 */ 358 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch) 359 o->iodepth_batch = o->iodepth; 360 361 if (o->nr_files > td->files_index) 362 o->nr_files = td->files_index; 363 364 if (o->open_files > o->nr_files || !o->open_files) 365 o->open_files = o->nr_files; 366 367 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))|| 368 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] + 369 o->rate_iops_min[1]))) { 370 log_err("fio: rate and rate_iops are mutually exclusive\n"); 371 ret = 1; 372 } 373 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) || 374 (o->rate_iops[0] < o->rate_iops_min[0]) || 375 (o->rate_iops[1] < o->rate_iops_min[1])) { 376 log_err("fio: minimum rate exceeds rate\n"); 377 ret = 1; 378 } 379 380 if (!o->timeout && o->time_based) { 381 log_err("fio: time_based requires a runtime/timeout setting\n"); 382 o->time_based = 0; 383 ret = warnings_fatal; 384 } 385 386 if (o->fill_device && !o->size) 387 o->size = -1ULL; 388 389 if (o->verify != VERIFY_NONE) { 390 if (td_write(td) && o->do_verify && o->numjobs > 1) { 391 log_info("Multiple writers may overwrite blocks that " 392 "belong to other jobs. This can cause " 393 "verification failures.\n"); 394 ret = warnings_fatal; 395 } 396 397 o->refill_buffers = 1; 398 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] && 399 !o->verify_interval) 400 o->verify_interval = o->min_bs[DDIR_WRITE]; 401 } 402 403 if (o->pre_read) { 404 o->invalidate_cache = 0; 405 if (td->io_ops->flags & FIO_PIPEIO) { 406 log_info("fio: cannot pre-read files with an IO engine" 407 " that isn't seekable. Pre-read disabled.\n"); 408 ret = warnings_fatal; 409 } 410 } 411 412#ifndef FIO_HAVE_FDATASYNC 413 if (o->fdatasync_blocks) { 414 log_info("fio: this platform does not support fdatasync()" 415 " falling back to using fsync(). Use the 'fsync'" 416 " option instead of 'fdatasync' to get rid of" 417 " this warning\n"); 418 o->fsync_blocks = o->fdatasync_blocks; 419 o->fdatasync_blocks = 0; 420 ret = warnings_fatal; 421 } 422#endif 423 424 return ret; 425} 426 427/* 428 * This function leaks the buffer 429 */ 430static char *to_kmg(unsigned int val) 431{ 432 char *buf = malloc(32); 433 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 }; 434 char *p = post; 435 436 do { 437 if (val & 1023) 438 break; 439 440 val >>= 10; 441 p++; 442 } while (*p); 443 444 snprintf(buf, 31, "%u%c", val, *p); 445 return buf; 446} 447 448/* External engines are specified by "external:name.o") */ 449static const char *get_engine_name(const char *str) 450{ 451 char *p = strstr(str, ":"); 452 453 if (!p) 454 return str; 455 456 p++; 457 strip_blank_front(&p); 458 strip_blank_end(p); 459 return p; 460} 461 462static int exists_and_not_file(const char *filename) 463{ 464 struct stat sb; 465 466 if (lstat(filename, &sb) == -1) 467 return 0; 468 469 /* \\.\ is the device namespace in Windows, where every file 470 * is a device node */ 471 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0) 472 return 0; 473 474 return 1; 475} 476 477static void td_fill_rand_seeds_os(struct thread_data *td) 478{ 479 os_random_seed(td->rand_seeds[0], &td->bsrange_state); 480 os_random_seed(td->rand_seeds[1], &td->verify_state); 481 os_random_seed(td->rand_seeds[2], &td->rwmix_state); 482 483 if (td->o.file_service_type == FIO_FSERVICE_RANDOM) 484 os_random_seed(td->rand_seeds[3], &td->next_file_state); 485 486 os_random_seed(td->rand_seeds[5], &td->file_size_state); 487 os_random_seed(td->rand_seeds[6], &td->trim_state); 488 489 if (!td_random(td)) 490 return; 491 492 if (td->o.rand_repeatable) 493 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number; 494 495 os_random_seed(td->rand_seeds[4], &td->random_state); 496} 497 498static void td_fill_rand_seeds_internal(struct thread_data *td) 499{ 500 init_rand_seed(&td->__bsrange_state, td->rand_seeds[0]); 501 init_rand_seed(&td->__verify_state, td->rand_seeds[1]); 502 init_rand_seed(&td->__rwmix_state, td->rand_seeds[2]); 503 504 if (td->o.file_service_type == FIO_FSERVICE_RANDOM) 505 init_rand_seed(&td->__next_file_state, td->rand_seeds[3]); 506 507 init_rand_seed(&td->__file_size_state, td->rand_seeds[5]); 508 init_rand_seed(&td->__trim_state, td->rand_seeds[6]); 509 510 if (!td_random(td)) 511 return; 512 513 if (td->o.rand_repeatable) 514 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number; 515 516 init_rand_seed(&td->__random_state, td->rand_seeds[4]); 517} 518 519void td_fill_rand_seeds(struct thread_data *td) 520{ 521 if (td->o.use_os_rand) 522 td_fill_rand_seeds_os(td); 523 else 524 td_fill_rand_seeds_internal(td); 525 526 init_rand_seed(&td->buf_state, td->rand_seeds[7]); 527} 528 529/* 530 * Initialize the various random states we need (random io, block size ranges, 531 * read/write mix, etc). 532 */ 533static int init_random_state(struct thread_data *td) 534{ 535 int fd; 536 537 fd = open("/dev/urandom", O_RDONLY); 538 if (fd == -1) { 539 td_verror(td, errno, "open"); 540 return 1; 541 } 542 543 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) < 544 (int) sizeof(td->rand_seeds)) { 545 td_verror(td, EIO, "read"); 546 close(fd); 547 return 1; 548 } 549 550 close(fd); 551 td_fill_rand_seeds(td); 552 return 0; 553} 554 555/* 556 * Adds a job to the list of things todo. Sanitizes the various options 557 * to make sure we don't have conflicts, and initializes various 558 * members of td. 559 */ 560static int add_job(struct thread_data *td, const char *jobname, int job_add_num) 561{ 562 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL, 563 "randread", "randwrite", "randrw" }; 564 unsigned int i; 565 const char *engine; 566 char fname[PATH_MAX]; 567 int numjobs, file_alloced; 568 569 /* 570 * the def_thread is just for options, it's not a real job 571 */ 572 if (td == &def_thread) 573 return 0; 574 575 /* 576 * if we are just dumping the output command line, don't add the job 577 */ 578 if (dump_cmdline) { 579 put_job(td); 580 return 0; 581 } 582 583 if (profile_td_init(td)) 584 goto err; 585 586 engine = get_engine_name(td->o.ioengine); 587 td->io_ops = load_ioengine(td, engine); 588 if (!td->io_ops) { 589 log_err("fio: failed to load engine %s\n", engine); 590 goto err; 591 } 592 593 if (td->o.use_thread) 594 nr_thread++; 595 else 596 nr_process++; 597 598 if (td->o.odirect) 599 td->io_ops->flags |= FIO_RAWIO; 600 601 file_alloced = 0; 602 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) { 603 file_alloced = 1; 604 605 if (td->o.nr_files == 1 && exists_and_not_file(jobname)) 606 add_file(td, jobname); 607 else { 608 for (i = 0; i < td->o.nr_files; i++) { 609 sprintf(fname, "%s.%d.%d", jobname, 610 td->thread_number, i); 611 add_file(td, fname); 612 } 613 } 614 } 615 616 if (fixup_options(td)) 617 goto err; 618 619 if (td->io_ops->flags & FIO_DISKLESSIO) { 620 struct fio_file *f; 621 622 for_each_file(td, f, i) 623 f->real_file_size = -1ULL; 624 } 625 626 td->mutex = fio_mutex_init(0); 627 628 td->ts.clat_percentiles = td->o.clat_percentiles; 629 if (td->o.overwrite_plist) 630 td->ts.percentile_list = td->o.percentile_list; 631 else 632 td->ts.percentile_list = NULL; 633 634 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX; 635 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX; 636 td->ts.lat_stat[0].min_val = td->ts.lat_stat[1].min_val = ULONG_MAX; 637 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX; 638 td->ddir_seq_nr = td->o.ddir_seq_nr; 639 640 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) { 641 prev_group_jobs = 0; 642 groupid++; 643 } 644 645 td->groupid = groupid; 646 prev_group_jobs++; 647 648 if (init_random_state(td)) 649 goto err; 650 651 if (setup_rate(td)) 652 goto err; 653 654 if (td->o.write_lat_log) { 655 setup_log(&td->ts.lat_log); 656 setup_log(&td->ts.slat_log); 657 setup_log(&td->ts.clat_log); 658 } 659 if (td->o.write_bw_log) 660 setup_log(&td->ts.bw_log); 661 662 if (!td->o.name) 663 td->o.name = strdup(jobname); 664 665 if (!terse_output) { 666 if (!job_add_num) { 667 if (!strcmp(td->io_ops->name, "cpuio")) { 668 log_info("%s: ioengine=cpu, cpuload=%u," 669 " cpucycle=%u\n", td->o.name, 670 td->o.cpuload, 671 td->o.cpucycle); 672 } else { 673 char *c1, *c2, *c3, *c4; 674 675 c1 = to_kmg(td->o.min_bs[DDIR_READ]); 676 c2 = to_kmg(td->o.max_bs[DDIR_READ]); 677 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]); 678 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]); 679 680 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s," 681 " ioengine=%s, iodepth=%u\n", 682 td->o.name, td->groupid, 683 ddir_str[td->o.td_ddir], 684 c1, c2, c3, c4, 685 td->io_ops->name, 686 td->o.iodepth); 687 688 free(c1); 689 free(c2); 690 free(c3); 691 free(c4); 692 } 693 } else if (job_add_num == 1) 694 log_info("...\n"); 695 } 696 697 /* 698 * recurse add identical jobs, clear numjobs and stonewall options 699 * as they don't apply to sub-jobs 700 */ 701 numjobs = td->o.numjobs; 702 while (--numjobs) { 703 struct thread_data *td_new = get_new_job(0, td); 704 705 if (!td_new) 706 goto err; 707 708 td_new->o.numjobs = 1; 709 td_new->o.stonewall = 0; 710 td_new->o.new_group = 0; 711 712 if (file_alloced) { 713 td_new->o.filename = NULL; 714 td_new->files_index = 0; 715 td_new->files_size = 0; 716 td_new->files = NULL; 717 } 718 719 job_add_num = numjobs - 1; 720 721 if (add_job(td_new, jobname, job_add_num)) 722 goto err; 723 } 724 725 return 0; 726err: 727 put_job(td); 728 return -1; 729} 730 731/* 732 * Parse as if 'o' was a command line 733 */ 734void add_job_opts(const char **o) 735{ 736 struct thread_data *td, *td_parent; 737 int i, in_global = 1; 738 char jobname[32]; 739 740 i = 0; 741 td_parent = td = NULL; 742 while (o[i]) { 743 if (!strncmp(o[i], "name", 4)) { 744 in_global = 0; 745 if (td) 746 add_job(td, jobname, 0); 747 td = NULL; 748 sprintf(jobname, "%s", o[i] + 5); 749 } 750 if (in_global && !td_parent) 751 td_parent = get_new_job(1, &def_thread); 752 else if (!in_global && !td) { 753 if (!td_parent) 754 td_parent = &def_thread; 755 td = get_new_job(0, td_parent); 756 } 757 if (in_global) 758 fio_options_parse(td_parent, (char **) &o[i], 1); 759 else 760 fio_options_parse(td, (char **) &o[i], 1); 761 i++; 762 } 763 764 if (td) 765 add_job(td, jobname, 0); 766} 767 768static int skip_this_section(const char *name) 769{ 770 int i; 771 772 if (!nr_job_sections) 773 return 0; 774 if (!strncmp(name, "global", 6)) 775 return 0; 776 777 for (i = 0; i < nr_job_sections; i++) 778 if (!strcmp(job_sections[i], name)) 779 return 0; 780 781 return 1; 782} 783 784static int is_empty_or_comment(char *line) 785{ 786 unsigned int i; 787 788 for (i = 0; i < strlen(line); i++) { 789 if (line[i] == ';') 790 return 1; 791 if (line[i] == '#') 792 return 1; 793 if (!isspace((int) line[i]) && !iscntrl((int) line[i])) 794 return 0; 795 } 796 797 return 1; 798} 799 800/* 801 * This is our [ini] type file parser. 802 */ 803static int parse_jobs_ini(char *file, int stonewall_flag) 804{ 805 unsigned int global; 806 struct thread_data *td; 807 char *string, *name; 808 FILE *f; 809 char *p; 810 int ret = 0, stonewall; 811 int first_sect = 1; 812 int skip_fgets = 0; 813 int inside_skip = 0; 814 char **opts; 815 int i, alloc_opts, num_opts; 816 817 if (!strcmp(file, "-")) 818 f = stdin; 819 else 820 f = fopen(file, "r"); 821 822 if (!f) { 823 perror("fopen job file"); 824 return 1; 825 } 826 827 string = malloc(4096); 828 829 /* 830 * it's really 256 + small bit, 280 should suffice 831 */ 832 name = malloc(280); 833 memset(name, 0, 280); 834 835 alloc_opts = 8; 836 opts = malloc(sizeof(char *) * alloc_opts); 837 num_opts = 0; 838 839 stonewall = stonewall_flag; 840 do { 841 /* 842 * if skip_fgets is set, we already have loaded a line we 843 * haven't handled. 844 */ 845 if (!skip_fgets) { 846 p = fgets(string, 4095, f); 847 if (!p) 848 break; 849 } 850 851 skip_fgets = 0; 852 strip_blank_front(&p); 853 strip_blank_end(p); 854 855 if (is_empty_or_comment(p)) 856 continue; 857 if (sscanf(p, "[%255[^\n]]", name) != 1) { 858 if (inside_skip) 859 continue; 860 log_err("fio: option <%s> outside of [] job section\n", 861 p); 862 break; 863 } 864 865 name[strlen(name) - 1] = '\0'; 866 867 if (skip_this_section(name)) { 868 inside_skip = 1; 869 continue; 870 } else 871 inside_skip = 0; 872 873 global = !strncmp(name, "global", 6); 874 875 if (dump_cmdline) { 876 if (first_sect) 877 log_info("fio "); 878 if (!global) 879 log_info("--name=%s ", name); 880 first_sect = 0; 881 } 882 883 td = get_new_job(global, &def_thread); 884 if (!td) { 885 ret = 1; 886 break; 887 } 888 889 /* 890 * Seperate multiple job files by a stonewall 891 */ 892 if (!global && stonewall) { 893 td->o.stonewall = stonewall; 894 stonewall = 0; 895 } 896 897 num_opts = 0; 898 memset(opts, 0, alloc_opts * sizeof(char *)); 899 900 while ((p = fgets(string, 4096, f)) != NULL) { 901 if (is_empty_or_comment(p)) 902 continue; 903 904 strip_blank_front(&p); 905 906 /* 907 * new section, break out and make sure we don't 908 * fgets() a new line at the top. 909 */ 910 if (p[0] == '[') { 911 skip_fgets = 1; 912 break; 913 } 914 915 strip_blank_end(p); 916 917 if (num_opts == alloc_opts) { 918 alloc_opts <<= 1; 919 opts = realloc(opts, 920 alloc_opts * sizeof(char *)); 921 } 922 923 opts[num_opts] = strdup(p); 924 num_opts++; 925 } 926 927 ret = fio_options_parse(td, opts, num_opts); 928 if (!ret) { 929 if (dump_cmdline) 930 for (i = 0; i < num_opts; i++) 931 log_info("--%s ", opts[i]); 932 933 ret = add_job(td, name, 0); 934 } else { 935 log_err("fio: job %s dropped\n", name); 936 put_job(td); 937 } 938 939 for (i = 0; i < num_opts; i++) 940 free(opts[i]); 941 num_opts = 0; 942 } while (!ret); 943 944 if (dump_cmdline) 945 log_info("\n"); 946 947 for (i = 0; i < num_opts; i++) 948 free(opts[i]); 949 950 free(string); 951 free(name); 952 free(opts); 953 if (f != stdin) 954 fclose(f); 955 return ret; 956} 957 958static int fill_def_thread(void) 959{ 960 memset(&def_thread, 0, sizeof(def_thread)); 961 962 fio_getaffinity(getpid(), &def_thread.o.cpumask); 963 def_thread.o.timeout = def_timeout; 964 965 /* 966 * fill default options 967 */ 968 fio_fill_default_options(&def_thread); 969 return 0; 970} 971 972static void free_shm(void) 973{ 974 struct shmid_ds sbuf; 975 976 if (threads) { 977 void *tp = threads; 978 979 threads = NULL; 980 file_hash_exit(); 981 fio_debug_jobp = NULL; 982 shmdt(tp); 983 shmctl(shm_id, IPC_RMID, &sbuf); 984 } 985 986 scleanup(); 987} 988 989/* 990 * The thread area is shared between the main process and the job 991 * threads/processes. So setup a shared memory segment that will hold 992 * all the job info. We use the end of the region for keeping track of 993 * open files across jobs, for file sharing. 994 */ 995static int setup_thread_area(void) 996{ 997 void *hash; 998 999 /* 1000 * 1024 is too much on some machines, scale max_jobs if 1001 * we get a failure that looks like too large a shm segment 1002 */ 1003 do { 1004 size_t size = max_jobs * sizeof(struct thread_data); 1005 1006 size += file_hash_size; 1007 size += sizeof(unsigned int); 1008 1009 shm_id = shmget(0, size, IPC_CREAT | 0600); 1010 if (shm_id != -1) 1011 break; 1012 if (errno != EINVAL) { 1013 perror("shmget"); 1014 break; 1015 } 1016 1017 max_jobs >>= 1; 1018 } while (max_jobs); 1019 1020 if (shm_id == -1) 1021 return 1; 1022 1023 threads = shmat(shm_id, NULL, 0); 1024 if (threads == (void *) -1) { 1025 perror("shmat"); 1026 return 1; 1027 } 1028 1029 memset(threads, 0, max_jobs * sizeof(struct thread_data)); 1030 hash = (void *) threads + max_jobs * sizeof(struct thread_data); 1031 fio_debug_jobp = (void *) hash + file_hash_size; 1032 *fio_debug_jobp = -1; 1033 file_hash_init(hash); 1034 atexit(free_shm); 1035 return 0; 1036} 1037 1038static void usage(const char *name) 1039{ 1040 printf("%s\n", fio_version_string); 1041 printf("%s [options] [job options] <job file(s)>\n", name); 1042 printf("\t--debug=options\tEnable debug logging\n"); 1043 printf("\t--output\tWrite output to file\n"); 1044 printf("\t--timeout\tRuntime in seconds\n"); 1045 printf("\t--latency-log\tGenerate per-job latency logs\n"); 1046 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n"); 1047 printf("\t--minimal\tMinimal (terse) output\n"); 1048 printf("\t--version\tPrint version info and exit\n"); 1049 printf("\t--terse-version=x Terse version output format\n"); 1050 printf("\t--help\t\tPrint this page\n"); 1051 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of" 1052 " them\n"); 1053 printf("\t--showcmd\tTurn a job file into command line options\n"); 1054 printf("\t--eta=when\tWhen ETA estimate should be printed\n"); 1055 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n"); 1056 printf("\t--readonly\tTurn on safety read-only checks, preventing" 1057 " writes\n"); 1058 printf("\t--section=name\tOnly run specified section in job file\n"); 1059 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb" 1060 " (def 1024)\n"); 1061 printf("\t--warnings-fatal Fio parser warnings are fatal\n"); 1062 printf("\t--max-jobs\tMaximum number of threads/processes to support\n"); 1063 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>"); 1064 printf("\n Jens Axboe <jaxboe@fusionio.com>\n"); 1065} 1066 1067#ifdef FIO_INC_DEBUG 1068struct debug_level debug_levels[] = { 1069 { .name = "process", .shift = FD_PROCESS, }, 1070 { .name = "file", .shift = FD_FILE, }, 1071 { .name = "io", .shift = FD_IO, }, 1072 { .name = "mem", .shift = FD_MEM, }, 1073 { .name = "blktrace", .shift = FD_BLKTRACE }, 1074 { .name = "verify", .shift = FD_VERIFY }, 1075 { .name = "random", .shift = FD_RANDOM }, 1076 { .name = "parse", .shift = FD_PARSE }, 1077 { .name = "diskutil", .shift = FD_DISKUTIL }, 1078 { .name = "job", .shift = FD_JOB }, 1079 { .name = "mutex", .shift = FD_MUTEX }, 1080 { .name = "profile", .shift = FD_PROFILE }, 1081 { .name = "time", .shift = FD_TIME }, 1082 { .name = NULL, }, 1083}; 1084 1085static int set_debug(const char *string) 1086{ 1087 struct debug_level *dl; 1088 char *p = (char *) string; 1089 char *opt; 1090 int i; 1091 1092 if (!strcmp(string, "?") || !strcmp(string, "help")) { 1093 log_info("fio: dumping debug options:"); 1094 for (i = 0; debug_levels[i].name; i++) { 1095 dl = &debug_levels[i]; 1096 log_info("%s,", dl->name); 1097 } 1098 log_info("all\n"); 1099 return 1; 1100 } 1101 1102 while ((opt = strsep(&p, ",")) != NULL) { 1103 int found = 0; 1104 1105 if (!strncmp(opt, "all", 3)) { 1106 log_info("fio: set all debug options\n"); 1107 fio_debug = ~0UL; 1108 continue; 1109 } 1110 1111 for (i = 0; debug_levels[i].name; i++) { 1112 dl = &debug_levels[i]; 1113 found = !strncmp(opt, dl->name, strlen(dl->name)); 1114 if (!found) 1115 continue; 1116 1117 if (dl->shift == FD_JOB) { 1118 opt = strchr(opt, ':'); 1119 if (!opt) { 1120 log_err("fio: missing job number\n"); 1121 break; 1122 } 1123 opt++; 1124 fio_debug_jobno = atoi(opt); 1125 log_info("fio: set debug jobno %d\n", 1126 fio_debug_jobno); 1127 } else { 1128 log_info("fio: set debug option %s\n", opt); 1129 fio_debug |= (1UL << dl->shift); 1130 } 1131 break; 1132 } 1133 1134 if (!found) 1135 log_err("fio: debug mask %s not found\n", opt); 1136 } 1137 return 0; 1138} 1139#else 1140static int set_debug(const char *string) 1141{ 1142 log_err("fio: debug tracing not included in build\n"); 1143 return 1; 1144} 1145#endif 1146 1147static void fio_options_fill_optstring(void) 1148{ 1149 char *ostr = cmd_optstr; 1150 int i, c; 1151 1152 c = i = 0; 1153 while (l_opts[i].name) { 1154 ostr[c++] = l_opts[i].val; 1155 if (l_opts[i].has_arg == required_argument) 1156 ostr[c++] = ':'; 1157 else if (l_opts[i].has_arg == optional_argument) { 1158 ostr[c++] = ':'; 1159 ostr[c++] = ':'; 1160 } 1161 i++; 1162 } 1163 ostr[c] = '\0'; 1164} 1165 1166static int parse_cmd_line(int argc, char *argv[]) 1167{ 1168 struct thread_data *td = NULL; 1169 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0; 1170 char *ostr = cmd_optstr; 1171 1172 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) { 1173 switch (c) { 1174 case 'a': 1175 smalloc_pool_size = atoi(optarg); 1176 break; 1177 case 't': 1178 def_timeout = atoi(optarg); 1179 break; 1180 case 'l': 1181 write_lat_log = 1; 1182 break; 1183 case 'b': 1184 write_bw_log = 1; 1185 break; 1186 case 'o': 1187 f_out = fopen(optarg, "w+"); 1188 if (!f_out) { 1189 perror("fopen output"); 1190 exit(1); 1191 } 1192 f_err = f_out; 1193 break; 1194 case 'm': 1195 terse_output = 1; 1196 break; 1197 case 'h': 1198 usage(argv[0]); 1199 exit(0); 1200 case 'c': 1201 exit(fio_show_option_help(optarg)); 1202 case 's': 1203 dump_cmdline = 1; 1204 break; 1205 case 'r': 1206 read_only = 1; 1207 break; 1208 case 'v': 1209 log_info("%s\n", fio_version_string); 1210 exit(0); 1211 case 'V': 1212 terse_version = atoi(optarg); 1213 if (terse_version != 2) { 1214 log_err("fio: bad terse version format\n"); 1215 exit_val = 1; 1216 do_exit++; 1217 } 1218 break; 1219 case 'e': 1220 if (!strcmp("always", optarg)) 1221 eta_print = FIO_ETA_ALWAYS; 1222 else if (!strcmp("never", optarg)) 1223 eta_print = FIO_ETA_NEVER; 1224 break; 1225 case 'd': 1226 if (set_debug(optarg)) 1227 do_exit++; 1228 break; 1229 case 'x': { 1230 size_t new_size; 1231 1232 if (!strcmp(optarg, "global")) { 1233 log_err("fio: can't use global as only " 1234 "section\n"); 1235 do_exit++; 1236 exit_val = 1; 1237 break; 1238 } 1239 new_size = (nr_job_sections + 1) * sizeof(char *); 1240 job_sections = realloc(job_sections, new_size); 1241 job_sections[nr_job_sections] = strdup(optarg); 1242 nr_job_sections++; 1243 break; 1244 } 1245 case 'p': 1246 exec_profile = strdup(optarg); 1247 break; 1248 case FIO_GETOPT_JOB: { 1249 const char *opt = l_opts[lidx].name; 1250 char *val = optarg; 1251 1252 if (!strncmp(opt, "name", 4) && td) { 1253 ret = add_job(td, td->o.name ?: "fio", 0); 1254 if (ret) 1255 return 0; 1256 td = NULL; 1257 } 1258 if (!td) { 1259 int is_section = !strncmp(opt, "name", 4); 1260 int global = 0; 1261 1262 if (!is_section || !strncmp(val, "global", 6)) 1263 global = 1; 1264 1265 if (is_section && skip_this_section(val)) 1266 continue; 1267 1268 td = get_new_job(global, &def_thread); 1269 if (!td) 1270 return 0; 1271 } 1272 1273 ret = fio_cmd_option_parse(td, opt, val); 1274 break; 1275 } 1276 case 'w': 1277 warnings_fatal = 1; 1278 break; 1279 case 'j': 1280 max_jobs = atoi(optarg); 1281 if (!max_jobs || max_jobs > REAL_MAX_JOBS) { 1282 log_err("fio: invalid max jobs: %d\n", max_jobs); 1283 do_exit++; 1284 exit_val = 1; 1285 } 1286 break; 1287 default: 1288 do_exit++; 1289 exit_val = 1; 1290 break; 1291 } 1292 } 1293 1294 if (do_exit) 1295 exit(exit_val); 1296 1297 if (td) { 1298 if (!ret) 1299 ret = add_job(td, td->o.name ?: "fio", 0); 1300 } 1301 1302 while (optind < argc) { 1303 ini_idx++; 1304 ini_file = realloc(ini_file, ini_idx * sizeof(char *)); 1305 ini_file[ini_idx - 1] = strdup(argv[optind]); 1306 optind++; 1307 } 1308 1309 return ini_idx; 1310} 1311 1312int parse_options(int argc, char *argv[]) 1313{ 1314 int job_files, i; 1315 1316 f_out = stdout; 1317 f_err = stderr; 1318 1319 fio_options_fill_optstring(); 1320 fio_options_dup_and_init(l_opts); 1321 1322 if (setup_thread_area()) 1323 return 1; 1324 if (fill_def_thread()) 1325 return 1; 1326 1327 job_files = parse_cmd_line(argc, argv); 1328 1329 for (i = 0; i < job_files; i++) { 1330 if (fill_def_thread()) 1331 return 1; 1332 if (parse_jobs_ini(ini_file[i], i)) 1333 return 1; 1334 free(ini_file[i]); 1335 } 1336 1337 free(ini_file); 1338 options_mem_free(&def_thread); 1339 1340 if (!thread_number) { 1341 if (dump_cmdline) 1342 return 0; 1343 if (exec_profile) 1344 return 0; 1345 1346 log_err("No jobs(s) defined\n\n"); 1347 usage(argv[0]); 1348 return 1; 1349 } 1350 1351 if (def_thread.o.gtod_offload) { 1352 fio_gtod_init(); 1353 fio_gtod_offload = 1; 1354 fio_gtod_cpu = def_thread.o.gtod_cpu; 1355 } 1356 1357 log_info("%s\n", fio_version_string); 1358 return 0; 1359} 1360