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