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