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",
87 .help = "Sequential read",
90 .oval = TD_DDIR_WRITE,
91 .help = "Sequential write",
94 .oval = TD_DDIR_RANDREAD,
95 .help = "Random read",
97 { .ival = "randwrite",
98 .oval = TD_DDIR_RANDWRITE,
99 .help = "Random write",
103 .help = "Sequential read and write mix",
106 .oval = TD_DDIR_RANDRW,
107 .help = "Random read and write mix"
113 .type = FIO_OPT_STR_STORE,
114 .off1 = td_var_offset(ioengine),
115 .help = "IO engine to use",
119 .help = "Use read/write",
121 #ifdef FIO_HAVE_LIBAIO
123 .help = "Linux native asynchronous IO",
126 #ifdef FIO_HAVE_POSIXAIO
127 { .ival = "posixaio",
128 .help = "POSIX asynchronous IO",
132 .help = "Memory mapped IO",
134 #ifdef FIO_HAVE_SPLICE
136 .help = "splice/vmsplice based IO",
141 .help = "SCSI generic v3 IO",
145 .help = "Testing engine (no data transfer)",
148 .help = "Network IO",
150 #ifdef FIO_HAVE_SYSLET
151 { .ival = "syslet-rw",
152 .help = "syslet enabled async pread/pwrite IO",
156 .help = "CPU cycler burner engine",
158 { .ival = "external",
159 .help = "Load external engine (append name)",
166 .off1 = td_var_offset(iodepth),
167 .help = "Amount of IO buffers to keep in flight",
171 .name = "iodepth_batch",
173 .off1 = td_var_offset(iodepth_batch),
174 .help = "Number of IO to submit in one go",
177 .name = "iodepth_low",
179 .off1 = td_var_offset(iodepth_low),
180 .help = "Low water mark for queuing depth",
184 .type = FIO_OPT_STR_VAL,
185 .off1 = td_var_offset(total_file_size),
186 .help = "Total size of device or files",
190 .type = FIO_OPT_STR_VAL,
191 .off1 = td_var_offset(file_size_low),
192 .off2 = td_var_offset(file_size_high),
193 .help = "Size of individual files",
197 .type = FIO_OPT_STR_VAL_INT,
198 .off1 = td_var_offset(bs[DDIR_READ]),
199 .off2 = td_var_offset(bs[DDIR_WRITE]),
200 .help = "Block size unit",
205 .type = FIO_OPT_RANGE,
206 .off1 = td_var_offset(min_bs[DDIR_READ]),
207 .off2 = td_var_offset(max_bs[DDIR_READ]),
208 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
209 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
210 .help = "Set block size range (in more detail than bs)",
213 .name = "bs_unaligned",
214 .type = FIO_OPT_STR_SET,
215 .off1 = td_var_offset(bs_unaligned),
216 .help = "Don't sector align IO buffer sizes",
220 .type = FIO_OPT_STR_VAL,
221 .off1 = td_var_offset(start_offset),
222 .help = "Start IO from this offset",
226 .name = "randrepeat",
227 .type = FIO_OPT_BOOL,
228 .off1 = td_var_offset(rand_repeatable),
229 .help = "Use repeatable random IO pattern",
233 .name = "norandommap",
234 .type = FIO_OPT_STR_SET,
235 .off1 = td_var_offset(norandommap),
236 .help = "Accept potential duplicate random blocks",
241 .off1 = td_var_offset(nr_files),
242 .help = "Split job workload between this number of files",
248 .off1 = td_var_offset(open_files),
249 .help = "Number of files to keep open at the same time",
252 .name = "file_service_type",
255 .off1 = td_var_offset(file_service_type),
256 .help = "How to select which file to service next",
260 .oval = FIO_FSERVICE_RANDOM,
261 .help = "Choose a file at random",
263 { .ival = "roundrobin",
264 .oval = FIO_FSERVICE_RR,
265 .help = "Round robin select files",
272 .off1 = td_var_offset(fsync_blocks),
273 .help = "Issue fsync for writes every given number of blocks",
278 .type = FIO_OPT_BOOL,
279 .off1 = td_var_offset(odirect),
280 .help = "Use O_DIRECT IO (negates buffered)",
285 .type = FIO_OPT_BOOL,
286 .off1 = td_var_offset(odirect),
288 .help = "Use buffered IO (negates direct)",
293 .type = FIO_OPT_BOOL,
294 .off1 = td_var_offset(overwrite),
295 .help = "When writing, set whether to overwrite current data",
301 .off1 = td_var_offset(loops),
302 .help = "Number of times to run the job",
308 .off1 = td_var_offset(numjobs),
309 .help = "Duplicate this job this many times",
313 .name = "startdelay",
315 .off1 = td_var_offset(start_delay),
316 .help = "Only start job when this period has passed",
322 .type = FIO_OPT_STR_VAL_TIME,
323 .off1 = td_var_offset(timeout),
324 .help = "Stop workload when this amount of time has passed",
331 .off1 = td_var_offset(mem_type),
332 .help = "Backing type for IO buffers",
337 .help = "Use malloc(3) for IO buffers",
341 .help = "Use shared memory segments for IO buffers",
343 #ifdef FIO_HAVE_HUGETLB
346 .help = "Like shm, but use huge pages",
351 .help = "Use mmap(2) (file or anon) for IO buffers",
353 #ifdef FIO_HAVE_HUGETLB
354 { .ival = "mmaphuge",
355 .oval = MEM_MMAPHUGE,
356 .help = "Like mmap, but use huge pages",
364 .off1 = td_var_offset(verify),
365 .help = "Verify data written",
370 .help = "Don't do IO verification",
373 .oval = VERIFY_CRC32,
374 .help = "Use crc32 checksums for verification",
378 .help = "Use md5 checksums for verification",
383 .name = "write_iolog",
384 .type = FIO_OPT_STR_STORE,
385 .off1 = td_var_offset(write_iolog_file),
386 .help = "Store IO pattern to file",
389 .name = "read_iolog",
390 .type = FIO_OPT_STR_STORE,
391 .off1 = td_var_offset(read_iolog_file),
392 .help = "Playback IO pattern from file",
395 .name = "exec_prerun",
396 .type = FIO_OPT_STR_STORE,
397 .off1 = td_var_offset(exec_prerun),
398 .help = "Execute this file prior to running job",
401 .name = "exec_postrun",
402 .type = FIO_OPT_STR_STORE,
403 .off1 = td_var_offset(exec_postrun),
404 .help = "Execute this file after running job",
406 #ifdef FIO_HAVE_IOSCHED_SWITCH
408 .name = "ioscheduler",
409 .type = FIO_OPT_STR_STORE,
410 .off1 = td_var_offset(ioscheduler),
411 .help = "Use this IO scheduler on the backing device",
416 .type = FIO_OPT_STR_VAL,
417 .off1 = td_var_offset(zone_size),
418 .help = "Give size of an IO zone",
423 .type = FIO_OPT_STR_VAL,
424 .off1 = td_var_offset(zone_skip),
425 .help = "Space between IO zones",
430 .type = FIO_OPT_STR_VAL,
431 .cb = str_lockmem_cb,
432 .help = "Lock down this amount of memory",
436 .name = "rwmixcycle",
438 .off1 = td_var_offset(rwmixcycle),
439 .help = "Cycle period for mixed read/write workloads (msec)",
445 .off1 = td_var_offset(rwmixread),
447 .help = "Percentage of mixed workload that is reads",
451 .name = "rwmixwrite",
453 .off1 = td_var_offset(rwmixwrite),
455 .help = "Percentage of mixed workload that is writes",
461 .off1 = td_var_offset(nice),
462 .help = "Set job CPU nice value",
467 #ifdef FIO_HAVE_IOPRIO
472 .help = "Set job IO priority value",
479 .cb = str_prioclass_cb,
480 .help = "Set job IO priority class",
488 .off1 = td_var_offset(thinktime),
489 .help = "Idle time between IO buffers (usec)",
493 .name = "thinktime_spin",
495 .off1 = td_var_offset(thinktime_spin),
496 .help = "Start think time by spinning this amount (usec)",
500 .name = "thinktime_blocks",
502 .off1 = td_var_offset(thinktime_blocks),
503 .help = "IO buffer period between 'thinktime'",
509 .off1 = td_var_offset(rate),
510 .help = "Set bandwidth rate",
515 .off1 = td_var_offset(ratemin),
516 .help = "The bottom limit accepted",
521 .off1 = td_var_offset(ratecycle),
522 .help = "Window average for rate limits (msec)",
526 .name = "invalidate",
527 .type = FIO_OPT_BOOL,
528 .off1 = td_var_offset(invalidate_cache),
529 .help = "Invalidate buffer/page cache prior to running job",
534 .type = FIO_OPT_BOOL,
535 .off1 = td_var_offset(sync_io),
536 .help = "Use O_SYNC for buffered writes",
542 .off1 = td_var_offset(bw_avg_time),
543 .help = "Time window over which to calculate bandwidth (msec)",
547 .name = "create_serialize",
548 .type = FIO_OPT_BOOL,
549 .off1 = td_var_offset(create_serialize),
550 .help = "Serialize creating of job files",
554 .name = "create_fsync",
555 .type = FIO_OPT_BOOL,
556 .off1 = td_var_offset(create_fsync),
557 .help = "Fsync file after creation",
563 .off1 = td_var_offset(cpuload),
564 .help = "Use this percentage of CPU",
569 .off1 = td_var_offset(cpucycle),
570 .help = "Length of the CPU burn cycles (usecs)",
573 #ifdef FIO_HAVE_CPU_AFFINITY
577 .cb = str_cpumask_cb,
578 .help = "CPU affinity mask",
583 .type = FIO_OPT_BOOL,
584 .off1 = td_var_offset(end_fsync),
585 .help = "Include fsync at the end of job",
589 .name = "fsync_on_close",
590 .type = FIO_OPT_BOOL,
591 .off1 = td_var_offset(fsync_on_close),
592 .help = "fsync files on close",
597 .type = FIO_OPT_BOOL,
598 .off1 = td_var_offset(unlink),
599 .help = "Unlink created files after job has completed",
604 .type = FIO_OPT_STR_SET,
605 .cb = str_exitall_cb,
606 .help = "Terminate all jobs when one exits",
610 .type = FIO_OPT_STR_SET,
611 .off1 = td_var_offset(stonewall),
612 .help = "Insert a hard barrier between this job and previous",
616 .type = FIO_OPT_STR_SET,
617 .off1 = td_var_offset(use_thread),
618 .help = "Use threads instead of forks",
621 .name = "write_bw_log",
622 .type = FIO_OPT_STR_SET,
623 .off1 = td_var_offset(write_bw_log),
624 .help = "Write log of bandwidth during run",
627 .name = "write_lat_log",
628 .type = FIO_OPT_STR_SET,
629 .off1 = td_var_offset(write_lat_log),
630 .help = "Write log of latency during run",
633 .name = "hugepage-size",
634 .type = FIO_OPT_STR_VAL,
635 .off1 = td_var_offset(hugepage_size),
636 .help = "When using hugepages, specify size of each page",
637 .def = __stringify(FIO_HUGE_PAGE),
640 .name = "group_reporting",
641 .type = FIO_OPT_STR_SET,
642 .off1 = td_var_offset(group_reporting),
643 .help = "Do reporting on a per-group basis",
650 #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
651 #define FIO_CMD_OPTS (16)
652 #define FIO_GETOPT_JOB (0x89988998)
655 * Command line options. These will contain the above, plus a few
656 * extra that only pertain to fio itself and not jobs.
658 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
661 .has_arg = required_argument,
666 .has_arg = required_argument,
670 .name = "latency-log",
671 .has_arg = required_argument,
675 .name = "bandwidth-log",
676 .has_arg = required_argument,
681 .has_arg = optional_argument,
686 .has_arg = no_argument,
691 .has_arg = no_argument,
696 .has_arg = optional_argument,
704 static int def_timeout = 0;
706 static char fio_version_string[] = "fio 1.14a";
708 static char **ini_file;
709 static int max_jobs = MAX_JOBS;
711 struct thread_data def_thread;
712 struct thread_data *threads = NULL;
714 int exitall_on_terminate = 0;
715 int terse_output = 0;
716 unsigned long long mlock_size = 0;
720 static int write_lat_log = 0;
721 int write_bw_log = 0;
723 static int prev_group_jobs;
736 * Return a free job structure.
738 static struct thread_data *get_new_job(int global, struct thread_data *parent)
740 struct thread_data *td;
744 if (thread_number >= max_jobs)
747 td = &threads[thread_number++];
750 td->thread_number = thread_number;
754 static void put_job(struct thread_data *td)
756 if (td == &def_thread)
760 fprintf(f_out, "fio: %s\n", td->verror);
762 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
767 * Lazy way of fixing up options that depend on each other. We could also
768 * define option callback handlers, but this is easier.
770 static void fixup_options(struct thread_data *td)
772 if (!td->rwmixread && td->rwmixwrite)
773 td->rwmixread = 100 - td->rwmixwrite;
775 if (td->write_iolog_file && td->read_iolog_file) {
776 log_err("fio: read iolog overrides write_iolog\n");
777 free(td->write_iolog_file);
778 td->write_iolog_file = NULL;
781 if (td->io_ops->flags & FIO_SYNCIO)
785 td->iodepth = td->open_files;
789 * only really works for sequential io for now, and with 1 file
791 if (td->zone_size && td_random(td) && td->open_files == 1)
795 * Reads can do overwrites, we always need to pre-create the file
797 if (td_read(td) || td_rw(td))
800 if (!td->min_bs[DDIR_READ])
801 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
802 if (!td->max_bs[DDIR_READ])
803 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
804 if (!td->min_bs[DDIR_WRITE])
805 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
806 if (!td->max_bs[DDIR_WRITE])
807 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
809 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
811 if (!td->file_size_high)
812 td->file_size_high = td->file_size_low;
814 if (td_read(td) && !td_rw(td))
817 if (td->norandommap && td->verify != VERIFY_NONE) {
818 log_err("fio: norandommap given, verify disabled\n");
819 td->verify = VERIFY_NONE;
821 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
822 log_err("fio: bs_unaligned may not work with raw io\n");
825 * thinktime_spin must be less than thinktime
827 if (td->thinktime_spin > td->thinktime)
828 td->thinktime_spin = td->thinktime;
831 * The low water mark cannot be bigger than the iodepth
833 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
835 * syslet work around - if the workload is sequential,
836 * we want to let the queue drain all the way down to
837 * avoid seeking between async threads
839 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
842 td->iodepth_low = td->iodepth;
846 * If batch number isn't set, default to the same as iodepth
848 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
849 td->iodepth_batch = td->iodepth;
851 if (td->nr_files > td->files_index)
852 td->nr_files = td->files_index;
854 if (td->open_files > td->nr_files || !td->open_files)
855 td->open_files = td->nr_files;
859 * This function leaks the buffer
861 static char *to_kmg(unsigned int val)
863 char *buf = malloc(32);
864 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
875 snprintf(buf, 31, "%u%c", val, *p);
879 /* External engines are specified by "external:name.o") */
880 static const char *get_engine_name(const char *str)
882 char *p = strstr(str, ":");
888 strip_blank_front(&p);
893 static int exists_and_not_file(const char *filename)
897 if (lstat(filename, &sb) == -1)
900 if (S_ISREG(sb.st_mode))
907 * Initialize the various random states we need (random io, block size ranges,
908 * read/write mix, etc).
910 static int init_random_state(struct thread_data *td)
912 unsigned long seeds[6];
913 int fd, num_maps, blocks;
917 fd = open("/dev/urandom", O_RDONLY);
919 td_verror(td, errno, "open");
923 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
924 td_verror(td, EIO, "read");
931 os_random_seed(seeds[0], &td->bsrange_state);
932 os_random_seed(seeds[1], &td->verify_state);
933 os_random_seed(seeds[2], &td->rwmix_state);
935 if (td->file_service_type == FIO_FSERVICE_RANDOM)
936 os_random_seed(seeds[3], &td->next_file_state);
938 os_random_seed(seeds[5], &td->file_size_state);
943 if (td->rand_repeatable)
944 seeds[4] = FIO_RANDSEED * td->thread_number;
946 if (!td->norandommap) {
947 for_each_file(td, f, i) {
948 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
949 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
950 f->file_map = malloc(num_maps * sizeof(long));
952 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
955 f->num_maps = num_maps;
956 memset(f->file_map, 0, num_maps * sizeof(long));
960 os_random_seed(seeds[4], &td->random_state);
966 * Adds a job to the list of things todo. Sanitizes the various options
967 * to make sure we don't have conflicts, and initializes various
970 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
972 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
973 "randread", "randwrite", "randrw" };
977 char fname[PATH_MAX];
978 int numjobs, file_alloced;
981 * the def_thread is just for options, it's not a real job
983 if (td == &def_thread)
986 engine = get_engine_name(td->ioengine);
987 td->io_ops = load_ioengine(td, engine);
989 log_err("fio: failed to load engine %s\n", engine);
999 td->io_ops->flags |= FIO_RAWIO;
1002 if (!td->filename && !td->files_index) {
1005 if (td->nr_files == 1 && exists_and_not_file(jobname))
1006 add_file(td, jobname);
1008 for (i = 0; i < td->nr_files; i++) {
1009 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
1010 add_file(td, fname);
1017 for_each_file(td, f, i) {
1018 if (td->directory && f->filetype == FIO_TYPE_FILE) {
1019 sprintf(fname, "%s/%s", td->directory, f->file_name);
1020 f->file_name = strdup(fname);
1024 td->mutex = fio_sem_init(0);
1026 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
1027 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
1028 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
1030 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
1031 prev_group_jobs = 0;
1035 td->groupid = groupid;
1038 if (init_random_state(td))
1044 if (td->write_lat_log) {
1045 setup_log(&td->ts.slat_log);
1046 setup_log(&td->ts.clat_log);
1048 if (td->write_bw_log)
1049 setup_log(&td->ts.bw_log);
1052 td->name = strdup(jobname);
1054 if (!terse_output) {
1056 if (!strcmp(td->io_ops->name, "cpuio"))
1057 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
1059 char *c1, *c2, *c3, *c4;
1061 c1 = to_kmg(td->min_bs[DDIR_READ]);
1062 c2 = to_kmg(td->max_bs[DDIR_READ]);
1063 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
1064 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
1066 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);
1073 } else if (job_add_num == 1)
1074 fprintf(f_out, "...\n");
1078 * recurse add identical jobs, clear numjobs and stonewall options
1079 * as they don't apply to sub-jobs
1081 numjobs = td->numjobs;
1083 struct thread_data *td_new = get_new_job(0, td);
1088 td_new->numjobs = 1;
1089 td_new->stonewall = 0;
1092 td_new->filename = NULL;
1093 td_new->files_index = 0;
1094 td_new->files = NULL;
1097 job_add_num = numjobs - 1;
1099 if (add_job(td_new, jobname, job_add_num))
1103 if (td->numjobs > 1) {
1105 prev_group_jobs = 0;
1114 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
1116 #ifdef FIO_HAVE_CPU_AFFINITY
1121 for (i = 0; i < sizeof(int) * 8; i++) {
1123 CPU_SET(i, &cpumask);
1128 static int is_empty_or_comment(char *line)
1132 for (i = 0; i < strlen(line); i++) {
1137 if (!isspace(line[i]) && !iscntrl(line[i]))
1145 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
1147 static char *get_opt_postfix(const char *str)
1149 char *p = strstr(str, ":");
1155 strip_blank_front(&p);
1160 static int str_mem_cb(void *data, const char *mem)
1162 struct thread_data *td = data;
1164 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
1165 td->mmapfile = get_opt_postfix(mem);
1166 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
1167 log_err("fio: mmaphuge:/path/to/file\n");
1175 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1181 #ifdef FIO_HAVE_IOPRIO
1182 static int str_prioclass_cb(void *data, unsigned int *val)
1184 struct thread_data *td = data;
1186 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1190 static int str_prio_cb(void *data, unsigned int *val)
1192 struct thread_data *td = data;
1199 static int str_exitall_cb(void)
1201 exitall_on_terminate = 1;
1205 static int str_cpumask_cb(void *data, unsigned int *val)
1207 struct thread_data *td = data;
1209 fill_cpu_mask(td->cpumask, *val);
1213 static int str_fst_cb(void *data, const char *str)
1215 struct thread_data *td = data;
1216 char *nr = get_opt_postfix(str);
1218 td->file_service_nr = 1;
1220 td->file_service_nr = atoi(nr);
1225 static int str_filename_cb(void *data, const char *input)
1227 struct thread_data *td = data;
1228 char *fname, *str, *p;
1230 p = str = strdup(input);
1232 strip_blank_front(&str);
1233 strip_blank_end(str);
1235 if (!td->files_index)
1238 while ((fname = strsep(&str, ":")) != NULL) {
1241 add_file(td, fname);
1249 static int str_directory_cb(void *data, const char fio_unused *str)
1251 struct thread_data *td = data;
1254 if (lstat(td->directory, &sb) < 0) {
1255 log_err("fio: %s is not a directory\n", td->directory);
1256 td_verror(td, errno, "lstat");
1259 if (!S_ISDIR(sb.st_mode)) {
1260 log_err("fio: %s is not a directory\n", td->directory);
1267 static int str_opendir_cb(void *data, const char fio_unused *str)
1269 struct thread_data *td = data;
1271 if (!td->files_index)
1274 return add_dir_files(td, td->opendir);
1278 * This is our [ini] type file parser.
1280 static int parse_jobs_ini(char *file, int stonewall_flag)
1282 unsigned int global;
1283 struct thread_data *td;
1284 char *string, *name;
1288 int ret = 0, stonewall;
1290 f = fopen(file, "r");
1292 perror("fopen job file");
1296 string = malloc(4096);
1298 memset(name, 0, 256);
1300 stonewall = stonewall_flag;
1302 p = fgets(string, 4095, f);
1305 if (is_empty_or_comment(p))
1307 if (sscanf(p, "[%255s]", name) != 1)
1310 global = !strncmp(name, "global", 6);
1312 name[strlen(name) - 1] = '\0';
1314 td = get_new_job(global, &def_thread);
1321 * Seperate multiple job files by a stonewall
1323 if (!global && stonewall) {
1324 td->stonewall = stonewall;
1329 while ((p = fgets(string, 4096, f)) != NULL) {
1330 if (is_empty_or_comment(p))
1333 strip_blank_front(&p);
1343 * Don't break here, continue parsing options so we
1344 * dump all the bad ones. Makes trial/error fixups
1345 * easier on the user.
1347 ret |= parse_option(p, options, td);
1352 ret = add_job(td, name, 0);
1354 log_err("fio: job %s dropped\n", name);
1365 static int fill_def_thread(void)
1367 memset(&def_thread, 0, sizeof(def_thread));
1369 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1370 perror("sched_getaffinity");
1375 * fill default options
1377 fill_default_options(&def_thread, options);
1379 def_thread.timeout = def_timeout;
1380 def_thread.write_bw_log = write_bw_log;
1381 def_thread.write_lat_log = write_lat_log;
1383 #ifdef FIO_HAVE_DISK_UTIL
1384 def_thread.do_disk_util = 1;
1390 static void usage(void)
1392 printf("%s\n", fio_version_string);
1393 printf("\t--output\tWrite output to file\n");
1394 printf("\t--timeout\tRuntime in seconds\n");
1395 printf("\t--latency-log\tGenerate per-job latency logs\n");
1396 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1397 printf("\t--minimal\tMinimal (terse) output\n");
1398 printf("\t--version\tPrint version info and exit\n");
1399 printf("\t--help\t\tPrint this page\n");
1400 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
1403 static int parse_cmd_line(int argc, char *argv[])
1405 struct thread_data *td = NULL;
1406 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
1408 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
1411 def_timeout = atoi(optarg);
1420 f_out = fopen(optarg, "w+");
1422 perror("fopen output");
1434 ret = show_cmd_help(options, optarg);
1437 printf("%s\n", fio_version_string);
1439 case FIO_GETOPT_JOB: {
1440 const char *opt = long_options[lidx].name;
1443 if (!strncmp(opt, "name", 4) && td) {
1444 ret = add_job(td, td->name ?: "fio", 0);
1452 int global = !strncmp(val, "global", 6);
1454 td = get_new_job(global, &def_thread);
1459 ret = parse_cmd_option(opt, val, options, td);
1473 ret = add_job(td, td->name ?: "fio", 0);
1479 while (optind < argc) {
1481 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1482 ini_file[ini_idx - 1] = strdup(argv[optind]);
1489 static void free_shm(void)
1491 struct shmid_ds sbuf;
1494 shmdt((void *) threads);
1496 shmctl(shm_id, IPC_RMID, &sbuf);
1501 * The thread area is shared between the main process and the job
1502 * threads/processes. So setup a shared memory segment that will hold
1505 static int setup_thread_area(void)
1508 * 1024 is too much on some machines, scale max_jobs if
1509 * we get a failure that looks like too large a shm segment
1512 size_t size = max_jobs * sizeof(struct thread_data);
1514 shm_id = shmget(0, size, IPC_CREAT | 0600);
1517 if (errno != EINVAL) {
1528 threads = shmat(shm_id, NULL, 0);
1529 if (threads == (void *) -1) {
1539 * Copy the fio options into the long options map, so we mirror
1540 * job and cmd line options.
1542 static void dupe_job_options(void)
1544 struct fio_option *o;
1548 while (long_options[i].name)
1553 long_options[i].name = o->name;
1554 long_options[i].val = FIO_GETOPT_JOB;
1555 if (o->type == FIO_OPT_STR_SET)
1556 long_options[i].has_arg = no_argument;
1558 long_options[i].has_arg = required_argument;
1562 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1566 int parse_options(int argc, char *argv[])
1573 options_init(options);
1577 if (setup_thread_area())
1579 if (fill_def_thread())
1582 job_files = parse_cmd_line(argc, argv);
1584 for (i = 0; i < job_files; i++) {
1585 if (fill_def_thread())
1587 if (parse_jobs_ini(ini_file[i], i))
1594 if (!thread_number) {
1595 log_err("No jobs defined(s)\n");