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