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