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