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