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 *);
34 static int str_filename_cb(void *, const char *);
35 static int str_directory_cb(void *, const char *);
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 .cb = str_directory_cb,
61 .help = "Directory to store files in",
65 .type = FIO_OPT_STR_STORE,
66 .off1 = td_var_offset(filename),
67 .cb = str_filename_cb,
68 .help = "File(s) to use for the workload",
73 .off1 = td_var_offset(td_ddir),
74 .help = "IO direction",
77 { .ival = "read", .oval = TD_DDIR_READ },
78 { .ival = "write", .oval = TD_DDIR_WRITE },
79 { .ival = "randread", .oval = TD_DDIR_RANDREAD },
80 { .ival = "randwrite", .oval = TD_DDIR_RANDWRITE },
81 { .ival = "rw", .oval = TD_DDIR_RW },
82 { .ival = "randrw", .oval = TD_DDIR_RANDRW },
87 .type = FIO_OPT_STR_STORE,
88 .off1 = td_var_offset(ioengine),
89 .help = "IO engine to use",
93 #ifdef FIO_HAVE_LIBAIO
94 { .ival = "libaio", },
96 #ifdef FIO_HAVE_POSIXAIO
97 { .ival = "posixaio", },
100 #ifdef FIO_HAVE_SPLICE
101 { .ival = "splice", },
106 { .ival = "null", }, { .ival = "net", },
107 #ifdef FIO_HAVE_SYSLET
108 { .ival = "syslet-rw", },
110 { .ival = "cpuio", },
111 { .ival = "external", },
117 .off1 = td_var_offset(iodepth),
118 .help = "Amount of IO buffers to keep in flight",
122 .name = "iodepth_batch",
124 .off1 = td_var_offset(iodepth_batch),
125 .help = "Number of IO to submit in one go",
128 .name = "iodepth_low",
130 .off1 = td_var_offset(iodepth_low),
131 .help = "Low water mark for queuing depth",
135 .type = FIO_OPT_STR_VAL,
136 .off1 = td_var_offset(total_file_size),
137 .help = "Size of device or file",
141 .type = FIO_OPT_STR_VAL_INT,
142 .off1 = td_var_offset(bs[DDIR_READ]),
143 .off2 = td_var_offset(bs[DDIR_WRITE]),
144 .help = "Block size unit",
149 .type = FIO_OPT_RANGE,
150 .off1 = td_var_offset(min_bs[DDIR_READ]),
151 .off2 = td_var_offset(max_bs[DDIR_READ]),
152 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
153 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
154 .help = "Set block size range (in more detail than bs)",
157 .name = "bs_unaligned",
158 .type = FIO_OPT_STR_SET,
159 .off1 = td_var_offset(bs_unaligned),
160 .help = "Don't sector align IO buffer sizes",
164 .type = FIO_OPT_STR_VAL,
165 .off1 = td_var_offset(start_offset),
166 .help = "Start IO from this offset",
170 .name = "randrepeat",
171 .type = FIO_OPT_BOOL,
172 .off1 = td_var_offset(rand_repeatable),
173 .help = "Use repeatable random IO pattern",
177 .name = "norandommap",
178 .type = FIO_OPT_STR_SET,
179 .off1 = td_var_offset(norandommap),
180 .help = "Accept potential duplicate random blocks",
185 .off1 = td_var_offset(nr_files),
186 .help = "Split job workload between this number of files",
192 .off1 = td_var_offset(open_files),
193 .help = "Number of files to keep open at the same time",
196 .name = "file_service_type",
199 .off1 = td_var_offset(file_service_type),
200 .help = "How to select which file to service next",
203 { .ival = "random", .oval = FIO_FSERVICE_RANDOM },
204 { .ival = "roundrobin", .oval = FIO_FSERVICE_RR },
210 .off1 = td_var_offset(fsync_blocks),
211 .help = "Issue fsync for writes every given number of blocks",
216 .type = FIO_OPT_BOOL,
217 .off1 = td_var_offset(odirect),
218 .help = "Use O_DIRECT IO (negates buffered)",
223 .type = FIO_OPT_BOOL,
224 .off1 = td_var_offset(odirect),
226 .help = "Use buffered IO (negates direct)",
231 .type = FIO_OPT_BOOL,
232 .off1 = td_var_offset(overwrite),
233 .help = "When writing, set whether to overwrite current data",
239 .off1 = td_var_offset(loops),
240 .help = "Number of times to run the job",
246 .off1 = td_var_offset(numjobs),
247 .help = "Duplicate this job this many times",
251 .name = "startdelay",
253 .off1 = td_var_offset(start_delay),
254 .help = "Only start job when this period has passed",
260 .type = FIO_OPT_STR_VAL_TIME,
261 .off1 = td_var_offset(timeout),
262 .help = "Stop workload when this amount of time has passed",
269 .off1 = td_var_offset(mem_type),
270 .help = "Backing type for IO buffers",
273 { .ival = "malloc", .oval = MEM_MALLOC },
274 { .ival = "shm", .oval = MEM_SHM },
275 #ifdef FIO_HAVE_HUGETLB
276 { .ival = "shmhuge", .oval = MEM_SHMHUGE },
278 { .ival = "mmap", .oval = MEM_MMAP },
279 #ifdef FIO_HAVE_HUGETLB
280 { .ival = "mmaphuge", .oval = MEM_MMAPHUGE },
287 .off1 = td_var_offset(verify),
288 .help = "Verify sum function",
291 { .ival = "0", .oval = VERIFY_NONE },
292 { .ival = "crc32", .oval = VERIFY_CRC32 },
293 { .ival = "md5", .oval = VERIFY_MD5 },
297 .name = "write_iolog",
298 .type = FIO_OPT_STR_STORE,
299 .off1 = td_var_offset(write_iolog_file),
300 .help = "Store IO pattern to file",
303 .name = "read_iolog",
304 .type = FIO_OPT_STR_STORE,
305 .off1 = td_var_offset(read_iolog_file),
306 .help = "Playback IO pattern from file",
309 .name = "exec_prerun",
310 .type = FIO_OPT_STR_STORE,
311 .off1 = td_var_offset(exec_prerun),
312 .help = "Execute this file prior to running job",
315 .name = "exec_postrun",
316 .type = FIO_OPT_STR_STORE,
317 .off1 = td_var_offset(exec_postrun),
318 .help = "Execute this file after running job",
320 #ifdef FIO_HAVE_IOSCHED_SWITCH
322 .name = "ioscheduler",
323 .type = FIO_OPT_STR_STORE,
324 .off1 = td_var_offset(ioscheduler),
325 .help = "Use this IO scheduler on the backing device",
330 .type = FIO_OPT_STR_VAL,
331 .off1 = td_var_offset(zone_size),
332 .help = "Give size of an IO zone",
337 .type = FIO_OPT_STR_VAL,
338 .off1 = td_var_offset(zone_skip),
339 .help = "Space between IO zones",
344 .type = FIO_OPT_STR_VAL,
345 .cb = str_lockmem_cb,
346 .help = "Lock down this amount of memory",
350 .name = "rwmixcycle",
352 .off1 = td_var_offset(rwmixcycle),
353 .help = "Cycle period for mixed read/write workloads (msec)",
359 .off1 = td_var_offset(rwmixread),
361 .help = "Percentage of mixed workload that is reads",
365 .name = "rwmixwrite",
367 .off1 = td_var_offset(rwmixwrite),
369 .help = "Percentage of mixed workload that is writes",
375 .off1 = td_var_offset(nice),
376 .help = "Set job CPU nice value",
381 #ifdef FIO_HAVE_IOPRIO
386 .help = "Set job IO priority value",
393 .cb = str_prioclass_cb,
394 .help = "Set job IO priority class",
402 .off1 = td_var_offset(thinktime),
403 .help = "Idle time between IO buffers (usec)",
407 .name = "thinktime_spin",
409 .off1 = td_var_offset(thinktime_spin),
410 .help = "Start think time by spinning this amount (usec)",
414 .name = "thinktime_blocks",
416 .off1 = td_var_offset(thinktime_blocks),
417 .help = "IO buffer period between 'thinktime'",
423 .off1 = td_var_offset(rate),
424 .help = "Set bandwidth rate",
429 .off1 = td_var_offset(ratemin),
430 .help = "The bottom limit accepted",
435 .off1 = td_var_offset(ratecycle),
436 .help = "Window average for rate limits (msec)",
440 .name = "invalidate",
441 .type = FIO_OPT_BOOL,
442 .off1 = td_var_offset(invalidate_cache),
443 .help = "Invalidate buffer/page cache prior to running job",
448 .type = FIO_OPT_BOOL,
449 .off1 = td_var_offset(sync_io),
450 .help = "Use O_SYNC for buffered writes",
456 .off1 = td_var_offset(bw_avg_time),
457 .help = "Time window over which to calculate bandwidth (msec)",
461 .name = "create_serialize",
462 .type = FIO_OPT_BOOL,
463 .off1 = td_var_offset(create_serialize),
464 .help = "Serialize creating of job files",
468 .name = "create_fsync",
469 .type = FIO_OPT_BOOL,
470 .off1 = td_var_offset(create_fsync),
471 .help = "Fsync file after creation",
477 .off1 = td_var_offset(cpuload),
478 .help = "Use this percentage of CPU",
483 .off1 = td_var_offset(cpucycle),
484 .help = "Length of the CPU burn cycles (usecs)",
487 #ifdef FIO_HAVE_CPU_AFFINITY
491 .cb = str_cpumask_cb,
492 .help = "CPU affinity mask",
497 .type = FIO_OPT_BOOL,
498 .off1 = td_var_offset(end_fsync),
499 .help = "Include fsync at the end of job",
503 .name = "fsync_on_close",
504 .type = FIO_OPT_BOOL,
505 .off1 = td_var_offset(fsync_on_close),
506 .help = "fsync files on close",
511 .type = FIO_OPT_BOOL,
512 .off1 = td_var_offset(unlink),
513 .help = "Unlink created files after job has completed",
518 .type = FIO_OPT_STR_SET,
519 .cb = str_exitall_cb,
520 .help = "Terminate all jobs when one exits",
524 .type = FIO_OPT_STR_SET,
525 .off1 = td_var_offset(stonewall),
526 .help = "Insert a hard barrier between this job and previous",
530 .type = FIO_OPT_STR_SET,
531 .off1 = td_var_offset(use_thread),
532 .help = "Use threads instead of forks",
535 .name = "write_bw_log",
536 .type = FIO_OPT_STR_SET,
537 .off1 = td_var_offset(write_bw_log),
538 .help = "Write log of bandwidth during run",
541 .name = "write_lat_log",
542 .type = FIO_OPT_STR_SET,
543 .off1 = td_var_offset(write_lat_log),
544 .help = "Write log of latency during run",
547 .name = "hugepage-size",
548 .type = FIO_OPT_STR_VAL,
549 .off1 = td_var_offset(hugepage_size),
550 .help = "When using hugepages, specify size of each page",
551 .def = __stringify(FIO_HUGE_PAGE),
554 .name = "group_reporting",
555 .type = FIO_OPT_STR_SET,
556 .off1 = td_var_offset(group_reporting),
557 .help = "Do reporting on a per-group basis",
564 #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
565 #define FIO_CMD_OPTS (16)
566 #define FIO_GETOPT_JOB (0x89988998)
569 * Command line options. These will contain the above, plus a few
570 * extra that only pertain to fio itself and not jobs.
572 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
575 .has_arg = required_argument,
580 .has_arg = required_argument,
584 .name = "latency-log",
585 .has_arg = required_argument,
589 .name = "bandwidth-log",
590 .has_arg = required_argument,
595 .has_arg = optional_argument,
600 .has_arg = no_argument,
605 .has_arg = no_argument,
610 .has_arg = optional_argument,
618 static int def_timeout = 0;
620 static char fio_version_string[] = "fio 1.13";
622 static char **ini_file;
623 static int max_jobs = MAX_JOBS;
625 struct thread_data def_thread;
626 struct thread_data *threads = NULL;
628 int exitall_on_terminate = 0;
629 int terse_output = 0;
630 unsigned long long mlock_size = 0;
634 static int write_lat_log = 0;
635 int write_bw_log = 0;
637 static int prev_group_jobs;
650 * Return a free job structure.
652 static struct thread_data *get_new_job(int global, struct thread_data *parent)
654 struct thread_data *td;
658 if (thread_number >= max_jobs)
661 td = &threads[thread_number++];
664 td->thread_number = thread_number;
668 static void put_job(struct thread_data *td)
670 if (td == &def_thread)
674 fprintf(f_out, "fio: %s\n", td->verror);
676 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
681 * Lazy way of fixing up options that depend on each other. We could also
682 * define option callback handlers, but this is easier.
684 static void fixup_options(struct thread_data *td)
686 if (!td->rwmixread && td->rwmixwrite)
687 td->rwmixread = 100 - td->rwmixwrite;
689 if (td->write_iolog_file && td->read_iolog_file) {
690 log_err("fio: read iolog overrides write_iolog\n");
691 free(td->write_iolog_file);
692 td->write_iolog_file = NULL;
695 if (td->io_ops->flags & FIO_SYNCIO)
699 td->iodepth = td->open_files;
703 * only really works for sequential io for now, and with 1 file
705 if (td->zone_size && td_random(td) && td->open_files == 1)
709 * Reads can do overwrites, we always need to pre-create the file
711 if (td_read(td) || td_rw(td))
714 if (!td->min_bs[DDIR_READ])
715 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
716 if (!td->max_bs[DDIR_READ])
717 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
718 if (!td->min_bs[DDIR_WRITE])
719 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
720 if (!td->max_bs[DDIR_WRITE])
721 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
723 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
725 if (td_read(td) && !td_rw(td))
728 if (td->norandommap && td->verify != VERIFY_NONE) {
729 log_err("fio: norandommap given, verify disabled\n");
730 td->verify = VERIFY_NONE;
732 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
733 log_err("fio: bs_unaligned may not work with raw io\n");
736 * thinktime_spin must be less than thinktime
738 if (td->thinktime_spin > td->thinktime)
739 td->thinktime_spin = td->thinktime;
742 * The low water mark cannot be bigger than the iodepth
744 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
746 * syslet work around - if the workload is sequential,
747 * we want to let the queue drain all the way down to
748 * avoid seeking between async threads
750 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
753 td->iodepth_low = td->iodepth;
757 * If batch number isn't set, default to the same as iodepth
759 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
760 td->iodepth_batch = td->iodepth;
763 td->nr_files = td->files_index;
764 else if (td->nr_files > td->files_index)
765 td->nr_files = td->files_index;
767 if (td->open_files > td->nr_files || !td->open_files)
768 td->open_files = td->nr_files;
772 * This function leaks the buffer
774 static char *to_kmg(unsigned int val)
776 char *buf = malloc(32);
777 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
788 snprintf(buf, 31, "%u%c", val, *p);
792 /* External engines are specified by "external:name.o") */
793 static const char *get_engine_name(const char *str)
795 char *p = strstr(str, ":");
801 strip_blank_front(&p);
807 * Adds a job to the list of things todo. Sanitizes the various options
808 * to make sure we don't have conflicts, and initializes various
811 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
813 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
814 "randread", "randwrite", "randrw" };
818 char fname[PATH_MAX];
822 * the def_thread is just for options, it's not a real job
824 if (td == &def_thread)
827 engine = get_engine_name(td->ioengine);
828 td->io_ops = load_ioengine(td, engine);
830 log_err("fio: failed to load engine %s\n", engine);
840 td->io_ops->flags |= FIO_RAWIO;
843 td->filename = strdup(jobname);
845 if (td->nr_files == 1)
846 add_file(td, td->filename);
848 for (i = 0; i < td->nr_files; i++) {
849 sprintf(fname, "%s.%d.%d", td->filename, td->thread_number, i);
857 for_each_file(td, f, i) {
858 if (td->directory && f->filetype == FIO_TYPE_FILE) {
859 sprintf(fname, "%s/%s", td->directory, f->file_name);
860 f->file_name = strdup(fname);
864 td->mutex = fio_sem_init(0);
866 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
867 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
868 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
870 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
875 td->groupid = groupid;
881 if (td->write_lat_log) {
882 setup_log(&td->ts.slat_log);
883 setup_log(&td->ts.clat_log);
885 if (td->write_bw_log)
886 setup_log(&td->ts.bw_log);
889 td->name = strdup(jobname);
893 if (!strcmp(td->io_ops->name, "cpuio"))
894 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
896 char *c1, *c2, *c3, *c4;
898 c1 = to_kmg(td->min_bs[DDIR_READ]);
899 c2 = to_kmg(td->max_bs[DDIR_READ]);
900 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
901 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
903 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);
910 } else if (job_add_num == 1)
911 fprintf(f_out, "...\n");
915 * recurse add identical jobs, clear numjobs and stonewall options
916 * as they don't apply to sub-jobs
918 numjobs = td->numjobs;
920 struct thread_data *td_new = get_new_job(0, td);
926 td_new->stonewall = 0;
927 job_add_num = numjobs - 1;
929 if (add_job(td_new, jobname, job_add_num))
933 if (td->numjobs > 1) {
945 * Initialize the various random states we need (random io, block size ranges,
946 * read/write mix, etc).
948 int init_random_state(struct thread_data *td)
950 unsigned long seeds[5];
951 int fd, num_maps, blocks;
955 if (td->io_ops->flags & FIO_DISKLESSIO)
958 fd = open("/dev/urandom", O_RDONLY);
960 td_verror(td, errno, "open");
964 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
965 td_verror(td, EIO, "read");
972 os_random_seed(seeds[0], &td->bsrange_state);
973 os_random_seed(seeds[1], &td->verify_state);
974 os_random_seed(seeds[2], &td->rwmix_state);
976 if (td->file_service_type == FIO_FSERVICE_RANDOM)
977 os_random_seed(seeds[3], &td->next_file_state);
982 if (td->rand_repeatable)
983 seeds[4] = FIO_RANDSEED * td->thread_number;
985 if (!td->norandommap) {
986 for_each_file(td, f, i) {
987 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
988 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
989 f->file_map = malloc(num_maps * sizeof(long));
991 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
994 f->num_maps = num_maps;
995 memset(f->file_map, 0, num_maps * sizeof(long));
999 os_random_seed(seeds[4], &td->random_state);
1003 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
1005 #ifdef FIO_HAVE_CPU_AFFINITY
1010 for (i = 0; i < sizeof(int) * 8; i++) {
1012 CPU_SET(i, &cpumask);
1017 static int is_empty_or_comment(char *line)
1021 for (i = 0; i < strlen(line); i++) {
1026 if (!isspace(line[i]) && !iscntrl(line[i]))
1034 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
1036 static char *get_opt_postfix(const char *str)
1038 char *p = strstr(str, ":");
1044 strip_blank_front(&p);
1049 static int str_mem_cb(void *data, const char *mem)
1051 struct thread_data *td = data;
1053 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
1054 td->mmapfile = get_opt_postfix(mem);
1055 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
1056 log_err("fio: mmaphuge:/path/to/file\n");
1064 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1070 #ifdef FIO_HAVE_IOPRIO
1071 static int str_prioclass_cb(void *data, unsigned int *val)
1073 struct thread_data *td = data;
1075 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1079 static int str_prio_cb(void *data, unsigned int *val)
1081 struct thread_data *td = data;
1088 static int str_exitall_cb(void)
1090 exitall_on_terminate = 1;
1094 static int str_cpumask_cb(void *data, unsigned int *val)
1096 struct thread_data *td = data;
1098 fill_cpu_mask(td->cpumask, *val);
1102 static int str_fst_cb(void *data, const char *str)
1104 struct thread_data *td = data;
1105 char *nr = get_opt_postfix(str);
1107 td->file_service_nr = 1;
1109 td->file_service_nr = atoi(nr);
1114 static int str_filename_cb(void *data, const char *input)
1116 struct thread_data *td = data;
1117 char *fname, *str, *p;
1119 p = str = strdup(input);
1121 strip_blank_front(&str);
1122 strip_blank_end(str);
1124 while ((fname = strsep(&str, ":")) != NULL) {
1127 add_file(td, fname);
1134 static int str_directory_cb(void *data, const char fio_unused *str)
1136 struct thread_data *td = data;
1139 if (lstat(td->directory, &sb) < 0) {
1140 log_err("fio: %s is not a directory\n", td->directory);
1141 td_verror(td, errno, "lstat");
1144 if (!S_ISDIR(sb.st_mode)) {
1145 log_err("fio: %s is not a directory\n", td->directory);
1153 * This is our [ini] type file parser.
1155 static int parse_jobs_ini(char *file, int stonewall_flag)
1157 unsigned int global;
1158 struct thread_data *td;
1159 char *string, *name;
1163 int ret = 0, stonewall;
1165 f = fopen(file, "r");
1167 perror("fopen job file");
1171 string = malloc(4096);
1173 memset(name, 0, 256);
1175 stonewall = stonewall_flag;
1177 p = fgets(string, 4095, f);
1180 if (is_empty_or_comment(p))
1182 if (sscanf(p, "[%255s]", name) != 1)
1185 global = !strncmp(name, "global", 6);
1187 name[strlen(name) - 1] = '\0';
1189 td = get_new_job(global, &def_thread);
1196 * Seperate multiple job files by a stonewall
1198 if (!global && stonewall) {
1199 td->stonewall = stonewall;
1204 while ((p = fgets(string, 4096, f)) != NULL) {
1205 if (is_empty_or_comment(p))
1208 strip_blank_front(&p);
1218 * Don't break here, continue parsing options so we
1219 * dump all the bad ones. Makes trial/error fixups
1220 * easier on the user.
1222 ret |= parse_option(p, options, td);
1227 ret = add_job(td, name, 0);
1229 log_err("fio: job %s dropped\n", name);
1240 static int fill_def_thread(void)
1242 memset(&def_thread, 0, sizeof(def_thread));
1244 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1245 perror("sched_getaffinity");
1250 * fill default options
1252 fill_default_options(&def_thread, options);
1254 def_thread.timeout = def_timeout;
1255 def_thread.write_bw_log = write_bw_log;
1256 def_thread.write_lat_log = write_lat_log;
1258 #ifdef FIO_HAVE_DISK_UTIL
1259 def_thread.do_disk_util = 1;
1265 static void usage(void)
1267 printf("%s\n", fio_version_string);
1268 printf("\t--output\tWrite output to file\n");
1269 printf("\t--timeout\tRuntime in seconds\n");
1270 printf("\t--latency-log\tGenerate per-job latency logs\n");
1271 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1272 printf("\t--minimal\tMinimal (terse) output\n");
1273 printf("\t--version\tPrint version info and exit\n");
1274 printf("\t--help\t\tPrint this page\n");
1275 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
1278 static int parse_cmd_line(int argc, char *argv[])
1280 struct thread_data *td = NULL;
1281 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
1283 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
1286 def_timeout = atoi(optarg);
1295 f_out = fopen(optarg, "w+");
1297 perror("fopen output");
1309 ret = show_cmd_help(options, optarg);
1312 printf("%s\n", fio_version_string);
1314 case FIO_GETOPT_JOB: {
1315 const char *opt = long_options[lidx].name;
1318 if (!strncmp(opt, "name", 4) && td) {
1319 ret = add_job(td, td->name ?: "fio", 0);
1327 int global = !strncmp(val, "global", 6);
1329 td = get_new_job(global, &def_thread);
1334 ret = parse_cmd_option(opt, val, options, td);
1348 ret = add_job(td, td->name ?: "fio", 0);
1354 while (optind < argc) {
1356 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1357 ini_file[ini_idx - 1] = strdup(argv[optind]);
1364 static void free_shm(void)
1366 struct shmid_ds sbuf;
1369 shmdt((void *) threads);
1371 shmctl(shm_id, IPC_RMID, &sbuf);
1376 * The thread area is shared between the main process and the job
1377 * threads/processes. So setup a shared memory segment that will hold
1380 static int setup_thread_area(void)
1383 * 1024 is too much on some machines, scale max_jobs if
1384 * we get a failure that looks like too large a shm segment
1387 size_t size = max_jobs * sizeof(struct thread_data);
1389 shm_id = shmget(0, size, IPC_CREAT | 0600);
1392 if (errno != EINVAL) {
1403 threads = shmat(shm_id, NULL, 0);
1404 if (threads == (void *) -1) {
1414 * Copy the fio options into the long options map, so we mirror
1415 * job and cmd line options.
1417 static void dupe_job_options(void)
1419 struct fio_option *o;
1423 while (long_options[i].name)
1428 long_options[i].name = o->name;
1429 long_options[i].val = FIO_GETOPT_JOB;
1430 if (o->type == FIO_OPT_STR_SET)
1431 long_options[i].has_arg = no_argument;
1433 long_options[i].has_arg = required_argument;
1437 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1441 int parse_options(int argc, char *argv[])
1448 options_init(options);
1452 if (setup_thread_area())
1454 if (fill_def_thread())
1457 job_files = parse_cmd_line(argc, argv);
1459 for (i = 0; i < job_files; i++) {
1460 if (fill_def_thread())
1462 if (parse_jobs_ini(ini_file[i], i))
1469 if (!thread_number) {
1470 log_err("No jobs defined(s)\n");