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[] = {
45 .name = "description",
46 .type = FIO_OPT_STR_STORE,
47 .off1 = td_var_offset(description),
48 .help = "Text job description",
52 .type = FIO_OPT_STR_STORE,
53 .off1 = td_var_offset(name),
54 .help = "Name of this job",
58 .type = FIO_OPT_STR_STORE,
59 .off1 = td_var_offset(directory),
60 .help = "Directory to store files in",
64 .type = FIO_OPT_STR_STORE,
65 .off1 = td_var_offset(filename),
66 .help = "Force the use of a specific file",
72 .help = "IO direction",
74 .posval = { "read", "write", "randwrite", "randread", "rw",
80 .cb = str_ioengine_cb,
81 .help = "IO engine to use",
83 .posval = { "sync", "libaio", "posixaio", "mmap", "splice",
84 "sg", "null", "net", "syslet-rw" },
89 .off1 = td_var_offset(iodepth),
90 .help = "Amount of IO buffers to keep in flight",
94 .name = "iodepth_low",
96 .off1 = td_var_offset(iodepth_low),
97 .help = "Low water mark for queuing depth",
101 .type = FIO_OPT_STR_VAL,
102 .off1 = td_var_offset(total_file_size),
103 .help = "Size of device or file",
107 .type = FIO_OPT_STR_VAL_INT,
108 .off1 = td_var_offset(bs[DDIR_READ]),
109 .off2 = td_var_offset(bs[DDIR_WRITE]),
110 .help = "Block size unit",
115 .type = FIO_OPT_RANGE,
116 .off1 = td_var_offset(min_bs[DDIR_READ]),
117 .off2 = td_var_offset(max_bs[DDIR_READ]),
118 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
119 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
120 .help = "Set block size range (in more detail than bs)",
123 .name = "bs_unaligned",
124 .type = FIO_OPT_STR_SET,
125 .off1 = td_var_offset(bs_unaligned),
126 .help = "Don't sector align IO buffer sizes",
130 .type = FIO_OPT_STR_VAL,
131 .off1 = td_var_offset(start_offset),
132 .help = "Start IO from this offset",
136 .name = "randrepeat",
137 .type = FIO_OPT_BOOL,
138 .off1 = td_var_offset(rand_repeatable),
139 .help = "Use repeatable random IO pattern",
143 .name = "norandommap",
144 .type = FIO_OPT_STR_SET,
145 .off1 = td_var_offset(norandommap),
146 .help = "Accept potential duplicate random blocks",
151 .off1 = td_var_offset(nr_files),
152 .help = "Split job workload between this number of files",
158 .off1 = td_var_offset(fsync_blocks),
159 .help = "Issue fsync for writes every given number of blocks",
164 .type = FIO_OPT_BOOL,
165 .off1 = td_var_offset(odirect),
166 .help = "Use O_DIRECT IO (negates buffered)",
171 .type = FIO_OPT_BOOL,
172 .off1 = td_var_offset(odirect),
174 .help = "Use buffered IO (negates direct)",
179 .type = FIO_OPT_BOOL,
180 .off1 = td_var_offset(overwrite),
181 .help = "When writing, set whether to overwrite current data",
187 .off1 = td_var_offset(loops),
188 .help = "Number of times to run the job",
194 .off1 = td_var_offset(numjobs),
195 .help = "Duplicate this job this many times",
199 .name = "startdelay",
201 .off1 = td_var_offset(start_delay),
202 .help = "Only start job when this period has passed",
208 .type = FIO_OPT_STR_VAL_TIME,
209 .off1 = td_var_offset(timeout),
210 .help = "Stop workload when this amount of time has passed",
217 .help = "Backing type for IO buffers",
219 .posval = { "malloc", "shm", "shmhuge", "mmap", "mmaphuge", },
225 .help = "Verify sum function",
227 .posval = { "crc32", "md5", },
230 .name = "write_iolog",
231 .type = FIO_OPT_STR_STORE,
232 .off1 = td_var_offset(write_iolog_file),
233 .help = "Store IO pattern to file",
236 .name = "read_iolog",
237 .type = FIO_OPT_STR_STORE,
238 .off1 = td_var_offset(read_iolog_file),
239 .help = "Playback IO pattern from file",
242 .name = "exec_prerun",
243 .type = FIO_OPT_STR_STORE,
244 .off1 = td_var_offset(exec_prerun),
245 .help = "Execute this file prior to running job",
248 .name = "exec_postrun",
249 .type = FIO_OPT_STR_STORE,
250 .off1 = td_var_offset(exec_postrun),
251 .help = "Execute this file after running job",
253 #ifdef FIO_HAVE_IOSCHED_SWITCH
255 .name = "ioscheduler",
256 .type = FIO_OPT_STR_STORE,
257 .off1 = td_var_offset(ioscheduler),
258 .help = "Use this IO scheduler on the backing device",
263 .type = FIO_OPT_STR_VAL,
264 .off1 = td_var_offset(zone_size),
265 .help = "Give size of an IO zone",
270 .type = FIO_OPT_STR_VAL,
271 .off1 = td_var_offset(zone_skip),
272 .help = "Space between IO zones",
277 .type = FIO_OPT_STR_VAL,
278 .cb = str_lockmem_cb,
279 .help = "Lock down this amount of memory",
283 .name = "rwmixcycle",
285 .off1 = td_var_offset(rwmixcycle),
286 .help = "Cycle period for mixed read/write workloads (msec)",
292 .off1 = td_var_offset(rwmixread),
294 .help = "Percentage of mixed workload that is reads",
298 .name = "rwmixwrite",
300 .off1 = td_var_offset(rwmixwrite),
302 .help = "Percentage of mixed workload that is writes",
308 .off1 = td_var_offset(nice),
309 .help = "Set job CPU nice value",
314 #ifdef FIO_HAVE_IOPRIO
319 .help = "Set job IO priority value",
326 .cb = str_prioclass_cb,
327 .help = "Set job IO priority class",
335 .off1 = td_var_offset(thinktime),
336 .help = "Idle time between IO buffers (usec)",
340 .name = "thinktime_spin",
342 .off1 = td_var_offset(thinktime_spin),
343 .help = "Start thinktime by spinning this amount (usec)",
347 .name = "thinktime_blocks",
349 .off1 = td_var_offset(thinktime_blocks),
350 .help = "IO buffer period between 'thinktime'",
356 .off1 = td_var_offset(rate),
357 .help = "Set bandwidth rate",
362 .off1 = td_var_offset(ratemin),
363 .help = "The bottom limit accepted",
368 .off1 = td_var_offset(ratecycle),
369 .help = "Window average for rate limits (msec)",
373 .name = "invalidate",
374 .type = FIO_OPT_BOOL,
375 .off1 = td_var_offset(invalidate_cache),
376 .help = "Invalidate buffer/page cache prior to running job",
381 .type = FIO_OPT_BOOL,
382 .off1 = td_var_offset(sync_io),
383 .help = "Use O_SYNC for buffered writes",
389 .off1 = td_var_offset(bw_avg_time),
390 .help = "Time window over which to calculate bandwidth (msec)",
394 .name = "create_serialize",
395 .type = FIO_OPT_BOOL,
396 .off1 = td_var_offset(create_serialize),
397 .help = "Serialize creating of job files",
401 .name = "create_fsync",
402 .type = FIO_OPT_BOOL,
403 .off1 = td_var_offset(create_fsync),
404 .help = "Fsync file after creation",
410 .off1 = td_var_offset(cpuload),
411 .help = "Use this percentage of CPU",
416 .off1 = td_var_offset(cpucycle),
417 .help = "Length of the CPU burn cycles",
419 #ifdef FIO_HAVE_CPU_AFFINITY
423 .cb = str_cpumask_cb,
424 .help = "CPU affinity mask",
429 .type = FIO_OPT_BOOL,
430 .off1 = td_var_offset(end_fsync),
431 .help = "Include fsync at the end of job",
436 .type = FIO_OPT_BOOL,
437 .off1 = td_var_offset(unlink),
438 .help = "Unlink created files after job has completed",
443 .type = FIO_OPT_STR_SET,
444 .cb = str_exitall_cb,
445 .help = "Terminate all jobs when one exits",
449 .type = FIO_OPT_STR_SET,
450 .off1 = td_var_offset(stonewall),
451 .help = "Insert a hard barrier between this job and previous",
455 .type = FIO_OPT_STR_SET,
456 .off1 = td_var_offset(use_thread),
457 .help = "Use threads instead of forks",
460 .name = "write_bw_log",
461 .type = FIO_OPT_STR_SET,
462 .off1 = td_var_offset(write_bw_log),
463 .help = "Write log of bandwidth during run",
466 .name = "write_lat_log",
467 .type = FIO_OPT_STR_SET,
468 .off1 = td_var_offset(write_lat_log),
469 .help = "Write log of latency during run",
472 .name = "hugepage-size",
473 .type = FIO_OPT_STR_VAL,
474 .off1 = td_var_offset(hugepage_size),
475 .help = "When using hugepages, specify size of each page",
476 .def = __stringify(FIO_HUGE_PAGE),
483 #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
484 #define FIO_CMD_OPTS (16)
485 #define FIO_GETOPT_JOB (0x89988998)
488 * Command line options. These will contain the above, plus a few
489 * extra that only pertain to fio itself and not jobs.
491 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
494 .has_arg = required_argument,
499 .has_arg = required_argument,
503 .name = "latency-log",
504 .has_arg = required_argument,
508 .name = "bandwidth-log",
509 .has_arg = required_argument,
514 .has_arg = optional_argument,
519 .has_arg = no_argument,
524 .has_arg = no_argument,
529 .has_arg = required_argument,
537 static int def_timeout = 0;
539 static char fio_version_string[] = "fio 1.11";
541 static char **ini_file;
542 static int max_jobs = MAX_JOBS;
544 struct thread_data def_thread;
545 struct thread_data *threads = NULL;
547 int exitall_on_terminate = 0;
548 int terse_output = 0;
549 unsigned long long mlock_size = 0;
553 static int write_lat_log = 0;
554 int write_bw_log = 0;
557 * Return a free job structure.
559 static struct thread_data *get_new_job(int global, struct thread_data *parent)
561 struct thread_data *td;
565 if (thread_number >= max_jobs)
568 td = &threads[thread_number++];
571 td->thread_number = thread_number;
575 static void put_job(struct thread_data *td)
577 if (td == &def_thread)
581 fprintf(f_out, "fio: %s\n", td->verror);
583 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
588 * Lazy way of fixing up options that depend on each other. We could also
589 * define option callback handlers, but this is easier.
591 static void fixup_options(struct thread_data *td)
593 if (!td->rwmixread && td->rwmixwrite)
594 td->rwmixread = 100 - td->rwmixwrite;
596 if (td->write_iolog_file && td->read_iolog_file) {
597 log_err("fio: read iolog overrides write_iolog\n");
598 free(td->write_iolog_file);
599 td->write_iolog_file = NULL;
602 if (td->io_ops->flags & FIO_SYNCIO)
606 td->iodepth = td->nr_files;
610 * only really works for sequential io for now, and with 1 file
612 if (td->zone_size && !td->sequential && td->nr_files == 1)
616 * Reads can do overwrites, we always need to pre-create the file
618 if (td_read(td) || td_rw(td))
621 if (!td->min_bs[DDIR_READ])
622 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
623 if (!td->max_bs[DDIR_READ])
624 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
625 if (!td->min_bs[DDIR_WRITE])
626 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
627 if (!td->max_bs[DDIR_WRITE])
628 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
630 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
632 if (td_read(td) && !td_rw(td))
635 if (td->norandommap && td->verify != VERIFY_NONE) {
636 log_err("fio: norandommap given, verify disabled\n");
637 td->verify = VERIFY_NONE;
639 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
640 log_err("fio: bs_unaligned may not work with raw io\n");
643 * O_DIRECT and char doesn't mix, clear that flag if necessary.
645 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
649 * thinktime_spin must be less than thinktime
651 if (td->thinktime_spin > td->thinktime)
652 td->thinktime_spin = td->thinktime;
655 * The low water mark cannot be bigger than the iodepth
657 if (td->iodepth_low > td->iodepth || !td->iodepth_low)
658 td->iodepth_low = td->iodepth;
660 printf("io depth %d/%d\n", td->iodepth_low, td->iodepth);
664 * This function leaks the buffer
666 static char *to_kmg(unsigned int val)
668 char *buf = malloc(32);
669 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
680 snprintf(buf, 31, "%u%c", val, *p);
685 * Adds a job to the list of things todo. Sanitizes the various options
686 * to make sure we don't have conflicts, and initializes various
689 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
691 const char *ddir_str[] = { "read", "write", "randread", "randwrite",
692 "rw", NULL, "randrw" };
694 int numjobs, ddir, i;
698 * the def_thread is just for options, it's not a real job
700 if (td == &def_thread)
706 td->io_ops->flags |= FIO_RAWIO;
708 td->filetype = FIO_TYPE_FILE;
709 if (td->filename && !lstat(td->filename, &sb)) {
710 if (S_ISBLK(sb.st_mode))
711 td->filetype = FIO_TYPE_BD;
712 else if (S_ISCHR(sb.st_mode))
713 td->filetype = FIO_TYPE_CHAR;
719 td->nr_uniq_files = 1;
721 td->nr_uniq_files = td->nr_files;
723 if (td->filetype == FIO_TYPE_FILE || td->filename) {
727 if (td->directory && td->directory[0] != '\0') {
728 if (lstat(td->directory, &sb) < 0) {
729 log_err("fio: %s is not a directory\n", td->directory);
730 td_verror(td, errno);
733 if (!S_ISDIR(sb.st_mode)) {
734 log_err("fio: %s is not a directory\n", td->directory);
737 len = sprintf(tmp, "%s/", td->directory);
740 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
742 for_each_file(td, f, i) {
743 memset(f, 0, sizeof(*f));
747 sprintf(tmp + len, "%s", td->filename);
749 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
750 f->file_name = strdup(tmp);
754 td->files = malloc(sizeof(struct fio_file));
757 memset(f, 0, sizeof(*f));
759 f->file_name = strdup(jobname);
762 for_each_file(td, f, i) {
763 f->file_size = td->total_file_size / td->nr_files;
764 f->file_offset = td->start_offset;
767 fio_sem_init(&td->mutex, 0);
769 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
770 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
771 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
773 if (td->stonewall && td->thread_number > 1)
776 td->groupid = groupid;
781 if (td->write_lat_log) {
782 setup_log(&td->ts.slat_log);
783 setup_log(&td->ts.clat_log);
785 if (td->write_bw_log)
786 setup_log(&td->ts.bw_log);
789 td->name = strdup(jobname);
791 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
795 if (td->io_ops->flags & FIO_CPUIO)
796 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
798 char *c1, *c2, *c3, *c4;
800 c1 = to_kmg(td->min_bs[DDIR_READ]);
801 c2 = to_kmg(td->max_bs[DDIR_READ]);
802 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
803 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
805 fprintf(f_out, "%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[ddir], c1, c2, c3, c4, td->io_ops->name, td->iodepth);
812 } else if (job_add_num == 1)
813 fprintf(f_out, "...\n");
817 * recurse add identical jobs, clear numjobs and stonewall options
818 * as they don't apply to sub-jobs
820 numjobs = td->numjobs;
822 struct thread_data *td_new = get_new_job(0, td);
828 td_new->stonewall = 0;
829 job_add_num = numjobs - 1;
831 if (add_job(td_new, jobname, job_add_num))
841 * Initialize the various random states we need (random io, block size ranges,
842 * read/write mix, etc).
844 int init_random_state(struct thread_data *td)
846 unsigned long seeds[4];
847 int fd, num_maps, blocks, i;
850 if (td->io_ops->flags & FIO_CPUIO)
853 fd = open("/dev/urandom", O_RDONLY);
855 td_verror(td, errno);
859 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
867 os_random_seed(seeds[0], &td->bsrange_state);
868 os_random_seed(seeds[1], &td->verify_state);
869 os_random_seed(seeds[2], &td->rwmix_state);
874 if (td->rand_repeatable)
875 seeds[3] = FIO_RANDSEED * td->thread_number;
877 if (!td->norandommap) {
878 for_each_file(td, f, i) {
879 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
880 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
881 f->file_map = malloc(num_maps * sizeof(long));
882 f->num_maps = num_maps;
883 memset(f->file_map, 0, num_maps * sizeof(long));
887 os_random_seed(seeds[3], &td->random_state);
891 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
893 #ifdef FIO_HAVE_CPU_AFFINITY
898 for (i = 0; i < sizeof(int) * 8; i++) {
900 CPU_SET(i, &cpumask);
905 static int is_empty_or_comment(char *line)
909 for (i = 0; i < strlen(line); i++) {
914 if (!isspace(line[i]) && !iscntrl(line[i]))
921 static int str_rw_cb(void *data, const char *mem)
923 struct thread_data *td = data;
925 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
926 td->ddir = DDIR_READ;
929 } else if (!strncmp(mem, "randread", 8)) {
930 td->ddir = DDIR_READ;
933 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
934 td->ddir = DDIR_WRITE;
937 } else if (!strncmp(mem, "randwrite", 9)) {
938 td->ddir = DDIR_WRITE;
941 } else if (!strncmp(mem, "rw", 2)) {
942 td->ddir = DDIR_READ;
946 } else if (!strncmp(mem, "randrw", 6)) {
947 td->ddir = DDIR_READ;
953 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
957 static int str_verify_cb(void *data, const char *mem)
959 struct thread_data *td = data;
961 if (!strncmp(mem, "0", 1)) {
962 td->verify = VERIFY_NONE;
964 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
965 td->verify = VERIFY_MD5;
967 } else if (!strncmp(mem, "crc32", 5)) {
968 td->verify = VERIFY_CRC32;
972 log_err("fio: verify types: md5, crc32\n");
977 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
979 static char *get_mmap_file(const char *str)
981 char *p = strstr(str, ":");
987 strip_blank_front(&p);
992 static int str_mem_cb(void *data, const char *mem)
994 struct thread_data *td = data;
996 if (!strncmp(mem, "malloc", 6)) {
997 td->mem_type = MEM_MALLOC;
999 } else if (!strncmp(mem, "mmaphuge", 8)) {
1000 #ifdef FIO_HAVE_HUGETLB
1002 * mmaphuge must be appended with the actual file
1004 td->mmapfile = get_mmap_file(mem);
1005 if (!td->mmapfile) {
1006 log_err("fio: mmaphuge:/path/to/file\n");
1010 td->mem_type = MEM_MMAPHUGE;
1013 log_err("fio: mmaphuge not available\n");
1016 } else if (!strncmp(mem, "mmap", 4)) {
1018 * Check if the user wants file backed memory. It's ok
1019 * if there's no file given, we'll just use anon mamp then.
1021 td->mmapfile = get_mmap_file(mem);
1022 td->mem_type = MEM_MMAP;
1024 } else if (!strncmp(mem, "shmhuge", 7)) {
1025 #ifdef FIO_HAVE_HUGETLB
1026 td->mem_type = MEM_SHMHUGE;
1029 log_err("fio: shmhuge not available\n");
1032 } else if (!strncmp(mem, "shm", 3)) {
1033 td->mem_type = MEM_SHM;
1037 log_err("fio: mem type: malloc, shm, shmhuge, mmap, mmaphuge\n");
1041 static int str_ioengine_cb(void *data, const char *str)
1043 struct thread_data *td = data;
1045 td->io_ops = load_ioengine(td, str);
1049 log_err("fio: ioengine= libaio, posixaio, sync, mmap, sgio, splice, cpu, null\n");
1050 log_err("fio: or specify path to dynamic ioengine module\n");
1054 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1060 #ifdef FIO_HAVE_IOPRIO
1061 static int str_prioclass_cb(void *data, unsigned int *val)
1063 struct thread_data *td = data;
1065 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1069 static int str_prio_cb(void *data, unsigned int *val)
1071 struct thread_data *td = data;
1078 static int str_exitall_cb(void)
1080 exitall_on_terminate = 1;
1084 static int str_cpumask_cb(void *data, unsigned int *val)
1086 struct thread_data *td = data;
1088 fill_cpu_mask(td->cpumask, *val);
1093 * This is our [ini] type file parser.
1095 static int parse_jobs_ini(char *file, int stonewall_flag)
1097 unsigned int global;
1098 struct thread_data *td;
1099 char *string, *name;
1103 int ret = 0, stonewall;
1105 f = fopen(file, "r");
1107 perror("fopen job file");
1111 string = malloc(4096);
1113 memset(name, 0, 256);
1115 stonewall = stonewall_flag;
1117 p = fgets(string, 4095, f);
1120 if (is_empty_or_comment(p))
1122 if (sscanf(p, "[%255s]", name) != 1)
1125 global = !strncmp(name, "global", 6);
1127 name[strlen(name) - 1] = '\0';
1129 td = get_new_job(global, &def_thread);
1136 * Seperate multiple job files by a stonewall
1138 if (!global && stonewall) {
1139 td->stonewall = stonewall;
1144 while ((p = fgets(string, 4096, f)) != NULL) {
1145 if (is_empty_or_comment(p))
1148 strip_blank_front(&p);
1158 * Don't break here, continue parsing options so we
1159 * dump all the bad ones. Makes trial/error fixups
1160 * easier on the user.
1162 ret |= parse_option(p, options, td);
1167 ret = add_job(td, name, 0);
1169 log_err("fio: job %s dropped\n", name);
1180 static int fill_def_thread(void)
1182 memset(&def_thread, 0, sizeof(def_thread));
1184 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1185 perror("sched_getaffinity");
1190 * fill default options
1192 fill_default_options(&def_thread, options);
1194 def_thread.timeout = def_timeout;
1195 def_thread.write_bw_log = write_bw_log;
1196 def_thread.write_lat_log = write_lat_log;
1198 #ifdef FIO_HAVE_DISK_UTIL
1199 def_thread.do_disk_util = 1;
1205 static void usage(void)
1207 printf("%s\n", fio_version_string);
1208 printf("\t--output\tWrite output to file\n");
1209 printf("\t--timeout\tRuntime in seconds\n");
1210 printf("\t--latency-log\tGenerate per-job latency logs\n");
1211 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1212 printf("\t--minimal\tMinimal (terse) output\n");
1213 printf("\t--version\tPrint version info and exit\n");
1214 printf("\t--help\t\tPrint this page\n");
1215 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
1218 static int parse_cmd_line(int argc, char *argv[])
1220 struct thread_data *td = NULL;
1221 int c, ini_idx = 0, lidx, ret;
1223 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
1226 def_timeout = atoi(optarg);
1235 f_out = fopen(optarg, "w+");
1237 perror("fopen output");
1249 ret = show_cmd_help(options, optarg);
1252 printf("%s\n", fio_version_string);
1254 case FIO_GETOPT_JOB: {
1255 const char *opt = long_options[lidx].name;
1258 if (!strncmp(opt, "name", 4) && td) {
1259 ret = add_job(td, td->name ?: "fio", 0);
1267 int global = !strncmp(val, "global", 6);
1269 td = get_new_job(global, &def_thread);
1274 ret = parse_cmd_option(opt, val, options, td);
1276 log_err("fio: job dropped\n");
1288 ret = add_job(td, td->name ?: "fio", 0);
1293 while (optind < argc) {
1295 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1296 ini_file[ini_idx - 1] = strdup(argv[optind]);
1303 static void free_shm(void)
1305 struct shmid_ds sbuf;
1308 shmdt((void *) threads);
1310 shmctl(shm_id, IPC_RMID, &sbuf);
1315 * The thread area is shared between the main process and the job
1316 * threads/processes. So setup a shared memory segment that will hold
1319 static int setup_thread_area(void)
1322 * 1024 is too much on some machines, scale max_jobs if
1323 * we get a failure that looks like too large a shm segment
1326 size_t size = max_jobs * sizeof(struct thread_data);
1328 shm_id = shmget(0, size, IPC_CREAT | 0600);
1331 if (errno != EINVAL) {
1342 threads = shmat(shm_id, NULL, 0);
1343 if (threads == (void *) -1) {
1353 * Copy the fio options into the long options map, so we mirror
1354 * job and cmd line options.
1356 static void dupe_job_options(void)
1358 struct fio_option *o;
1362 while (long_options[i].name)
1367 long_options[i].name = o->name;
1368 long_options[i].val = FIO_GETOPT_JOB;
1369 if (o->type == FIO_OPT_STR_SET)
1370 long_options[i].has_arg = no_argument;
1372 long_options[i].has_arg = required_argument;
1376 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1380 int parse_options(int argc, char *argv[])
1387 options_init(options);
1391 if (setup_thread_area())
1393 if (fill_def_thread())
1396 job_files = parse_cmd_line(argc, argv);
1398 for (i = 0; i < job_files; i++) {
1399 if (fill_def_thread())
1401 if (parse_jobs_ini(ini_file[i], i))
1408 if (!thread_number) {
1409 log_err("No jobs defined(s)\n");