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