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