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