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 *);
36 static int str_opendir_cb(void *, const char *);
38 #define __stringify_1(x) #x
39 #define __stringify(x) __stringify_1(x)
42 * Map of job/command line options
44 static struct fio_option options[] = {
46 .name = "description",
47 .type = FIO_OPT_STR_STORE,
48 .off1 = td_var_offset(description),
49 .help = "Text job description",
53 .type = FIO_OPT_STR_STORE,
54 .off1 = td_var_offset(name),
55 .help = "Name of this job",
59 .type = FIO_OPT_STR_STORE,
60 .off1 = td_var_offset(directory),
61 .cb = str_directory_cb,
62 .help = "Directory to store files in",
66 .type = FIO_OPT_STR_STORE,
67 .off1 = td_var_offset(filename),
68 .cb = str_filename_cb,
69 .help = "File(s) to use for the workload",
73 .type = FIO_OPT_STR_STORE,
74 .off1 = td_var_offset(opendir),
76 .help = "Recursively add files from this directory and down",
81 .off1 = td_var_offset(td_ddir),
82 .help = "IO direction",
85 { .ival = "read", .oval = TD_DDIR_READ },
86 { .ival = "write", .oval = TD_DDIR_WRITE },
87 { .ival = "randread", .oval = TD_DDIR_RANDREAD },
88 { .ival = "randwrite", .oval = TD_DDIR_RANDWRITE },
89 { .ival = "rw", .oval = TD_DDIR_RW },
90 { .ival = "randrw", .oval = TD_DDIR_RANDRW },
95 .type = FIO_OPT_STR_STORE,
96 .off1 = td_var_offset(ioengine),
97 .help = "IO engine to use",
101 #ifdef FIO_HAVE_LIBAIO
102 { .ival = "libaio", },
104 #ifdef FIO_HAVE_POSIXAIO
105 { .ival = "posixaio", },
108 #ifdef FIO_HAVE_SPLICE
109 { .ival = "splice", },
114 { .ival = "null", }, { .ival = "net", },
115 #ifdef FIO_HAVE_SYSLET
116 { .ival = "syslet-rw", },
118 { .ival = "cpuio", },
119 { .ival = "external", },
125 .off1 = td_var_offset(iodepth),
126 .help = "Amount of IO buffers to keep in flight",
130 .name = "iodepth_batch",
132 .off1 = td_var_offset(iodepth_batch),
133 .help = "Number of IO to submit in one go",
136 .name = "iodepth_low",
138 .off1 = td_var_offset(iodepth_low),
139 .help = "Low water mark for queuing depth",
143 .type = FIO_OPT_STR_VAL,
144 .off1 = td_var_offset(total_file_size),
145 .help = "Size of device or file",
149 .type = FIO_OPT_STR_VAL_INT,
150 .off1 = td_var_offset(bs[DDIR_READ]),
151 .off2 = td_var_offset(bs[DDIR_WRITE]),
152 .help = "Block size unit",
157 .type = FIO_OPT_RANGE,
158 .off1 = td_var_offset(min_bs[DDIR_READ]),
159 .off2 = td_var_offset(max_bs[DDIR_READ]),
160 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
161 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
162 .help = "Set block size range (in more detail than bs)",
165 .name = "bs_unaligned",
166 .type = FIO_OPT_STR_SET,
167 .off1 = td_var_offset(bs_unaligned),
168 .help = "Don't sector align IO buffer sizes",
172 .type = FIO_OPT_STR_VAL,
173 .off1 = td_var_offset(start_offset),
174 .help = "Start IO from this offset",
178 .name = "randrepeat",
179 .type = FIO_OPT_BOOL,
180 .off1 = td_var_offset(rand_repeatable),
181 .help = "Use repeatable random IO pattern",
185 .name = "norandommap",
186 .type = FIO_OPT_STR_SET,
187 .off1 = td_var_offset(norandommap),
188 .help = "Accept potential duplicate random blocks",
193 .off1 = td_var_offset(nr_files),
194 .help = "Split job workload between this number of files",
200 .off1 = td_var_offset(open_files),
201 .help = "Number of files to keep open at the same time",
204 .name = "file_service_type",
207 .off1 = td_var_offset(file_service_type),
208 .help = "How to select which file to service next",
211 { .ival = "random", .oval = FIO_FSERVICE_RANDOM },
212 { .ival = "roundrobin", .oval = FIO_FSERVICE_RR },
218 .off1 = td_var_offset(fsync_blocks),
219 .help = "Issue fsync for writes every given number of blocks",
224 .type = FIO_OPT_BOOL,
225 .off1 = td_var_offset(odirect),
226 .help = "Use O_DIRECT IO (negates buffered)",
231 .type = FIO_OPT_BOOL,
232 .off1 = td_var_offset(odirect),
234 .help = "Use buffered IO (negates direct)",
239 .type = FIO_OPT_BOOL,
240 .off1 = td_var_offset(overwrite),
241 .help = "When writing, set whether to overwrite current data",
247 .off1 = td_var_offset(loops),
248 .help = "Number of times to run the job",
254 .off1 = td_var_offset(numjobs),
255 .help = "Duplicate this job this many times",
259 .name = "startdelay",
261 .off1 = td_var_offset(start_delay),
262 .help = "Only start job when this period has passed",
268 .type = FIO_OPT_STR_VAL_TIME,
269 .off1 = td_var_offset(timeout),
270 .help = "Stop workload when this amount of time has passed",
277 .off1 = td_var_offset(mem_type),
278 .help = "Backing type for IO buffers",
281 { .ival = "malloc", .oval = MEM_MALLOC },
282 { .ival = "shm", .oval = MEM_SHM },
283 #ifdef FIO_HAVE_HUGETLB
284 { .ival = "shmhuge", .oval = MEM_SHMHUGE },
286 { .ival = "mmap", .oval = MEM_MMAP },
287 #ifdef FIO_HAVE_HUGETLB
288 { .ival = "mmaphuge", .oval = MEM_MMAPHUGE },
295 .off1 = td_var_offset(verify),
296 .help = "Verify sum function",
299 { .ival = "0", .oval = VERIFY_NONE },
300 { .ival = "crc32", .oval = VERIFY_CRC32 },
301 { .ival = "md5", .oval = VERIFY_MD5 },
305 .name = "write_iolog",
306 .type = FIO_OPT_STR_STORE,
307 .off1 = td_var_offset(write_iolog_file),
308 .help = "Store IO pattern to file",
311 .name = "read_iolog",
312 .type = FIO_OPT_STR_STORE,
313 .off1 = td_var_offset(read_iolog_file),
314 .help = "Playback IO pattern from file",
317 .name = "exec_prerun",
318 .type = FIO_OPT_STR_STORE,
319 .off1 = td_var_offset(exec_prerun),
320 .help = "Execute this file prior to running job",
323 .name = "exec_postrun",
324 .type = FIO_OPT_STR_STORE,
325 .off1 = td_var_offset(exec_postrun),
326 .help = "Execute this file after running job",
328 #ifdef FIO_HAVE_IOSCHED_SWITCH
330 .name = "ioscheduler",
331 .type = FIO_OPT_STR_STORE,
332 .off1 = td_var_offset(ioscheduler),
333 .help = "Use this IO scheduler on the backing device",
338 .type = FIO_OPT_STR_VAL,
339 .off1 = td_var_offset(zone_size),
340 .help = "Give size of an IO zone",
345 .type = FIO_OPT_STR_VAL,
346 .off1 = td_var_offset(zone_skip),
347 .help = "Space between IO zones",
352 .type = FIO_OPT_STR_VAL,
353 .cb = str_lockmem_cb,
354 .help = "Lock down this amount of memory",
358 .name = "rwmixcycle",
360 .off1 = td_var_offset(rwmixcycle),
361 .help = "Cycle period for mixed read/write workloads (msec)",
367 .off1 = td_var_offset(rwmixread),
369 .help = "Percentage of mixed workload that is reads",
373 .name = "rwmixwrite",
375 .off1 = td_var_offset(rwmixwrite),
377 .help = "Percentage of mixed workload that is writes",
383 .off1 = td_var_offset(nice),
384 .help = "Set job CPU nice value",
389 #ifdef FIO_HAVE_IOPRIO
394 .help = "Set job IO priority value",
401 .cb = str_prioclass_cb,
402 .help = "Set job IO priority class",
410 .off1 = td_var_offset(thinktime),
411 .help = "Idle time between IO buffers (usec)",
415 .name = "thinktime_spin",
417 .off1 = td_var_offset(thinktime_spin),
418 .help = "Start think time by spinning this amount (usec)",
422 .name = "thinktime_blocks",
424 .off1 = td_var_offset(thinktime_blocks),
425 .help = "IO buffer period between 'thinktime'",
431 .off1 = td_var_offset(rate),
432 .help = "Set bandwidth rate",
437 .off1 = td_var_offset(ratemin),
438 .help = "The bottom limit accepted",
443 .off1 = td_var_offset(ratecycle),
444 .help = "Window average for rate limits (msec)",
448 .name = "invalidate",
449 .type = FIO_OPT_BOOL,
450 .off1 = td_var_offset(invalidate_cache),
451 .help = "Invalidate buffer/page cache prior to running job",
456 .type = FIO_OPT_BOOL,
457 .off1 = td_var_offset(sync_io),
458 .help = "Use O_SYNC for buffered writes",
464 .off1 = td_var_offset(bw_avg_time),
465 .help = "Time window over which to calculate bandwidth (msec)",
469 .name = "create_serialize",
470 .type = FIO_OPT_BOOL,
471 .off1 = td_var_offset(create_serialize),
472 .help = "Serialize creating of job files",
476 .name = "create_fsync",
477 .type = FIO_OPT_BOOL,
478 .off1 = td_var_offset(create_fsync),
479 .help = "Fsync file after creation",
485 .off1 = td_var_offset(cpuload),
486 .help = "Use this percentage of CPU",
491 .off1 = td_var_offset(cpucycle),
492 .help = "Length of the CPU burn cycles (usecs)",
495 #ifdef FIO_HAVE_CPU_AFFINITY
499 .cb = str_cpumask_cb,
500 .help = "CPU affinity mask",
505 .type = FIO_OPT_BOOL,
506 .off1 = td_var_offset(end_fsync),
507 .help = "Include fsync at the end of job",
511 .name = "fsync_on_close",
512 .type = FIO_OPT_BOOL,
513 .off1 = td_var_offset(fsync_on_close),
514 .help = "fsync files on close",
519 .type = FIO_OPT_BOOL,
520 .off1 = td_var_offset(unlink),
521 .help = "Unlink created files after job has completed",
526 .type = FIO_OPT_STR_SET,
527 .cb = str_exitall_cb,
528 .help = "Terminate all jobs when one exits",
532 .type = FIO_OPT_STR_SET,
533 .off1 = td_var_offset(stonewall),
534 .help = "Insert a hard barrier between this job and previous",
538 .type = FIO_OPT_STR_SET,
539 .off1 = td_var_offset(use_thread),
540 .help = "Use threads instead of forks",
543 .name = "write_bw_log",
544 .type = FIO_OPT_STR_SET,
545 .off1 = td_var_offset(write_bw_log),
546 .help = "Write log of bandwidth during run",
549 .name = "write_lat_log",
550 .type = FIO_OPT_STR_SET,
551 .off1 = td_var_offset(write_lat_log),
552 .help = "Write log of latency during run",
555 .name = "hugepage-size",
556 .type = FIO_OPT_STR_VAL,
557 .off1 = td_var_offset(hugepage_size),
558 .help = "When using hugepages, specify size of each page",
559 .def = __stringify(FIO_HUGE_PAGE),
562 .name = "group_reporting",
563 .type = FIO_OPT_STR_SET,
564 .off1 = td_var_offset(group_reporting),
565 .help = "Do reporting on a per-group basis",
572 #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
573 #define FIO_CMD_OPTS (16)
574 #define FIO_GETOPT_JOB (0x89988998)
577 * Command line options. These will contain the above, plus a few
578 * extra that only pertain to fio itself and not jobs.
580 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
583 .has_arg = required_argument,
588 .has_arg = required_argument,
592 .name = "latency-log",
593 .has_arg = required_argument,
597 .name = "bandwidth-log",
598 .has_arg = required_argument,
603 .has_arg = optional_argument,
608 .has_arg = no_argument,
613 .has_arg = no_argument,
618 .has_arg = optional_argument,
626 static int def_timeout = 0;
628 static char fio_version_string[] = "fio 1.13";
630 static char **ini_file;
631 static int max_jobs = MAX_JOBS;
633 struct thread_data def_thread;
634 struct thread_data *threads = NULL;
636 int exitall_on_terminate = 0;
637 int terse_output = 0;
638 unsigned long long mlock_size = 0;
642 static int write_lat_log = 0;
643 int write_bw_log = 0;
645 static int prev_group_jobs;
658 * Return a free job structure.
660 static struct thread_data *get_new_job(int global, struct thread_data *parent)
662 struct thread_data *td;
666 if (thread_number >= max_jobs)
669 td = &threads[thread_number++];
672 td->thread_number = thread_number;
676 static void put_job(struct thread_data *td)
678 if (td == &def_thread)
682 fprintf(f_out, "fio: %s\n", td->verror);
684 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
689 * Lazy way of fixing up options that depend on each other. We could also
690 * define option callback handlers, but this is easier.
692 static void fixup_options(struct thread_data *td)
694 if (!td->rwmixread && td->rwmixwrite)
695 td->rwmixread = 100 - td->rwmixwrite;
697 if (td->write_iolog_file && td->read_iolog_file) {
698 log_err("fio: read iolog overrides write_iolog\n");
699 free(td->write_iolog_file);
700 td->write_iolog_file = NULL;
703 if (td->io_ops->flags & FIO_SYNCIO)
707 td->iodepth = td->open_files;
711 * only really works for sequential io for now, and with 1 file
713 if (td->zone_size && td_random(td) && td->open_files == 1)
717 * Reads can do overwrites, we always need to pre-create the file
719 if (td_read(td) || td_rw(td))
722 if (!td->min_bs[DDIR_READ])
723 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
724 if (!td->max_bs[DDIR_READ])
725 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
726 if (!td->min_bs[DDIR_WRITE])
727 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
728 if (!td->max_bs[DDIR_WRITE])
729 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
731 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
733 if (td_read(td) && !td_rw(td))
736 if (td->norandommap && td->verify != VERIFY_NONE) {
737 log_err("fio: norandommap given, verify disabled\n");
738 td->verify = VERIFY_NONE;
740 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
741 log_err("fio: bs_unaligned may not work with raw io\n");
744 * thinktime_spin must be less than thinktime
746 if (td->thinktime_spin > td->thinktime)
747 td->thinktime_spin = td->thinktime;
750 * The low water mark cannot be bigger than the iodepth
752 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
754 * syslet work around - if the workload is sequential,
755 * we want to let the queue drain all the way down to
756 * avoid seeking between async threads
758 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
761 td->iodepth_low = td->iodepth;
765 * If batch number isn't set, default to the same as iodepth
767 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
768 td->iodepth_batch = td->iodepth;
770 if (td->nr_files > td->files_index)
771 td->nr_files = td->files_index;
773 if (td->open_files > td->nr_files || !td->open_files)
774 td->open_files = td->nr_files;
778 * This function leaks the buffer
780 static char *to_kmg(unsigned int val)
782 char *buf = malloc(32);
783 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
794 snprintf(buf, 31, "%u%c", val, *p);
798 /* External engines are specified by "external:name.o") */
799 static const char *get_engine_name(const char *str)
801 char *p = strstr(str, ":");
807 strip_blank_front(&p);
813 * Adds a job to the list of things todo. Sanitizes the various options
814 * to make sure we don't have conflicts, and initializes various
817 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
819 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
820 "randread", "randwrite", "randrw" };
824 char fname[PATH_MAX];
828 * the def_thread is just for options, it's not a real job
830 if (td == &def_thread)
833 engine = get_engine_name(td->ioengine);
834 td->io_ops = load_ioengine(td, engine);
836 log_err("fio: failed to load engine %s\n", engine);
846 td->io_ops->flags |= FIO_RAWIO;
848 if (!td->filename && !td->files_index) {
849 td->filename = strdup(jobname);
851 if (td->nr_files == 1)
852 add_file(td, td->filename);
854 for (i = 0; i < td->nr_files; i++) {
855 sprintf(fname, "%s.%d.%d", td->filename, td->thread_number, i);
863 for_each_file(td, f, i) {
864 if (td->directory && f->filetype == FIO_TYPE_FILE) {
865 sprintf(fname, "%s/%s", td->directory, f->file_name);
866 f->file_name = strdup(fname);
870 td->mutex = fio_sem_init(0);
872 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
873 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
874 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
876 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
881 td->groupid = groupid;
887 if (td->write_lat_log) {
888 setup_log(&td->ts.slat_log);
889 setup_log(&td->ts.clat_log);
891 if (td->write_bw_log)
892 setup_log(&td->ts.bw_log);
895 td->name = strdup(jobname);
899 if (!strcmp(td->io_ops->name, "cpuio"))
900 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
902 char *c1, *c2, *c3, *c4;
904 c1 = to_kmg(td->min_bs[DDIR_READ]);
905 c2 = to_kmg(td->max_bs[DDIR_READ]);
906 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
907 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
909 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);
916 } else if (job_add_num == 1)
917 fprintf(f_out, "...\n");
921 * recurse add identical jobs, clear numjobs and stonewall options
922 * as they don't apply to sub-jobs
924 numjobs = td->numjobs;
926 struct thread_data *td_new = get_new_job(0, td);
932 td_new->stonewall = 0;
933 job_add_num = numjobs - 1;
935 if (add_job(td_new, jobname, job_add_num))
939 if (td->numjobs > 1) {
951 * Initialize the various random states we need (random io, block size ranges,
952 * read/write mix, etc).
954 int init_random_state(struct thread_data *td)
956 unsigned long seeds[5];
957 int fd, num_maps, blocks;
961 if (td->io_ops->flags & FIO_DISKLESSIO)
964 fd = open("/dev/urandom", O_RDONLY);
966 td_verror(td, errno, "open");
970 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
971 td_verror(td, EIO, "read");
978 os_random_seed(seeds[0], &td->bsrange_state);
979 os_random_seed(seeds[1], &td->verify_state);
980 os_random_seed(seeds[2], &td->rwmix_state);
982 if (td->file_service_type == FIO_FSERVICE_RANDOM)
983 os_random_seed(seeds[3], &td->next_file_state);
988 if (td->rand_repeatable)
989 seeds[4] = FIO_RANDSEED * td->thread_number;
991 if (!td->norandommap) {
992 for_each_file(td, f, i) {
993 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
994 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
995 f->file_map = malloc(num_maps * sizeof(long));
997 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
1000 f->num_maps = num_maps;
1001 memset(f->file_map, 0, num_maps * sizeof(long));
1005 os_random_seed(seeds[4], &td->random_state);
1009 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
1011 #ifdef FIO_HAVE_CPU_AFFINITY
1016 for (i = 0; i < sizeof(int) * 8; i++) {
1018 CPU_SET(i, &cpumask);
1023 static int is_empty_or_comment(char *line)
1027 for (i = 0; i < strlen(line); i++) {
1032 if (!isspace(line[i]) && !iscntrl(line[i]))
1040 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
1042 static char *get_opt_postfix(const char *str)
1044 char *p = strstr(str, ":");
1050 strip_blank_front(&p);
1055 static int str_mem_cb(void *data, const char *mem)
1057 struct thread_data *td = data;
1059 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
1060 td->mmapfile = get_opt_postfix(mem);
1061 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
1062 log_err("fio: mmaphuge:/path/to/file\n");
1070 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1076 #ifdef FIO_HAVE_IOPRIO
1077 static int str_prioclass_cb(void *data, unsigned int *val)
1079 struct thread_data *td = data;
1081 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1085 static int str_prio_cb(void *data, unsigned int *val)
1087 struct thread_data *td = data;
1094 static int str_exitall_cb(void)
1096 exitall_on_terminate = 1;
1100 static int str_cpumask_cb(void *data, unsigned int *val)
1102 struct thread_data *td = data;
1104 fill_cpu_mask(td->cpumask, *val);
1108 static int str_fst_cb(void *data, const char *str)
1110 struct thread_data *td = data;
1111 char *nr = get_opt_postfix(str);
1113 td->file_service_nr = 1;
1115 td->file_service_nr = atoi(nr);
1120 static int str_filename_cb(void *data, const char *input)
1122 struct thread_data *td = data;
1123 char *fname, *str, *p;
1125 p = str = strdup(input);
1127 strip_blank_front(&str);
1128 strip_blank_end(str);
1130 if (!td->files_index)
1133 while ((fname = strsep(&str, ":")) != NULL) {
1136 add_file(td, fname);
1144 static int str_directory_cb(void *data, const char fio_unused *str)
1146 struct thread_data *td = data;
1149 if (lstat(td->directory, &sb) < 0) {
1150 log_err("fio: %s is not a directory\n", td->directory);
1151 td_verror(td, errno, "lstat");
1154 if (!S_ISDIR(sb.st_mode)) {
1155 log_err("fio: %s is not a directory\n", td->directory);
1162 static int str_opendir_cb(void *data, const char fio_unused *str)
1164 struct thread_data *td = data;
1166 if (!td->files_index)
1169 return add_dir_files(td, td->opendir);
1173 * This is our [ini] type file parser.
1175 static int parse_jobs_ini(char *file, int stonewall_flag)
1177 unsigned int global;
1178 struct thread_data *td;
1179 char *string, *name;
1183 int ret = 0, stonewall;
1185 f = fopen(file, "r");
1187 perror("fopen job file");
1191 string = malloc(4096);
1193 memset(name, 0, 256);
1195 stonewall = stonewall_flag;
1197 p = fgets(string, 4095, f);
1200 if (is_empty_or_comment(p))
1202 if (sscanf(p, "[%255s]", name) != 1)
1205 global = !strncmp(name, "global", 6);
1207 name[strlen(name) - 1] = '\0';
1209 td = get_new_job(global, &def_thread);
1216 * Seperate multiple job files by a stonewall
1218 if (!global && stonewall) {
1219 td->stonewall = stonewall;
1224 while ((p = fgets(string, 4096, f)) != NULL) {
1225 if (is_empty_or_comment(p))
1228 strip_blank_front(&p);
1238 * Don't break here, continue parsing options so we
1239 * dump all the bad ones. Makes trial/error fixups
1240 * easier on the user.
1242 ret |= parse_option(p, options, td);
1247 ret = add_job(td, name, 0);
1249 log_err("fio: job %s dropped\n", name);
1260 static int fill_def_thread(void)
1262 memset(&def_thread, 0, sizeof(def_thread));
1264 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1265 perror("sched_getaffinity");
1270 * fill default options
1272 fill_default_options(&def_thread, options);
1274 def_thread.timeout = def_timeout;
1275 def_thread.write_bw_log = write_bw_log;
1276 def_thread.write_lat_log = write_lat_log;
1278 #ifdef FIO_HAVE_DISK_UTIL
1279 def_thread.do_disk_util = 1;
1285 static void usage(void)
1287 printf("%s\n", fio_version_string);
1288 printf("\t--output\tWrite output to file\n");
1289 printf("\t--timeout\tRuntime in seconds\n");
1290 printf("\t--latency-log\tGenerate per-job latency logs\n");
1291 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1292 printf("\t--minimal\tMinimal (terse) output\n");
1293 printf("\t--version\tPrint version info and exit\n");
1294 printf("\t--help\t\tPrint this page\n");
1295 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
1298 static int parse_cmd_line(int argc, char *argv[])
1300 struct thread_data *td = NULL;
1301 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
1303 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
1306 def_timeout = atoi(optarg);
1315 f_out = fopen(optarg, "w+");
1317 perror("fopen output");
1329 ret = show_cmd_help(options, optarg);
1332 printf("%s\n", fio_version_string);
1334 case FIO_GETOPT_JOB: {
1335 const char *opt = long_options[lidx].name;
1338 if (!strncmp(opt, "name", 4) && td) {
1339 ret = add_job(td, td->name ?: "fio", 0);
1347 int global = !strncmp(val, "global", 6);
1349 td = get_new_job(global, &def_thread);
1354 ret = parse_cmd_option(opt, val, options, td);
1368 ret = add_job(td, td->name ?: "fio", 0);
1374 while (optind < argc) {
1376 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1377 ini_file[ini_idx - 1] = strdup(argv[optind]);
1384 static void free_shm(void)
1386 struct shmid_ds sbuf;
1389 shmdt((void *) threads);
1391 shmctl(shm_id, IPC_RMID, &sbuf);
1396 * The thread area is shared between the main process and the job
1397 * threads/processes. So setup a shared memory segment that will hold
1400 static int setup_thread_area(void)
1403 * 1024 is too much on some machines, scale max_jobs if
1404 * we get a failure that looks like too large a shm segment
1407 size_t size = max_jobs * sizeof(struct thread_data);
1409 shm_id = shmget(0, size, IPC_CREAT | 0600);
1412 if (errno != EINVAL) {
1423 threads = shmat(shm_id, NULL, 0);
1424 if (threads == (void *) -1) {
1434 * Copy the fio options into the long options map, so we mirror
1435 * job and cmd line options.
1437 static void dupe_job_options(void)
1439 struct fio_option *o;
1443 while (long_options[i].name)
1448 long_options[i].name = o->name;
1449 long_options[i].val = FIO_GETOPT_JOB;
1450 if (o->type == FIO_OPT_STR_SET)
1451 long_options[i].has_arg = no_argument;
1453 long_options[i].has_arg = required_argument;
1457 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1461 int parse_options(int argc, char *argv[])
1468 options_init(options);
1472 if (setup_thread_area())
1474 if (fill_def_thread())
1477 job_files = parse_cmd_line(argc, argv);
1479 for (i = 0; i < job_files; i++) {
1480 if (fill_def_thread())
1482 if (parse_jobs_ini(ini_file[i], i))
1489 if (!thread_number) {
1490 log_err("No jobs defined(s)\n");