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