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