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