2 * This file contains job initialization and setup functions.
13 #include <sys/types.h>
24 #include "lib/getopt.h"
27 static char fio_version_string[] = __fio_stringify(FIO_MAJOR) "." \
28 __fio_stringify(FIO_MINOR) "." \
29 __fio_stringify(FIO_PATCH);
31 static char fio_version_string[] = __fio_stringify(FIO_MAJOR) "." \
32 __fio_stringify(FIO_MINOR);
35 #define FIO_RANDSEED (0xb1899bedUL)
37 static char **ini_file;
38 static int max_jobs = FIO_MAX_JOBS;
39 static int dump_cmdline;
41 static struct thread_data def_thread;
42 struct thread_data *threads = NULL;
44 int exitall_on_terminate = 0;
47 unsigned long long mlock_size = 0;
50 char **job_sections = NULL;
51 int nr_job_sections = 0;
52 char *exec_profile = NULL;
53 int warnings_fatal = 0;
54 int terse_version = 2;
62 static int write_lat_log;
64 static int prev_group_jobs;
66 unsigned long fio_debug = 0;
67 unsigned int fio_debug_jobno = -1;
68 unsigned int *fio_debug_jobp = NULL;
70 static char cmd_optstr[256];
72 const fio_fp64_t def_percentile_list[FIO_IO_U_LIST_MAX_LEN] = {
91 * Command line options. These will contain the above, plus a few
92 * extra that only pertain to fio itself and not jobs.
94 static struct option l_opts[FIO_NR_OPTIONS] = {
96 .name = (char *) "output",
97 .has_arg = required_argument,
101 .name = (char *) "timeout",
102 .has_arg = required_argument,
106 .name = (char *) "latency-log",
107 .has_arg = required_argument,
111 .name = (char *) "bandwidth-log",
112 .has_arg = required_argument,
116 .name = (char *) "minimal",
117 .has_arg = optional_argument,
121 .name = (char *) "version",
122 .has_arg = no_argument,
126 .name = (char *) "help",
127 .has_arg = no_argument,
131 .name = (char *) "cmdhelp",
132 .has_arg = optional_argument,
136 .name = (char *) "showcmd",
137 .has_arg = no_argument,
141 .name = (char *) "readonly",
142 .has_arg = no_argument,
146 .name = (char *) "eta",
147 .has_arg = required_argument,
151 .name = (char *) "debug",
152 .has_arg = required_argument,
156 .name = (char *) "section",
157 .has_arg = required_argument,
161 .name = (char *) "alloc-size",
162 .has_arg = required_argument,
166 .name = (char *) "profile",
167 .has_arg = required_argument,
171 .name = (char *) "warnings-fatal",
172 .has_arg = no_argument,
176 .name = (char *) "max-jobs",
177 .has_arg = required_argument,
181 .name = (char *) "terse-version",
182 .has_arg = required_argument,
186 .name = (char *) "server",
187 .has_arg = no_argument,
190 { .name = (char *) "daemonize",
191 .has_arg = no_argument,
195 .name = (char *) "net-port",
196 .has_arg = required_argument,
200 .name = (char *) "client",
201 .has_arg = required_argument,
209 static void free_shm(void)
211 struct shmid_ds sbuf;
218 fio_debug_jobp = NULL;
220 shmctl(shm_id, IPC_RMID, &sbuf);
227 * The thread area is shared between the main process and the job
228 * threads/processes. So setup a shared memory segment that will hold
229 * all the job info. We use the end of the region for keeping track of
230 * open files across jobs, for file sharing.
232 static int setup_thread_area(void)
240 * 1024 is too much on some machines, scale max_jobs if
241 * we get a failure that looks like too large a shm segment
244 size_t size = max_jobs * sizeof(struct thread_data);
246 size += file_hash_size;
247 size += sizeof(unsigned int);
249 shm_id = shmget(0, size, IPC_CREAT | 0600);
252 if (errno != EINVAL) {
263 threads = shmat(shm_id, NULL, 0);
264 if (threads == (void *) -1) {
269 memset(threads, 0, max_jobs * sizeof(struct thread_data));
270 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
271 fio_debug_jobp = (void *) hash + file_hash_size;
272 *fio_debug_jobp = -1;
273 file_hash_init(hash);
278 * Return a free job structure.
280 static struct thread_data *get_new_job(int global, struct thread_data *parent)
282 struct thread_data *td;
286 if (setup_thread_area()) {
287 log_err("error: failed to setup shm segment\n");
290 if (thread_number >= max_jobs) {
291 log_err("error: maximum number of jobs (%d) reached.\n",
296 td = &threads[thread_number++];
299 td->o.uid = td->o.gid = -1U;
301 dup_files(td, parent);
302 options_mem_dupe(td);
304 profile_add_hooks(td);
306 td->thread_number = thread_number;
310 static void put_job(struct thread_data *td)
312 if (td == &def_thread)
318 log_info("fio: %s\n", td->verror);
320 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
324 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
326 unsigned int bs = td->o.min_bs[ddir];
327 unsigned long long bytes_per_sec;
329 assert(ddir_rw(ddir));
331 if (td->o.rate[ddir])
332 bytes_per_sec = td->o.rate[ddir];
334 bytes_per_sec = td->o.rate_iops[ddir] * bs;
336 if (!bytes_per_sec) {
337 log_err("rate lower than supported\n");
341 td->rate_nsec_cycle[ddir] = 1000000000ULL / bytes_per_sec;
342 td->rate_pending_usleep[ddir] = 0;
346 static int setup_rate(struct thread_data *td)
350 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
351 ret = __setup_rate(td, DDIR_READ);
352 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
353 ret |= __setup_rate(td, DDIR_WRITE);
358 static int fixed_block_size(struct thread_options *o)
360 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
361 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
362 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
366 * Lazy way of fixing up options that depend on each other. We could also
367 * define option callback handlers, but this is easier.
369 static int fixup_options(struct thread_data *td)
371 struct thread_options *o = &td->o;
374 #ifndef FIO_HAVE_PSHARED_MUTEX
375 if (!o->use_thread) {
376 log_info("fio: this platform does not support process shared"
377 " mutexes, forcing use of threads. Use the 'thread'"
378 " option to get rid of this warning.\n");
380 ret = warnings_fatal;
384 if (o->write_iolog_file && o->read_iolog_file) {
385 log_err("fio: read iolog overrides write_iolog\n");
386 free(o->write_iolog_file);
387 o->write_iolog_file = NULL;
388 ret = warnings_fatal;
392 * only really works for sequential io for now, and with 1 file
394 if (o->zone_size && td_random(td) && o->open_files == 1)
398 * Reads can do overwrites, we always need to pre-create the file
400 if (td_read(td) || td_rw(td))
403 if (!o->min_bs[DDIR_READ])
404 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
405 if (!o->max_bs[DDIR_READ])
406 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
407 if (!o->min_bs[DDIR_WRITE])
408 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
409 if (!o->max_bs[DDIR_WRITE])
410 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
412 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
415 * For random IO, allow blockalign offset other than min_bs.
417 if (!o->ba[DDIR_READ] || !td_random(td))
418 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
419 if (!o->ba[DDIR_WRITE] || !td_random(td))
420 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
422 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
423 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
425 log_err("fio: Any use of blockalign= turns off randommap\n");
427 ret = warnings_fatal;
430 if (!o->file_size_high)
431 o->file_size_high = o->file_size_low;
433 if (o->norandommap && o->verify != VERIFY_NONE
434 && !fixed_block_size(o)) {
435 log_err("fio: norandommap given for variable block sizes, "
436 "verify disabled\n");
437 o->verify = VERIFY_NONE;
438 ret = warnings_fatal;
440 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
441 log_err("fio: bs_unaligned may not work with raw io\n");
444 * thinktime_spin must be less than thinktime
446 if (o->thinktime_spin > o->thinktime)
447 o->thinktime_spin = o->thinktime;
450 * The low water mark cannot be bigger than the iodepth
452 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
454 * syslet work around - if the workload is sequential,
455 * we want to let the queue drain all the way down to
456 * avoid seeking between async threads
458 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
461 o->iodepth_low = o->iodepth;
465 * If batch number isn't set, default to the same as iodepth
467 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
468 o->iodepth_batch = o->iodepth;
470 if (o->nr_files > td->files_index)
471 o->nr_files = td->files_index;
473 if (o->open_files > o->nr_files || !o->open_files)
474 o->open_files = o->nr_files;
476 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))||
477 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] +
478 o->rate_iops_min[1]))) {
479 log_err("fio: rate and rate_iops are mutually exclusive\n");
482 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) ||
483 (o->rate_iops[0] < o->rate_iops_min[0]) ||
484 (o->rate_iops[1] < o->rate_iops_min[1])) {
485 log_err("fio: minimum rate exceeds rate\n");
489 if (!o->timeout && o->time_based) {
490 log_err("fio: time_based requires a runtime/timeout setting\n");
492 ret = warnings_fatal;
495 if (o->fill_device && !o->size)
498 if (o->verify != VERIFY_NONE) {
499 if (td_write(td) && o->do_verify && o->numjobs > 1) {
500 log_info("Multiple writers may overwrite blocks that "
501 "belong to other jobs. This can cause "
502 "verification failures.\n");
503 ret = warnings_fatal;
506 o->refill_buffers = 1;
507 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
509 o->verify_interval = o->min_bs[DDIR_WRITE];
513 o->invalidate_cache = 0;
514 if (td->io_ops->flags & FIO_PIPEIO) {
515 log_info("fio: cannot pre-read files with an IO engine"
516 " that isn't seekable. Pre-read disabled.\n");
517 ret = warnings_fatal;
521 #ifndef FIO_HAVE_FDATASYNC
522 if (o->fdatasync_blocks) {
523 log_info("fio: this platform does not support fdatasync()"
524 " falling back to using fsync(). Use the 'fsync'"
525 " option instead of 'fdatasync' to get rid of"
527 o->fsync_blocks = o->fdatasync_blocks;
528 o->fdatasync_blocks = 0;
529 ret = warnings_fatal;
537 * This function leaks the buffer
539 static char *to_kmg(unsigned int val)
541 char *buf = malloc(32);
542 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
553 snprintf(buf, 31, "%u%c", val, *p);
557 /* External engines are specified by "external:name.o") */
558 static const char *get_engine_name(const char *str)
560 char *p = strstr(str, ":");
566 strip_blank_front(&p);
571 static int exists_and_not_file(const char *filename)
575 if (lstat(filename, &sb) == -1)
578 /* \\.\ is the device namespace in Windows, where every file
579 * is a device node */
580 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
586 static void td_fill_rand_seeds_os(struct thread_data *td)
588 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
589 os_random_seed(td->rand_seeds[1], &td->verify_state);
590 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
592 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
593 os_random_seed(td->rand_seeds[3], &td->next_file_state);
595 os_random_seed(td->rand_seeds[5], &td->file_size_state);
596 os_random_seed(td->rand_seeds[6], &td->trim_state);
601 if (td->o.rand_repeatable)
602 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
604 os_random_seed(td->rand_seeds[4], &td->random_state);
607 static void td_fill_rand_seeds_internal(struct thread_data *td)
609 init_rand_seed(&td->__bsrange_state, td->rand_seeds[0]);
610 init_rand_seed(&td->__verify_state, td->rand_seeds[1]);
611 init_rand_seed(&td->__rwmix_state, td->rand_seeds[2]);
613 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
614 init_rand_seed(&td->__next_file_state, td->rand_seeds[3]);
616 init_rand_seed(&td->__file_size_state, td->rand_seeds[5]);
617 init_rand_seed(&td->__trim_state, td->rand_seeds[6]);
622 if (td->o.rand_repeatable)
623 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
625 init_rand_seed(&td->__random_state, td->rand_seeds[4]);
628 void td_fill_rand_seeds(struct thread_data *td)
630 if (td->o.use_os_rand)
631 td_fill_rand_seeds_os(td);
633 td_fill_rand_seeds_internal(td);
635 init_rand_seed(&td->buf_state, td->rand_seeds[7]);
639 * Initialize the various random states we need (random io, block size ranges,
640 * read/write mix, etc).
642 static int init_random_state(struct thread_data *td)
646 fd = open("/dev/urandom", O_RDONLY);
648 td_verror(td, errno, "open");
652 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
653 (int) sizeof(td->rand_seeds)) {
654 td_verror(td, EIO, "read");
660 td_fill_rand_seeds(td);
665 * Adds a job to the list of things todo. Sanitizes the various options
666 * to make sure we don't have conflicts, and initializes various
669 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
671 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
672 "randread", "randwrite", "randrw" };
675 char fname[PATH_MAX];
676 int numjobs, file_alloced;
679 * the def_thread is just for options, it's not a real job
681 if (td == &def_thread)
685 * if we are just dumping the output command line, don't add the job
692 if (profile_td_init(td))
695 engine = get_engine_name(td->o.ioengine);
696 td->io_ops = load_ioengine(td, engine);
698 log_err("fio: failed to load engine %s\n", engine);
702 if (td->o.use_thread)
708 td->io_ops->flags |= FIO_RAWIO;
711 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
714 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
715 add_file(td, jobname);
717 for (i = 0; i < td->o.nr_files; i++) {
718 sprintf(fname, "%s.%d.%d", jobname,
719 td->thread_number, i);
725 if (fixup_options(td))
728 if (td->io_ops->flags & FIO_DISKLESSIO) {
731 for_each_file(td, f, i)
732 f->real_file_size = -1ULL;
735 td->mutex = fio_mutex_init(0);
737 td->ts.clat_percentiles = td->o.clat_percentiles;
738 if (td->o.overwrite_plist)
739 memcpy(td->ts.percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
741 memcpy(td->ts.percentile_list, def_percentile_list, sizeof(def_percentile_list));
743 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
744 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
745 td->ts.lat_stat[0].min_val = td->ts.lat_stat[1].min_val = ULONG_MAX;
746 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
747 td->ddir_seq_nr = td->o.ddir_seq_nr;
749 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
754 td->groupid = groupid;
757 if (init_random_state(td))
763 if (td->o.write_lat_log) {
764 setup_log(&td->lat_log);
765 setup_log(&td->slat_log);
766 setup_log(&td->clat_log);
768 if (td->o.write_bw_log)
769 setup_log(&td->bw_log);
772 td->o.name = strdup(jobname);
776 if (!strcmp(td->io_ops->name, "cpuio")) {
777 log_info("%s: ioengine=cpu, cpuload=%u,"
778 " cpucycle=%u\n", td->o.name,
782 char *c1, *c2, *c3, *c4;
784 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
785 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
786 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
787 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
789 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
790 " ioengine=%s, iodepth=%u\n",
791 td->o.name, td->groupid,
792 ddir_str[td->o.td_ddir],
802 } else if (job_add_num == 1)
807 * recurse add identical jobs, clear numjobs and stonewall options
808 * as they don't apply to sub-jobs
810 numjobs = td->o.numjobs;
812 struct thread_data *td_new = get_new_job(0, td);
817 td_new->o.numjobs = 1;
818 td_new->o.stonewall = 0;
819 td_new->o.new_group = 0;
822 td_new->o.filename = NULL;
823 td_new->files_index = 0;
824 td_new->files_size = 0;
825 td_new->files = NULL;
828 job_add_num = numjobs - 1;
830 if (add_job(td_new, jobname, job_add_num))
841 * Parse as if 'o' was a command line
843 void add_job_opts(const char **o)
845 struct thread_data *td, *td_parent;
846 int i, in_global = 1;
850 td_parent = td = NULL;
852 if (!strncmp(o[i], "name", 4)) {
855 add_job(td, jobname, 0);
857 sprintf(jobname, "%s", o[i] + 5);
859 if (in_global && !td_parent)
860 td_parent = get_new_job(1, &def_thread);
861 else if (!in_global && !td) {
863 td_parent = &def_thread;
864 td = get_new_job(0, td_parent);
867 fio_options_parse(td_parent, (char **) &o[i], 1);
869 fio_options_parse(td, (char **) &o[i], 1);
874 add_job(td, jobname, 0);
877 static int skip_this_section(const char *name)
881 if (!nr_job_sections)
883 if (!strncmp(name, "global", 6))
886 for (i = 0; i < nr_job_sections; i++)
887 if (!strcmp(job_sections[i], name))
893 static int is_empty_or_comment(char *line)
897 for (i = 0; i < strlen(line); i++) {
902 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
910 * This is our [ini] type file parser.
912 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag)
915 struct thread_data *td;
919 int ret = 0, stonewall;
924 int i, alloc_opts, num_opts;
929 if (!strcmp(file, "-"))
932 f = fopen(file, "r");
935 perror("fopen job file");
940 string = malloc(4096);
943 * it's really 256 + small bit, 280 should suffice
946 memset(name, 0, 280);
949 opts = malloc(sizeof(char *) * alloc_opts);
952 stonewall = stonewall_flag;
955 * if skip_fgets is set, we already have loaded a line we
960 p = strsep(&file, "\n");
962 p = fgets(string, 4095, f);
968 strip_blank_front(&p);
971 if (is_empty_or_comment(p))
973 if (sscanf(p, "[%255[^\n]]", name) != 1) {
976 log_err("fio: option <%s> outside of [] job section\n",
981 name[strlen(name) - 1] = '\0';
983 if (skip_this_section(name)) {
989 global = !strncmp(name, "global", 6);
995 log_info("--name=%s ", name);
999 td = get_new_job(global, &def_thread);
1006 * Seperate multiple job files by a stonewall
1008 if (!global && stonewall) {
1009 td->o.stonewall = stonewall;
1014 memset(opts, 0, alloc_opts * sizeof(char *));
1018 p = strsep(&file, "\n");
1020 p = fgets(string, 4096, f);
1024 if (is_empty_or_comment(p))
1027 strip_blank_front(&p);
1030 * new section, break out and make sure we don't
1031 * fgets() a new line at the top.
1040 if (num_opts == alloc_opts) {
1042 opts = realloc(opts,
1043 alloc_opts * sizeof(char *));
1046 opts[num_opts] = strdup(p);
1050 ret = fio_options_parse(td, opts, num_opts);
1053 for (i = 0; i < num_opts; i++)
1054 log_info("--%s ", opts[i]);
1056 ret = add_job(td, name, 0);
1058 log_err("fio: job %s dropped\n", name);
1062 for (i = 0; i < num_opts; i++)
1070 for (i = 0; i < num_opts; i++)
1076 if (!is_buf && f != stdin)
1081 static int fill_def_thread(void)
1083 memset(&def_thread, 0, sizeof(def_thread));
1085 fio_getaffinity(getpid(), &def_thread.o.cpumask);
1088 * fill default options
1090 fio_fill_default_options(&def_thread);
1094 static void usage(const char *name)
1096 printf("%s\n", fio_version_string);
1097 printf("%s [options] [job options] <job file(s)>\n", name);
1098 printf("\t--debug=options\tEnable debug logging\n");
1099 printf("\t--output\tWrite output to file\n");
1100 printf("\t--timeout\tRuntime in seconds\n");
1101 printf("\t--latency-log\tGenerate per-job latency logs\n");
1102 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1103 printf("\t--minimal\tMinimal (terse) output\n");
1104 printf("\t--version\tPrint version info and exit\n");
1105 printf("\t--terse-version=x Terse version output format\n");
1106 printf("\t--help\t\tPrint this page\n");
1107 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
1109 printf("\t--showcmd\tTurn a job file into command line options\n");
1110 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
1111 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
1112 printf("\t--readonly\tTurn on safety read-only checks, preventing"
1114 printf("\t--section=name\tOnly run specified section in job file\n");
1115 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
1117 printf("\t--warnings-fatal Fio parser warnings are fatal\n");
1118 printf("\t--max-jobs\tMaximum number of threads/processes to support\n");
1119 printf("\t--server\tStart a backend fio server\n");
1120 printf("\t--client=hostname Talk to remove backend fio server at hostname\n");
1121 printf("\t--net-port=port\tUse specified port for client/server connection\n");
1122 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1123 printf("\n Jens Axboe <jaxboe@fusionio.com>\n");
1126 #ifdef FIO_INC_DEBUG
1127 struct debug_level debug_levels[] = {
1128 { .name = "process", .shift = FD_PROCESS, },
1129 { .name = "file", .shift = FD_FILE, },
1130 { .name = "io", .shift = FD_IO, },
1131 { .name = "mem", .shift = FD_MEM, },
1132 { .name = "blktrace", .shift = FD_BLKTRACE },
1133 { .name = "verify", .shift = FD_VERIFY },
1134 { .name = "random", .shift = FD_RANDOM },
1135 { .name = "parse", .shift = FD_PARSE },
1136 { .name = "diskutil", .shift = FD_DISKUTIL },
1137 { .name = "job", .shift = FD_JOB },
1138 { .name = "mutex", .shift = FD_MUTEX },
1139 { .name = "profile", .shift = FD_PROFILE },
1140 { .name = "time", .shift = FD_TIME },
1141 { .name = "net", .shift = FD_NET },
1145 static int set_debug(const char *string)
1147 struct debug_level *dl;
1148 char *p = (char *) string;
1152 if (!strcmp(string, "?") || !strcmp(string, "help")) {
1153 log_info("fio: dumping debug options:");
1154 for (i = 0; debug_levels[i].name; i++) {
1155 dl = &debug_levels[i];
1156 log_info("%s,", dl->name);
1162 while ((opt = strsep(&p, ",")) != NULL) {
1165 if (!strncmp(opt, "all", 3)) {
1166 log_info("fio: set all debug options\n");
1171 for (i = 0; debug_levels[i].name; i++) {
1172 dl = &debug_levels[i];
1173 found = !strncmp(opt, dl->name, strlen(dl->name));
1177 if (dl->shift == FD_JOB) {
1178 opt = strchr(opt, ':');
1180 log_err("fio: missing job number\n");
1184 fio_debug_jobno = atoi(opt);
1185 log_info("fio: set debug jobno %d\n",
1188 log_info("fio: set debug option %s\n", opt);
1189 fio_debug |= (1UL << dl->shift);
1195 log_err("fio: debug mask %s not found\n", opt);
1200 static int set_debug(const char *string)
1202 log_err("fio: debug tracing not included in build\n");
1207 static void fio_options_fill_optstring(void)
1209 char *ostr = cmd_optstr;
1213 while (l_opts[i].name) {
1214 ostr[c++] = l_opts[i].val;
1215 if (l_opts[i].has_arg == required_argument)
1217 else if (l_opts[i].has_arg == optional_argument) {
1226 int parse_cmd_line(int argc, char *argv[])
1228 struct thread_data *td = NULL;
1229 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
1230 char *ostr = cmd_optstr;
1231 int daemonize_server = 0;
1232 char *cur_client = NULL;
1235 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
1238 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1239 smalloc_pool_size = atoi(optarg);
1242 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1243 def_thread.o.timeout = atoi(optarg);
1246 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1250 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1254 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1255 f_out = fopen(optarg, "w+");
1257 perror("fopen output");
1263 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1270 exit(fio_show_option_help(optarg));
1272 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1276 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1280 log_info("%s\n", fio_version_string);
1283 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1284 terse_version = atoi(optarg);
1285 if (terse_version != 2) {
1286 log_err("fio: bad terse version format\n");
1292 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1293 if (!strcmp("always", optarg))
1294 eta_print = FIO_ETA_ALWAYS;
1295 else if (!strcmp("never", optarg))
1296 eta_print = FIO_ETA_NEVER;
1299 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1300 if (set_debug(optarg))
1306 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1308 if (!strcmp(optarg, "global")) {
1309 log_err("fio: can't use global as only "
1315 new_size = (nr_job_sections + 1) * sizeof(char *);
1316 job_sections = realloc(job_sections, new_size);
1317 job_sections[nr_job_sections] = strdup(optarg);
1322 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1323 exec_profile = strdup(optarg);
1325 case FIO_GETOPT_JOB: {
1326 const char *opt = l_opts[lidx].name;
1329 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1331 if (!strncmp(opt, "name", 4) && td) {
1332 ret = add_job(td, td->o.name ?: "fio", 0);
1338 int is_section = !strncmp(opt, "name", 4);
1341 if (!is_section || !strncmp(val, "global", 6))
1344 if (is_section && skip_this_section(val))
1347 td = get_new_job(global, &def_thread);
1352 ret = fio_cmd_option_parse(td, opt, val);
1356 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1360 fio_client_add_cmd_option(cur_client, argv[optind-1]);
1361 max_jobs = atoi(optarg);
1362 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1363 log_err("fio: invalid max jobs: %d\n", max_jobs);
1370 log_err("fio: can't be both client and server\n");
1379 daemonize_server = 1;
1382 fio_net_port = atoi(optarg);
1386 log_err("fio: can't be both client and server\n");
1391 fio_client_add(optarg);
1392 cur_client = optarg;
1406 if (nr_clients && fio_clients_connect()) {
1412 if (is_backend && backend)
1413 return fio_start_server(daemonize_server);
1417 ret = add_job(td, td->o.name ?: "fio", 0);
1420 while (optind < argc) {
1422 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1423 ini_file[ini_idx - 1] = strdup(argv[optind]);
1430 int parse_options(int argc, char *argv[])
1437 fio_options_fill_optstring();
1438 fio_options_dup_and_init(l_opts);
1442 if (fill_def_thread())
1445 job_files = parse_cmd_line(argc, argv);
1447 if (job_files > 0) {
1448 for (i = 0; i < job_files; i++) {
1449 if (fill_def_thread())
1452 if (fio_clients_send_ini(ini_file[i]))
1455 } else if (!is_backend) {
1456 if (parse_jobs_ini(ini_file[i], 0, i))
1464 options_mem_free(&def_thread);
1466 if (!thread_number) {
1471 if (is_backend || nr_clients)
1474 if (job_files > 0) {
1475 log_err("No jobs(s) defined\n\n");
1481 if (def_thread.o.gtod_offload) {
1483 fio_gtod_offload = 1;
1484 fio_gtod_cpu = def_thread.o.gtod_cpu;
1487 log_info("%s\n", fio_version_string);