[PATCH] Add support for the read/write interface of SG v3
[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.1";
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->thread_number);
217                 else
218                         sprintf(td->file_name, "%s.%d", jobname, td->thread_number);
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
267                 if (add_job(td_new, jobname, prioclass, prio))
268                         goto err;
269         }
270         return 0;
271 err:
272         put_job(td);
273         return -1;
274 }
275
276 int init_random_state(struct thread_data *td)
277 {
278         unsigned long seed;
279         int fd, num_maps, blocks;
280
281         fd = open("/dev/random", O_RDONLY);
282         if (fd == -1) {
283                 td_verror(td, errno);
284                 return 1;
285         }
286
287         if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
288                 td_verror(td, EIO);
289                 close(fd);
290                 return 1;
291         }
292
293         close(fd);
294
295         srand48_r(seed, &td->bsrange_state);
296         srand48_r(seed, &td->verify_state);
297
298         if (td->sequential)
299                 return 0;
300
301         if (repeatable)
302                 seed = DEF_RANDSEED;
303
304         blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
305         num_maps = blocks / BLOCKS_PER_MAP;
306         td->file_map = malloc(num_maps * sizeof(long));
307         td->num_maps = num_maps;
308         memset(td->file_map, 0, num_maps * sizeof(long));
309
310         srand48_r(seed, &td->random_state);
311         return 0;
312 }
313
314 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
315 {
316 #ifdef FIO_HAVE_CPU_AFFINITY
317         unsigned int i;
318
319         CPU_ZERO(&cpumask);
320
321         for (i = 0; i < sizeof(int) * 8; i++) {
322                 if ((1 << i) & cpu)
323                         CPU_SET(i, &cpumask);
324         }
325 #endif
326 }
327
328 static unsigned long get_mult(char c)
329 {
330         switch (c) {
331                 case 'k':
332                 case 'K':
333                         return 1024;
334                 case 'm':
335                 case 'M':
336                         return 1024 * 1024;
337                 case 'g':
338                 case 'G':
339                         return 1024 * 1024 * 1024;
340                 default:
341                         return 1;
342         }
343 }
344
345 /*
346  * convert string after '=' into decimal value, noting any size suffix
347  */
348 static int str_cnv(char *p, unsigned long long *val)
349 {
350         char *str;
351         int len;
352
353         str = strchr(p, '=');
354         if (!str)
355                 return 1;
356
357         str++;
358         len = strlen(str);
359
360         *val = strtoul(str, NULL, 10);
361         if (*val == ULONG_MAX && errno == ERANGE)
362                 return 1;
363
364         *val *= get_mult(str[len - 2]);
365         return 0;
366 }
367
368 static int check_strcnv(char *p, char *name, unsigned long long *val)
369 {
370         if (strncmp(p, name, strlen(name) - 1))
371                 return 1;
372
373         return str_cnv(p, val);
374 }
375
376 static void strip_blank_front(char **p)
377 {
378         char *s = *p;
379
380         while (isblank(*s))
381                 s++;
382 }
383
384 static void strip_blank_end(char *p)
385 {
386         while (isblank(*p)) {
387                 *p = '\0';
388                 p--;
389         }
390 }
391
392 typedef int (str_cb_fn)(struct thread_data *, char *);
393
394 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
395 {
396         char *s = strstr(p, name);
397
398         if (!s)
399                 return 1;
400
401         s = strchr(s, '=');
402         if (!s)
403                 return 1;
404
405         s++;
406         strip_blank_front(&s);
407         return cb(td, s);
408 }
409
410 static int check_strstore(char *p, char *name, char *dest)
411 {
412         char *s = strstr(p, name);
413
414         if (!s)
415                 return 1;
416
417         s = strchr(p, '=');
418         if (!s)
419                 return 1;
420
421         s++;
422         strip_blank_front(&s);
423
424         strcpy(dest, s);
425
426         s = dest + strlen(dest) - 1;
427         strip_blank_end(s);
428         return 0;
429 }
430
431 static int __check_range(char *str, unsigned long *val)
432 {
433         char suffix;
434
435         if (sscanf(str, "%lu%c", val, &suffix) == 2) {
436                 *val *= get_mult(suffix);
437                 return 0;
438         }
439
440         if (sscanf(str, "%lu", val) == 1)
441                 return 0;
442
443         return 1;
444 }
445
446 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
447 {
448         char option[128];
449         char *str, *p1, *p2;
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(p1, s) && !__check_range(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         str = strstr(p, name);
490         if (!str)
491                 return 1;
492
493         str = strchr(p, '=');
494         if (!str)
495                 return 1;
496
497         str++;
498
499         if (sscanf(str, "%u", val) == 1)
500                 return 0;
501
502         return 1;
503 }
504
505 static int check_strset(char *p, char *name)
506 {
507         return strncmp(p, name, strlen(name));
508 }
509
510 static int is_empty_or_comment(char *line)
511 {
512         unsigned int i;
513
514         for (i = 0; i < strlen(line); i++) {
515                 if (line[i] == ';')
516                         return 1;
517                 if (!isspace(line[i]) && !iscntrl(line[i]))
518                         return 0;
519         }
520
521         return 1;
522 }
523
524 static int str_rw_cb(struct thread_data *td, char *mem)
525 {
526         if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
527                 td->ddir = DDIR_READ;
528                 td->sequential = 1;
529                 return 0;
530         } else if (!strncmp(mem, "randread", 8)) {
531                 td->ddir = DDIR_READ;
532                 td->sequential = 0;
533                 return 0;
534         } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
535                 td->ddir = DDIR_WRITE;
536                 td->sequential = 1;
537                 return 0;
538         } else if (!strncmp(mem, "randwrite", 9)) {
539                 td->ddir = DDIR_WRITE;
540                 td->sequential = 0;
541                 return 0;
542         }
543
544         fprintf(stderr, "bad data direction: %s\n", mem);
545         return 1;
546 }
547
548 static int str_verify_cb(struct thread_data *td, char *mem)
549 {
550         if (!strncmp(mem, "0", 1)) {
551                 td->verify = VERIFY_NONE;
552                 return 0;
553         } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
554                 td->verify = VERIFY_MD5;
555                 return 0;
556         } else if (!strncmp(mem, "crc32", 5)) {
557                 td->verify = VERIFY_CRC32;
558                 return 0;
559         }
560
561         fprintf(stderr, "bad verify type: %s\n", mem);
562         return 1;
563 }
564
565 static int str_mem_cb(struct thread_data *td, char *mem)
566 {
567         if (!strncmp(mem, "malloc", 6)) {
568                 td->mem_type = MEM_MALLOC;
569                 return 0;
570         } else if (!strncmp(mem, "shm", 3)) {
571                 td->mem_type = MEM_SHM;
572                 return 0;
573         } else if (!strncmp(mem, "mmap", 4)) {
574                 td->mem_type = MEM_MMAP;
575                 return 0;
576         }
577
578         fprintf(stderr, "bad mem type: %s\n", mem);
579         return 1;
580 }
581
582 static int str_ioengine_cb(struct thread_data *td, char *str)
583 {
584         if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
585             !strncmp(str, "libaio", 6)) {
586                 strcpy(td->io_engine_name, "libaio");
587                 td->io_engine = FIO_LIBAIO;
588                 return 0;
589         } else if (!strncmp(str, "posixaio", 8)) {
590                 strcpy(td->io_engine_name, "posixaio");
591                 td->io_engine = FIO_POSIXAIO;
592                 return 0;
593         } else if (!strncmp(str, "sync", 4)) {
594                 strcpy(td->io_engine_name, "sync");
595                 td->io_engine = FIO_SYNCIO;
596                 return 0;
597         } else if (!strncmp(str, "mmap", 4)) {
598                 strcpy(td->io_engine_name, "mmap");
599                 td->io_engine = FIO_MMAPIO;
600                 return 0;
601         } else if (!strncmp(str, "sgio", 4)) {
602                 strcpy(td->io_engine_name, "sgio");
603                 td->io_engine = FIO_SGIO;
604                 return 0;
605         }
606
607         fprintf(stderr, "bad ioengine type: %s\n", str);
608         return 1;
609 }
610
611
612 int parse_jobs_ini(char *file)
613 {
614         unsigned int prioclass, prio, cpu, global;
615         unsigned long long ull;
616         unsigned long ul1, ul2;
617         struct thread_data *td;
618         char *string, *name;
619         fpos_t off;
620         FILE *f;
621         char *p;
622
623         f = fopen(file, "r");
624         if (!f) {
625                 perror("fopen");
626                 return 1;
627         }
628
629         string = malloc(4096);
630         name = malloc(256);
631
632         while ((p = fgets(string, 4096, f)) != NULL) {
633                 if (is_empty_or_comment(p))
634                         continue;
635                 if (sscanf(p, "[%s]", name) != 1)
636                         continue;
637
638                 global = !strncmp(name, "global", 6);
639
640                 name[strlen(name) - 1] = '\0';
641
642                 td = get_new_job(global, &def_thread);
643                 if (!td)
644                         return 1;
645
646                 prioclass = 2;
647                 prio = 4;
648
649                 fgetpos(f, &off);
650                 while ((p = fgets(string, 4096, f)) != NULL) {
651                         if (is_empty_or_comment(p))
652                                 continue;
653                         if (strstr(p, "["))
654                                 break;
655                         if (!check_int(p, "prio", &prio)) {
656 #ifndef FIO_HAVE_IOPRIO
657                                 fprintf(stderr, "io priorities not available\n");
658                                 return 1;
659 #endif
660                                 fgetpos(f, &off);
661                                 continue;
662                         }
663                         if (!check_int(p, "prioclass", &prioclass)) {
664 #ifndef FIO_HAVE_IOPRIO
665                                 fprintf(stderr, "io priorities not available\n");
666                                 return 1;
667 #endif
668                                 fgetpos(f, &off);
669                                 continue;
670                         }
671                         if (!check_int(p, "direct", &td->odirect)) {
672                                 fgetpos(f, &off);
673                                 continue;
674                         }
675                         if (!check_int(p, "rate", &td->rate)) {
676                                 fgetpos(f, &off);
677                                 continue;
678                         }
679                         if (!check_int(p, "ratemin", &td->ratemin)) {
680                                 fgetpos(f, &off);
681                                 continue;
682                         }
683                         if (!check_int(p, "ratecycle", &td->ratecycle)) {
684                                 fgetpos(f, &off);
685                                 continue;
686                         }
687                         if (!check_int(p, "thinktime", &td->thinktime)) {
688                                 fgetpos(f, &off);
689                                 continue;
690                         }
691                         if (!check_int(p, "cpumask", &cpu)) {
692 #ifndef FIO_HAVE_CPU_AFFINITY
693                                 fprintf(stderr, "cpu affinity not available\n");
694                                 return 1;
695 #endif
696                                 fill_cpu_mask(td->cpumask, cpu);
697                                 fgetpos(f, &off);
698                                 continue;
699                         }
700                         if (!check_int(p, "fsync", &td->fsync_blocks)) {
701                                 fgetpos(f, &off);
702                                 continue;
703                         }
704                         if (!check_int(p, "startdelay", &td->start_delay)) {
705                                 fgetpos(f, &off);
706                                 continue;
707                         }
708                         if (!check_int(p, "timeout", &td->timeout)) {
709                                 fgetpos(f, &off);
710                                 continue;
711                         }
712                         if (!check_int(p, "invalidate",&td->invalidate_cache)) {
713                                 fgetpos(f, &off);
714                                 continue;
715                         }
716                         if (!check_int(p, "iodepth", &td->iodepth)) {
717                                 fgetpos(f, &off);
718                                 continue;
719                         }
720                         if (!check_int(p, "sync", &td->sync_io)) {
721                                 fgetpos(f, &off);
722                                 continue;
723                         }
724                         if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
725                                 fgetpos(f, &off);
726                                 continue;
727                         }
728                         if (!check_int(p, "create_serialize", &td->create_serialize)) {
729                                 fgetpos(f, &off);
730                                 continue;
731                         }
732                         if (!check_int(p, "create_fsync", &td->create_fsync)) {
733                                 fgetpos(f, &off);
734                                 continue;
735                         }
736                         if (!check_int(p, "loops", &td->loops)) {
737                                 fgetpos(f, &off);
738                                 continue;
739                         }
740                         if (!check_int(p, "numjobs", &td->numjobs)) {
741                                 fgetpos(f, &off);
742                                 continue;
743                         }
744                         if (!check_int(p, "overwrite", &td->overwrite)) {
745                                 fgetpos(f, &off);
746                                 continue;
747                         }
748                         if (!check_range(p, "bsrange", &ul1, &ul2)) {
749                                 if (ul1 > ul2) {
750                                         td->max_bs = ul1;
751                                         td->min_bs = ul2;
752                                 } else {
753                                         td->max_bs = ul2;
754                                         td->min_bs = ul1;
755                                 }
756                                 fgetpos(f, &off);
757                                 continue;
758                         }
759                         if (!check_strcnv(p, "bs", &ull)) {
760                                 td->bs = ull;
761                                 fgetpos(f, &off);
762                                 continue;
763                         }
764                         if (!check_strcnv(p, "size", &td->file_size)) {
765                                 fgetpos(f, &off);
766                                 continue;
767                         }
768                         if (!check_strcnv(p, "offset", &td->file_offset)) {
769                                 fgetpos(f, &off);
770                                 continue;
771                         }
772                         if (!check_strcnv(p, "zonesize", &td->zone_size)) {
773                                 fgetpos(f, &off);
774                                 continue;
775                         }
776                         if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
777                                 fgetpos(f, &off);
778                                 continue;
779                         }
780                         if (!check_strstore(p, "directory", td->directory)) {
781                                 fgetpos(f, &off);
782                                 continue;
783                         }
784                         if (!check_str(p, "mem", str_mem_cb, td)) {
785                                 fgetpos(f, &off);
786                                 continue;
787                         }
788                         if (!check_str(p, "verify", str_verify_cb, td)) {
789                                 fgetpos(f, &off);
790                                 continue;
791                         }
792                         if (!check_str(p, "rw", str_rw_cb, td)) {
793                                 fgetpos(f, &off);
794                                 continue;
795                         }
796                         if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
797                                 fgetpos(f, &off);
798                                 continue;
799                         }
800                         if (!check_strset(p, "create")) {
801                                 td->create_file = 1;
802                                 fgetpos(f, &off);
803                                 continue;
804                         }
805                         if (!check_strset(p, "exitall")) {
806                                 exitall_on_terminate = 1;
807                                 fgetpos(f, &off);
808                                 continue;
809                         }
810                         if (!check_strset(p, "stonewall")) {
811                                 td->stonewall = 1;
812                                 fgetpos(f, &off);
813                                 continue;
814                         }
815                         if (!check_strset(p, "thread")) {
816                                 td->use_thread = 1;
817                                 fgetpos(f, &off);
818                                 continue;
819                         }
820
821                         printf("Client%d: bad option %s\n",td->thread_number,p);
822                 }
823                 fsetpos(f, &off);
824
825                 if (add_job(td, name, prioclass, prio))
826                         return 1;
827         }
828
829         free(string);
830         free(name);
831         fclose(f);
832         return 0;
833 }
834
835 static int fill_def_thread(void)
836 {
837         memset(&def_thread, 0, sizeof(def_thread));
838
839         if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
840                 perror("sched_getaffinity");
841                 return 1;
842         }
843
844         /*
845          * fill globals
846          */
847         def_thread.ddir = DDIR_READ;
848         def_thread.bs = DEF_BS;
849         def_thread.min_bs = -1;
850         def_thread.max_bs = -1;
851         def_thread.io_engine = DEF_IO_ENGINE;
852         strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
853         def_thread.odirect = DEF_ODIRECT;
854         def_thread.ratecycle = DEF_RATE_CYCLE;
855         def_thread.sequential = DEF_SEQUENTIAL;
856         def_thread.timeout = DEF_TIMEOUT;
857         def_thread.create_file = DEF_CREATE;
858         def_thread.overwrite = DEF_OVERWRITE;
859         def_thread.invalidate_cache = DEF_INVALIDATE;
860         def_thread.sync_io = DEF_SYNCIO;
861         def_thread.mem_type = MEM_MALLOC;
862         def_thread.bw_avg_time = DEF_BWAVGTIME;
863         def_thread.create_serialize = DEF_CREATE_SER;
864         def_thread.create_fsync = DEF_CREATE_FSYNC;
865         def_thread.loops = DEF_LOOPS;
866         def_thread.verify = DEF_VERIFY;
867         def_thread.stonewall = DEF_STONEWALL;
868         def_thread.numjobs = DEF_NUMJOBS;
869         def_thread.use_thread = DEF_USE_THREAD;
870 #ifdef FIO_HAVE_DISK_UTIL
871         def_thread.do_disk_util = 1;
872 #endif
873
874         return 0;
875 }
876
877 static void parse_cmd_line(int argc, char *argv[])
878 {
879         int c;
880
881         while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwv")) != EOF) {
882                 switch (c) {
883                         case 's':
884                                 def_thread.sequential = !!atoi(optarg);
885                                 break;
886                         case 'b':
887                                 def_thread.bs = atoi(optarg);
888                                 def_thread.bs <<= 10;
889                                 if (!def_thread.bs) {
890                                         printf("bad block size\n");
891                                         def_thread.bs = DEF_BS;
892                                 }
893                                 break;
894                         case 't':
895                                 def_thread.timeout = atoi(optarg);
896                                 break;
897                         case 'r':
898                                 repeatable = !!atoi(optarg);
899                                 break;
900                         case 'R':
901                                 rate_quit = !!atoi(optarg);
902                                 break;
903                         case 'o':
904                                 def_thread.odirect = !!atoi(optarg);
905                                 break;
906                         case 'f':
907                                 ini_file = strdup(optarg);
908                                 break;
909                         case 'l':
910                                 write_lat_log = 1;
911                                 break;
912                         case 'w':
913                                 write_bw_log = 1;
914                                 break;
915                         case 'v':
916                                 printf("%s\n", fio_version_string);
917                                 exit(0);
918                 }
919         }
920 }
921
922 static void free_shm(void)
923 {
924         struct shmid_ds sbuf;
925
926         if (threads) {
927                 shmdt(threads);
928                 threads = NULL;
929                 shmctl(shm_id, IPC_RMID, &sbuf);
930         }
931 }
932
933 static int setup_thread_area(void)
934 {
935         /*
936          * 1024 is too much on some machines, scale max_jobs if
937          * we get a failure that looks like too large a shm segment
938          */
939         do {
940                 int s = max_jobs * sizeof(struct thread_data);
941
942                 shm_id = shmget(0, s, IPC_CREAT | 0600);
943                 if (shm_id != -1)
944                         break;
945                 if (errno != EINVAL) {
946                         perror("shmget");
947                         break;
948                 }
949
950                 max_jobs >>= 1;
951         } while (max_jobs);
952
953         if (shm_id == -1)
954                 return 1;
955
956         threads = shmat(shm_id, NULL, 0);
957         if (threads == (void *) -1) {
958                 perror("shmat");
959                 return 1;
960         }
961
962         atexit(free_shm);
963         return 0;
964 }
965
966 int parse_options(int argc, char *argv[])
967 {
968         if (setup_thread_area())
969                 return 1;
970         if (fill_def_thread())
971                 return 1;
972
973         parse_cmd_line(argc, argv);
974
975         if (!ini_file) {
976                 printf("Need job file\n");
977                 return 1;
978         }
979
980         if (parse_jobs_ini(ini_file))
981                 return 1;
982
983         return 0;
984 }