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