dcad1b392828f7ab1be5065011b641ea226b18f9
[fio.git] / init.c
1 /*
2  * This file contains job initialization and setup functions.
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <getopt.h>
12 #include <sys/ipc.h>
13 #include <sys/shm.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16
17 #include "fio.h"
18 #include "parse.h"
19
20 static char fio_version_string[] = "fio 1.17.3";
21
22 #define FIO_RANDSEED            (0xb1899bedUL)
23
24 static char **ini_file;
25 static int max_jobs = MAX_JOBS;
26 static int dump_cmdline;
27
28 struct thread_data def_thread;
29 struct thread_data *threads = NULL;
30
31 int exitall_on_terminate = 0;
32 int terse_output = 0;
33 int eta_print;
34 unsigned long long mlock_size = 0;
35 FILE *f_out = NULL;
36 FILE *f_err = NULL;
37
38 int write_bw_log = 0;
39 int read_only = 0;
40
41 static int def_timeout = 0;
42 static int write_lat_log = 0;
43
44 static int prev_group_jobs;
45
46 unsigned long fio_debug = 0;
47
48 /*
49  * Command line options. These will contain the above, plus a few
50  * extra that only pertain to fio itself and not jobs.
51  */
52 static struct option long_options[FIO_NR_OPTIONS] = {
53         {
54                 .name           = "output",
55                 .has_arg        = required_argument,
56                 .val            = 'o',
57         },
58         {
59                 .name           = "timeout",
60                 .has_arg        = required_argument,
61                 .val            = 't',
62         },
63         {
64                 .name           = "latency-log",
65                 .has_arg        = required_argument,
66                 .val            = 'l',
67         },
68         {
69                 .name           = "bandwidth-log",
70                 .has_arg        = required_argument,
71                 .val            = 'b',
72         },
73         {
74                 .name           = "minimal",
75                 .has_arg        = optional_argument,
76                 .val            = 'm',
77         },
78         {
79                 .name           = "version",
80                 .has_arg        = no_argument,
81                 .val            = 'v',
82         },
83         {
84                 .name           = "help",
85                 .has_arg        = no_argument,
86                 .val            = 'h',
87         },
88         {
89                 .name           = "cmdhelp",
90                 .has_arg        = optional_argument,
91                 .val            = 'c',
92         },
93         {
94                 .name           = "showcmd",
95                 .has_arg        = no_argument,
96                 .val            = 's',
97         },
98         {
99                 .name           = "readonly",
100                 .has_arg        = no_argument,
101                 .val            = 'r',
102         },
103         {
104                 .name           = "eta",
105                 .has_arg        = required_argument,
106                 .val            = 'e',
107         },
108         {
109                 .name           = "debug",
110                 .has_arg        = required_argument,
111                 .val            = 'd',
112         },
113         {
114                 .name           = NULL,
115         },
116 };
117
118 FILE *get_f_out()
119 {
120         return f_out;
121 }
122
123 FILE *get_f_err()
124 {
125         return f_err;
126 }
127
128 /*
129  * Return a free job structure.
130  */
131 static struct thread_data *get_new_job(int global, struct thread_data *parent)
132 {
133         struct thread_data *td;
134
135         if (global)
136                 return &def_thread;
137         if (thread_number >= max_jobs)
138                 return NULL;
139
140         td = &threads[thread_number++];
141         *td = *parent;
142
143         dup_files(td, parent);
144         options_mem_dupe(td);
145
146         td->thread_number = thread_number;
147         return td;
148 }
149
150 static void put_job(struct thread_data *td)
151 {
152         if (td == &def_thread)
153                 return;
154
155         if (td->error)
156                 log_info("fio: %s\n", td->verror);
157
158         memset(&threads[td->thread_number - 1], 0, sizeof(*td));
159         thread_number--;
160 }
161
162 static int setup_rate(struct thread_data *td)
163 {
164         unsigned long nr_reads_per_msec;
165         unsigned long long rate;
166         unsigned int bs;
167
168         if (!td->o.rate && !td->o.rate_iops)
169                 return 0;
170
171         if (td_rw(td))
172                 bs = td->o.rw_min_bs;
173         else if (td_read(td))
174                 bs = td->o.min_bs[DDIR_READ];
175         else
176                 bs = td->o.min_bs[DDIR_WRITE];
177
178         if (td->o.rate) {
179                 rate = td->o.rate;
180                 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
181         } else
182                 nr_reads_per_msec = td->o.rate_iops * 1000UL;
183
184         if (!nr_reads_per_msec) {
185                 log_err("rate lower than supported\n");
186                 return -1;
187         }
188
189         td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
190         td->rate_pending_usleep = 0;
191         return 0;
192 }
193
194 /*
195  * Lazy way of fixing up options that depend on each other. We could also
196  * define option callback handlers, but this is easier.
197  */
198 static int fixup_options(struct thread_data *td)
199 {
200         struct thread_options *o = &td->o;
201
202         if (read_only && td_write(td)) {
203                 log_err("fio: job <%s> has write bit set, but fio is in read-only mode\n", td->o.name);
204                 return 1;
205         }
206         
207         if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
208                 o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
209
210         if (o->write_iolog_file && o->read_iolog_file) {
211                 log_err("fio: read iolog overrides write_iolog\n");
212                 free(o->write_iolog_file);
213                 o->write_iolog_file = NULL;
214         }
215
216         if (td->io_ops->flags & FIO_SYNCIO)
217                 o->iodepth = 1;
218         else {
219                 if (!o->iodepth)
220                         o->iodepth = o->open_files;
221         }
222
223         /*
224          * only really works for sequential io for now, and with 1 file
225          */
226         if (o->zone_size && td_random(td) && o->open_files == 1)
227                 o->zone_size = 0;
228
229         /*
230          * Reads can do overwrites, we always need to pre-create the file
231          */
232         if (td_read(td) || td_rw(td))
233                 o->overwrite = 1;
234
235         if (!o->min_bs[DDIR_READ])
236                 o->min_bs[DDIR_READ]= o->bs[DDIR_READ];
237         if (!o->max_bs[DDIR_READ])
238                 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
239         if (!o->min_bs[DDIR_WRITE])
240                 o->min_bs[DDIR_WRITE]= o->bs[DDIR_WRITE];
241         if (!o->max_bs[DDIR_WRITE])
242                 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
243
244         o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
245
246         if (!o->file_size_high)
247                 o->file_size_high = o->file_size_low;
248
249         if (o->norandommap && o->verify != VERIFY_NONE) {
250                 log_err("fio: norandommap given, verify disabled\n");
251                 o->verify = VERIFY_NONE;
252         }
253         if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
254                 log_err("fio: bs_unaligned may not work with raw io\n");
255
256         /*
257          * thinktime_spin must be less than thinktime
258          */
259         if (o->thinktime_spin > o->thinktime)
260                 o->thinktime_spin = o->thinktime;
261
262         /*
263          * The low water mark cannot be bigger than the iodepth
264          */
265         if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
266                 /*
267                  * syslet work around - if the workload is sequential,
268                  * we want to let the queue drain all the way down to
269                  * avoid seeking between async threads
270                  */
271                 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
272                         o->iodepth_low = 1;
273                 else
274                         o->iodepth_low = o->iodepth;
275         }
276
277         /*
278          * If batch number isn't set, default to the same as iodepth
279          */
280         if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
281                 o->iodepth_batch = o->iodepth;
282
283         if (o->nr_files > td->files_index)
284                 o->nr_files = td->files_index;
285
286         if (o->open_files > o->nr_files || !o->open_files)
287                 o->open_files = o->nr_files;
288
289         if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
290                 log_err("fio: rate and rate_iops are mutually exclusive\n");
291                 return 1;
292         }
293         if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
294                 log_err("fio: minimum rate exceeds rate\n");
295                 return 1;
296         }
297
298         if (!o->timeout && o->time_based) {
299                 log_err("fio: time_based requires a runtime/timeout setting\n");
300                 o->time_based = 0;
301         }
302
303         if (o->fill_device && !o->size)
304                 o->size = ULONG_LONG_MAX;
305
306         return 0;
307 }
308
309 /*
310  * This function leaks the buffer
311  */
312 static char *to_kmg(unsigned int val)
313 {
314         char *buf = malloc(32);
315         char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
316         char *p = post;
317
318         do {
319                 if (val & 1023)
320                         break;
321
322                 val >>= 10;
323                 p++;
324         } while (*p);
325
326         snprintf(buf, 31, "%u%c", val, *p);
327         return buf;
328 }
329
330 /* External engines are specified by "external:name.o") */
331 static const char *get_engine_name(const char *str)
332 {
333         char *p = strstr(str, ":");
334
335         if (!p)
336                 return str;
337
338         p++;
339         strip_blank_front(&p);
340         strip_blank_end(p);
341         return p;
342 }
343
344 static int exists_and_not_file(const char *filename)
345 {
346         struct stat sb;
347
348         if (lstat(filename, &sb) == -1)
349                 return 0;
350
351         if (S_ISREG(sb.st_mode))
352                 return 0;
353
354         return 1;
355 }
356
357 /*
358  * Initialize the various random states we need (random io, block size ranges,
359  * read/write mix, etc).
360  */
361 static int init_random_state(struct thread_data *td)
362 {
363         unsigned long seeds[6];
364         int fd;
365
366         fd = open("/dev/urandom", O_RDONLY);
367         if (fd == -1) {
368                 td_verror(td, errno, "open");
369                 return 1;
370         }
371
372         if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
373                 td_verror(td, EIO, "read");
374                 close(fd);
375                 return 1;
376         }
377
378         close(fd);
379
380         os_random_seed(seeds[0], &td->bsrange_state);
381         os_random_seed(seeds[1], &td->verify_state);
382         os_random_seed(seeds[2], &td->rwmix_state);
383
384         if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
385                 os_random_seed(seeds[3], &td->next_file_state);
386
387         os_random_seed(seeds[5], &td->file_size_state);
388
389         if (!td_random(td))
390                 return 0;
391
392         if (td->o.rand_repeatable)
393                 seeds[4] = FIO_RANDSEED * td->thread_number;
394
395         os_random_seed(seeds[4], &td->random_state);
396         return 0;
397 }
398
399 /*
400  * Adds a job to the list of things todo. Sanitizes the various options
401  * to make sure we don't have conflicts, and initializes various
402  * members of td.
403  */
404 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
405 {
406         const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
407                                    "randread", "randwrite", "randrw" };
408         unsigned int i;
409         const char *engine;
410         char fname[PATH_MAX];
411         int numjobs, file_alloced;
412
413         /*
414          * the def_thread is just for options, it's not a real job
415          */
416         if (td == &def_thread)
417                 return 0;
418
419         /*
420          * if we are just dumping the output command line, don't add the job
421          */
422         if (dump_cmdline) {
423                 put_job(td);
424                 return 0;
425         }
426
427         engine = get_engine_name(td->o.ioengine);
428         td->io_ops = load_ioengine(td, engine);
429         if (!td->io_ops) {
430                 log_err("fio: failed to load engine %s\n", engine);
431                 goto err;
432         }
433
434         if (td->o.use_thread)
435                 nr_thread++;
436         else
437                 nr_process++;
438
439         if (td->o.odirect)
440                 td->io_ops->flags |= FIO_RAWIO;
441
442         file_alloced = 0;
443         if (!td->o.filename && !td->files_index) {
444                 file_alloced = 1;
445
446                 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
447                         add_file(td, jobname);
448                 else {
449                         for (i = 0; i < td->o.nr_files; i++) {
450                                 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
451                                 add_file(td, fname);
452                         }
453                 }
454         }
455
456         if (fixup_options(td))
457                 goto err;
458
459         if (td->io_ops->flags & FIO_DISKLESSIO) {
460                 struct fio_file *f;
461
462                 for_each_file(td, f, i)
463                         f->real_file_size = -1ULL;
464         }
465
466         td->mutex = fio_sem_init(0);
467
468         td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
469         td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
470         td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
471         td->ddir_nr = td->o.ddir_nr;
472
473         if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
474              && prev_group_jobs) {
475                 prev_group_jobs = 0;
476                 groupid++;
477         }
478
479         td->groupid = groupid;
480         prev_group_jobs++;
481
482         if (init_random_state(td))
483                 goto err;
484
485         if (setup_rate(td))
486                 goto err;
487
488         if (td->o.write_lat_log) {
489                 setup_log(&td->ts.slat_log);
490                 setup_log(&td->ts.clat_log);
491         }
492         if (td->o.write_bw_log)
493                 setup_log(&td->ts.bw_log);
494
495         if (!td->o.name)
496                 td->o.name = strdup(jobname);
497
498         if (!terse_output) {
499                 if (!job_add_num) {
500                         if (!strcmp(td->io_ops->name, "cpuio"))
501                                 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->o.name, td->o.cpuload, td->o.cpucycle);
502                         else {
503                                 char *c1, *c2, *c3, *c4;
504
505                                 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
506                                 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
507                                 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
508                                 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
509
510                                 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->o.name, td->groupid, ddir_str[td->o.td_ddir], c1, c2, c3, c4, td->io_ops->name, td->o.iodepth);
511
512                                 free(c1);
513                                 free(c2);
514                                 free(c3);
515                                 free(c4);
516                         }
517                 } else if (job_add_num == 1)
518                         log_info("...\n");
519         }
520
521         /*
522          * recurse add identical jobs, clear numjobs and stonewall options
523          * as they don't apply to sub-jobs
524          */
525         numjobs = td->o.numjobs;
526         while (--numjobs) {
527                 struct thread_data *td_new = get_new_job(0, td);
528
529                 if (!td_new)
530                         goto err;
531
532                 td_new->o.numjobs = 1;
533                 td_new->o.stonewall = 0;
534                 td_new->o.new_group = 0;
535
536                 if (file_alloced) {
537                         td_new->o.filename = NULL;
538                         td_new->files_index = 0;
539                         td_new->files = NULL;
540                 }
541
542                 job_add_num = numjobs - 1;
543
544                 if (add_job(td_new, jobname, job_add_num))
545                         goto err;
546         }
547
548         return 0;
549 err:
550         put_job(td);
551         return -1;
552 }
553
554 static int is_empty_or_comment(char *line)
555 {
556         unsigned int i;
557
558         for (i = 0; i < strlen(line); i++) {
559                 if (line[i] == ';')
560                         return 1;
561                 if (line[i] == '#')
562                         return 1;
563                 if (!isspace(line[i]) && !iscntrl(line[i]))
564                         return 0;
565         }
566
567         return 1;
568 }
569
570 /*
571  * This is our [ini] type file parser.
572  */
573 static int parse_jobs_ini(char *file, int stonewall_flag)
574 {
575         unsigned int global;
576         struct thread_data *td;
577         char *string, *name;
578         FILE *f;
579         char *p;
580         int ret = 0, stonewall;
581         int first_sect = 1;
582         int skip_fgets = 0;
583
584         if (!strcmp(file, "-"))
585                 f = stdin;
586         else
587                 f = fopen(file, "r");
588
589         if (!f) {
590                 perror("fopen job file");
591                 return 1;
592         }
593
594         string = malloc(4096);
595
596         /*
597          * it's really 256 + small bit, 280 should suffice
598          */
599         name = malloc(280);
600         memset(name, 0, 280);
601
602         stonewall = stonewall_flag;
603         do {
604                 /*
605                  * if skip_fgets is set, we already have loaded a line we
606                  * haven't handled.
607                  */
608                 if (!skip_fgets) {
609                         p = fgets(string, 4095, f);
610                         if (!p)
611                                 break;
612                 }
613
614                 skip_fgets = 0;
615                 strip_blank_front(&p);
616                 strip_blank_end(p);
617
618                 if (is_empty_or_comment(p))
619                         continue;
620                 if (sscanf(p, "[%255s]", name) != 1) {
621                         log_err("fio: option <%s> outside of [] job section\n", p);
622                         break;
623                 }
624
625                 global = !strncmp(name, "global", 6);
626
627                 name[strlen(name) - 1] = '\0';
628
629                 if (dump_cmdline) {
630                         if (first_sect)
631                                 log_info("fio ");
632                         if (!global)
633                                 log_info("--name=%s ", name);
634                         first_sect = 0;
635                 }
636
637                 td = get_new_job(global, &def_thread);
638                 if (!td) {
639                         ret = 1;
640                         break;
641                 }
642
643                 /*
644                  * Seperate multiple job files by a stonewall
645                  */
646                 if (!global && stonewall) {
647                         td->o.stonewall = stonewall;
648                         stonewall = 0;
649                 }
650
651                 while ((p = fgets(string, 4096, f)) != NULL) {
652                         if (is_empty_or_comment(p))
653                                 continue;
654
655                         strip_blank_front(&p);
656
657                         /*
658                          * new section, break out and make sure we don't
659                          * fgets() a new line at the top.
660                          */
661                         if (p[0] == '[') {
662                                 skip_fgets = 1;
663                                 break;
664                         }
665
666                         strip_blank_end(p);
667
668                         /*
669                          * Don't break here, continue parsing options so we
670                          * dump all the bad ones. Makes trial/error fixups
671                          * easier on the user.
672                          */
673                         ret |= fio_option_parse(td, p);
674                         if (!ret && dump_cmdline)
675                                 log_info("--%s ", p);
676                 }
677
678                 if (!ret)
679                         ret = add_job(td, name, 0);
680                 else {
681                         log_err("fio: job %s dropped\n", name);
682                         put_job(td);
683                 }
684         } while (!ret);
685
686         if (dump_cmdline)
687                 log_info("\n");
688
689         free(string);
690         free(name);
691         if (f != stdin)
692                 fclose(f);
693         return ret;
694 }
695
696 static int fill_def_thread(void)
697 {
698         memset(&def_thread, 0, sizeof(def_thread));
699
700         fio_getaffinity(getpid(), &def_thread.o.cpumask);
701
702         /*
703          * fill default options
704          */
705         fio_fill_default_options(&def_thread);
706
707         def_thread.o.timeout = def_timeout;
708         def_thread.o.write_bw_log = write_bw_log;
709         def_thread.o.write_lat_log = write_lat_log;
710
711         return 0;
712 }
713
714 static void free_shm(void)
715 {
716         struct shmid_ds sbuf;
717
718         if (threads) {
719                 shmdt((void *) threads);
720                 threads = NULL;
721                 shmctl(shm_id, IPC_RMID, &sbuf);
722         }
723 }
724
725 /*
726  * The thread area is shared between the main process and the job
727  * threads/processes. So setup a shared memory segment that will hold
728  * all the job info.
729  */
730 static int setup_thread_area(void)
731 {
732         /*
733          * 1024 is too much on some machines, scale max_jobs if
734          * we get a failure that looks like too large a shm segment
735          */
736         do {
737                 size_t size = max_jobs * sizeof(struct thread_data);
738
739                 shm_id = shmget(0, size, IPC_CREAT | 0600);
740                 if (shm_id != -1)
741                         break;
742                 if (errno != EINVAL) {
743                         perror("shmget");
744                         break;
745                 }
746
747                 max_jobs >>= 1;
748         } while (max_jobs);
749
750         if (shm_id == -1)
751                 return 1;
752
753         threads = shmat(shm_id, NULL, 0);
754         if (threads == (void *) -1) {
755                 perror("shmat");
756                 return 1;
757         }
758
759         memset(threads, 0, max_jobs * sizeof(struct thread_data));
760         atexit(free_shm);
761         return 0;
762 }
763
764 static void usage(const char *name)
765 {
766         printf("%s\n", fio_version_string);
767         printf("%s [options] [job options] <job file(s)>\n", name);
768         printf("\t--debug=options\tEnable debug logging\n");
769         printf("\t--output\tWrite output to file\n");
770         printf("\t--timeout\tRuntime in seconds\n");
771         printf("\t--latency-log\tGenerate per-job latency logs\n");
772         printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
773         printf("\t--minimal\tMinimal (terse) output\n");
774         printf("\t--version\tPrint version info and exit\n");
775         printf("\t--help\t\tPrint this page\n");
776         printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
777         printf("\t--showcmd\tTurn a job file into command line options\n");
778         printf("\t--eta=when\tWhen ETA estimate should be printed\n");
779         printf("\t          \tMay be \"always\", \"never\" or \"auto\"\n");
780 }
781
782 struct debug_level {
783         const char *name;
784         unsigned long mask;
785 };
786
787 struct debug_level debug_levels[] = {
788         { .name = "process", .mask = FD_PROCESS, },
789         { .name = "file", .mask = FD_PROCESS, },
790         { .name = "io", .mask = FD_IO, },
791         { .name = "mem", .mask = FD_MEM, },
792         { },
793 };
794
795 static void set_debug(const char *string)
796 {
797         struct debug_level *dl;
798         char *p = (char *) string;
799         char *opt;
800         int i;
801
802         if (!strcmp(string, "?") || !strcmp(string, "help")) {
803                 int i;
804
805                 log_info("fio: dumping debug options:");
806                 for (i = 0; debug_levels[i].name; i++) {
807                         dl = &debug_levels[i];
808                         log_info("%s,", dl->name);
809                 }
810                 log_info("\n");
811                 return;
812         }
813
814         while ((opt = strsep(&p, ",")) != NULL) {
815                 int found = 0;
816
817                 for (i = 0; debug_levels[i].name; i++) {
818                         dl = &debug_levels[i];
819                         if (!strncmp(opt, dl->name, strlen(opt))) {
820                                 log_info("fio: set debug option %s\n", opt);
821                                 found = 1;
822                                 fio_debug |= dl->mask;
823                                 break;
824                         }
825                 }
826
827                 if (!found)
828                         log_err("fio: debug mask %s not found\n", opt);
829         }
830 }
831
832 static int parse_cmd_line(int argc, char *argv[])
833 {
834         struct thread_data *td = NULL;
835         int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
836
837         while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
838                 switch (c) {
839                 case 't':
840                         def_timeout = atoi(optarg);
841                         break;
842                 case 'l':
843                         write_lat_log = 1;
844                         break;
845                 case 'w':
846                         write_bw_log = 1;
847                         break;
848                 case 'o':
849                         f_out = fopen(optarg, "w+");
850                         if (!f_out) {
851                                 perror("fopen output");
852                                 exit(1);
853                         }
854                         f_err = f_out;
855                         break;
856                 case 'm':
857                         terse_output = 1;
858                         break;
859                 case 'h':
860                         usage(argv[0]);
861                         exit(0);
862                 case 'c':
863                         exit(fio_show_option_help(optarg));
864                 case 's':
865                         dump_cmdline = 1;
866                         break;
867                 case 'r':
868                         read_only = 1;
869                         break;
870                 case 'v':
871                         printf("%s\n", fio_version_string);
872                         exit(0);
873                 case 'e':
874                         if (!strcmp("always", optarg))
875                                 eta_print = FIO_ETA_ALWAYS;
876                         else if (!strcmp("never", optarg))
877                                 eta_print = FIO_ETA_NEVER;
878                         break;
879                 case 'd':
880                         set_debug(optarg);
881                         break;
882                 case FIO_GETOPT_JOB: {
883                         const char *opt = long_options[lidx].name;
884                         char *val = optarg;
885
886                         if (!strncmp(opt, "name", 4) && td) {
887                                 ret = add_job(td, td->o.name ?: "fio", 0);
888                                 if (ret) {
889                                         put_job(td);
890                                         return 0;
891                                 }
892                                 td = NULL;
893                         }
894                         if (!td) {
895                                 int global = 0;
896
897                                 if (strncmp(opt, "name", 4) ||
898                                     !strncmp(val, "global", 6))
899                                         global = 1;
900
901                                 td = get_new_job(global, &def_thread);
902                                 if (!td)
903                                         return 0;
904                         }
905
906                         ret = fio_cmd_option_parse(td, opt, val);
907                         if (ret)
908                                 dont_add_job = 1;
909                         break;
910                 }
911                 default:
912                         break;
913                 }
914         }
915
916         if (td) {
917                 if (dont_add_job)
918                         put_job(td);
919                 else {
920                         ret = add_job(td, td->o.name ?: "fio", 0);
921                         if (ret)
922                                 put_job(td);
923                 }
924         }
925
926         while (optind < argc) {
927                 ini_idx++;
928                 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
929                 ini_file[ini_idx - 1] = strdup(argv[optind]);
930                 optind++;
931         }
932
933         return ini_idx;
934 }
935
936
937 int parse_options(int argc, char *argv[])
938 {
939         int job_files, i;
940
941         f_out = stdout;
942         f_err = stderr;
943
944         fio_options_dup_and_init(long_options);
945
946         if (setup_thread_area())
947                 return 1;
948         if (fill_def_thread())
949                 return 1;
950
951         job_files = parse_cmd_line(argc, argv);
952
953         for (i = 0; i < job_files; i++) {
954                 if (fill_def_thread())
955                         return 1;
956                 if (parse_jobs_ini(ini_file[i], i))
957                         return 1;
958                 free(ini_file[i]);
959         }
960
961         free(ini_file);
962         options_mem_free(&def_thread);
963
964         if (!thread_number) {
965                 if (dump_cmdline)
966                         return 0;
967
968                 log_err("No jobs defined(s)\n");
969                 usage(argv[0]);
970                 return 1;
971         }
972
973         return 0;
974 }