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