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