2 * This file contains job initialization and setup functions.
15 #include <sys/types.h>
21 #define FIO_RANDSEED (0xb1899bedUL)
23 #define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
25 static int str_rw_cb(void *, const char *);
26 static int str_ioengine_cb(void *, const char *);
27 static int str_mem_cb(void *, const char *);
28 static int str_verify_cb(void *, const char *);
29 static int str_lockmem_cb(void *, unsigned long *);
30 #ifdef FIO_HAVE_IOPRIO
31 static int str_prio_cb(void *, unsigned int *);
32 static int str_prioclass_cb(void *, unsigned int *);
34 static int str_exitall_cb(void);
35 static int str_cpumask_cb(void *, unsigned int *);
37 #define __stringify_1(x) #x
38 #define __stringify(x) __stringify_1(x)
41 * Map of job/command line options
43 static struct fio_option options[] = {
46 .type = FIO_OPT_STR_STORE,
47 .off1 = td_var_offset(name),
48 .help = "Name of this job",
52 .type = FIO_OPT_STR_STORE,
53 .off1 = td_var_offset(directory),
54 .help = "Directory to store files in",
58 .type = FIO_OPT_STR_STORE,
59 .off1 = td_var_offset(filename),
60 .help = "Force the use of a specific file",
66 .help = "IO direction",
68 .posval = { "read", "write", "randwrite", "randread", "rw",
74 .cb = str_ioengine_cb,
75 .help = "IO engine to use",
77 .posval = { "sync", "libaio", "posixaio", "mmap", "splice",
84 .help = "Backing type for IO buffers",
86 .posval = { "malloc", "shm", "shmhuge", "mmap", "mmaphuge", },
92 .help = "Verify sum function",
94 .posval = { "crc32", "md5", },
97 .name = "write_iolog",
98 .type = FIO_OPT_STR_STORE,
99 .off1 = td_var_offset(write_iolog_file),
100 .help = "Store IO pattern to file",
103 .name = "read_iolog",
104 .type = FIO_OPT_STR_STORE,
105 .off1 = td_var_offset(read_iolog_file),
106 .help = "Playback IO pattern from file",
109 .name = "exec_prerun",
110 .type = FIO_OPT_STR_STORE,
111 .off1 = td_var_offset(exec_prerun),
112 .help = "Execute this file prior to running job",
115 .name = "exec_postrun",
116 .type = FIO_OPT_STR_STORE,
117 .off1 = td_var_offset(exec_postrun),
118 .help = "Execute this file after running job",
120 #ifdef FIO_HAVE_IOSCHED_SWITCH
122 .name = "ioscheduler",
123 .type = FIO_OPT_STR_STORE,
124 .off1 = td_var_offset(ioscheduler),
125 .help = "Use this IO scheduler on the backing device",
130 .type = FIO_OPT_STR_VAL,
131 .off1 = td_var_offset(total_file_size),
132 .help = "Size of device or file",
136 .type = FIO_OPT_STR_VAL_INT,
137 .off1 = td_var_offset(bs[DDIR_READ]),
138 .off2 = td_var_offset(bs[DDIR_WRITE]),
139 .help = "Block size unit",
144 .type = FIO_OPT_STR_VAL,
145 .off1 = td_var_offset(start_offset),
146 .help = "Start IO from this offset",
151 .type = FIO_OPT_STR_VAL,
152 .off1 = td_var_offset(zone_size),
153 .help = "Give size of an IO zone",
158 .type = FIO_OPT_STR_VAL,
159 .off1 = td_var_offset(zone_skip),
160 .help = "Space between IO zones",
165 .type = FIO_OPT_STR_VAL,
166 .cb = str_lockmem_cb,
167 .help = "Lock down this amount of memory",
172 .type = FIO_OPT_RANGE,
173 .off1 = td_var_offset(min_bs[DDIR_READ]),
174 .off2 = td_var_offset(max_bs[DDIR_READ]),
175 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
176 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
177 .help = "Set block size range",
180 .name = "randrepeat",
181 .type = FIO_OPT_BOOL,
182 .off1 = td_var_offset(rand_repeatable),
183 .help = "Use repeatable random IO pattern",
189 .off1 = td_var_offset(nr_files),
190 .help = "Split job workload between this number of files",
196 .off1 = td_var_offset(iodepth),
197 .help = "Amount of IO buffers to keep in flight",
203 .off1 = td_var_offset(fsync_blocks),
204 .help = "Issue fsync for writes every given number of blocks",
208 .name = "rwmixcycle",
210 .off1 = td_var_offset(rwmixcycle),
211 .help = "Cycle period for mixed read/write workloads (msec)",
217 .off1 = td_var_offset(rwmixread),
219 .help = "Percentage of mixed workload that is reads",
223 .name = "rwmixwrite",
225 .off1 = td_var_offset(rwmixwrite),
227 .help = "Percentage of mixed workload that is writes",
233 .off1 = td_var_offset(nice),
234 .help = "Set job CPU nice value",
239 #ifdef FIO_HAVE_IOPRIO
244 .help = "Set job IO priority value",
251 .cb = str_prioclass_cb,
252 .help = "Set job IO priority class",
260 .off1 = td_var_offset(thinktime),
261 .help = "Idle time between IO buffers",
265 .name = "thinktime_blocks",
267 .off1 = td_var_offset(thinktime_blocks),
268 .help = "IO buffer period between 'thinktime'",
274 .off1 = td_var_offset(rate),
275 .help = "Set bandwidth rate",
280 .off1 = td_var_offset(ratemin),
281 .help = "The bottom limit accepted",
286 .off1 = td_var_offset(ratecycle),
287 .help = "Window average for rate limits (msec)",
291 .name = "startdelay",
293 .off1 = td_var_offset(start_delay),
294 .help = "Only start job when this period has passed",
299 .type = FIO_OPT_STR_VAL_TIME,
300 .off1 = td_var_offset(timeout),
301 .help = "Stop workload when this amount of time has passed",
305 .name = "invalidate",
306 .type = FIO_OPT_BOOL,
307 .off1 = td_var_offset(invalidate_cache),
308 .help = "Invalidate buffer/page cache prior to running job",
313 .type = FIO_OPT_BOOL,
314 .off1 = td_var_offset(sync_io),
315 .help = "Use O_SYNC for buffered writes",
321 .off1 = td_var_offset(bw_avg_time),
322 .help = "Time window over which to calculate bandwidth (msec)",
326 .name = "create_serialize",
327 .type = FIO_OPT_BOOL,
328 .off1 = td_var_offset(create_serialize),
329 .help = "Serialize creating of job files",
333 .name = "create_fsync",
334 .type = FIO_OPT_BOOL,
335 .off1 = td_var_offset(create_fsync),
336 .help = "Fsync file after creation",
342 .off1 = td_var_offset(loops),
343 .help = "Number of times to run the job",
349 .off1 = td_var_offset(numjobs),
350 .help = "Duplicate this job this many times",
356 .off1 = td_var_offset(cpuload),
357 .help = "Use this percentage of CPU",
362 .off1 = td_var_offset(cpucycle),
363 .help = "Length of the CPU burn cycles",
367 .type = FIO_OPT_BOOL,
368 .off1 = td_var_offset(odirect),
369 .help = "Use O_DIRECT IO",
374 .type = FIO_OPT_BOOL,
375 .off1 = td_var_offset(overwrite),
376 .help = "When writing, set whether to overwrite current data",
379 #ifdef FIO_HAVE_CPU_AFFINITY
383 .cb = str_cpumask_cb,
384 .help = "CPU affinity mask",
389 .type = FIO_OPT_BOOL,
390 .off1 = td_var_offset(end_fsync),
391 .help = "Include fsync at the end of job",
396 .type = FIO_OPT_BOOL,
397 .off1 = td_var_offset(unlink),
398 .help = "Unlink created files after job has completed",
403 .type = FIO_OPT_STR_SET,
404 .cb = str_exitall_cb,
405 .help = "Terminate all jobs when one exits",
409 .type = FIO_OPT_STR_SET,
410 .off1 = td_var_offset(stonewall),
411 .help = "Insert a hard barrier between this job and previous",
415 .type = FIO_OPT_STR_SET,
416 .off1 = td_var_offset(use_thread),
417 .help = "Use threads instead of forks",
420 .name = "write_bw_log",
421 .type = FIO_OPT_STR_SET,
422 .off1 = td_var_offset(write_bw_log),
423 .help = "Write log of bandwidth during run",
426 .name = "write_lat_log",
427 .type = FIO_OPT_STR_SET,
428 .off1 = td_var_offset(write_lat_log),
429 .help = "Write log of latency during run",
432 .name = "norandommap",
433 .type = FIO_OPT_STR_SET,
434 .off1 = td_var_offset(norandommap),
435 .help = "Accept potential duplicate random blocks",
438 .name = "bs_unaligned",
439 .type = FIO_OPT_STR_SET,
440 .off1 = td_var_offset(bs_unaligned),
441 .help = "Don't sector align IO buffer sizes",
444 .name = "hugepage-size",
445 .type = FIO_OPT_STR_VAL,
446 .off1 = td_var_offset(hugepage_size),
447 .help = "When using hugepages, specify size of each page",
448 .def = __stringify(FIO_HUGE_PAGE),
455 #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
456 #define FIO_CMD_OPTS (16)
457 #define FIO_GETOPT_JOB (0x89988998)
460 * Command line options. These will contain the above, plus a few
461 * extra that only pertain to fio itself and not jobs.
463 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
466 .has_arg = required_argument,
471 .has_arg = required_argument,
475 .name = "latency-log",
476 .has_arg = required_argument,
480 .name = "bandwidth-log",
481 .has_arg = required_argument,
486 .has_arg = optional_argument,
491 .has_arg = no_argument,
496 .has_arg = no_argument,
501 .has_arg = required_argument,
509 static int def_timeout = 0;
511 static char fio_version_string[] = "fio 1.11";
513 static char **ini_file;
514 static int max_jobs = MAX_JOBS;
516 struct thread_data def_thread;
517 struct thread_data *threads = NULL;
519 int exitall_on_terminate = 0;
520 int terse_output = 0;
521 unsigned long long mlock_size = 0;
525 static int write_lat_log = 0;
526 static int write_bw_log = 0;
529 * Return a free job structure.
531 static struct thread_data *get_new_job(int global, struct thread_data *parent)
533 struct thread_data *td;
537 if (thread_number >= max_jobs)
540 td = &threads[thread_number++];
543 td->thread_number = thread_number;
547 static void put_job(struct thread_data *td)
549 if (td == &def_thread)
552 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
557 * Lazy way of fixing up options that depend on each other. We could also
558 * define option callback handlers, but this is easier.
560 static void fixup_options(struct thread_data *td)
562 if (!td->rwmixread && td->rwmixwrite)
563 td->rwmixread = 100 - td->rwmixwrite;
565 if (td->write_iolog_file && td->read_iolog_file) {
566 log_err("fio: read iolog overrides write_iolog\n");
567 free(td->write_iolog_file);
568 td->write_iolog_file = NULL;
571 if (td->io_ops->flags & FIO_SYNCIO)
575 td->iodepth = td->nr_files;
579 * only really works for sequential io for now, and with 1 file
581 if (td->zone_size && !td->sequential && td->nr_files == 1)
585 * Reads can do overwrites, we always need to pre-create the file
587 if (td_read(td) || td_rw(td))
590 if (!td->min_bs[DDIR_READ])
591 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
592 if (!td->max_bs[DDIR_READ])
593 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
594 if (!td->min_bs[DDIR_WRITE])
595 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
596 if (!td->max_bs[DDIR_WRITE])
597 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
599 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
601 if (td_read(td) && !td_rw(td))
604 if (td->norandommap && td->verify != VERIFY_NONE) {
605 log_err("fio: norandommap given, verify disabled\n");
606 td->verify = VERIFY_NONE;
608 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
609 log_err("fio: bs_unaligned may not work with raw io\n");
612 * O_DIRECT and char doesn't mix, clear that flag if necessary.
614 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
619 * This function leaks the buffer
621 static char *to_kmg(unsigned int val)
623 char *buf = malloc(32);
624 char post[] = { 0, 'K', 'M', 'G', 'P', 0 };
635 snprintf(buf, 31, "%u%c", val, *p);
640 * Adds a job to the list of things todo. Sanitizes the various options
641 * to make sure we don't have conflicts, and initializes various
644 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
646 const char *ddir_str[] = { "read", "write", "randread", "randwrite",
647 "rw", NULL, "randrw" };
649 int numjobs, ddir, i;
653 * the def_thread is just for options, it's not a real job
655 if (td == &def_thread)
661 td->io_ops->flags |= FIO_RAWIO;
663 td->filetype = FIO_TYPE_FILE;
664 if (!stat(jobname, &sb)) {
665 if (S_ISBLK(sb.st_mode))
666 td->filetype = FIO_TYPE_BD;
667 else if (S_ISCHR(sb.st_mode))
668 td->filetype = FIO_TYPE_CHAR;
674 td->nr_uniq_files = 1;
676 td->nr_uniq_files = td->nr_files;
678 if (td->filetype == FIO_TYPE_FILE || td->filename) {
682 if (td->directory && td->directory[0] != '\0')
683 len = sprintf(tmp, "%s/", td->directory);
685 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
687 for_each_file(td, f, i) {
688 memset(f, 0, sizeof(*f));
692 sprintf(tmp + len, "%s", td->filename);
694 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
695 f->file_name = strdup(tmp);
699 td->files = malloc(sizeof(struct fio_file));
702 memset(f, 0, sizeof(*f));
704 f->file_name = strdup(jobname);
707 for_each_file(td, f, i) {
708 f->file_size = td->total_file_size / td->nr_files;
709 f->file_offset = td->start_offset;
712 fio_sem_init(&td->mutex, 0);
714 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
715 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
716 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
718 if (td->stonewall && td->thread_number > 1)
721 td->groupid = groupid;
726 if (td->write_lat_log) {
727 setup_log(&td->slat_log);
728 setup_log(&td->clat_log);
730 if (td->write_bw_log)
731 setup_log(&td->bw_log);
734 td->name = strdup(jobname);
736 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
740 if (td->io_ops->flags & FIO_CPUIO)
741 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
743 char *c1, *c2, *c3, *c4;
745 c1 = to_kmg(td->min_bs[DDIR_READ]);
746 c2 = to_kmg(td->max_bs[DDIR_READ]);
747 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
748 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
750 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%u, bs=%s-%s/%s-%s, rate=%u, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[ddir], td->odirect, c1, c2, c3, c4, td->rate, td->io_ops->name, td->iodepth);
757 } else if (job_add_num == 1)
758 fprintf(f_out, "...\n");
762 * recurse add identical jobs, clear numjobs and stonewall options
763 * as they don't apply to sub-jobs
765 numjobs = td->numjobs;
767 struct thread_data *td_new = get_new_job(0, td);
773 td_new->stonewall = 0;
774 job_add_num = numjobs - 1;
776 if (add_job(td_new, jobname, job_add_num))
786 * Initialize the various random states we need (random io, block size ranges,
787 * read/write mix, etc).
789 int init_random_state(struct thread_data *td)
791 unsigned long seeds[4];
792 int fd, num_maps, blocks, i;
795 if (td->io_ops->flags & FIO_CPUIO)
798 fd = open("/dev/urandom", O_RDONLY);
800 td_verror(td, errno);
804 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
812 os_random_seed(seeds[0], &td->bsrange_state);
813 os_random_seed(seeds[1], &td->verify_state);
814 os_random_seed(seeds[2], &td->rwmix_state);
819 if (td->rand_repeatable)
820 seeds[3] = FIO_RANDSEED;
822 if (!td->norandommap) {
823 for_each_file(td, f, i) {
824 blocks = (f->file_size + td->rw_min_bs - 1) / td->rw_min_bs;
825 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
826 f->file_map = malloc(num_maps * sizeof(long));
827 f->num_maps = num_maps;
828 memset(f->file_map, 0, num_maps * sizeof(long));
832 os_random_seed(seeds[3], &td->random_state);
836 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
838 #ifdef FIO_HAVE_CPU_AFFINITY
843 for (i = 0; i < sizeof(int) * 8; i++) {
845 CPU_SET(i, &cpumask);
850 static int is_empty_or_comment(char *line)
854 for (i = 0; i < strlen(line); i++) {
857 if (!isspace(line[i]) && !iscntrl(line[i]))
864 static int str_rw_cb(void *data, const char *mem)
866 struct thread_data *td = data;
868 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
869 td->ddir = DDIR_READ;
872 } else if (!strncmp(mem, "randread", 8)) {
873 td->ddir = DDIR_READ;
876 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
877 td->ddir = DDIR_WRITE;
880 } else if (!strncmp(mem, "randwrite", 9)) {
881 td->ddir = DDIR_WRITE;
884 } else if (!strncmp(mem, "rw", 2)) {
885 td->ddir = DDIR_READ;
889 } else if (!strncmp(mem, "randrw", 6)) {
890 td->ddir = DDIR_READ;
896 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
900 static int str_verify_cb(void *data, const char *mem)
902 struct thread_data *td = data;
904 if (!strncmp(mem, "0", 1)) {
905 td->verify = VERIFY_NONE;
907 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
908 td->verify = VERIFY_MD5;
910 } else if (!strncmp(mem, "crc32", 5)) {
911 td->verify = VERIFY_CRC32;
915 log_err("fio: verify types: md5, crc32\n");
920 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
922 static char *get_mmap_file(const char *str)
924 char *p = strstr(str, ":");
930 strip_blank_front(&p);
935 static int str_mem_cb(void *data, const char *mem)
937 struct thread_data *td = data;
939 if (!strncmp(mem, "malloc", 6)) {
940 td->mem_type = MEM_MALLOC;
942 } else if (!strncmp(mem, "mmaphuge", 8)) {
943 #ifdef FIO_HAVE_HUGETLB
945 * mmaphuge must be appended with the actual file
947 td->mmapfile = get_mmap_file(mem);
949 log_err("fio: mmaphuge:/path/to/file\n");
953 td->mem_type = MEM_MMAPHUGE;
956 log_err("fio: mmaphuge not available\n");
959 } else if (!strncmp(mem, "mmap", 4)) {
961 * Check if the user wants file backed memory. It's ok
962 * if there's no file given, we'll just use anon mamp then.
964 td->mmapfile = get_mmap_file(mem);
965 td->mem_type = MEM_MMAP;
967 } else if (!strncmp(mem, "shmhuge", 7)) {
968 #ifdef FIO_HAVE_HUGETLB
969 td->mem_type = MEM_SHMHUGE;
972 log_err("fio: shmhuge not available\n");
975 } else if (!strncmp(mem, "shm", 3)) {
976 td->mem_type = MEM_SHM;
980 log_err("fio: mem type: malloc, shm, shmhuge, mmap, mmaphuge\n");
984 static int str_ioengine_cb(void *data, const char *str)
986 struct thread_data *td = data;
988 td->io_ops = load_ioengine(td, str);
992 log_err("fio: ioengine= libaio, posixaio, sync, mmap, sgio, splice, cpu, null\n");
993 log_err("fio: or specify path to dynamic ioengine module\n");
997 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1003 #ifdef FIO_HAVE_IOPRIO
1004 static int str_prioclass_cb(void *data, unsigned int *val)
1006 struct thread_data *td = data;
1008 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1012 static int str_prio_cb(void *data, unsigned int *val)
1014 struct thread_data *td = data;
1021 static int str_exitall_cb(void)
1023 exitall_on_terminate = 1;
1027 static int str_cpumask_cb(void *data, unsigned int *val)
1029 struct thread_data *td = data;
1031 fill_cpu_mask(td->cpumask, *val);
1036 * This is our [ini] type file parser.
1038 static int parse_jobs_ini(char *file, int stonewall_flag)
1040 unsigned int global;
1041 struct thread_data *td;
1042 char *string, *name;
1046 int ret = 0, stonewall;
1048 f = fopen(file, "r");
1050 perror("fopen job file");
1054 string = malloc(4096);
1056 memset(name, 0, 256);
1058 stonewall = stonewall_flag;
1060 p = fgets(string, 4095, f);
1063 if (is_empty_or_comment(p))
1065 if (sscanf(p, "[%255s]", name) != 1)
1068 global = !strncmp(name, "global", 6);
1070 name[strlen(name) - 1] = '\0';
1072 td = get_new_job(global, &def_thread);
1079 * Seperate multiple job files by a stonewall
1081 if (!global && stonewall) {
1082 td->stonewall = stonewall;
1087 while ((p = fgets(string, 4096, f)) != NULL) {
1088 if (is_empty_or_comment(p))
1091 strip_blank_front(&p);
1101 * Don't break here, continue parsing options so we
1102 * dump all the bad ones. Makes trial/error fixups
1103 * easier on the user.
1105 ret |= parse_option(p, options, td);
1110 ret = add_job(td, name, 0);
1112 log_err("fio: job %s dropped\n", name);
1123 static int fill_def_thread(void)
1125 memset(&def_thread, 0, sizeof(def_thread));
1127 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1128 perror("sched_getaffinity");
1133 * fill default options
1135 fill_default_options(&def_thread, options);
1137 def_thread.timeout = def_timeout;
1138 def_thread.write_bw_log = write_bw_log;
1139 def_thread.write_lat_log = write_lat_log;
1141 #ifdef FIO_HAVE_DISK_UTIL
1142 def_thread.do_disk_util = 1;
1148 static void usage(void)
1150 printf("%s\n", fio_version_string);
1151 printf("\t--output\tWrite output to file\n");
1152 printf("\t--timeout\tRuntime in seconds\n");
1153 printf("\t--latency-log\tGenerate per-job latency logs\n");
1154 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1155 printf("\t--minimal\tMinimal (terse) output\n");
1156 printf("\t--version\tPrint version info and exit\n");
1157 printf("\t--help\t\tPrint this page\n");
1158 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
1161 static int parse_cmd_line(int argc, char *argv[])
1163 struct thread_data *td = NULL;
1164 int c, ini_idx = 0, lidx, ret;
1166 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
1169 def_timeout = atoi(optarg);
1178 f_out = fopen(optarg, "w+");
1180 perror("fopen output");
1192 ret = show_cmd_help(options, optarg);
1195 printf("%s\n", fio_version_string);
1197 case FIO_GETOPT_JOB: {
1198 const char *opt = long_options[lidx].name;
1201 if (!strncmp(opt, "name", 4) && td) {
1202 ret = add_job(td, td->name ?: "fio", 0);
1210 int global = !strncmp(val, "global", 6);
1212 td = get_new_job(global, &def_thread);
1217 ret = parse_cmd_option(opt, val, options, td);
1219 log_err("fio: job dropped\n");
1231 ret = add_job(td, td->name ?: "fio", 0);
1236 while (optind < argc) {
1238 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1239 ini_file[ini_idx - 1] = strdup(argv[optind]);
1246 static void free_shm(void)
1248 struct shmid_ds sbuf;
1251 shmdt((void *) threads);
1253 shmctl(shm_id, IPC_RMID, &sbuf);
1258 * The thread area is shared between the main process and the job
1259 * threads/processes. So setup a shared memory segment that will hold
1262 static int setup_thread_area(void)
1265 * 1024 is too much on some machines, scale max_jobs if
1266 * we get a failure that looks like too large a shm segment
1269 size_t size = max_jobs * sizeof(struct thread_data);
1271 shm_id = shmget(0, size, IPC_CREAT | 0600);
1274 if (errno != EINVAL) {
1285 threads = shmat(shm_id, NULL, 0);
1286 if (threads == (void *) -1) {
1296 * Copy the fio options into the long options map, so we mirror
1297 * job and cmd line options.
1299 static void dupe_job_options(void)
1301 struct fio_option *o;
1305 while (long_options[i].name)
1310 long_options[i].name = o->name;
1311 long_options[i].val = FIO_GETOPT_JOB;
1312 if (o->type == FIO_OPT_STR_SET)
1313 long_options[i].has_arg = no_argument;
1315 long_options[i].has_arg = required_argument;
1319 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1323 int parse_options(int argc, char *argv[])
1330 options_init(options);
1334 if (setup_thread_area())
1336 if (fill_def_thread())
1339 job_files = parse_cmd_line(argc, argv);
1341 for (i = 0; i < job_files; i++) {
1342 if (fill_def_thread())
1344 if (parse_jobs_ini(ini_file[i], i))
1351 if (!thread_number) {
1352 log_err("No jobs defined(s)\n");