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