23810bafd2423210f7bb3b015e31695090d5bba2
[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[256];
85         FILE *f;
86         unsigned int i;
87
88         snprintf(file_name, 200, "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                              "rw", NULL, "randrw" };
169         struct stat sb;
170         int numjobs, ddir;
171
172 #ifndef FIO_HAVE_LIBAIO
173         if (td->io_engine == FIO_LIBAIO) {
174                 fprintf(stderr, "Linux libaio not available\n");
175                 return 1;
176         }
177 #endif
178 #ifndef FIO_HAVE_POSIXAIO
179         if (td->io_engine == FIO_POSIXAIO) {
180                 fprintf(stderr, "posix aio not available\n");
181                 return 1;
182         }
183 #endif
184 #ifdef FIO_HAVE_IOPRIO
185         td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
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, 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);
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, prioclass, prio))
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 - 2]);
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 (isblank(*s))
383                 s++;
384 }
385
386 static void strip_blank_end(char *p)
387 {
388         while (isblank(*p)) {
389                 *p = '\0';
390                 p--;
391         }
392 }
393
394 static void terminate_line(char *p)
395 {
396         while (*p != '\n' && *p != '\0')
397                 p++;
398
399         *p = '\0';
400 }
401
402 typedef int (str_cb_fn)(struct thread_data *, char *);
403
404 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
405 {
406         char *s = strstr(p, name);
407
408         if (!s)
409                 return 1;
410
411         s = strchr(s, '=');
412         if (!s)
413                 return 1;
414
415         s++;
416         strip_blank_front(&s);
417         return cb(td, s);
418 }
419
420 static int check_strstore(char *p, char *name, char *dest)
421 {
422         char *s = strstr(p, name);
423
424         if (!s)
425                 return 1;
426
427         s = strchr(p, '=');
428         if (!s)
429                 return 1;
430
431         s++;
432         strip_blank_front(&s);
433
434         strcpy(dest, s);
435
436         s = dest + strlen(dest) - 1;
437         strip_blank_end(s);
438         return 0;
439 }
440
441 static int __check_range(char *str, unsigned long *val)
442 {
443         char suffix;
444
445         if (sscanf(str, "%lu%c", val, &suffix) == 2) {
446                 *val *= get_mult(suffix);
447                 return 0;
448         }
449
450         if (sscanf(str, "%lu", val) == 1)
451                 return 0;
452
453         return 1;
454 }
455
456 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
457 {
458         char option[128];
459         char *str, *p1, *p2;
460
461         strcpy(option, p);
462         p = option;
463
464         str = strstr(p, name);
465         if (!str)
466                 return 1;
467
468         p += strlen(name);
469
470         str = strchr(p, '=');
471         if (!str)
472                 return 1;
473
474         /*
475          * 'p' now holds whatever is after the '=' sign
476          */
477         p1 = str + 1;
478
479         /*
480          * terminate p1 at the '-' sign
481          */
482         p = strchr(p1, '-');
483         if (!p)
484                 return 1;
485
486         p2 = p + 1;
487         *p = '\0';
488
489         if (!__check_range(p1, s) && !__check_range(p2, e))
490                 return 0;
491
492         return 1;
493 }
494
495 static int check_int(char *p, char *name, unsigned int *val)
496 {
497         char *str;
498
499         str = strstr(p, name);
500         if (!str)
501                 return 1;
502
503         str = strchr(p, '=');
504         if (!str)
505                 return 1;
506
507         str++;
508
509         if (sscanf(str, "%u", val) == 1)
510                 return 0;
511
512         return 1;
513 }
514
515 static int check_strset(char *p, char *name)
516 {
517         return strncmp(p, name, strlen(name));
518 }
519
520 static int is_empty_or_comment(char *line)
521 {
522         unsigned int i;
523
524         for (i = 0; i < strlen(line); i++) {
525                 if (line[i] == ';')
526                         return 1;
527                 if (!isspace(line[i]) && !iscntrl(line[i]))
528                         return 0;
529         }
530
531         return 1;
532 }
533
534 static int str_rw_cb(struct thread_data *td, char *mem)
535 {
536         if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
537                 td->ddir = DDIR_READ;
538                 td->sequential = 1;
539                 return 0;
540         } else if (!strncmp(mem, "randread", 8)) {
541                 td->ddir = DDIR_READ;
542                 td->sequential = 0;
543                 return 0;
544         } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
545                 td->ddir = DDIR_WRITE;
546                 td->sequential = 1;
547                 return 0;
548         } else if (!strncmp(mem, "randwrite", 9)) {
549                 td->ddir = DDIR_WRITE;
550                 td->sequential = 0;
551                 return 0;
552         } else if (!strncmp(mem, "rw", 2)) {
553                 td->ddir = 0;
554                 td->iomix = 1;
555                 td->sequential = 1;
556                 return 0;
557         } else if (!strncmp(mem, "randrw", 6)) {
558                 td->ddir = 0;
559                 td->iomix = 1;
560                 td->sequential = 0;
561                 return 0;
562         }
563
564         fprintf(stderr, "bad data direction: %s\n", mem);
565         return 1;
566 }
567
568 static int str_verify_cb(struct thread_data *td, char *mem)
569 {
570         if (!strncmp(mem, "0", 1)) {
571                 td->verify = VERIFY_NONE;
572                 return 0;
573         } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
574                 td->verify = VERIFY_MD5;
575                 return 0;
576         } else if (!strncmp(mem, "crc32", 5)) {
577                 td->verify = VERIFY_CRC32;
578                 return 0;
579         }
580
581         fprintf(stderr, "bad verify type: %s\n", mem);
582         return 1;
583 }
584
585 static int str_mem_cb(struct thread_data *td, char *mem)
586 {
587         if (!strncmp(mem, "malloc", 6)) {
588                 td->mem_type = MEM_MALLOC;
589                 return 0;
590         } else if (!strncmp(mem, "shm", 3)) {
591                 td->mem_type = MEM_SHM;
592                 return 0;
593         } else if (!strncmp(mem, "mmap", 4)) {
594                 td->mem_type = MEM_MMAP;
595                 return 0;
596         }
597
598         fprintf(stderr, "bad mem type: %s\n", mem);
599         return 1;
600 }
601
602 static int str_ioengine_cb(struct thread_data *td, char *str)
603 {
604         if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
605             !strncmp(str, "libaio", 6)) {
606                 strcpy(td->io_engine_name, "libaio");
607                 td->io_engine = FIO_LIBAIO;
608                 return 0;
609         } else if (!strncmp(str, "posixaio", 8)) {
610                 strcpy(td->io_engine_name, "posixaio");
611                 td->io_engine = FIO_POSIXAIO;
612                 return 0;
613         } else if (!strncmp(str, "sync", 4)) {
614                 strcpy(td->io_engine_name, "sync");
615                 td->io_engine = FIO_SYNCIO;
616                 return 0;
617         } else if (!strncmp(str, "mmap", 4)) {
618                 strcpy(td->io_engine_name, "mmap");
619                 td->io_engine = FIO_MMAPIO;
620                 return 0;
621         } else if (!strncmp(str, "sgio", 4)) {
622                 strcpy(td->io_engine_name, "sgio");
623                 td->io_engine = FIO_SGIO;
624                 return 0;
625         }
626
627         fprintf(stderr, "bad ioengine type: %s\n", str);
628         return 1;
629 }
630
631 static int str_iolog_cb(struct thread_data *td, char *file)
632 {
633         terminate_line(file);
634         strncpy(td->iolog_file, file, sizeof(td->iolog_file) - 1);
635
636         return 0;
637 }
638
639 int parse_jobs_ini(char *file)
640 {
641         unsigned int prioclass, prio, cpu, global;
642         unsigned long long ull;
643         unsigned long ul1, ul2;
644         struct thread_data *td;
645         char *string, *name;
646         fpos_t off;
647         FILE *f;
648         char *p;
649
650         f = fopen(file, "r");
651         if (!f) {
652                 perror("fopen job file");
653                 return 1;
654         }
655
656         string = malloc(4096);
657         name = malloc(256);
658
659         while ((p = fgets(string, 4096, f)) != NULL) {
660                 if (is_empty_or_comment(p))
661                         continue;
662                 if (sscanf(p, "[%s]", name) != 1)
663                         continue;
664
665                 global = !strncmp(name, "global", 6);
666
667                 name[strlen(name) - 1] = '\0';
668
669                 td = get_new_job(global, &def_thread);
670                 if (!td)
671                         return 1;
672
673                 prioclass = 2;
674                 prio = 4;
675
676                 fgetpos(f, &off);
677                 while ((p = fgets(string, 4096, f)) != NULL) {
678                         if (is_empty_or_comment(p))
679                                 continue;
680                         if (strstr(p, "["))
681                                 break;
682
683                         if (!check_int(p, "prio", &prio)) {
684 #ifndef FIO_HAVE_IOPRIO
685                                 fprintf(stderr, "io priorities not available\n");
686                                 return 1;
687 #endif
688                                 fgetpos(f, &off);
689                                 continue;
690                         }
691                         if (!check_int(p, "prioclass", &prioclass)) {
692 #ifndef FIO_HAVE_IOPRIO
693                                 fprintf(stderr, "io priorities not available\n");
694                                 return 1;
695 #endif
696                                 fgetpos(f, &off);
697                                 continue;
698                         }
699                         if (!check_int(p, "direct", &td->odirect)) {
700                                 fgetpos(f, &off);
701                                 continue;
702                         }
703                         if (!check_int(p, "rate", &td->rate)) {
704                                 fgetpos(f, &off);
705                                 continue;
706                         }
707                         if (!check_int(p, "ratemin", &td->ratemin)) {
708                                 fgetpos(f, &off);
709                                 continue;
710                         }
711                         if (!check_int(p, "ratecycle", &td->ratecycle)) {
712                                 fgetpos(f, &off);
713                                 continue;
714                         }
715                         if (!check_int(p, "thinktime", &td->thinktime)) {
716                                 fgetpos(f, &off);
717                                 continue;
718                         }
719                         if (!check_int(p, "cpumask", &cpu)) {
720 #ifndef FIO_HAVE_CPU_AFFINITY
721                                 fprintf(stderr, "cpu affinity not available\n");
722                                 return 1;
723 #endif
724                                 fill_cpu_mask(td->cpumask, cpu);
725                                 fgetpos(f, &off);
726                                 continue;
727                         }
728                         if (!check_int(p, "fsync", &td->fsync_blocks)) {
729                                 fgetpos(f, &off);
730                                 continue;
731                         }
732                         if (!check_int(p, "startdelay", &td->start_delay)) {
733                                 fgetpos(f, &off);
734                                 continue;
735                         }
736                         if (!check_int(p, "timeout", &td->timeout)) {
737                                 fgetpos(f, &off);
738                                 continue;
739                         }
740                         if (!check_int(p, "invalidate",&td->invalidate_cache)) {
741                                 fgetpos(f, &off);
742                                 continue;
743                         }
744                         if (!check_int(p, "iodepth", &td->iodepth)) {
745                                 fgetpos(f, &off);
746                                 continue;
747                         }
748                         if (!check_int(p, "sync", &td->sync_io)) {
749                                 fgetpos(f, &off);
750                                 continue;
751                         }
752                         if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
753                                 fgetpos(f, &off);
754                                 continue;
755                         }
756                         if (!check_int(p, "create_serialize", &td->create_serialize)) {
757                                 fgetpos(f, &off);
758                                 continue;
759                         }
760                         if (!check_int(p, "create_fsync", &td->create_fsync)) {
761                                 fgetpos(f, &off);
762                                 continue;
763                         }
764                         if (!check_int(p, "loops", &td->loops)) {
765                                 fgetpos(f, &off);
766                                 continue;
767                         }
768                         if (!check_int(p, "numjobs", &td->numjobs)) {
769                                 fgetpos(f, &off);
770                                 continue;
771                         }
772                         if (!check_int(p, "overwrite", &td->overwrite)) {
773                                 fgetpos(f, &off);
774                                 continue;
775                         }
776                         if (!check_range(p, "bsrange", &ul1, &ul2)) {
777                                 if (ul1 > ul2) {
778                                         td->max_bs = ul1;
779                                         td->min_bs = ul2;
780                                 } else {
781                                         td->max_bs = ul2;
782                                         td->min_bs = ul1;
783                                 }
784                                 fgetpos(f, &off);
785                                 continue;
786                         }
787                         if (!check_strcnv(p, "bs", &ull)) {
788                                 td->bs = ull;
789                                 fgetpos(f, &off);
790                                 continue;
791                         }
792                         if (!check_strcnv(p, "size", &td->file_size)) {
793                                 fgetpos(f, &off);
794                                 continue;
795                         }
796                         if (!check_strcnv(p, "offset", &td->file_offset)) {
797                                 fgetpos(f, &off);
798                                 continue;
799                         }
800                         if (!check_strcnv(p, "zonesize", &td->zone_size)) {
801                                 fgetpos(f, &off);
802                                 continue;
803                         }
804                         if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
805                                 fgetpos(f, &off);
806                                 continue;
807                         }
808                         if (!check_strstore(p, "directory", td->directory)) {
809                                 fgetpos(f, &off);
810                                 continue;
811                         }
812                         if (!check_str(p, "mem", str_mem_cb, td)) {
813                                 fgetpos(f, &off);
814                                 continue;
815                         }
816                         if (!check_str(p, "verify", str_verify_cb, td)) {
817                                 fgetpos(f, &off);
818                                 continue;
819                         }
820                         if (!check_str(p, "rw", str_rw_cb, td)) {
821                                 fgetpos(f, &off);
822                                 continue;
823                         }
824                         if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
825                                 fgetpos(f, &off);
826                                 continue;
827                         }
828                         if (!check_strset(p, "create")) {
829                                 td->create_file = 1;
830                                 fgetpos(f, &off);
831                                 continue;
832                         }
833                         if (!check_strset(p, "exitall")) {
834                                 exitall_on_terminate = 1;
835                                 fgetpos(f, &off);
836                                 continue;
837                         }
838                         if (!check_strset(p, "stonewall")) {
839                                 td->stonewall = 1;
840                                 fgetpos(f, &off);
841                                 continue;
842                         }
843                         if (!check_strset(p, "thread")) {
844                                 td->use_thread = 1;
845                                 fgetpos(f, &off);
846                                 continue;
847                         }
848                         if (!check_str(p, "iolog", str_iolog_cb, td)) {
849                                 td->iolog = 1;
850                                 fgetpos(f, &off);
851                                 continue;
852                         }
853
854                         printf("Client%d: bad option %s\n",td->thread_number,p);
855                         return 1;
856                 }
857                 fsetpos(f, &off);
858
859                 if (add_job(td, name, prioclass, prio))
860                         return 1;
861         }
862
863         free(string);
864         free(name);
865         fclose(f);
866         return 0;
867 }
868
869 static int fill_def_thread(void)
870 {
871         memset(&def_thread, 0, sizeof(def_thread));
872
873         if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
874                 perror("sched_getaffinity");
875                 return 1;
876         }
877
878         /*
879          * fill globals
880          */
881         def_thread.ddir = DDIR_READ;
882         def_thread.iomix = 0;
883         def_thread.bs = DEF_BS;
884         def_thread.min_bs = -1;
885         def_thread.max_bs = -1;
886         def_thread.io_engine = DEF_IO_ENGINE;
887         strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
888         def_thread.odirect = DEF_ODIRECT;
889         def_thread.ratecycle = DEF_RATE_CYCLE;
890         def_thread.sequential = DEF_SEQUENTIAL;
891         def_thread.timeout = DEF_TIMEOUT;
892         def_thread.create_file = DEF_CREATE;
893         def_thread.overwrite = DEF_OVERWRITE;
894         def_thread.invalidate_cache = DEF_INVALIDATE;
895         def_thread.sync_io = DEF_SYNCIO;
896         def_thread.mem_type = MEM_MALLOC;
897         def_thread.bw_avg_time = DEF_BWAVGTIME;
898         def_thread.create_serialize = DEF_CREATE_SER;
899         def_thread.create_fsync = DEF_CREATE_FSYNC;
900         def_thread.loops = DEF_LOOPS;
901         def_thread.verify = DEF_VERIFY;
902         def_thread.stonewall = DEF_STONEWALL;
903         def_thread.numjobs = DEF_NUMJOBS;
904         def_thread.use_thread = DEF_USE_THREAD;
905 #ifdef FIO_HAVE_DISK_UTIL
906         def_thread.do_disk_util = 1;
907 #endif
908
909         return 0;
910 }
911
912 static void usage(char *name)
913 {
914         printf("%s\n", fio_version_string);
915         printf("\t-s IO is sequential\n");
916         printf("\t-b Block size in KiB for each IO\n");
917         printf("\t-t Runtime in seconds\n");
918         printf("\t-R Exit all threads on failure to meet rate goal\n");
919         printf("\t-o Use O_DIRECT\n");
920         printf("\t-l Generate per-job latency logs\n");
921         printf("\t-w Generate per-job bandwidth logs\n");
922         printf("\t-f Job file (Required)\n");
923         printf("\t-v Print version info and exit\n");
924 }
925
926 static void parse_cmd_line(int argc, char *argv[])
927 {
928         int c;
929
930         while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
931                 switch (c) {
932                         case 's':
933                                 def_thread.sequential = !!atoi(optarg);
934                                 break;
935                         case 'b':
936                                 def_thread.bs = atoi(optarg);
937                                 def_thread.bs <<= 10;
938                                 if (!def_thread.bs) {
939                                         printf("bad block size\n");
940                                         def_thread.bs = DEF_BS;
941                                 }
942                                 break;
943                         case 't':
944                                 def_thread.timeout = atoi(optarg);
945                                 break;
946                         case 'r':
947                                 repeatable = !!atoi(optarg);
948                                 break;
949                         case 'R':
950                                 rate_quit = !!atoi(optarg);
951                                 break;
952                         case 'o':
953                                 def_thread.odirect = !!atoi(optarg);
954                                 break;
955                         case 'f':
956                                 ini_file = strdup(optarg);
957                                 break;
958                         case 'l':
959                                 write_lat_log = 1;
960                                 break;
961                         case 'w':
962                                 write_bw_log = 1;
963                                 break;
964                         case 'h':
965                                 usage(argv[0]);
966                                 exit(0);
967                         case 'v':
968                                 printf("%s\n", fio_version_string);
969                                 exit(0);
970                 }
971         }
972
973         if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
974                 ini_file = strdup(argv[argc - 1]);
975 }
976
977 static void free_shm(void)
978 {
979         struct shmid_ds sbuf;
980
981         if (threads) {
982                 shmdt(threads);
983                 threads = NULL;
984                 shmctl(shm_id, IPC_RMID, &sbuf);
985         }
986 }
987
988 static int setup_thread_area(void)
989 {
990         /*
991          * 1024 is too much on some machines, scale max_jobs if
992          * we get a failure that looks like too large a shm segment
993          */
994         do {
995                 int s = max_jobs * sizeof(struct thread_data);
996
997                 shm_id = shmget(0, s, IPC_CREAT | 0600);
998                 if (shm_id != -1)
999                         break;
1000                 if (errno != EINVAL) {
1001                         perror("shmget");
1002                         break;
1003                 }
1004
1005                 max_jobs >>= 1;
1006         } while (max_jobs);
1007
1008         if (shm_id == -1)
1009                 return 1;
1010
1011         threads = shmat(shm_id, NULL, 0);
1012         if (threads == (void *) -1) {
1013                 perror("shmat");
1014                 return 1;
1015         }
1016
1017         atexit(free_shm);
1018         return 0;
1019 }
1020
1021 int parse_options(int argc, char *argv[])
1022 {
1023         if (setup_thread_area())
1024                 return 1;
1025         if (fill_def_thread())
1026                 return 1;
1027
1028         parse_cmd_line(argc, argv);
1029
1030         if (!ini_file) {
1031                 printf("Need job file\n");
1032                 usage(argv[0]);
1033                 return 1;
1034         }
1035
1036         if (parse_jobs_ini(ini_file)) {
1037                 usage(argv[0]);
1038                 return 1;
1039         }
1040
1041         return 0;
1042 }