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