Add 'filesize' option
[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 #define FIO_RANDSEED            (0xb1899bedUL)
22
23 #define td_var_offset(var)      ((size_t) &((struct thread_data *)0)->var)
24
25 static int str_mem_cb(void *, const char *);
26 static int str_lockmem_cb(void *, unsigned long *);
27 #ifdef FIO_HAVE_IOPRIO
28 static int str_prio_cb(void *, unsigned int *);
29 static int str_prioclass_cb(void *, unsigned int *);
30 #endif
31 static int str_exitall_cb(void);
32 static int str_cpumask_cb(void *, unsigned int *);
33 static int str_fst_cb(void *, const char *);
34 static int str_filename_cb(void *, const char *);
35 static int str_directory_cb(void *, const char *);
36 static int str_opendir_cb(void *, const char *);
37
38 #define __stringify_1(x)        #x
39 #define __stringify(x)          __stringify_1(x)
40
41 /*
42  * Map of job/command line options
43  */
44 static struct fio_option options[] = {
45         {
46                 .name   = "description",
47                 .type   = FIO_OPT_STR_STORE,
48                 .off1   = td_var_offset(description),
49                 .help   = "Text job description",
50         },
51         {
52                 .name   = "name",
53                 .type   = FIO_OPT_STR_STORE,
54                 .off1   = td_var_offset(name),
55                 .help   = "Name of this job",
56         },
57         {
58                 .name   = "directory",
59                 .type   = FIO_OPT_STR_STORE,
60                 .off1   = td_var_offset(directory),
61                 .cb     = str_directory_cb,
62                 .help   = "Directory to store files in",
63         },
64         {
65                 .name   = "filename",
66                 .type   = FIO_OPT_STR_STORE,
67                 .off1   = td_var_offset(filename),
68                 .cb     = str_filename_cb,
69                 .help   = "File(s) to use for the workload",
70         },
71         {
72                 .name   = "opendir",
73                 .type   = FIO_OPT_STR_STORE,
74                 .off1   = td_var_offset(opendir),
75                 .cb     = str_opendir_cb,
76                 .help   = "Recursively add files from this directory and down",
77         },
78         {
79                 .name   = "rw",
80                 .type   = FIO_OPT_STR,
81                 .off1   = td_var_offset(td_ddir),
82                 .help   = "IO direction",
83                 .def    = "read",
84                 .posval = {
85                           { .ival = "read",
86                             .oval = TD_DDIR_READ,
87                             .help = "Sequential read",
88                           },
89                           { .ival = "write",
90                             .oval = TD_DDIR_WRITE,
91                             .help = "Sequential write",
92                           },
93                           { .ival = "randread",
94                             .oval = TD_DDIR_RANDREAD,
95                             .help = "Random read",
96                           },
97                           { .ival = "randwrite",
98                             .oval = TD_DDIR_RANDWRITE,
99                             .help = "Random write",
100                           },
101                           { .ival = "rw",
102                             .oval = TD_DDIR_RW,
103                             .help = "Sequential read and write mix",
104                           },
105                           { .ival = "randrw",
106                             .oval = TD_DDIR_RANDRW,
107                             .help = "Random read and write mix"
108                           },
109                 },
110         },
111         {
112                 .name   = "ioengine",
113                 .type   = FIO_OPT_STR_STORE,
114                 .off1   = td_var_offset(ioengine),
115                 .help   = "IO engine to use",
116                 .def    = "sync",
117                 .posval = {
118                           { .ival = "sync",
119                             .help = "Use read/write",
120                           },
121 #ifdef FIO_HAVE_LIBAIO
122                           { .ival = "libaio",
123                             .help = "Linux native asynchronous IO",
124                           },
125 #endif
126 #ifdef FIO_HAVE_POSIXAIO
127                           { .ival = "posixaio",
128                             .help = "POSIX asynchronous IO",
129                           },
130 #endif
131                           { .ival = "mmap",
132                             .help = "Memory mapped IO",
133                           },
134 #ifdef FIO_HAVE_SPLICE
135                           { .ival = "splice",
136                             .help = "splice/vmsplice based IO",
137                           },
138 #endif
139 #ifdef FIO_HAVE_SGIO
140                           { .ival = "sg",
141                             .help = "SCSI generic v3 IO",
142                           },
143 #endif
144                           { .ival = "null",
145                             .help = "Testing engine (no data transfer)",
146                           },
147                           { .ival = "net",
148                             .help = "Network IO",
149                           },
150 #ifdef FIO_HAVE_SYSLET
151                           { .ival = "syslet-rw",
152                             .help = "syslet enabled async pread/pwrite IO",
153                           },
154 #endif
155                           { .ival = "cpuio",
156                             .help = "CPU cycler burner engine",
157                           },
158                           { .ival = "external",
159                             .help = "Load external engine (append name)",
160                           },
161                 },
162         },
163         {
164                 .name   = "iodepth",
165                 .type   = FIO_OPT_INT,
166                 .off1   = td_var_offset(iodepth),
167                 .help   = "Amount of IO buffers to keep in flight",
168                 .def    = "1",
169         },
170         {
171                 .name   = "iodepth_batch",
172                 .type   = FIO_OPT_INT,
173                 .off1   = td_var_offset(iodepth_batch),
174                 .help   = "Number of IO to submit in one go",
175         },
176         {
177                 .name   = "iodepth_low",
178                 .type   = FIO_OPT_INT,
179                 .off1   = td_var_offset(iodepth_low),
180                 .help   = "Low water mark for queuing depth",
181         },
182         {
183                 .name   = "size",
184                 .type   = FIO_OPT_STR_VAL,
185                 .off1   = td_var_offset(total_file_size),
186                 .help   = "Total size of device or files",
187         },
188         {
189                 .name   = "filesize",
190                 .type   = FIO_OPT_STR_VAL,
191                 .off1   = td_var_offset(file_size_low),
192                 .off2   = td_var_offset(file_size_high),
193                 .help   = "Size of individual files",
194         },
195         {
196                 .name   = "bs",
197                 .type   = FIO_OPT_STR_VAL_INT,
198                 .off1   = td_var_offset(bs[DDIR_READ]),
199                 .off2   = td_var_offset(bs[DDIR_WRITE]),
200                 .help   = "Block size unit",
201                 .def    = "4k",
202         },
203         {
204                 .name   = "bsrange",
205                 .type   = FIO_OPT_RANGE,
206                 .off1   = td_var_offset(min_bs[DDIR_READ]),
207                 .off2   = td_var_offset(max_bs[DDIR_READ]),
208                 .off3   = td_var_offset(min_bs[DDIR_WRITE]),
209                 .off4   = td_var_offset(max_bs[DDIR_WRITE]),
210                 .help   = "Set block size range (in more detail than bs)",
211         },
212         {
213                 .name   = "bs_unaligned",
214                 .type   = FIO_OPT_STR_SET,
215                 .off1   = td_var_offset(bs_unaligned),
216                 .help   = "Don't sector align IO buffer sizes",
217         },
218         {
219                 .name   = "offset",
220                 .type   = FIO_OPT_STR_VAL,
221                 .off1   = td_var_offset(start_offset),
222                 .help   = "Start IO from this offset",
223                 .def    = "0",
224         },
225         {
226                 .name   = "randrepeat",
227                 .type   = FIO_OPT_BOOL,
228                 .off1   = td_var_offset(rand_repeatable),
229                 .help   = "Use repeatable random IO pattern",
230                 .def    = "1",
231         },
232         {
233                 .name   = "norandommap",
234                 .type   = FIO_OPT_STR_SET,
235                 .off1   = td_var_offset(norandommap),
236                 .help   = "Accept potential duplicate random blocks",
237         },
238         {
239                 .name   = "nrfiles",
240                 .type   = FIO_OPT_INT,
241                 .off1   = td_var_offset(nr_files),
242                 .help   = "Split job workload between this number of files",
243                 .def    = "1",
244         },
245         {
246                 .name   = "openfiles",
247                 .type   = FIO_OPT_INT,
248                 .off1   = td_var_offset(open_files),
249                 .help   = "Number of files to keep open at the same time",
250         },
251         {
252                 .name   = "file_service_type",
253                 .type   = FIO_OPT_STR,
254                 .cb     = str_fst_cb,
255                 .off1   = td_var_offset(file_service_type),
256                 .help   = "How to select which file to service next",
257                 .def    = "roundrobin",
258                 .posval = {
259                           { .ival = "random",
260                             .oval = FIO_FSERVICE_RANDOM,
261                             .help = "Choose a file at random",
262                           },
263                           { .ival = "roundrobin",
264                             .oval = FIO_FSERVICE_RR,
265                             .help = "Round robin select files",
266                           },
267                 },
268         },
269         {
270                 .name   = "fsync",
271                 .type   = FIO_OPT_INT,
272                 .off1   = td_var_offset(fsync_blocks),
273                 .help   = "Issue fsync for writes every given number of blocks",
274                 .def    = "0",
275         },
276         {
277                 .name   = "direct",
278                 .type   = FIO_OPT_BOOL,
279                 .off1   = td_var_offset(odirect),
280                 .help   = "Use O_DIRECT IO (negates buffered)",
281                 .def    = "0",
282         },
283         {
284                 .name   = "buffered",
285                 .type   = FIO_OPT_BOOL,
286                 .off1   = td_var_offset(odirect),
287                 .neg    = 1,
288                 .help   = "Use buffered IO (negates direct)",
289                 .def    = "1",
290         },
291         {
292                 .name   = "overwrite",
293                 .type   = FIO_OPT_BOOL,
294                 .off1   = td_var_offset(overwrite),
295                 .help   = "When writing, set whether to overwrite current data",
296                 .def    = "0",
297         },
298         {
299                 .name   = "loops",
300                 .type   = FIO_OPT_INT,
301                 .off1   = td_var_offset(loops),
302                 .help   = "Number of times to run the job",
303                 .def    = "1",
304         },
305         {
306                 .name   = "numjobs",
307                 .type   = FIO_OPT_INT,
308                 .off1   = td_var_offset(numjobs),
309                 .help   = "Duplicate this job this many times",
310                 .def    = "1",
311         },
312         {
313                 .name   = "startdelay",
314                 .type   = FIO_OPT_INT,
315                 .off1   = td_var_offset(start_delay),
316                 .help   = "Only start job when this period has passed",
317                 .def    = "0",
318         },
319         {
320                 .name   = "runtime",
321                 .alias  = "timeout",
322                 .type   = FIO_OPT_STR_VAL_TIME,
323                 .off1   = td_var_offset(timeout),
324                 .help   = "Stop workload when this amount of time has passed",
325                 .def    = "0",
326         },
327         {
328                 .name   = "mem",
329                 .type   = FIO_OPT_STR,
330                 .cb     = str_mem_cb,
331                 .off1   = td_var_offset(mem_type),
332                 .help   = "Backing type for IO buffers",
333                 .def    = "malloc",
334                 .posval = {
335                           { .ival = "malloc",
336                             .oval = MEM_MALLOC,
337                             .help = "Use malloc(3) for IO buffers",
338                           },
339                           { .ival = "shm",
340                             .oval = MEM_SHM,
341                             .help = "Use shared memory segments for IO buffers",
342                           },
343 #ifdef FIO_HAVE_HUGETLB
344                           { .ival = "shmhuge",
345                             .oval = MEM_SHMHUGE,
346                             .help = "Like shm, but use huge pages",
347                           },
348 #endif
349                           { .ival = "mmap",
350                             .oval = MEM_MMAP,
351                             .help = "Use mmap(2) (file or anon) for IO buffers",
352                           },
353 #ifdef FIO_HAVE_HUGETLB
354                           { .ival = "mmaphuge",
355                             .oval = MEM_MMAPHUGE,
356                             .help = "Like mmap, but use huge pages",
357                           },
358 #endif
359                   },
360         },
361         {
362                 .name   = "verify",
363                 .type   = FIO_OPT_STR,
364                 .off1   = td_var_offset(verify),
365                 .help   = "Verify data written",
366                 .def    = "0",
367                 .posval = {
368                           { .ival = "0",
369                             .oval = VERIFY_NONE,
370                             .help = "Don't do IO verification",
371                           },
372                           { .ival = "crc32",
373                             .oval = VERIFY_CRC32,
374                             .help = "Use crc32 checksums for verification",
375                           },
376                           { .ival = "md5",
377                             .oval = VERIFY_MD5,
378                             .help = "Use md5 checksums for verification",
379                           },
380                 },
381         },
382         {
383                 .name   = "write_iolog",
384                 .type   = FIO_OPT_STR_STORE,
385                 .off1   = td_var_offset(write_iolog_file),
386                 .help   = "Store IO pattern to file",
387         },
388         {
389                 .name   = "read_iolog",
390                 .type   = FIO_OPT_STR_STORE,
391                 .off1   = td_var_offset(read_iolog_file),
392                 .help   = "Playback IO pattern from file",
393         },
394         {
395                 .name   = "exec_prerun",
396                 .type   = FIO_OPT_STR_STORE,
397                 .off1   = td_var_offset(exec_prerun),
398                 .help   = "Execute this file prior to running job",
399         },
400         {
401                 .name   = "exec_postrun",
402                 .type   = FIO_OPT_STR_STORE,
403                 .off1   = td_var_offset(exec_postrun),
404                 .help   = "Execute this file after running job",
405         },
406 #ifdef FIO_HAVE_IOSCHED_SWITCH
407         {
408                 .name   = "ioscheduler",
409                 .type   = FIO_OPT_STR_STORE,
410                 .off1   = td_var_offset(ioscheduler),
411                 .help   = "Use this IO scheduler on the backing device",
412         },
413 #endif
414         {
415                 .name   = "zonesize",
416                 .type   = FIO_OPT_STR_VAL,
417                 .off1   = td_var_offset(zone_size),
418                 .help   = "Give size of an IO zone",
419                 .def    = "0",
420         },
421         {
422                 .name   = "zoneskip",
423                 .type   = FIO_OPT_STR_VAL,
424                 .off1   = td_var_offset(zone_skip),
425                 .help   = "Space between IO zones",
426                 .def    = "0",
427         },
428         {
429                 .name   = "lockmem",
430                 .type   = FIO_OPT_STR_VAL,
431                 .cb     = str_lockmem_cb,
432                 .help   = "Lock down this amount of memory",
433                 .def    = "0",
434         },
435         {
436                 .name   = "rwmixcycle",
437                 .type   = FIO_OPT_INT,
438                 .off1   = td_var_offset(rwmixcycle),
439                 .help   = "Cycle period for mixed read/write workloads (msec)",
440                 .def    = "500",
441         },
442         {
443                 .name   = "rwmixread",
444                 .type   = FIO_OPT_INT,
445                 .off1   = td_var_offset(rwmixread),
446                 .maxval = 100,
447                 .help   = "Percentage of mixed workload that is reads",
448                 .def    = "50",
449         },
450         {
451                 .name   = "rwmixwrite",
452                 .type   = FIO_OPT_INT,
453                 .off1   = td_var_offset(rwmixwrite),
454                 .maxval = 100,
455                 .help   = "Percentage of mixed workload that is writes",
456                 .def    = "50",
457         },
458         {
459                 .name   = "nice",
460                 .type   = FIO_OPT_INT,
461                 .off1   = td_var_offset(nice),
462                 .help   = "Set job CPU nice value",
463                 .minval = -19,
464                 .maxval = 20,
465                 .def    = "0",
466         },
467 #ifdef FIO_HAVE_IOPRIO
468         {
469                 .name   = "prio",
470                 .type   = FIO_OPT_INT,
471                 .cb     = str_prio_cb,
472                 .help   = "Set job IO priority value",
473                 .minval = 0,
474                 .maxval = 7,
475         },
476         {
477                 .name   = "prioclass",
478                 .type   = FIO_OPT_INT,
479                 .cb     = str_prioclass_cb,
480                 .help   = "Set job IO priority class",
481                 .minval = 0,
482                 .maxval = 3,
483         },
484 #endif
485         {
486                 .name   = "thinktime",
487                 .type   = FIO_OPT_INT,
488                 .off1   = td_var_offset(thinktime),
489                 .help   = "Idle time between IO buffers (usec)",
490                 .def    = "0",
491         },
492         {
493                 .name   = "thinktime_spin",
494                 .type   = FIO_OPT_INT,
495                 .off1   = td_var_offset(thinktime_spin),
496                 .help   = "Start think time by spinning this amount (usec)",
497                 .def    = "0",
498         },
499         {
500                 .name   = "thinktime_blocks",
501                 .type   = FIO_OPT_INT,
502                 .off1   = td_var_offset(thinktime_blocks),
503                 .help   = "IO buffer period between 'thinktime'",
504                 .def    = "1",
505         },
506         {
507                 .name   = "rate",
508                 .type   = FIO_OPT_INT,
509                 .off1   = td_var_offset(rate),
510                 .help   = "Set bandwidth rate",
511         },
512         {
513                 .name   = "ratemin",
514                 .type   = FIO_OPT_INT,
515                 .off1   = td_var_offset(ratemin),
516                 .help   = "The bottom limit accepted",
517         },
518         {
519                 .name   = "ratecycle",
520                 .type   = FIO_OPT_INT,
521                 .off1   = td_var_offset(ratecycle),
522                 .help   = "Window average for rate limits (msec)",
523                 .def    = "1000",
524         },
525         {
526                 .name   = "invalidate",
527                 .type   = FIO_OPT_BOOL,
528                 .off1   = td_var_offset(invalidate_cache),
529                 .help   = "Invalidate buffer/page cache prior to running job",
530                 .def    = "1",
531         },
532         {
533                 .name   = "sync",
534                 .type   = FIO_OPT_BOOL,
535                 .off1   = td_var_offset(sync_io),
536                 .help   = "Use O_SYNC for buffered writes",
537                 .def    = "0",
538         },
539         {
540                 .name   = "bwavgtime",
541                 .type   = FIO_OPT_INT,
542                 .off1   = td_var_offset(bw_avg_time),
543                 .help   = "Time window over which to calculate bandwidth (msec)",
544                 .def    = "500",
545         },
546         {
547                 .name   = "create_serialize",
548                 .type   = FIO_OPT_BOOL,
549                 .off1   = td_var_offset(create_serialize),
550                 .help   = "Serialize creating of job files",
551                 .def    = "1",
552         },
553         {
554                 .name   = "create_fsync",
555                 .type   = FIO_OPT_BOOL,
556                 .off1   = td_var_offset(create_fsync),
557                 .help   = "Fsync file after creation",
558                 .def    = "1",
559         },
560         {
561                 .name   = "cpuload",
562                 .type   = FIO_OPT_INT,
563                 .off1   = td_var_offset(cpuload),
564                 .help   = "Use this percentage of CPU",
565         },
566         {
567                 .name   = "cpuchunks",
568                 .type   = FIO_OPT_INT,
569                 .off1   = td_var_offset(cpucycle),
570                 .help   = "Length of the CPU burn cycles (usecs)",
571                 .def    = "50000",
572         },
573 #ifdef FIO_HAVE_CPU_AFFINITY
574         {
575                 .name   = "cpumask",
576                 .type   = FIO_OPT_INT,
577                 .cb     = str_cpumask_cb,
578                 .help   = "CPU affinity mask",
579         },
580 #endif
581         {
582                 .name   = "end_fsync",
583                 .type   = FIO_OPT_BOOL,
584                 .off1   = td_var_offset(end_fsync),
585                 .help   = "Include fsync at the end of job",
586                 .def    = "0",
587         },
588         {
589                 .name   = "fsync_on_close",
590                 .type   = FIO_OPT_BOOL,
591                 .off1   = td_var_offset(fsync_on_close),
592                 .help   = "fsync files on close",
593                 .def    = "0",
594         },
595         {
596                 .name   = "unlink",
597                 .type   = FIO_OPT_BOOL,
598                 .off1   = td_var_offset(unlink),
599                 .help   = "Unlink created files after job has completed",
600                 .def    = "0",
601         },
602         {
603                 .name   = "exitall",
604                 .type   = FIO_OPT_STR_SET,
605                 .cb     = str_exitall_cb,
606                 .help   = "Terminate all jobs when one exits",
607         },
608         {
609                 .name   = "stonewall",
610                 .type   = FIO_OPT_STR_SET,
611                 .off1   = td_var_offset(stonewall),
612                 .help   = "Insert a hard barrier between this job and previous",
613         },
614         {
615                 .name   = "thread",
616                 .type   = FIO_OPT_STR_SET,
617                 .off1   = td_var_offset(use_thread),
618                 .help   = "Use threads instead of forks",
619         },
620         {
621                 .name   = "write_bw_log",
622                 .type   = FIO_OPT_STR_SET,
623                 .off1   = td_var_offset(write_bw_log),
624                 .help   = "Write log of bandwidth during run",
625         },
626         {
627                 .name   = "write_lat_log",
628                 .type   = FIO_OPT_STR_SET,
629                 .off1   = td_var_offset(write_lat_log),
630                 .help   = "Write log of latency during run",
631         },
632         {
633                 .name   = "hugepage-size",
634                 .type   = FIO_OPT_STR_VAL,
635                 .off1   = td_var_offset(hugepage_size),
636                 .help   = "When using hugepages, specify size of each page",
637                 .def    = __stringify(FIO_HUGE_PAGE),
638         },
639         {
640                 .name   = "group_reporting",
641                 .type   = FIO_OPT_STR_SET,
642                 .off1   = td_var_offset(group_reporting),
643                 .help   = "Do reporting on a per-group basis",
644         },
645         {
646                 .name = NULL,
647         },
648 };
649
650 #define FIO_JOB_OPTS    (sizeof(options) / sizeof(struct fio_option))
651 #define FIO_CMD_OPTS    (16)
652 #define FIO_GETOPT_JOB  (0x89988998)
653
654 /*
655  * Command line options. These will contain the above, plus a few
656  * extra that only pertain to fio itself and not jobs.
657  */
658 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
659         {
660                 .name           = "output",
661                 .has_arg        = required_argument,
662                 .val            = 'o',
663         },
664         {
665                 .name           = "timeout",
666                 .has_arg        = required_argument,
667                 .val            = 't',
668         },
669         {
670                 .name           = "latency-log",
671                 .has_arg        = required_argument,
672                 .val            = 'l',
673         },
674         {
675                 .name           = "bandwidth-log",
676                 .has_arg        = required_argument,
677                 .val            = 'b',
678         },
679         {
680                 .name           = "minimal",
681                 .has_arg        = optional_argument,
682                 .val            = 'm',
683         },
684         {
685                 .name           = "version",
686                 .has_arg        = no_argument,
687                 .val            = 'v',
688         },
689         {
690                 .name           = "help",
691                 .has_arg        = no_argument,
692                 .val            = 'h',
693         },
694         {
695                 .name           = "cmdhelp",
696                 .has_arg        = optional_argument,
697                 .val            = 'c',
698         },
699         {
700                 .name           = NULL,
701         },
702 };
703
704 static int def_timeout = 0;
705
706 static char fio_version_string[] = "fio 1.14a";
707
708 static char **ini_file;
709 static int max_jobs = MAX_JOBS;
710
711 struct thread_data def_thread;
712 struct thread_data *threads = NULL;
713
714 int exitall_on_terminate = 0;
715 int terse_output = 0;
716 unsigned long long mlock_size = 0;
717 FILE *f_out = NULL;
718 FILE *f_err = NULL;
719
720 static int write_lat_log = 0;
721 int write_bw_log = 0;
722
723 static int prev_group_jobs;
724
725 FILE *get_f_out()
726 {
727         return f_out;
728 }
729
730 FILE *get_f_err()
731 {
732         return f_err;
733 }
734
735 /*
736  * Return a free job structure.
737  */
738 static struct thread_data *get_new_job(int global, struct thread_data *parent)
739 {
740         struct thread_data *td;
741
742         if (global)
743                 return &def_thread;
744         if (thread_number >= max_jobs)
745                 return NULL;
746
747         td = &threads[thread_number++];
748         *td = *parent;
749
750         td->thread_number = thread_number;
751         return td;
752 }
753
754 static void put_job(struct thread_data *td)
755 {
756         if (td == &def_thread)
757                 return;
758
759         if (td->error)
760                 fprintf(f_out, "fio: %s\n", td->verror);
761
762         memset(&threads[td->thread_number - 1], 0, sizeof(*td));
763         thread_number--;
764 }
765
766 /*
767  * Lazy way of fixing up options that depend on each other. We could also
768  * define option callback handlers, but this is easier.
769  */
770 static void fixup_options(struct thread_data *td)
771 {
772         if (!td->rwmixread && td->rwmixwrite)
773                 td->rwmixread = 100 - td->rwmixwrite;
774
775         if (td->write_iolog_file && td->read_iolog_file) {
776                 log_err("fio: read iolog overrides write_iolog\n");
777                 free(td->write_iolog_file);
778                 td->write_iolog_file = NULL;
779         }
780
781         if (td->io_ops->flags & FIO_SYNCIO)
782                 td->iodepth = 1;
783         else {
784                 if (!td->iodepth)
785                         td->iodepth = td->open_files;
786         }
787
788         /*
789          * only really works for sequential io for now, and with 1 file
790          */
791         if (td->zone_size && td_random(td) && td->open_files == 1)
792                 td->zone_size = 0;
793
794         /*
795          * Reads can do overwrites, we always need to pre-create the file
796          */
797         if (td_read(td) || td_rw(td))
798                 td->overwrite = 1;
799
800         if (!td->min_bs[DDIR_READ])
801                 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
802         if (!td->max_bs[DDIR_READ])
803                 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
804         if (!td->min_bs[DDIR_WRITE])
805                 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
806         if (!td->max_bs[DDIR_WRITE])
807                 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
808
809         td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
810
811         if (!td->file_size_high)
812                 td->file_size_high = td->file_size_low;
813
814         if (td_read(td) && !td_rw(td))
815                 td->verify = 0;
816
817         if (td->norandommap && td->verify != VERIFY_NONE) {
818                 log_err("fio: norandommap given, verify disabled\n");
819                 td->verify = VERIFY_NONE;
820         }
821         if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
822                 log_err("fio: bs_unaligned may not work with raw io\n");
823
824         /*
825          * thinktime_spin must be less than thinktime
826          */
827         if (td->thinktime_spin > td->thinktime)
828                 td->thinktime_spin = td->thinktime;
829
830         /*
831          * The low water mark cannot be bigger than the iodepth
832          */
833         if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
834                 /*
835                  * syslet work around - if the workload is sequential,
836                  * we want to let the queue drain all the way down to
837                  * avoid seeking between async threads
838                  */
839                 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
840                         td->iodepth_low = 1;
841                 else
842                         td->iodepth_low = td->iodepth;
843         }
844
845         /*
846          * If batch number isn't set, default to the same as iodepth
847          */
848         if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
849                 td->iodepth_batch = td->iodepth;
850
851         if (td->nr_files > td->files_index)
852                 td->nr_files = td->files_index;
853
854         if (td->open_files > td->nr_files || !td->open_files)
855                 td->open_files = td->nr_files;
856 }
857
858 /*
859  * This function leaks the buffer
860  */
861 static char *to_kmg(unsigned int val)
862 {
863         char *buf = malloc(32);
864         char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
865         char *p = post;
866
867         do {
868                 if (val & 1023)
869                         break;
870
871                 val >>= 10;
872                 p++;
873         } while (*p);
874
875         snprintf(buf, 31, "%u%c", val, *p);
876         return buf;
877 }
878
879 /* External engines are specified by "external:name.o") */
880 static const char *get_engine_name(const char *str)
881 {
882         char *p = strstr(str, ":");
883
884         if (!p)
885                 return str;
886
887         p++;
888         strip_blank_front(&p);
889         strip_blank_end(p);
890         return p;
891 }
892
893 static int exists_and_not_file(const char *filename)
894 {
895         struct stat sb;
896
897         if (lstat(filename, &sb) == -1)
898                 return 0;
899
900         if (S_ISREG(sb.st_mode))
901                 return 0;
902
903         return 1;
904 }
905
906 /*
907  * Initialize the various random states we need (random io, block size ranges,
908  * read/write mix, etc).
909  */
910 static int init_random_state(struct thread_data *td)
911 {
912         unsigned long seeds[6];
913         int fd, num_maps, blocks;
914         struct fio_file *f;
915         unsigned int i;
916
917         fd = open("/dev/urandom", O_RDONLY);
918         if (fd == -1) {
919                 td_verror(td, errno, "open");
920                 return 1;
921         }
922
923         if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
924                 td_verror(td, EIO, "read");
925                 close(fd);
926                 return 1;
927         }
928
929         close(fd);
930
931         os_random_seed(seeds[0], &td->bsrange_state);
932         os_random_seed(seeds[1], &td->verify_state);
933         os_random_seed(seeds[2], &td->rwmix_state);
934
935         if (td->file_service_type == FIO_FSERVICE_RANDOM)
936                 os_random_seed(seeds[3], &td->next_file_state);
937
938         os_random_seed(seeds[5], &td->file_size_state);
939
940         if (!td_random(td))
941                 return 0;
942
943         if (td->rand_repeatable)
944                 seeds[4] = FIO_RANDSEED * td->thread_number;
945
946         if (!td->norandommap) {
947                 for_each_file(td, f, i) {
948                         blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
949                         num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
950                         f->file_map = malloc(num_maps * sizeof(long));
951                         if (!f->file_map) {
952                                 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
953                                 return 1;
954                         }
955                         f->num_maps = num_maps;
956                         memset(f->file_map, 0, num_maps * sizeof(long));
957                 }
958         }
959
960         os_random_seed(seeds[4], &td->random_state);
961         return 0;
962 }
963
964
965 /*
966  * Adds a job to the list of things todo. Sanitizes the various options
967  * to make sure we don't have conflicts, and initializes various
968  * members of td.
969  */
970 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
971 {
972         const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
973                                    "randread", "randwrite", "randrw" };
974         unsigned int i;
975         struct fio_file *f;
976         const char *engine;
977         char fname[PATH_MAX];
978         int numjobs, file_alloced;
979
980         /*
981          * the def_thread is just for options, it's not a real job
982          */
983         if (td == &def_thread)
984                 return 0;
985
986         engine = get_engine_name(td->ioengine);
987         td->io_ops = load_ioengine(td, engine);
988         if (!td->io_ops) {
989                 log_err("fio: failed to load engine %s\n", engine);
990                 return 1;
991         }
992
993         if (td->use_thread)
994                 nr_thread++;
995         else
996                 nr_process++;
997
998         if (td->odirect)
999                 td->io_ops->flags |= FIO_RAWIO;
1000
1001         file_alloced = 0;
1002         if (!td->filename && !td->files_index) {
1003                 file_alloced = 1;
1004
1005                 if (td->nr_files == 1 && exists_and_not_file(jobname))
1006                         add_file(td, jobname);
1007                 else {
1008                         for (i = 0; i < td->nr_files; i++) {
1009                                 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
1010                                 add_file(td, fname);
1011                         }
1012                 }
1013         }
1014
1015         fixup_options(td);
1016
1017         for_each_file(td, f, i) {
1018                 if (td->directory && f->filetype == FIO_TYPE_FILE) {
1019                         sprintf(fname, "%s/%s", td->directory, f->file_name);
1020                         f->file_name = strdup(fname);
1021                 }
1022         }
1023                 
1024         td->mutex = fio_sem_init(0);
1025
1026         td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
1027         td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
1028         td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
1029
1030         if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
1031                 prev_group_jobs = 0;
1032                 groupid++;
1033         }
1034
1035         td->groupid = groupid;
1036         prev_group_jobs++;
1037
1038         if (init_random_state(td))
1039                 goto err;
1040
1041         if (setup_rate(td))
1042                 goto err;
1043
1044         if (td->write_lat_log) {
1045                 setup_log(&td->ts.slat_log);
1046                 setup_log(&td->ts.clat_log);
1047         }
1048         if (td->write_bw_log)
1049                 setup_log(&td->ts.bw_log);
1050
1051         if (!td->name)
1052                 td->name = strdup(jobname);
1053
1054         if (!terse_output) {
1055                 if (!job_add_num) {
1056                         if (!strcmp(td->io_ops->name, "cpuio"))
1057                                 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
1058                         else {
1059                                 char *c1, *c2, *c3, *c4;
1060
1061                                 c1 = to_kmg(td->min_bs[DDIR_READ]);
1062                                 c2 = to_kmg(td->max_bs[DDIR_READ]);
1063                                 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
1064                                 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
1065
1066                                 fprintf(f_out, "%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[td->td_ddir], c1, c2, c3, c4, td->io_ops->name, td->iodepth);
1067
1068                                 free(c1);
1069                                 free(c2);
1070                                 free(c3);
1071                                 free(c4);
1072                         }
1073                 } else if (job_add_num == 1)
1074                         fprintf(f_out, "...\n");
1075         }
1076
1077         /*
1078          * recurse add identical jobs, clear numjobs and stonewall options
1079          * as they don't apply to sub-jobs
1080          */
1081         numjobs = td->numjobs;
1082         while (--numjobs) {
1083                 struct thread_data *td_new = get_new_job(0, td);
1084
1085                 if (!td_new)
1086                         goto err;
1087
1088                 td_new->numjobs = 1;
1089                 td_new->stonewall = 0;
1090
1091                 if (file_alloced) {
1092                         td_new->filename = NULL;
1093                         td_new->files_index = 0;
1094                         td_new->files = NULL;
1095                 }
1096
1097                 job_add_num = numjobs - 1;
1098
1099                 if (add_job(td_new, jobname, job_add_num))
1100                         goto err;
1101         }
1102
1103         if (td->numjobs > 1) {
1104                 groupid++;
1105                 prev_group_jobs = 0;
1106         }
1107
1108         return 0;
1109 err:
1110         put_job(td);
1111         return -1;
1112 }
1113
1114 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
1115 {
1116 #ifdef FIO_HAVE_CPU_AFFINITY
1117         unsigned int i;
1118
1119         CPU_ZERO(&cpumask);
1120
1121         for (i = 0; i < sizeof(int) * 8; i++) {
1122                 if ((1 << i) & cpu)
1123                         CPU_SET(i, &cpumask);
1124         }
1125 #endif
1126 }
1127
1128 static int is_empty_or_comment(char *line)
1129 {
1130         unsigned int i;
1131
1132         for (i = 0; i < strlen(line); i++) {
1133                 if (line[i] == ';')
1134                         return 1;
1135                 if (line[i] == '#')
1136                         return 1;
1137                 if (!isspace(line[i]) && !iscntrl(line[i]))
1138                         return 0;
1139         }
1140
1141         return 1;
1142 }
1143
1144 /*
1145  * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
1146  */
1147 static char *get_opt_postfix(const char *str)
1148 {
1149         char *p = strstr(str, ":");
1150
1151         if (!p)
1152                 return NULL;
1153
1154         p++;
1155         strip_blank_front(&p);
1156         strip_blank_end(p);
1157         return strdup(p);
1158 }
1159
1160 static int str_mem_cb(void *data, const char *mem)
1161 {
1162         struct thread_data *td = data;
1163
1164         if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
1165                 td->mmapfile = get_opt_postfix(mem);
1166                 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
1167                         log_err("fio: mmaphuge:/path/to/file\n");
1168                         return 1;
1169                 }
1170         }
1171
1172         return 0;
1173 }
1174
1175 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1176 {
1177         mlock_size = *val;
1178         return 0;
1179 }
1180
1181 #ifdef FIO_HAVE_IOPRIO
1182 static int str_prioclass_cb(void *data, unsigned int *val)
1183 {
1184         struct thread_data *td = data;
1185
1186         td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1187         return 0;
1188 }
1189
1190 static int str_prio_cb(void *data, unsigned int *val)
1191 {
1192         struct thread_data *td = data;
1193
1194         td->ioprio |= *val;
1195         return 0;
1196 }
1197 #endif
1198
1199 static int str_exitall_cb(void)
1200 {
1201         exitall_on_terminate = 1;
1202         return 0;
1203 }
1204
1205 static int str_cpumask_cb(void *data, unsigned int *val)
1206 {
1207         struct thread_data *td = data;
1208
1209         fill_cpu_mask(td->cpumask, *val);
1210         return 0;
1211 }
1212
1213 static int str_fst_cb(void *data, const char *str)
1214 {
1215         struct thread_data *td = data;
1216         char *nr = get_opt_postfix(str);
1217
1218         td->file_service_nr = 1;
1219         if (nr)
1220                 td->file_service_nr = atoi(nr);
1221
1222         return 0;
1223 }
1224
1225 static int str_filename_cb(void *data, const char *input)
1226 {
1227         struct thread_data *td = data;
1228         char *fname, *str, *p;
1229
1230         p = str = strdup(input);
1231
1232         strip_blank_front(&str);
1233         strip_blank_end(str);
1234
1235         if (!td->files_index)
1236                 td->nr_files = 0;
1237
1238         while ((fname = strsep(&str, ":")) != NULL) {
1239                 if (!strlen(fname))
1240                         break;
1241                 add_file(td, fname);
1242                 td->nr_files++;
1243         }
1244
1245         free(p);
1246         return 0;
1247 }
1248
1249 static int str_directory_cb(void *data, const char fio_unused *str)
1250 {
1251         struct thread_data *td = data;
1252         struct stat sb;
1253
1254         if (lstat(td->directory, &sb) < 0) {
1255                 log_err("fio: %s is not a directory\n", td->directory);
1256                 td_verror(td, errno, "lstat");
1257                 return 1;
1258         }
1259         if (!S_ISDIR(sb.st_mode)) {
1260                 log_err("fio: %s is not a directory\n", td->directory);
1261                 return 1;
1262         }
1263
1264         return 0;
1265 }
1266
1267 static int str_opendir_cb(void *data, const char fio_unused *str)
1268 {
1269         struct thread_data *td = data;
1270
1271         if (!td->files_index)
1272                 td->nr_files = 0;
1273
1274         return add_dir_files(td, td->opendir);
1275 }
1276
1277 /*
1278  * This is our [ini] type file parser.
1279  */
1280 static int parse_jobs_ini(char *file, int stonewall_flag)
1281 {
1282         unsigned int global;
1283         struct thread_data *td;
1284         char *string, *name;
1285         fpos_t off;
1286         FILE *f;
1287         char *p;
1288         int ret = 0, stonewall;
1289
1290         f = fopen(file, "r");
1291         if (!f) {
1292                 perror("fopen job file");
1293                 return 1;
1294         }
1295
1296         string = malloc(4096);
1297         name = malloc(256);
1298         memset(name, 0, 256);
1299
1300         stonewall = stonewall_flag;
1301         do {
1302                 p = fgets(string, 4095, f);
1303                 if (!p)
1304                         break;
1305                 if (is_empty_or_comment(p))
1306                         continue;
1307                 if (sscanf(p, "[%255s]", name) != 1)
1308                         continue;
1309
1310                 global = !strncmp(name, "global", 6);
1311
1312                 name[strlen(name) - 1] = '\0';
1313
1314                 td = get_new_job(global, &def_thread);
1315                 if (!td) {
1316                         ret = 1;
1317                         break;
1318                 }
1319
1320                 /*
1321                  * Seperate multiple job files by a stonewall
1322                  */
1323                 if (!global && stonewall) {
1324                         td->stonewall = stonewall;
1325                         stonewall = 0;
1326                 }
1327
1328                 fgetpos(f, &off);
1329                 while ((p = fgets(string, 4096, f)) != NULL) {
1330                         if (is_empty_or_comment(p))
1331                                 continue;
1332
1333                         strip_blank_front(&p);
1334
1335                         if (p[0] == '[')
1336                                 break;
1337
1338                         strip_blank_end(p);
1339
1340                         fgetpos(f, &off);
1341
1342                         /*
1343                          * Don't break here, continue parsing options so we
1344                          * dump all the bad ones. Makes trial/error fixups
1345                          * easier on the user.
1346                          */
1347                         ret |= parse_option(p, options, td);
1348                 }
1349
1350                 if (!ret) {
1351                         fsetpos(f, &off);
1352                         ret = add_job(td, name, 0);
1353                 } else {
1354                         log_err("fio: job %s dropped\n", name);
1355                         put_job(td);
1356                 }
1357         } while (!ret);
1358
1359         free(string);
1360         free(name);
1361         fclose(f);
1362         return ret;
1363 }
1364
1365 static int fill_def_thread(void)
1366 {
1367         memset(&def_thread, 0, sizeof(def_thread));
1368
1369         if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1370                 perror("sched_getaffinity");
1371                 return 1;
1372         }
1373
1374         /*
1375          * fill default options
1376          */
1377         fill_default_options(&def_thread, options);
1378
1379         def_thread.timeout = def_timeout;
1380         def_thread.write_bw_log = write_bw_log;
1381         def_thread.write_lat_log = write_lat_log;
1382
1383 #ifdef FIO_HAVE_DISK_UTIL
1384         def_thread.do_disk_util = 1;
1385 #endif
1386
1387         return 0;
1388 }
1389
1390 static void usage(void)
1391 {
1392         printf("%s\n", fio_version_string);
1393         printf("\t--output\tWrite output to file\n");
1394         printf("\t--timeout\tRuntime in seconds\n");
1395         printf("\t--latency-log\tGenerate per-job latency logs\n");
1396         printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1397         printf("\t--minimal\tMinimal (terse) output\n");
1398         printf("\t--version\tPrint version info and exit\n");
1399         printf("\t--help\t\tPrint this page\n");
1400         printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
1401 }
1402
1403 static int parse_cmd_line(int argc, char *argv[])
1404 {
1405         struct thread_data *td = NULL;
1406         int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
1407
1408         while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
1409                 switch (c) {
1410                 case 't':
1411                         def_timeout = atoi(optarg);
1412                         break;
1413                 case 'l':
1414                         write_lat_log = 1;
1415                         break;
1416                 case 'w':
1417                         write_bw_log = 1;
1418                         break;
1419                 case 'o':
1420                         f_out = fopen(optarg, "w+");
1421                         if (!f_out) {
1422                                 perror("fopen output");
1423                                 exit(1);
1424                         }
1425                         f_err = f_out;
1426                         break;
1427                 case 'm':
1428                         terse_output = 1;
1429                         break;
1430                 case 'h':
1431                         usage();
1432                         exit(0);
1433                 case 'c':
1434                         ret = show_cmd_help(options, optarg);
1435                         exit(ret);
1436                 case 'v':
1437                         printf("%s\n", fio_version_string);
1438                         exit(0);
1439                 case FIO_GETOPT_JOB: {
1440                         const char *opt = long_options[lidx].name;
1441                         char *val = optarg;
1442
1443                         if (!strncmp(opt, "name", 4) && td) {
1444                                 ret = add_job(td, td->name ?: "fio", 0);
1445                                 if (ret) {
1446                                         put_job(td);
1447                                         return 0;
1448                                 }
1449                                 td = NULL;
1450                         }
1451                         if (!td) {
1452                                 int global = !strncmp(val, "global", 6);
1453
1454                                 td = get_new_job(global, &def_thread);
1455                                 if (!td)
1456                                         return 0;
1457                         }
1458
1459                         ret = parse_cmd_option(opt, val, options, td);
1460                         if (ret)
1461                                 dont_add_job = 1;
1462                         break;
1463                 }
1464                 default:
1465                         break;
1466                 }
1467         }
1468
1469         if (td) {
1470                 if (dont_add_job)
1471                         put_job(td);
1472                 else {
1473                         ret = add_job(td, td->name ?: "fio", 0);
1474                         if (ret)
1475                                 put_job(td);
1476                 }
1477         }
1478
1479         while (optind < argc) {
1480                 ini_idx++;
1481                 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1482                 ini_file[ini_idx - 1] = strdup(argv[optind]);
1483                 optind++;
1484         }
1485
1486         return ini_idx;
1487 }
1488
1489 static void free_shm(void)
1490 {
1491         struct shmid_ds sbuf;
1492
1493         if (threads) {
1494                 shmdt((void *) threads);
1495                 threads = NULL;
1496                 shmctl(shm_id, IPC_RMID, &sbuf);
1497         }
1498 }
1499
1500 /*
1501  * The thread area is shared between the main process and the job
1502  * threads/processes. So setup a shared memory segment that will hold
1503  * all the job info.
1504  */
1505 static int setup_thread_area(void)
1506 {
1507         /*
1508          * 1024 is too much on some machines, scale max_jobs if
1509          * we get a failure that looks like too large a shm segment
1510          */
1511         do {
1512                 size_t size = max_jobs * sizeof(struct thread_data);
1513
1514                 shm_id = shmget(0, size, IPC_CREAT | 0600);
1515                 if (shm_id != -1)
1516                         break;
1517                 if (errno != EINVAL) {
1518                         perror("shmget");
1519                         break;
1520                 }
1521
1522                 max_jobs >>= 1;
1523         } while (max_jobs);
1524
1525         if (shm_id == -1)
1526                 return 1;
1527
1528         threads = shmat(shm_id, NULL, 0);
1529         if (threads == (void *) -1) {
1530                 perror("shmat");
1531                 return 1;
1532         }
1533
1534         atexit(free_shm);
1535         return 0;
1536 }
1537
1538 /*
1539  * Copy the fio options into the long options map, so we mirror
1540  * job and cmd line options.
1541  */
1542 static void dupe_job_options(void)
1543 {
1544         struct fio_option *o;
1545         unsigned int i;
1546
1547         i = 0;
1548         while (long_options[i].name)
1549                 i++;
1550
1551         o = &options[0];
1552         while (o->name) {
1553                 long_options[i].name = o->name;
1554                 long_options[i].val = FIO_GETOPT_JOB;
1555                 if (o->type == FIO_OPT_STR_SET)
1556                         long_options[i].has_arg = no_argument;
1557                 else
1558                         long_options[i].has_arg = required_argument;
1559
1560                 i++;
1561                 o++;
1562                 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1563         }
1564 }
1565
1566 int parse_options(int argc, char *argv[])
1567 {
1568         int job_files, i;
1569
1570         f_out = stdout;
1571         f_err = stderr;
1572
1573         options_init(options);
1574
1575         dupe_job_options();
1576
1577         if (setup_thread_area())
1578                 return 1;
1579         if (fill_def_thread())
1580                 return 1;
1581
1582         job_files = parse_cmd_line(argc, argv);
1583
1584         for (i = 0; i < job_files; i++) {
1585                 if (fill_def_thread())
1586                         return 1;
1587                 if (parse_jobs_ini(ini_file[i], i))
1588                         return 1;
1589                 free(ini_file[i]);
1590         }
1591
1592         free(ini_file);
1593
1594         if (!thread_number) {
1595                 log_err("No jobs defined(s)\n");
1596                 return 1;
1597         }
1598
1599         return 0;
1600 }