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_mem_cb(void *, const char *);
26 static int str_lockmem_cb(void *, unsigned long *);
27 #ifdef FIO_HAVE_IOPRIO
28 static int str_prio_cb(void *, unsigned int *);
29 static int str_prioclass_cb(void *, unsigned int *);
31 static int str_exitall_cb(void);
32 static int str_cpumask_cb(void *, unsigned int *);
33 static int str_fst_cb(void *, const char *);
35 #define __stringify_1(x) #x
36 #define __stringify(x) __stringify_1(x)
39 * Map of job/command line options
41 static struct fio_option options[] = {
43 .name = "description",
44 .type = FIO_OPT_STR_STORE,
45 .off1 = td_var_offset(description),
46 .help = "Text job description",
50 .type = FIO_OPT_STR_STORE,
51 .off1 = td_var_offset(name),
52 .help = "Name of this job",
56 .type = FIO_OPT_STR_STORE,
57 .off1 = td_var_offset(directory),
58 .help = "Directory to store files in",
62 .type = FIO_OPT_STR_STORE,
63 .off1 = td_var_offset(filename),
64 .help = "Force the use of a specific file",
69 .off1 = td_var_offset(td_ddir),
70 .help = "IO direction",
73 { .ival = "read", .oval = TD_DDIR_READ },
74 { .ival = "write", .oval = TD_DDIR_WRITE },
75 { .ival = "randread", .oval = TD_DDIR_RANDREAD },
76 { .ival = "randwrite", .oval = TD_DDIR_RANDWRITE },
77 { .ival = "rw", .oval = TD_DDIR_RW },
78 { .ival = "randrw", .oval = TD_DDIR_RANDRW },
83 .type = FIO_OPT_STR_STORE,
84 .off1 = td_var_offset(ioengine),
85 .help = "IO engine to use",
89 #ifdef FIO_HAVE_LIBAIO
90 { .ival = "libaio", },
92 #ifdef FIO_HAVE_POSIXAIO
93 { .ival = "posixaio", },
96 #ifdef FIO_HAVE_SPLICE
97 { .ival = "splice", },
102 { .ival = "null", }, { .ival = "net", },
103 #ifdef FIO_HAVE_SYSLET
104 { .ival = "syslet-rw", },
106 { .ival = "cpuio", },
107 { .ival = "external", },
113 .off1 = td_var_offset(iodepth),
114 .help = "Amount of IO buffers to keep in flight",
118 .name = "iodepth_batch",
120 .off1 = td_var_offset(iodepth_batch),
121 .help = "Number of IO to submit in one go",
124 .name = "iodepth_low",
126 .off1 = td_var_offset(iodepth_low),
127 .help = "Low water mark for queuing depth",
131 .type = FIO_OPT_STR_VAL,
132 .off1 = td_var_offset(total_file_size),
133 .help = "Size of device or file",
137 .type = FIO_OPT_STR_VAL_INT,
138 .off1 = td_var_offset(bs[DDIR_READ]),
139 .off2 = td_var_offset(bs[DDIR_WRITE]),
140 .help = "Block size unit",
145 .type = FIO_OPT_RANGE,
146 .off1 = td_var_offset(min_bs[DDIR_READ]),
147 .off2 = td_var_offset(max_bs[DDIR_READ]),
148 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
149 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
150 .help = "Set block size range (in more detail than bs)",
153 .name = "bs_unaligned",
154 .type = FIO_OPT_STR_SET,
155 .off1 = td_var_offset(bs_unaligned),
156 .help = "Don't sector align IO buffer sizes",
160 .type = FIO_OPT_STR_VAL,
161 .off1 = td_var_offset(start_offset),
162 .help = "Start IO from this offset",
166 .name = "randrepeat",
167 .type = FIO_OPT_BOOL,
168 .off1 = td_var_offset(rand_repeatable),
169 .help = "Use repeatable random IO pattern",
173 .name = "norandommap",
174 .type = FIO_OPT_STR_SET,
175 .off1 = td_var_offset(norandommap),
176 .help = "Accept potential duplicate random blocks",
181 .off1 = td_var_offset(nr_files),
182 .help = "Split job workload between this number of files",
188 .off1 = td_var_offset(open_files),
189 .help = "Number of files to keep open at the same time",
192 .name = "file_service_type",
195 .off1 = td_var_offset(file_service_type),
196 .help = "How to select which file to service next",
199 { .ival = "random", .oval = FIO_FSERVICE_RANDOM },
200 { .ival = "roundrobin", .oval = FIO_FSERVICE_RR },
206 .off1 = td_var_offset(fsync_blocks),
207 .help = "Issue fsync for writes every given number of blocks",
212 .type = FIO_OPT_BOOL,
213 .off1 = td_var_offset(odirect),
214 .help = "Use O_DIRECT IO (negates buffered)",
219 .type = FIO_OPT_BOOL,
220 .off1 = td_var_offset(odirect),
222 .help = "Use buffered IO (negates direct)",
227 .type = FIO_OPT_BOOL,
228 .off1 = td_var_offset(overwrite),
229 .help = "When writing, set whether to overwrite current data",
235 .off1 = td_var_offset(loops),
236 .help = "Number of times to run the job",
242 .off1 = td_var_offset(numjobs),
243 .help = "Duplicate this job this many times",
247 .name = "startdelay",
249 .off1 = td_var_offset(start_delay),
250 .help = "Only start job when this period has passed",
256 .type = FIO_OPT_STR_VAL_TIME,
257 .off1 = td_var_offset(timeout),
258 .help = "Stop workload when this amount of time has passed",
265 .off1 = td_var_offset(mem_type),
266 .help = "Backing type for IO buffers",
269 { .ival = "malloc", .oval = MEM_MALLOC },
270 { .ival = "shm", .oval = MEM_SHM },
271 #ifdef FIO_HAVE_HUGETLB
272 { .ival = "shmhuge", .oval = MEM_SHMHUGE },
274 { .ival = "mmap", .oval = MEM_MMAP },
275 #ifdef FIO_HAVE_HUGETLB
276 { .ival = "mmaphuge", .oval = MEM_MMAPHUGE },
283 .off1 = td_var_offset(verify),
284 .help = "Verify sum function",
287 { .ival = "0", .oval = VERIFY_NONE },
288 { .ival = "crc32", .oval = VERIFY_CRC32 },
289 { .ival = "md5", .oval = VERIFY_MD5 },
293 .name = "write_iolog",
294 .type = FIO_OPT_STR_STORE,
295 .off1 = td_var_offset(write_iolog_file),
296 .help = "Store IO pattern to file",
299 .name = "read_iolog",
300 .type = FIO_OPT_STR_STORE,
301 .off1 = td_var_offset(read_iolog_file),
302 .help = "Playback IO pattern from file",
305 .name = "exec_prerun",
306 .type = FIO_OPT_STR_STORE,
307 .off1 = td_var_offset(exec_prerun),
308 .help = "Execute this file prior to running job",
311 .name = "exec_postrun",
312 .type = FIO_OPT_STR_STORE,
313 .off1 = td_var_offset(exec_postrun),
314 .help = "Execute this file after running job",
316 #ifdef FIO_HAVE_IOSCHED_SWITCH
318 .name = "ioscheduler",
319 .type = FIO_OPT_STR_STORE,
320 .off1 = td_var_offset(ioscheduler),
321 .help = "Use this IO scheduler on the backing device",
326 .type = FIO_OPT_STR_VAL,
327 .off1 = td_var_offset(zone_size),
328 .help = "Give size of an IO zone",
333 .type = FIO_OPT_STR_VAL,
334 .off1 = td_var_offset(zone_skip),
335 .help = "Space between IO zones",
340 .type = FIO_OPT_STR_VAL,
341 .cb = str_lockmem_cb,
342 .help = "Lock down this amount of memory",
346 .name = "rwmixcycle",
348 .off1 = td_var_offset(rwmixcycle),
349 .help = "Cycle period for mixed read/write workloads (msec)",
355 .off1 = td_var_offset(rwmixread),
357 .help = "Percentage of mixed workload that is reads",
361 .name = "rwmixwrite",
363 .off1 = td_var_offset(rwmixwrite),
365 .help = "Percentage of mixed workload that is writes",
371 .off1 = td_var_offset(nice),
372 .help = "Set job CPU nice value",
377 #ifdef FIO_HAVE_IOPRIO
382 .help = "Set job IO priority value",
389 .cb = str_prioclass_cb,
390 .help = "Set job IO priority class",
398 .off1 = td_var_offset(thinktime),
399 .help = "Idle time between IO buffers (usec)",
403 .name = "thinktime_spin",
405 .off1 = td_var_offset(thinktime_spin),
406 .help = "Start think time by spinning this amount (usec)",
410 .name = "thinktime_blocks",
412 .off1 = td_var_offset(thinktime_blocks),
413 .help = "IO buffer period between 'thinktime'",
419 .off1 = td_var_offset(rate),
420 .help = "Set bandwidth rate",
425 .off1 = td_var_offset(ratemin),
426 .help = "The bottom limit accepted",
431 .off1 = td_var_offset(ratecycle),
432 .help = "Window average for rate limits (msec)",
436 .name = "invalidate",
437 .type = FIO_OPT_BOOL,
438 .off1 = td_var_offset(invalidate_cache),
439 .help = "Invalidate buffer/page cache prior to running job",
444 .type = FIO_OPT_BOOL,
445 .off1 = td_var_offset(sync_io),
446 .help = "Use O_SYNC for buffered writes",
452 .off1 = td_var_offset(bw_avg_time),
453 .help = "Time window over which to calculate bandwidth (msec)",
457 .name = "create_serialize",
458 .type = FIO_OPT_BOOL,
459 .off1 = td_var_offset(create_serialize),
460 .help = "Serialize creating of job files",
464 .name = "create_fsync",
465 .type = FIO_OPT_BOOL,
466 .off1 = td_var_offset(create_fsync),
467 .help = "Fsync file after creation",
473 .off1 = td_var_offset(cpuload),
474 .help = "Use this percentage of CPU",
479 .off1 = td_var_offset(cpucycle),
480 .help = "Length of the CPU burn cycles (usecs)",
483 #ifdef FIO_HAVE_CPU_AFFINITY
487 .cb = str_cpumask_cb,
488 .help = "CPU affinity mask",
493 .type = FIO_OPT_BOOL,
494 .off1 = td_var_offset(end_fsync),
495 .help = "Include fsync at the end of job",
500 .type = FIO_OPT_BOOL,
501 .off1 = td_var_offset(unlink),
502 .help = "Unlink created files after job has completed",
507 .type = FIO_OPT_STR_SET,
508 .cb = str_exitall_cb,
509 .help = "Terminate all jobs when one exits",
513 .type = FIO_OPT_STR_SET,
514 .off1 = td_var_offset(stonewall),
515 .help = "Insert a hard barrier between this job and previous",
519 .type = FIO_OPT_STR_SET,
520 .off1 = td_var_offset(use_thread),
521 .help = "Use threads instead of forks",
524 .name = "write_bw_log",
525 .type = FIO_OPT_STR_SET,
526 .off1 = td_var_offset(write_bw_log),
527 .help = "Write log of bandwidth during run",
530 .name = "write_lat_log",
531 .type = FIO_OPT_STR_SET,
532 .off1 = td_var_offset(write_lat_log),
533 .help = "Write log of latency during run",
536 .name = "hugepage-size",
537 .type = FIO_OPT_STR_VAL,
538 .off1 = td_var_offset(hugepage_size),
539 .help = "When using hugepages, specify size of each page",
540 .def = __stringify(FIO_HUGE_PAGE),
543 .name = "group_reporting",
544 .type = FIO_OPT_STR_SET,
545 .off1 = td_var_offset(group_reporting),
546 .help = "Do reporting on a per-group basis",
553 #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
554 #define FIO_CMD_OPTS (16)
555 #define FIO_GETOPT_JOB (0x89988998)
558 * Command line options. These will contain the above, plus a few
559 * extra that only pertain to fio itself and not jobs.
561 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
564 .has_arg = required_argument,
569 .has_arg = required_argument,
573 .name = "latency-log",
574 .has_arg = required_argument,
578 .name = "bandwidth-log",
579 .has_arg = required_argument,
584 .has_arg = optional_argument,
589 .has_arg = no_argument,
594 .has_arg = no_argument,
599 .has_arg = optional_argument,
607 static int def_timeout = 0;
609 static char fio_version_string[] = "fio 1.13";
611 static char **ini_file;
612 static int max_jobs = MAX_JOBS;
614 struct thread_data def_thread;
615 struct thread_data *threads = NULL;
617 int exitall_on_terminate = 0;
618 int terse_output = 0;
619 unsigned long long mlock_size = 0;
623 static int write_lat_log = 0;
624 int write_bw_log = 0;
626 static int prev_group_jobs;
639 * Return a free job structure.
641 static struct thread_data *get_new_job(int global, struct thread_data *parent)
643 struct thread_data *td;
647 if (thread_number >= max_jobs)
650 td = &threads[thread_number++];
653 td->thread_number = thread_number;
657 static void put_job(struct thread_data *td)
659 if (td == &def_thread)
663 fprintf(f_out, "fio: %s\n", td->verror);
665 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
670 * Lazy way of fixing up options that depend on each other. We could also
671 * define option callback handlers, but this is easier.
673 static void fixup_options(struct thread_data *td)
675 if (!td->rwmixread && td->rwmixwrite)
676 td->rwmixread = 100 - td->rwmixwrite;
678 if (td->write_iolog_file && td->read_iolog_file) {
679 log_err("fio: read iolog overrides write_iolog\n");
680 free(td->write_iolog_file);
681 td->write_iolog_file = NULL;
684 if (td->io_ops->flags & FIO_SYNCIO)
688 td->iodepth = td->open_files;
692 * only really works for sequential io for now, and with 1 file
694 if (td->zone_size && td_random(td) && td->open_files == 1)
698 * Reads can do overwrites, we always need to pre-create the file
700 if (td_read(td) || td_rw(td))
703 if (!td->min_bs[DDIR_READ])
704 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
705 if (!td->max_bs[DDIR_READ])
706 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
707 if (!td->min_bs[DDIR_WRITE])
708 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
709 if (!td->max_bs[DDIR_WRITE])
710 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
712 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
714 if (td_read(td) && !td_rw(td))
717 if (td->norandommap && td->verify != VERIFY_NONE) {
718 log_err("fio: norandommap given, verify disabled\n");
719 td->verify = VERIFY_NONE;
721 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
722 log_err("fio: bs_unaligned may not work with raw io\n");
725 * O_DIRECT and char doesn't mix, clear that flag if necessary.
727 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
731 * thinktime_spin must be less than thinktime
733 if (td->thinktime_spin > td->thinktime)
734 td->thinktime_spin = td->thinktime;
737 * The low water mark cannot be bigger than the iodepth
739 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
741 * syslet work around - if the workload is sequential,
742 * we want to let the queue drain all the way down to
743 * avoid seeking between async threads
745 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
748 td->iodepth_low = td->iodepth;
752 * If batch number isn't set, default to the same as iodepth
754 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
755 td->iodepth_batch = td->iodepth;
757 if (td->open_files > td->nr_files || !td->open_files)
758 td->open_files = td->nr_files;
762 * This function leaks the buffer
764 static char *to_kmg(unsigned int val)
766 char *buf = malloc(32);
767 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
778 snprintf(buf, 31, "%u%c", val, *p);
782 /* External engines are specified by "external:name.o") */
783 static const char *get_engine_name(const char *str)
785 char *p = strstr(str, ":");
791 strip_blank_front(&p);
797 * Adds a job to the list of things todo. Sanitizes the various options
798 * to make sure we don't have conflicts, and initializes various
801 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
803 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
804 "randread", "randwrite", "randrw" };
812 * the def_thread is just for options, it's not a real job
814 if (td == &def_thread)
817 engine = get_engine_name(td->ioengine);
818 td->io_ops = load_ioengine(td, engine);
820 log_err("fio: failed to load engine %s\n", engine);
830 td->io_ops->flags |= FIO_RAWIO;
832 fn_given = (long) td->filename;
834 td->filename = strdup(jobname);
836 td->filetype = FIO_TYPE_FILE;
837 if (!lstat(td->filename, &sb)) {
838 if (S_ISBLK(sb.st_mode))
839 td->filetype = FIO_TYPE_BD;
840 else if (S_ISCHR(sb.st_mode))
841 td->filetype = FIO_TYPE_CHAR;
847 td->nr_uniq_files = 1;
849 td->nr_uniq_files = td->open_files;
851 if (td->filetype == FIO_TYPE_FILE) {
855 if (td->directory && td->directory[0] != '\0') {
856 if (lstat(td->directory, &sb) < 0) {
857 log_err("fio: %s is not a directory\n", td->directory);
858 td_verror(td, errno, "lstat");
861 if (!S_ISDIR(sb.st_mode)) {
862 log_err("fio: %s is not a directory\n", td->directory);
865 len = sprintf(tmp, "%s/", td->directory);
868 td->files = malloc(sizeof(struct fio_file) * td->open_files);
870 for_each_file(td, f, i) {
871 memset(f, 0, sizeof(*f));
875 sprintf(tmp + len, "%s", td->filename);
877 sprintf(tmp + len, "%s.%d.%d", td->filename, td->thread_number, i);
878 f->file_name = strdup(tmp);
881 td->open_files = td->nr_files = 1;
882 td->files = malloc(sizeof(struct fio_file));
885 memset(f, 0, sizeof(*f));
887 f->file_name = strdup(td->filename);
890 for_each_file(td, f, i) {
891 f->file_size = td->total_file_size / td->nr_files;
892 f->file_offset = td->start_offset;
895 td->mutex = fio_sem_init(0);
897 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
898 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
899 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
901 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
906 td->groupid = groupid;
912 if (td->write_lat_log) {
913 setup_log(&td->ts.slat_log);
914 setup_log(&td->ts.clat_log);
916 if (td->write_bw_log)
917 setup_log(&td->ts.bw_log);
920 td->name = strdup(jobname);
924 if (!strcmp(td->io_ops->name, "cpuio"))
925 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
927 char *c1, *c2, *c3, *c4;
929 c1 = to_kmg(td->min_bs[DDIR_READ]);
930 c2 = to_kmg(td->max_bs[DDIR_READ]);
931 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
932 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
934 fprintf(f_out, "%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[td->td_ddir], c1, c2, c3, c4, td->io_ops->name, td->iodepth);
941 } else if (job_add_num == 1)
942 fprintf(f_out, "...\n");
946 * recurse add identical jobs, clear numjobs and stonewall options
947 * as they don't apply to sub-jobs
949 numjobs = td->numjobs;
951 struct thread_data *td_new = get_new_job(0, td);
957 td_new->stonewall = 0;
958 job_add_num = numjobs - 1;
960 if (add_job(td_new, jobname, job_add_num))
964 if (td->numjobs > 1) {
976 * Initialize the various random states we need (random io, block size ranges,
977 * read/write mix, etc).
979 int init_random_state(struct thread_data *td)
981 unsigned long seeds[5];
982 int fd, num_maps, blocks, i;
985 if (td->io_ops->flags & FIO_DISKLESSIO)
988 fd = open("/dev/urandom", O_RDONLY);
990 td_verror(td, errno, "open");
994 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
995 td_verror(td, EIO, "read");
1002 os_random_seed(seeds[0], &td->bsrange_state);
1003 os_random_seed(seeds[1], &td->verify_state);
1004 os_random_seed(seeds[2], &td->rwmix_state);
1006 if (td->file_service_type == FIO_FSERVICE_RANDOM)
1007 os_random_seed(seeds[3], &td->next_file_state);
1012 if (td->rand_repeatable)
1013 seeds[4] = FIO_RANDSEED * td->thread_number;
1015 if (!td->norandommap) {
1016 for_each_file(td, f, i) {
1017 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
1018 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
1019 f->file_map = malloc(num_maps * sizeof(long));
1021 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
1024 f->num_maps = num_maps;
1025 memset(f->file_map, 0, num_maps * sizeof(long));
1029 os_random_seed(seeds[4], &td->random_state);
1033 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
1035 #ifdef FIO_HAVE_CPU_AFFINITY
1040 for (i = 0; i < sizeof(int) * 8; i++) {
1042 CPU_SET(i, &cpumask);
1047 static int is_empty_or_comment(char *line)
1051 for (i = 0; i < strlen(line); i++) {
1056 if (!isspace(line[i]) && !iscntrl(line[i]))
1064 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
1066 static char *get_opt_postfix(const char *str)
1068 char *p = strstr(str, ":");
1074 strip_blank_front(&p);
1079 static int str_mem_cb(void *data, const char *mem)
1081 struct thread_data *td = data;
1083 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
1084 td->mmapfile = get_opt_postfix(mem);
1085 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
1086 log_err("fio: mmaphuge:/path/to/file\n");
1094 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1100 #ifdef FIO_HAVE_IOPRIO
1101 static int str_prioclass_cb(void *data, unsigned int *val)
1103 struct thread_data *td = data;
1105 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1109 static int str_prio_cb(void *data, unsigned int *val)
1111 struct thread_data *td = data;
1118 static int str_exitall_cb(void)
1120 exitall_on_terminate = 1;
1124 static int str_cpumask_cb(void *data, unsigned int *val)
1126 struct thread_data *td = data;
1128 fill_cpu_mask(td->cpumask, *val);
1132 static int str_fst_cb(void *data, const char *str)
1134 struct thread_data *td = data;
1135 char *nr = get_opt_postfix(str);
1137 td->file_service_nr = 1;
1139 td->file_service_nr = atoi(nr);
1145 * This is our [ini] type file parser.
1147 static int parse_jobs_ini(char *file, int stonewall_flag)
1149 unsigned int global;
1150 struct thread_data *td;
1151 char *string, *name;
1155 int ret = 0, stonewall;
1157 f = fopen(file, "r");
1159 perror("fopen job file");
1163 string = malloc(4096);
1165 memset(name, 0, 256);
1167 stonewall = stonewall_flag;
1169 p = fgets(string, 4095, f);
1172 if (is_empty_or_comment(p))
1174 if (sscanf(p, "[%255s]", name) != 1)
1177 global = !strncmp(name, "global", 6);
1179 name[strlen(name) - 1] = '\0';
1181 td = get_new_job(global, &def_thread);
1188 * Seperate multiple job files by a stonewall
1190 if (!global && stonewall) {
1191 td->stonewall = stonewall;
1196 while ((p = fgets(string, 4096, f)) != NULL) {
1197 if (is_empty_or_comment(p))
1200 strip_blank_front(&p);
1210 * Don't break here, continue parsing options so we
1211 * dump all the bad ones. Makes trial/error fixups
1212 * easier on the user.
1214 ret |= parse_option(p, options, td);
1219 ret = add_job(td, name, 0);
1221 log_err("fio: job %s dropped\n", name);
1232 static int fill_def_thread(void)
1234 memset(&def_thread, 0, sizeof(def_thread));
1236 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1237 perror("sched_getaffinity");
1242 * fill default options
1244 fill_default_options(&def_thread, options);
1246 def_thread.timeout = def_timeout;
1247 def_thread.write_bw_log = write_bw_log;
1248 def_thread.write_lat_log = write_lat_log;
1250 #ifdef FIO_HAVE_DISK_UTIL
1251 def_thread.do_disk_util = 1;
1257 static void usage(void)
1259 printf("%s\n", fio_version_string);
1260 printf("\t--output\tWrite output to file\n");
1261 printf("\t--timeout\tRuntime in seconds\n");
1262 printf("\t--latency-log\tGenerate per-job latency logs\n");
1263 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1264 printf("\t--minimal\tMinimal (terse) output\n");
1265 printf("\t--version\tPrint version info and exit\n");
1266 printf("\t--help\t\tPrint this page\n");
1267 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
1270 static int parse_cmd_line(int argc, char *argv[])
1272 struct thread_data *td = NULL;
1273 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
1275 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
1278 def_timeout = atoi(optarg);
1287 f_out = fopen(optarg, "w+");
1289 perror("fopen output");
1301 ret = show_cmd_help(options, optarg);
1304 printf("%s\n", fio_version_string);
1306 case FIO_GETOPT_JOB: {
1307 const char *opt = long_options[lidx].name;
1310 if (!strncmp(opt, "name", 4) && td) {
1311 ret = add_job(td, td->name ?: "fio", 0);
1319 int global = !strncmp(val, "global", 6);
1321 td = get_new_job(global, &def_thread);
1326 ret = parse_cmd_option(opt, val, options, td);
1340 ret = add_job(td, td->name ?: "fio", 0);
1346 while (optind < argc) {
1348 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1349 ini_file[ini_idx - 1] = strdup(argv[optind]);
1356 static void free_shm(void)
1358 struct shmid_ds sbuf;
1361 shmdt((void *) threads);
1363 shmctl(shm_id, IPC_RMID, &sbuf);
1368 * The thread area is shared between the main process and the job
1369 * threads/processes. So setup a shared memory segment that will hold
1372 static int setup_thread_area(void)
1375 * 1024 is too much on some machines, scale max_jobs if
1376 * we get a failure that looks like too large a shm segment
1379 size_t size = max_jobs * sizeof(struct thread_data);
1381 shm_id = shmget(0, size, IPC_CREAT | 0600);
1384 if (errno != EINVAL) {
1395 threads = shmat(shm_id, NULL, 0);
1396 if (threads == (void *) -1) {
1406 * Copy the fio options into the long options map, so we mirror
1407 * job and cmd line options.
1409 static void dupe_job_options(void)
1411 struct fio_option *o;
1415 while (long_options[i].name)
1420 long_options[i].name = o->name;
1421 long_options[i].val = FIO_GETOPT_JOB;
1422 if (o->type == FIO_OPT_STR_SET)
1423 long_options[i].has_arg = no_argument;
1425 long_options[i].has_arg = required_argument;
1429 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1433 int parse_options(int argc, char *argv[])
1440 options_init(options);
1444 if (setup_thread_area())
1446 if (fill_def_thread())
1449 job_files = parse_cmd_line(argc, argv);
1451 for (i = 0; i < job_files; i++) {
1452 if (fill_def_thread())
1454 if (parse_jobs_ini(ini_file[i], i))
1461 if (!thread_number) {
1462 log_err("No jobs defined(s)\n");