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