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