[PATCH] Add end_fsync option
[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)
165 {
166         char *ddir_str[] = { "read", "write", "randread", "randwrite",
167                              "rw", NULL, "randrw" };
168         struct stat sb;
169         int numjobs, ddir;
170
171 #ifndef FIO_HAVE_LIBAIO
172         if (td->io_engine == FIO_LIBAIO) {
173                 fprintf(stderr, "Linux libaio not available\n");
174                 return 1;
175         }
176 #endif
177 #ifndef FIO_HAVE_POSIXAIO
178         if (td->io_engine == FIO_POSIXAIO) {
179                 fprintf(stderr, "posix aio not available\n");
180                 return 1;
181         }
182 #endif
183
184         /*
185          * the def_thread is just for options, it's not a real job
186          */
187         if (td == &def_thread)
188                 return 0;
189
190         if (td->io_engine & FIO_SYNCIO)
191                 td->iodepth = 1;
192         else {
193                 if (!td->iodepth)
194                         td->iodepth = 1;
195         }
196
197         /*
198          * only really works for sequential io for now
199          */
200         if (td->zone_size && !td->sequential)
201                 td->zone_size = 0;
202
203         td->filetype = FIO_TYPE_FILE;
204         if (!stat(jobname, &sb)) {
205                 if (S_ISBLK(sb.st_mode))
206                         td->filetype = FIO_TYPE_BD;
207                 else if (S_ISCHR(sb.st_mode))
208                         td->filetype = FIO_TYPE_CHAR;
209         }
210
211         if (td->filetype == FIO_TYPE_FILE) {
212                 if (td->directory[0] != '\0')
213                         sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->jobnum);
214                 else
215                         sprintf(td->file_name, "%s.%d", jobname, td->jobnum);
216         } else
217                 strncpy(td->file_name, jobname, sizeof(td->file_name) - 1);
218
219         sem_init(&td->mutex, 0, 0);
220
221         td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
222         td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
223         td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
224
225         if (td->min_bs == -1U)
226                 td->min_bs = td->bs;
227         if (td->max_bs == -1U)
228                 td->max_bs = td->bs;
229         if (td_read(td) && !td_rw(td))
230                 td->verify = 0;
231
232         if (td->stonewall && td->thread_number > 1)
233                 groupid++;
234
235         td->groupid = groupid;
236
237         if (setup_rate(td))
238                 goto err;
239
240         if (write_lat_log) {
241                 setup_log(&td->slat_log);
242                 setup_log(&td->clat_log);
243         }
244         if (write_bw_log)
245                 setup_log(&td->bw_log);
246
247         ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
248         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);
249
250         /*
251          * recurse add identical jobs, clear numjobs and stonewall options
252          * as they don't apply to sub-jobs
253          */
254         numjobs = td->numjobs;
255         while (--numjobs) {
256                 struct thread_data *td_new = get_new_job(0, td);
257
258                 if (!td_new)
259                         goto err;
260
261                 td_new->numjobs = 1;
262                 td_new->stonewall = 0;
263                 td_new->jobnum = numjobs;
264
265                 if (add_job(td_new, jobname))
266                         goto err;
267         }
268         return 0;
269 err:
270         put_job(td);
271         return -1;
272 }
273
274 int init_random_state(struct thread_data *td)
275 {
276         unsigned long seed;
277         int fd, num_maps, blocks;
278
279         fd = open("/dev/random", O_RDONLY);
280         if (fd == -1) {
281                 td_verror(td, errno);
282                 return 1;
283         }
284
285         if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
286                 td_verror(td, EIO);
287                 close(fd);
288                 return 1;
289         }
290
291         close(fd);
292
293         srand48_r(seed, &td->bsrange_state);
294         srand48_r(seed, &td->verify_state);
295
296         if (td->sequential)
297                 return 0;
298
299         if (repeatable)
300                 seed = DEF_RANDSEED;
301
302         blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
303         num_maps = blocks / BLOCKS_PER_MAP;
304         td->file_map = malloc(num_maps * sizeof(long));
305         td->num_maps = num_maps;
306         memset(td->file_map, 0, num_maps * sizeof(long));
307
308         srand48_r(seed, &td->random_state);
309         return 0;
310 }
311
312 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
313 {
314 #ifdef FIO_HAVE_CPU_AFFINITY
315         unsigned int i;
316
317         CPU_ZERO(&cpumask);
318
319         for (i = 0; i < sizeof(int) * 8; i++) {
320                 if ((1 << i) & cpu)
321                         CPU_SET(i, &cpumask);
322         }
323 #endif
324 }
325
326 static unsigned long get_mult(char c)
327 {
328         switch (c) {
329                 case 'k':
330                 case 'K':
331                         return 1024;
332                 case 'm':
333                 case 'M':
334                         return 1024 * 1024;
335                 case 'g':
336                 case 'G':
337                         return 1024 * 1024 * 1024;
338                 default:
339                         return 1;
340         }
341 }
342
343 /*
344  * convert string after '=' into decimal value, noting any size suffix
345  */
346 static int str_cnv(char *p, unsigned long long *val)
347 {
348         char *str;
349         int len;
350
351         str = strchr(p, '=');
352         if (!str)
353                 return 1;
354
355         str++;
356         len = strlen(str);
357
358         *val = strtoul(str, NULL, 10);
359         if (*val == ULONG_MAX && errno == ERANGE)
360                 return 1;
361
362         *val *= get_mult(str[len - 2]);
363         return 0;
364 }
365
366 static int check_strcnv(char *p, char *name, unsigned long long *val)
367 {
368         if (strncmp(p, name, strlen(name) - 1))
369                 return 1;
370
371         return str_cnv(p, val);
372 }
373
374 static void strip_blank_front(char **p)
375 {
376         char *s = *p;
377
378         while (isblank(*s))
379                 s++;
380 }
381
382 static void strip_blank_end(char *p)
383 {
384         while (isblank(*p)) {
385                 *p = '\0';
386                 p--;
387         }
388 }
389
390 static void terminate_line(char *p)
391 {
392         while (*p != '\n' && *p != '\0')
393                 p++;
394
395         *p = '\0';
396 }
397
398 typedef int (str_cb_fn)(struct thread_data *, char *);
399
400 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
401 {
402         char *s = strstr(p, name);
403
404         if (!s)
405                 return 1;
406
407         s = strchr(s, '=');
408         if (!s)
409                 return 1;
410
411         s++;
412         strip_blank_front(&s);
413         return cb(td, s);
414 }
415
416 static int check_strstore(char *p, char *name, char *dest)
417 {
418         char *s = strstr(p, name);
419
420         if (!s)
421                 return 1;
422
423         s = strchr(p, '=');
424         if (!s)
425                 return 1;
426
427         s++;
428         strip_blank_front(&s);
429
430         strcpy(dest, s);
431
432         s = dest + strlen(dest) - 1;
433         strip_blank_end(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         str = strstr(p, name);
496         if (!str)
497                 return 1;
498
499         str = strchr(p, '=');
500         if (!str)
501                 return 1;
502
503         str++;
504
505         if (sscanf(str, "%u", val) == 1)
506                 return 0;
507
508         return 1;
509 }
510
511 static int check_strset(char *p, char *name)
512 {
513         return strncmp(p, name, strlen(name));
514 }
515
516 static int is_empty_or_comment(char *line)
517 {
518         unsigned int i;
519
520         for (i = 0; i < strlen(line); i++) {
521                 if (line[i] == ';')
522                         return 1;
523                 if (!isspace(line[i]) && !iscntrl(line[i]))
524                         return 0;
525         }
526
527         return 1;
528 }
529
530 static int str_rw_cb(struct thread_data *td, char *mem)
531 {
532         if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
533                 td->ddir = DDIR_READ;
534                 td->sequential = 1;
535                 return 0;
536         } else if (!strncmp(mem, "randread", 8)) {
537                 td->ddir = DDIR_READ;
538                 td->sequential = 0;
539                 return 0;
540         } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
541                 td->ddir = DDIR_WRITE;
542                 td->sequential = 1;
543                 return 0;
544         } else if (!strncmp(mem, "randwrite", 9)) {
545                 td->ddir = DDIR_WRITE;
546                 td->sequential = 0;
547                 return 0;
548         } else if (!strncmp(mem, "rw", 2)) {
549                 td->ddir = 0;
550                 td->iomix = 1;
551                 td->sequential = 1;
552                 return 0;
553         } else if (!strncmp(mem, "randrw", 6)) {
554                 td->ddir = 0;
555                 td->iomix = 1;
556                 td->sequential = 0;
557                 return 0;
558         }
559
560         fprintf(stderr, "bad data direction: %s\n", mem);
561         return 1;
562 }
563
564 static int str_verify_cb(struct thread_data *td, char *mem)
565 {
566         if (!strncmp(mem, "0", 1)) {
567                 td->verify = VERIFY_NONE;
568                 return 0;
569         } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
570                 td->verify = VERIFY_MD5;
571                 return 0;
572         } else if (!strncmp(mem, "crc32", 5)) {
573                 td->verify = VERIFY_CRC32;
574                 return 0;
575         }
576
577         fprintf(stderr, "bad verify type: %s\n", mem);
578         return 1;
579 }
580
581 static int str_mem_cb(struct thread_data *td, char *mem)
582 {
583         if (!strncmp(mem, "malloc", 6)) {
584                 td->mem_type = MEM_MALLOC;
585                 return 0;
586         } else if (!strncmp(mem, "shm", 3)) {
587                 td->mem_type = MEM_SHM;
588                 return 0;
589         } else if (!strncmp(mem, "mmap", 4)) {
590                 td->mem_type = MEM_MMAP;
591                 return 0;
592         }
593
594         fprintf(stderr, "bad mem type: %s\n", mem);
595         return 1;
596 }
597
598 static int str_ioengine_cb(struct thread_data *td, char *str)
599 {
600         if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
601             !strncmp(str, "libaio", 6)) {
602                 strcpy(td->io_engine_name, "libaio");
603                 td->io_engine = FIO_LIBAIO;
604                 return 0;
605         } else if (!strncmp(str, "posixaio", 8)) {
606                 strcpy(td->io_engine_name, "posixaio");
607                 td->io_engine = FIO_POSIXAIO;
608                 return 0;
609         } else if (!strncmp(str, "sync", 4)) {
610                 strcpy(td->io_engine_name, "sync");
611                 td->io_engine = FIO_SYNCIO;
612                 return 0;
613         } else if (!strncmp(str, "mmap", 4)) {
614                 strcpy(td->io_engine_name, "mmap");
615                 td->io_engine = FIO_MMAPIO;
616                 return 0;
617         } else if (!strncmp(str, "sgio", 4)) {
618                 strcpy(td->io_engine_name, "sgio");
619                 td->io_engine = FIO_SGIO;
620                 return 0;
621         } else if (!strncmp(str, "splice", 6)) {
622                 strcpy(td->io_engine_name, "splice");
623                 td->io_engine = FIO_SPLICEIO;
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                 fgetpos(f, &off);
674                 while ((p = fgets(string, 4096, f)) != NULL) {
675                         if (is_empty_or_comment(p))
676                                 continue;
677                         if (strstr(p, "["))
678                                 break;
679
680                         if (!check_int(p, "prio", &prio)) {
681 #ifndef FIO_HAVE_IOPRIO
682                                 fprintf(stderr, "io priorities not available\n");
683                                 return 1;
684 #endif
685                                 td->ioprio |= prio;
686                                 fgetpos(f, &off);
687                                 continue;
688                         }
689                         if (!check_int(p, "prioclass", &prioclass)) {
690 #ifndef FIO_HAVE_IOPRIO
691                                 fprintf(stderr, "io priorities not available\n");
692                                 return 1;
693 #endif
694                                 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
695                                 fgetpos(f, &off);
696                                 continue;
697                         }
698                         if (!check_int(p, "direct", &td->odirect)) {
699                                 fgetpos(f, &off);
700                                 continue;
701                         }
702                         if (!check_int(p, "rate", &td->rate)) {
703                                 fgetpos(f, &off);
704                                 continue;
705                         }
706                         if (!check_int(p, "ratemin", &td->ratemin)) {
707                                 fgetpos(f, &off);
708                                 continue;
709                         }
710                         if (!check_int(p, "ratecycle", &td->ratecycle)) {
711                                 fgetpos(f, &off);
712                                 continue;
713                         }
714                         if (!check_int(p, "thinktime", &td->thinktime)) {
715                                 fgetpos(f, &off);
716                                 continue;
717                         }
718                         if (!check_int(p, "cpumask", &cpu)) {
719 #ifndef FIO_HAVE_CPU_AFFINITY
720                                 fprintf(stderr, "cpu affinity not available\n");
721                                 return 1;
722 #endif
723                                 fill_cpu_mask(td->cpumask, cpu);
724                                 fgetpos(f, &off);
725                                 continue;
726                         }
727                         if (!check_int(p, "fsync", &td->fsync_blocks)) {
728                                 fgetpos(f, &off);
729                                 td->end_fsync = 1;
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, "end_fsync", &td->end_fsync)) {
765                                 fgetpos(f, &off);
766                                 continue;
767                         }
768                         if (!check_int(p, "loops", &td->loops)) {
769                                 fgetpos(f, &off);
770                                 continue;
771                         }
772                         if (!check_int(p, "numjobs", &td->numjobs)) {
773                                 fgetpos(f, &off);
774                                 continue;
775                         }
776                         if (!check_int(p, "overwrite", &td->overwrite)) {
777                                 fgetpos(f, &off);
778                                 continue;
779                         }
780                         if (!check_range(p, "bsrange", &ul1, &ul2)) {
781                                 if (ul1 > ul2) {
782                                         td->max_bs = ul1;
783                                         td->min_bs = ul2;
784                                 } else {
785                                         td->max_bs = ul2;
786                                         td->min_bs = ul1;
787                                 }
788                                 fgetpos(f, &off);
789                                 continue;
790                         }
791                         if (!check_strcnv(p, "bs", &ull)) {
792                                 td->bs = ull;
793                                 fgetpos(f, &off);
794                                 continue;
795                         }
796                         if (!check_strcnv(p, "size", &td->file_size)) {
797                                 fgetpos(f, &off);
798                                 continue;
799                         }
800                         if (!check_strcnv(p, "offset", &td->file_offset)) {
801                                 fgetpos(f, &off);
802                                 continue;
803                         }
804                         if (!check_strcnv(p, "zonesize", &td->zone_size)) {
805                                 fgetpos(f, &off);
806                                 continue;
807                         }
808                         if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
809                                 fgetpos(f, &off);
810                                 continue;
811                         }
812                         if (!check_strstore(p, "directory", td->directory)) {
813                                 fgetpos(f, &off);
814                                 continue;
815                         }
816                         if (!check_str(p, "mem", str_mem_cb, td)) {
817                                 fgetpos(f, &off);
818                                 continue;
819                         }
820                         if (!check_str(p, "verify", str_verify_cb, td)) {
821                                 fgetpos(f, &off);
822                                 continue;
823                         }
824                         if (!check_str(p, "rw", str_rw_cb, td)) {
825                                 fgetpos(f, &off);
826                                 continue;
827                         }
828                         if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
829                                 fgetpos(f, &off);
830                                 continue;
831                         }
832                         if (!check_strset(p, "create")) {
833                                 td->create_file = 1;
834                                 fgetpos(f, &off);
835                                 continue;
836                         }
837                         if (!check_strset(p, "exitall")) {
838                                 exitall_on_terminate = 1;
839                                 fgetpos(f, &off);
840                                 continue;
841                         }
842                         if (!check_strset(p, "stonewall")) {
843                                 td->stonewall = 1;
844                                 fgetpos(f, &off);
845                                 continue;
846                         }
847                         if (!check_strset(p, "thread")) {
848                                 td->use_thread = 1;
849                                 fgetpos(f, &off);
850                                 continue;
851                         }
852                         if (!check_str(p, "iolog", str_iolog_cb, td)) {
853                                 td->iolog = 1;
854                                 fgetpos(f, &off);
855                                 continue;
856                         }
857
858                         printf("Client%d: bad option %s\n",td->thread_number,p);
859                         return 1;
860                 }
861                 fsetpos(f, &off);
862
863                 if (add_job(td, name))
864                         return 1;
865         }
866
867         free(string);
868         free(name);
869         fclose(f);
870         return 0;
871 }
872
873 static int fill_def_thread(void)
874 {
875         memset(&def_thread, 0, sizeof(def_thread));
876
877         if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
878                 perror("sched_getaffinity");
879                 return 1;
880         }
881
882         /*
883          * fill globals
884          */
885         def_thread.ddir = DDIR_READ;
886         def_thread.iomix = 0;
887         def_thread.bs = DEF_BS;
888         def_thread.min_bs = -1;
889         def_thread.max_bs = -1;
890         def_thread.io_engine = DEF_IO_ENGINE;
891         strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
892         def_thread.odirect = DEF_ODIRECT;
893         def_thread.ratecycle = DEF_RATE_CYCLE;
894         def_thread.sequential = DEF_SEQUENTIAL;
895         def_thread.timeout = DEF_TIMEOUT;
896         def_thread.create_file = DEF_CREATE;
897         def_thread.overwrite = DEF_OVERWRITE;
898         def_thread.invalidate_cache = DEF_INVALIDATE;
899         def_thread.sync_io = DEF_SYNCIO;
900         def_thread.mem_type = MEM_MALLOC;
901         def_thread.bw_avg_time = DEF_BWAVGTIME;
902         def_thread.create_serialize = DEF_CREATE_SER;
903         def_thread.create_fsync = DEF_CREATE_FSYNC;
904         def_thread.loops = DEF_LOOPS;
905         def_thread.verify = DEF_VERIFY;
906         def_thread.stonewall = DEF_STONEWALL;
907         def_thread.numjobs = DEF_NUMJOBS;
908         def_thread.use_thread = DEF_USE_THREAD;
909 #ifdef FIO_HAVE_DISK_UTIL
910         def_thread.do_disk_util = 1;
911 #endif
912
913         return 0;
914 }
915
916 static void usage(char *name)
917 {
918         printf("%s\n", fio_version_string);
919         printf("\t-s IO is sequential\n");
920         printf("\t-b Block size in KiB for each IO\n");
921         printf("\t-t Runtime in seconds\n");
922         printf("\t-R Exit all threads on failure to meet rate goal\n");
923         printf("\t-o Use O_DIRECT\n");
924         printf("\t-l Generate per-job latency logs\n");
925         printf("\t-w Generate per-job bandwidth logs\n");
926         printf("\t-f Job file (Required)\n");
927         printf("\t-v Print version info and exit\n");
928 }
929
930 static void parse_cmd_line(int argc, char *argv[])
931 {
932         int c;
933
934         while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
935                 switch (c) {
936                         case 's':
937                                 def_thread.sequential = !!atoi(optarg);
938                                 break;
939                         case 'b':
940                                 def_thread.bs = atoi(optarg);
941                                 def_thread.bs <<= 10;
942                                 if (!def_thread.bs) {
943                                         printf("bad block size\n");
944                                         def_thread.bs = DEF_BS;
945                                 }
946                                 break;
947                         case 't':
948                                 def_thread.timeout = atoi(optarg);
949                                 break;
950                         case 'r':
951                                 repeatable = !!atoi(optarg);
952                                 break;
953                         case 'R':
954                                 rate_quit = !!atoi(optarg);
955                                 break;
956                         case 'o':
957                                 def_thread.odirect = !!atoi(optarg);
958                                 break;
959                         case 'f':
960                                 ini_file = strdup(optarg);
961                                 break;
962                         case 'l':
963                                 write_lat_log = 1;
964                                 break;
965                         case 'w':
966                                 write_bw_log = 1;
967                                 break;
968                         case 'h':
969                                 usage(argv[0]);
970                                 exit(0);
971                         case 'v':
972                                 printf("%s\n", fio_version_string);
973                                 exit(0);
974                 }
975         }
976
977         if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
978                 ini_file = strdup(argv[argc - 1]);
979 }
980
981 static void free_shm(void)
982 {
983         struct shmid_ds sbuf;
984
985         if (threads) {
986                 shmdt(threads);
987                 threads = NULL;
988                 shmctl(shm_id, IPC_RMID, &sbuf);
989         }
990 }
991
992 static int setup_thread_area(void)
993 {
994         /*
995          * 1024 is too much on some machines, scale max_jobs if
996          * we get a failure that looks like too large a shm segment
997          */
998         do {
999                 int s = max_jobs * sizeof(struct thread_data);
1000
1001                 shm_id = shmget(0, s, IPC_CREAT | 0600);
1002                 if (shm_id != -1)
1003                         break;
1004                 if (errno != EINVAL) {
1005                         perror("shmget");
1006                         break;
1007                 }
1008
1009                 max_jobs >>= 1;
1010         } while (max_jobs);
1011
1012         if (shm_id == -1)
1013                 return 1;
1014
1015         threads = shmat(shm_id, NULL, 0);
1016         if (threads == (void *) -1) {
1017                 perror("shmat");
1018                 return 1;
1019         }
1020
1021         atexit(free_shm);
1022         return 0;
1023 }
1024
1025 int parse_options(int argc, char *argv[])
1026 {
1027         if (setup_thread_area())
1028                 return 1;
1029         if (fill_def_thread())
1030                 return 1;
1031
1032         parse_cmd_line(argc, argv);
1033
1034         if (!ini_file) {
1035                 printf("Need job file\n");
1036                 usage(argv[0]);
1037                 return 1;
1038         }
1039
1040         if (parse_jobs_ini(ini_file)) {
1041                 usage(argv[0]);
1042                 return 1;
1043         }
1044
1045         return 0;
1046 }