init.c revision 9ebc27e1352b905fe3396ce2350a7765fe9c57b8
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <ctype.h>
6#include <string.h>
7#include <errno.h>
8#include <sys/ipc.h>
9#include <sys/shm.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12
13#include "fio.h"
14
15#define DEF_BS			(4096)
16#define DEF_TIMEOUT		(0)
17#define DEF_RATE_CYCLE		(1000)
18#define DEF_ODIRECT		(1)
19#define DEF_IO_ENGINE		(FIO_SYNCIO)
20#define DEF_IO_ENGINE_NAME	"sync"
21#define DEF_SEQUENTIAL		(1)
22#define DEF_RAND_REPEAT		(1)
23#define DEF_OVERWRITE		(1)
24#define DEF_CREATE		(1)
25#define DEF_INVALIDATE		(1)
26#define DEF_SYNCIO		(0)
27#define DEF_RANDSEED		(0xb1899bedUL)
28#define DEF_BWAVGTIME		(500)
29#define DEF_CREATE_SER		(1)
30#define DEF_CREATE_FSYNC	(1)
31#define DEF_LOOPS		(1)
32#define DEF_VERIFY		(0)
33#define DEF_STONEWALL		(0)
34#define DEF_NUMJOBS		(1)
35#define DEF_USE_THREAD		(0)
36#define DEF_FILE_SIZE		(1024 * 1024 * 1024UL)
37#define DEF_ZONE_SIZE		(0)
38#define DEF_ZONE_SKIP		(0)
39#define DEF_RWMIX_CYCLE		(500)
40#define DEF_RWMIX_READ		(50)
41#define DEF_NICE		(0)
42
43static int def_timeout = DEF_TIMEOUT;
44
45static char fio_version_string[] = "fio 1.4";
46
47static char **ini_file;
48static int max_jobs = MAX_JOBS;
49
50struct thread_data def_thread;
51struct thread_data *threads = NULL;
52
53int rate_quit = 0;
54int write_lat_log = 0;
55int write_bw_log = 0;
56int exitall_on_terminate = 0;
57unsigned long long mlock_size = 0;
58FILE *f_out = NULL;
59FILE *f_err = NULL;
60
61static struct thread_data *get_new_job(int global, struct thread_data *parent)
62{
63	struct thread_data *td;
64
65	if (global)
66		return &def_thread;
67	if (thread_number >= max_jobs)
68		return NULL;
69
70	td = &threads[thread_number++];
71	*td = *parent;
72	td->name[0] = '\0';
73
74	td->fd = -1;
75	td->thread_number = thread_number;
76	return td;
77}
78
79static void put_job(struct thread_data *td)
80{
81	memset(&threads[td->thread_number - 1], 0, sizeof(*td));
82	thread_number--;
83}
84
85static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
86{
87	char *ddir_str[] = { "read", "write", "randread", "randwrite",
88			     "rw", NULL, "randrw" };
89	struct stat sb;
90	int numjobs, ddir;
91
92#ifndef FIO_HAVE_LIBAIO
93	if (td->io_engine == FIO_LIBAIO) {
94		log_err("Linux libaio not available\n");
95		return 1;
96	}
97#endif
98#ifndef FIO_HAVE_POSIXAIO
99	if (td->io_engine == FIO_POSIXAIO) {
100		log_err("posix aio not available\n");
101		return 1;
102	}
103#endif
104
105	/*
106	 * the def_thread is just for options, it's not a real job
107	 */
108	if (td == &def_thread)
109		return 0;
110
111	if (td->io_engine & FIO_SYNCIO)
112		td->iodepth = 1;
113	else {
114		if (!td->iodepth)
115			td->iodepth = 1;
116	}
117
118	/*
119	 * only really works for sequential io for now
120	 */
121	if (td->zone_size && !td->sequential)
122		td->zone_size = 0;
123
124	td->filetype = FIO_TYPE_FILE;
125	if (!stat(jobname, &sb)) {
126		if (S_ISBLK(sb.st_mode))
127			td->filetype = FIO_TYPE_BD;
128		else if (S_ISCHR(sb.st_mode))
129			td->filetype = FIO_TYPE_CHAR;
130	}
131
132	if (td->filetype == FIO_TYPE_FILE) {
133		char tmp[PATH_MAX];
134
135		if (td->directory && td->directory[0] != '\0')
136			sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->thread_number);
137		else
138			sprintf(tmp, "%s.%d", jobname, td->thread_number);
139		td->file_name = strdup(tmp);
140	} else
141		td->file_name = strdup(jobname);
142
143	fio_sem_init(&td->mutex, 0);
144
145	td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
146	td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
147	td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
148
149	if (td->min_bs == -1U)
150		td->min_bs = td->bs;
151	if (td->max_bs == -1U)
152		td->max_bs = td->bs;
153	if (td_read(td) && !td_rw(td))
154		td->verify = 0;
155
156	if (td->stonewall && td->thread_number > 1)
157		groupid++;
158
159	td->groupid = groupid;
160
161	if (setup_rate(td))
162		goto err;
163
164	if (write_lat_log) {
165		setup_log(&td->slat_log);
166		setup_log(&td->clat_log);
167	}
168	if (write_bw_log)
169		setup_log(&td->bw_log);
170
171	if (td->name[0] == '\0')
172		snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
173
174	ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
175
176	if (!job_add_num)
177		fprintf(f_out, "%s: (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->name, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_engine_name, td->iodepth);
178	else if (job_add_num == 1)
179		fprintf(f_out, "...\n");
180
181	/*
182	 * recurse add identical jobs, clear numjobs and stonewall options
183	 * as they don't apply to sub-jobs
184	 */
185	numjobs = td->numjobs;
186	while (--numjobs) {
187		struct thread_data *td_new = get_new_job(0, td);
188
189		if (!td_new)
190			goto err;
191
192		td_new->numjobs = 1;
193		td_new->stonewall = 0;
194		job_add_num = numjobs - 1;
195
196		if (add_job(td_new, jobname, job_add_num))
197			goto err;
198	}
199	return 0;
200err:
201	put_job(td);
202	return -1;
203}
204
205int init_random_state(struct thread_data *td)
206{
207	unsigned long seeds[4];
208	int fd, num_maps, blocks;
209
210	fd = open("/dev/urandom", O_RDONLY);
211	if (fd == -1) {
212		td_verror(td, errno);
213		return 1;
214	}
215
216	if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
217		td_verror(td, EIO);
218		close(fd);
219		return 1;
220	}
221
222	close(fd);
223
224	os_random_seed(seeds[0], &td->bsrange_state);
225	os_random_seed(seeds[1], &td->verify_state);
226	os_random_seed(seeds[2], &td->rwmix_state);
227
228	if (td->sequential)
229		return 0;
230
231	if (td->rand_repeatable)
232		seeds[3] = DEF_RANDSEED;
233
234	blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
235	num_maps = blocks / BLOCKS_PER_MAP;
236	td->file_map = malloc(num_maps * sizeof(long));
237	td->num_maps = num_maps;
238	memset(td->file_map, 0, num_maps * sizeof(long));
239
240	os_random_seed(seeds[3], &td->random_state);
241	return 0;
242}
243
244static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
245{
246#ifdef FIO_HAVE_CPU_AFFINITY
247	unsigned int i;
248
249	CPU_ZERO(&cpumask);
250
251	for (i = 0; i < sizeof(int) * 8; i++) {
252		if ((1 << i) & cpu)
253			CPU_SET(i, &cpumask);
254	}
255#endif
256}
257
258static unsigned long get_mult(char c)
259{
260	switch (c) {
261		case 'k':
262		case 'K':
263			return 1024;
264		case 'm':
265		case 'M':
266			return 1024 * 1024;
267		case 'g':
268		case 'G':
269			return 1024 * 1024 * 1024;
270		default:
271			return 1;
272	}
273}
274
275/*
276 * convert string after '=' into decimal value, noting any size suffix
277 */
278static int str_cnv(char *p, unsigned long long *val)
279{
280	char *str;
281	int len;
282
283	str = strchr(p, '=');
284	if (!str)
285		return 1;
286
287	str++;
288	len = strlen(str);
289
290	*val = strtoul(str, NULL, 10);
291	if (*val == ULONG_MAX && errno == ERANGE)
292		return 1;
293
294	*val *= get_mult(str[len - 1]);
295	return 0;
296}
297
298static int check_strcnv(char *p, char *name, unsigned long long *val)
299{
300	if (strncmp(p, name, strlen(name) - 1))
301		return 1;
302
303	return str_cnv(p, val);
304}
305
306static void strip_blank_front(char **p)
307{
308	char *s = *p;
309
310	while (isspace(*s))
311		s++;
312}
313
314static void strip_blank_end(char *p)
315{
316	char *s = p + strlen(p) - 1;
317
318	while (isspace(*s) || iscntrl(*s))
319		s--;
320
321	*(s + 1) = '\0';
322}
323
324typedef int (str_cb_fn)(struct thread_data *, char *);
325
326static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
327{
328	char *s;
329
330	if (strncmp(p, name, strlen(name)))
331		return 1;
332
333	s = strstr(p, name);
334	if (!s)
335		return 1;
336
337	s = strchr(s, '=');
338	if (!s)
339		return 1;
340
341	s++;
342	strip_blank_front(&s);
343	return cb(td, s);
344}
345
346static int check_strstore(char *p, char *name, char *dest)
347{
348	char *s;
349
350	if (strncmp(p, name, strlen(name)))
351		return 1;
352
353	s = strstr(p, name);
354	if (!s)
355		return 1;
356
357	s = strchr(p, '=');
358	if (!s)
359		return 1;
360
361	s++;
362	strip_blank_front(&s);
363
364	strcpy(dest, s);
365	return 0;
366}
367
368static int __check_range(char *str, unsigned long *val)
369{
370	char suffix;
371
372	if (sscanf(str, "%lu%c", val, &suffix) == 2) {
373		*val *= get_mult(suffix);
374		return 0;
375	}
376
377	if (sscanf(str, "%lu", val) == 1)
378		return 0;
379
380	return 1;
381}
382
383static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
384{
385	char option[128];
386	char *str, *p1, *p2;
387
388	if (strncmp(p, name, strlen(name)))
389		return 1;
390
391	strcpy(option, p);
392	p = option;
393
394	str = strstr(p, name);
395	if (!str)
396		return 1;
397
398	p += strlen(name);
399
400	str = strchr(p, '=');
401	if (!str)
402		return 1;
403
404	/*
405	 * 'p' now holds whatever is after the '=' sign
406	 */
407	p1 = str + 1;
408
409	/*
410	 * terminate p1 at the '-' sign
411	 */
412	p = strchr(p1, '-');
413	if (!p)
414		return 1;
415
416	p2 = p + 1;
417	*p = '\0';
418
419	if (!__check_range(p1, s) && !__check_range(p2, e))
420		return 0;
421
422	return 1;
423}
424
425static int check_int(char *p, char *name, unsigned int *val)
426{
427	char *str;
428
429	if (strncmp(p, name, strlen(name)))
430		return 1;
431
432	str = strstr(p, name);
433	if (!str)
434		return 1;
435
436	str = strchr(p, '=');
437	if (!str)
438		return 1;
439
440	str++;
441
442	if (sscanf(str, "%u", val) == 1)
443		return 0;
444
445	return 1;
446}
447
448static int check_strset(char *p, char *name)
449{
450	return strncmp(p, name, strlen(name));
451}
452
453static int is_empty_or_comment(char *line)
454{
455	unsigned int i;
456
457	for (i = 0; i < strlen(line); i++) {
458		if (line[i] == ';')
459			return 1;
460		if (!isspace(line[i]) && !iscntrl(line[i]))
461			return 0;
462	}
463
464	return 1;
465}
466
467static int str_rw_cb(struct thread_data *td, char *mem)
468{
469	if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
470		td->ddir = DDIR_READ;
471		td->sequential = 1;
472		return 0;
473	} else if (!strncmp(mem, "randread", 8)) {
474		td->ddir = DDIR_READ;
475		td->sequential = 0;
476		return 0;
477	} else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
478		td->ddir = DDIR_WRITE;
479		td->sequential = 1;
480		return 0;
481	} else if (!strncmp(mem, "randwrite", 9)) {
482		td->ddir = DDIR_WRITE;
483		td->sequential = 0;
484		return 0;
485	} else if (!strncmp(mem, "rw", 2)) {
486		td->ddir = 0;
487		td->iomix = 1;
488		td->sequential = 1;
489		return 0;
490	} else if (!strncmp(mem, "randrw", 6)) {
491		td->ddir = 0;
492		td->iomix = 1;
493		td->sequential = 0;
494		return 0;
495	}
496
497	log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
498	return 1;
499}
500
501static int str_verify_cb(struct thread_data *td, char *mem)
502{
503	if (!strncmp(mem, "0", 1)) {
504		td->verify = VERIFY_NONE;
505		return 0;
506	} else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
507		td->verify = VERIFY_MD5;
508		return 0;
509	} else if (!strncmp(mem, "crc32", 5)) {
510		td->verify = VERIFY_CRC32;
511		return 0;
512	}
513
514	log_err("fio: verify types: md5, crc32\n");
515	return 1;
516}
517
518static int str_mem_cb(struct thread_data *td, char *mem)
519{
520	if (!strncmp(mem, "malloc", 6)) {
521		td->mem_type = MEM_MALLOC;
522		return 0;
523	} else if (!strncmp(mem, "shm", 3)) {
524		td->mem_type = MEM_SHM;
525		return 0;
526	} else if (!strncmp(mem, "mmap", 4)) {
527		td->mem_type = MEM_MMAP;
528		return 0;
529	}
530
531	log_err("fio: mem type: malloc, shm, mmap\n");
532	return 1;
533}
534
535static int str_ioengine_cb(struct thread_data *td, char *str)
536{
537	if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
538	    !strncmp(str, "libaio", 6)) {
539		strcpy(td->io_engine_name, "libaio");
540		td->io_engine = FIO_LIBAIO;
541		return 0;
542	} else if (!strncmp(str, "posixaio", 8)) {
543		strcpy(td->io_engine_name, "posixaio");
544		td->io_engine = FIO_POSIXAIO;
545		return 0;
546	} else if (!strncmp(str, "sync", 4)) {
547		strcpy(td->io_engine_name, "sync");
548		td->io_engine = FIO_SYNCIO;
549		return 0;
550	} else if (!strncmp(str, "mmap", 4)) {
551		strcpy(td->io_engine_name, "mmap");
552		td->io_engine = FIO_MMAPIO;
553		return 0;
554	} else if (!strncmp(str, "sgio", 4)) {
555		strcpy(td->io_engine_name, "sgio");
556		td->io_engine = FIO_SGIO;
557		return 0;
558	} else if (!strncmp(str, "splice", 6)) {
559		strcpy(td->io_engine_name, "splice");
560		td->io_engine = FIO_SPLICEIO;
561		return 0;
562	}
563
564	log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
565	return 1;
566}
567
568/*
569 * This is our [ini] type file parser.
570 */
571int parse_jobs_ini(char *file)
572{
573	unsigned int prioclass, prio, cpu, global, il;
574	unsigned long long ull;
575	unsigned long ul1, ul2;
576	struct thread_data *td;
577	char *string, *name, *tmpbuf;
578	fpos_t off;
579	FILE *f;
580	char *p;
581	int ret = 0, stonewall = 1;
582
583	f = fopen(file, "r");
584	if (!f) {
585		perror("fopen job file");
586		return 1;
587	}
588
589	string = malloc(4096);
590	name = malloc(256);
591	tmpbuf = malloc(4096);
592
593	while ((p = fgets(string, 4096, f)) != NULL) {
594		if (ret)
595			break;
596		if (is_empty_or_comment(p))
597			continue;
598		if (sscanf(p, "[%s]", name) != 1)
599			continue;
600
601		global = !strncmp(name, "global", 6);
602
603		name[strlen(name) - 1] = '\0';
604
605		td = get_new_job(global, &def_thread);
606		if (!td) {
607			ret = 1;
608			break;
609		}
610
611		/*
612		 * Seperate multiple job files by a stonewall
613		 */
614		if (!global && stonewall) {
615			td->stonewall = stonewall;
616			stonewall = 0;
617		}
618
619		fgetpos(f, &off);
620		while ((p = fgets(string, 4096, f)) != NULL) {
621			if (is_empty_or_comment(p))
622				continue;
623			if (strstr(p, "["))
624				break;
625			strip_blank_front(&p);
626			strip_blank_end(p);
627
628			if (!check_int(p, "prio", &prio)) {
629#ifndef FIO_HAVE_IOPRIO
630				log_err("io priorities not available\n");
631				ret = 1;
632				break;
633#endif
634				td->ioprio |= prio;
635				fgetpos(f, &off);
636				continue;
637			}
638			if (!check_int(p, "prioclass", &prioclass)) {
639#ifndef FIO_HAVE_IOPRIO
640				log_err("io priorities not available\n");
641				ret = 1;
642				break;
643#else
644				td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
645				fgetpos(f, &off);
646				continue;
647#endif
648			}
649			if (!check_int(p, "direct", &il)) {
650				td->odirect = il;
651				fgetpos(f, &off);
652				continue;
653			}
654			if (!check_int(p, "rand_repeatable", &il)) {
655				td->rand_repeatable = il;
656				fgetpos(f, &off);
657				continue;
658			}
659			if (!check_int(p, "rate", &td->rate)) {
660				fgetpos(f, &off);
661				continue;
662			}
663			if (!check_int(p, "ratemin", &td->ratemin)) {
664				fgetpos(f, &off);
665				continue;
666			}
667			if (!check_int(p, "ratecycle", &td->ratecycle)) {
668				fgetpos(f, &off);
669				continue;
670			}
671			if (!check_int(p, "thinktime", &td->thinktime)) {
672				fgetpos(f, &off);
673				continue;
674			}
675			if (!check_int(p, "cpumask", &cpu)) {
676#ifndef FIO_HAVE_CPU_AFFINITY
677				log_err("cpu affinity not available\n");
678				ret = 1;
679				break;
680#endif
681				fill_cpu_mask(td->cpumask, cpu);
682				fgetpos(f, &off);
683				continue;
684			}
685			if (!check_int(p, "fsync", &td->fsync_blocks)) {
686				fgetpos(f, &off);
687				td->end_fsync = 1;
688				continue;
689			}
690			if (!check_int(p, "startdelay", &td->start_delay)) {
691				fgetpos(f, &off);
692				continue;
693			}
694			if (!check_int(p, "timeout", &td->timeout)) {
695				fgetpos(f, &off);
696				continue;
697			}
698			if (!check_int(p, "invalidate", &il)) {
699				td->invalidate_cache = il;
700				fgetpos(f, &off);
701				continue;
702			}
703			if (!check_int(p, "iodepth", &td->iodepth)) {
704				fgetpos(f, &off);
705				continue;
706			}
707			if (!check_int(p, "sync", &il)) {
708				td->sync_io = il;
709				fgetpos(f, &off);
710				continue;
711			}
712			if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
713				fgetpos(f, &off);
714				continue;
715			}
716			if (!check_int(p, "create_serialize", &il)) {
717				td->create_serialize = il;
718				fgetpos(f, &off);
719				continue;
720			}
721			if (!check_int(p, "create_fsync", &il)) {
722				td->create_fsync = il;
723				fgetpos(f, &off);
724				continue;
725			}
726			if (!check_int(p, "end_fsync", &il)) {
727				td->end_fsync = il;
728				fgetpos(f, &off);
729				continue;
730			}
731			if (!check_int(p, "loops", &td->loops)) {
732				fgetpos(f, &off);
733				continue;
734			}
735			if (!check_int(p, "numjobs", &td->numjobs)) {
736				fgetpos(f, &off);
737				continue;
738			}
739			if (!check_int(p, "overwrite", &il)) {
740				td->overwrite = il;
741				fgetpos(f, &off);
742				continue;
743			}
744			if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
745				fgetpos(f, &off);
746				continue;
747			}
748			if (!check_int(p, "rwmixread", &il)) {
749				if (il > 100)
750					il = 100;
751				td->rwmixread = il;
752				fgetpos(f, &off);
753				continue;
754			}
755			if (!check_int(p, "rwmixwrite", &il)) {
756				if (il > 100)
757					il = 100;
758				td->rwmixread = 100 - il;
759				fgetpos(f, &off);
760				continue;
761			}
762			if (!check_int(p, "nice", &td->nice)) {
763				fgetpos(f, &off);
764				continue;
765			}
766			if (!check_range(p, "bsrange", &ul1, &ul2)) {
767				if (ul1 > ul2) {
768					td->max_bs = ul1;
769					td->min_bs = ul2;
770				} else {
771					td->max_bs = ul2;
772					td->min_bs = ul1;
773				}
774				fgetpos(f, &off);
775				continue;
776			}
777			if (!check_strcnv(p, "bs", &ull)) {
778				td->bs = ull;
779				fgetpos(f, &off);
780				continue;
781			}
782			if (!check_strcnv(p, "size", &td->file_size)) {
783				fgetpos(f, &off);
784				continue;
785			}
786			if (!check_strcnv(p, "offset", &td->file_offset)) {
787				fgetpos(f, &off);
788				continue;
789			}
790			if (!check_strcnv(p, "zonesize", &td->zone_size)) {
791				fgetpos(f, &off);
792				continue;
793			}
794			if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
795				fgetpos(f, &off);
796				continue;
797			}
798			if (!check_strcnv(p, "lockmem", &mlock_size)) {
799				fgetpos(f, &off);
800				continue;
801			}
802			if (!check_strstore(p, "directory", tmpbuf)) {
803				td->directory = strdup(tmpbuf);
804				fgetpos(f, &off);
805				continue;
806			}
807			if (!check_strstore(p, "name", tmpbuf)) {
808				snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
809				fgetpos(f, &off);
810				continue;
811			}
812			if (!check_str(p, "mem", str_mem_cb, td)) {
813				fgetpos(f, &off);
814				continue;
815			}
816			if (!check_str(p, "verify", str_verify_cb, td)) {
817				fgetpos(f, &off);
818				continue;
819			}
820			if (!check_str(p, "rw", str_rw_cb, td)) {
821				fgetpos(f, &off);
822				continue;
823			}
824			if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
825				fgetpos(f, &off);
826				continue;
827			}
828			if (!check_strset(p, "create")) {
829				td->create_file = 1;
830				fgetpos(f, &off);
831				continue;
832			}
833			if (!check_strset(p, "exitall")) {
834				exitall_on_terminate = 1;
835				fgetpos(f, &off);
836				continue;
837			}
838			if (!check_strset(p, "stonewall")) {
839				td->stonewall = 1;
840				fgetpos(f, &off);
841				continue;
842			}
843			if (!check_strset(p, "thread")) {
844				td->use_thread = 1;
845				fgetpos(f, &off);
846				continue;
847			}
848			if (!check_strstore(p, "iolog", tmpbuf)) {
849				if (td->write_iolog) {
850					log_err("fio: read iolog overrides given write_iolog\n");
851					free(td->iolog_file);
852					td->write_iolog = 0;
853				}
854				td->iolog_file = strdup(tmpbuf);
855				td->read_iolog = 1;
856				fgetpos(f, &off);
857				continue;
858			}
859			if (!check_strstore(p, "write_iolog", tmpbuf)) {
860				if (!td->read_iolog) {
861					td->iolog_file = strdup(tmpbuf);
862					td->write_iolog = 1;
863				} else
864					log_err("fio: read iolog overrides given write_iolog\n");
865				fgetpos(f, &off);
866				continue;
867			}
868			if (!check_strstore(p, "exec_prerun", tmpbuf)) {
869				td->exec_prerun = strdup(tmpbuf);
870				fgetpos(f, &off);
871				continue;
872			}
873			if (!check_strstore(p, "exec_postrun", tmpbuf)) {
874				td->exec_postrun = strdup(tmpbuf);
875				fgetpos(f, &off);
876				continue;
877			}
878			if (!check_strstore(p, "ioscheduler", tmpbuf)) {
879#ifndef FIO_HAVE_IOSCHED_SWITCH
880				log_err("io scheduler switching not available\n");
881				ret = 1;
882				break;
883#else
884				td->ioscheduler = strdup(tmpbuf);
885				fgetpos(f, &off);
886				continue;
887#endif
888			}
889
890			/*
891			 * Don't break here, continue parsing options so we
892			 * dump all the bad ones. Makes trial/error fixups
893			 * easier on the user.
894			 */
895			printf("Client%d: bad option %s\n",td->thread_number,p);
896			ret = 1;
897		}
898
899		if (!ret) {
900			fsetpos(f, &off);
901			ret = add_job(td, name, 0);
902		}
903		if (ret)
904			break;
905	}
906
907	free(string);
908	free(name);
909	free(tmpbuf);
910	fclose(f);
911	return ret;
912}
913
914static int fill_def_thread(void)
915{
916	memset(&def_thread, 0, sizeof(def_thread));
917
918	if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
919		perror("sched_getaffinity");
920		return 1;
921	}
922
923	/*
924	 * fill globals
925	 */
926	def_thread.ddir = DDIR_READ;
927	def_thread.iomix = 0;
928	def_thread.bs = DEF_BS;
929	def_thread.min_bs = -1;
930	def_thread.max_bs = -1;
931	def_thread.io_engine = DEF_IO_ENGINE;
932	strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
933	def_thread.odirect = DEF_ODIRECT;
934	def_thread.ratecycle = DEF_RATE_CYCLE;
935	def_thread.sequential = DEF_SEQUENTIAL;
936	def_thread.timeout = def_timeout;
937	def_thread.create_file = DEF_CREATE;
938	def_thread.overwrite = DEF_OVERWRITE;
939	def_thread.invalidate_cache = DEF_INVALIDATE;
940	def_thread.sync_io = DEF_SYNCIO;
941	def_thread.mem_type = MEM_MALLOC;
942	def_thread.bw_avg_time = DEF_BWAVGTIME;
943	def_thread.create_serialize = DEF_CREATE_SER;
944	def_thread.create_fsync = DEF_CREATE_FSYNC;
945	def_thread.loops = DEF_LOOPS;
946	def_thread.verify = DEF_VERIFY;
947	def_thread.stonewall = DEF_STONEWALL;
948	def_thread.numjobs = DEF_NUMJOBS;
949	def_thread.use_thread = DEF_USE_THREAD;
950	def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
951	def_thread.rwmixread = DEF_RWMIX_READ;
952	def_thread.nice = DEF_NICE;
953	def_thread.rand_repeatable = DEF_RAND_REPEAT;
954#ifdef FIO_HAVE_DISK_UTIL
955	def_thread.do_disk_util = 1;
956#endif
957
958	return 0;
959}
960
961static void usage(char *name)
962{
963	printf("%s\n", fio_version_string);
964	printf("\t-s IO is sequential\n");
965	printf("\t-b Block size in KiB for each IO\n");
966	printf("\t-t Runtime in seconds\n");
967	printf("\t-R Exit all threads on failure to meet rate goal\n");
968	printf("\t-o Use O_DIRECT\n");
969	printf("\t-l Generate per-job latency logs\n");
970	printf("\t-w Generate per-job bandwidth logs\n");
971	printf("\t-f Job file (Required)\n");
972	printf("\t-v Print version info and exit\n");
973}
974
975static int parse_cmd_line(int argc, char *argv[])
976{
977	int c, idx = 1, ini_idx = 0;
978
979	while ((c = getopt(argc, argv, "t:o:f:lwvh")) != EOF) {
980		switch (c) {
981			case 't':
982				def_timeout = atoi(optarg);
983				idx++;
984				break;
985			case 'f':
986				ini_idx++;
987				ini_file = realloc(ini_file, ini_idx * sizeof(char *));
988				ini_file[ini_idx - 1] = strdup(optarg);
989				idx++;
990				break;
991			case 'l':
992				write_lat_log = 1;
993				idx++;
994				break;
995			case 'w':
996				write_bw_log = 1;
997				idx++;
998				break;
999			case 'o':
1000				f_out = fopen(optarg, "w+");
1001				if (!f_out) {
1002					perror("fopen output");
1003					exit(1);
1004				}
1005				f_err = f_out;
1006				idx++;
1007				break;
1008			case 'h':
1009				usage(argv[0]);
1010				exit(0);
1011			case 'v':
1012				printf("%s\n", fio_version_string);
1013				exit(0);
1014		}
1015	}
1016
1017	while (idx < argc) {
1018		ini_idx++;
1019		ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1020		ini_file[ini_idx - 1] = strdup(argv[idx]);
1021		idx++;
1022	}
1023
1024	if (!f_out) {
1025		f_out = stdout;
1026		f_err = stderr;
1027	}
1028
1029	return ini_idx;
1030}
1031
1032static void free_shm(void)
1033{
1034	struct shmid_ds sbuf;
1035
1036	if (threads) {
1037		shmdt((void *) threads);
1038		threads = NULL;
1039		shmctl(shm_id, IPC_RMID, &sbuf);
1040	}
1041}
1042
1043static int setup_thread_area(void)
1044{
1045	/*
1046	 * 1024 is too much on some machines, scale max_jobs if
1047	 * we get a failure that looks like too large a shm segment
1048	 */
1049	do {
1050		int s = max_jobs * sizeof(struct thread_data);
1051
1052		shm_id = shmget(0, s, IPC_CREAT | 0600);
1053		if (shm_id != -1)
1054			break;
1055		if (errno != EINVAL) {
1056			perror("shmget");
1057			break;
1058		}
1059
1060		max_jobs >>= 1;
1061	} while (max_jobs);
1062
1063	if (shm_id == -1)
1064		return 1;
1065
1066	threads = shmat(shm_id, NULL, 0);
1067	if (threads == (void *) -1) {
1068		perror("shmat");
1069		return 1;
1070	}
1071
1072	atexit(free_shm);
1073	return 0;
1074}
1075
1076int parse_options(int argc, char *argv[])
1077{
1078	int job_files, i;
1079
1080	if (setup_thread_area())
1081		return 1;
1082	if (fill_def_thread())
1083		return 1;
1084
1085	job_files = parse_cmd_line(argc, argv);
1086	if (!job_files) {
1087		log_err("Need job file(s)\n");
1088		usage(argv[0]);
1089		return 1;
1090	}
1091
1092	for (i = 0; i < job_files; i++) {
1093		if (fill_def_thread())
1094			return 1;
1095		if (parse_jobs_ini(ini_file[i]))
1096			return 1;
1097		free(ini_file[i]);
1098	}
1099
1100	free(ini_file);
1101	return 0;
1102}
1103