Default to terse version 3
[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 <sys/ipc.h>
12 #include <sys/shm.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15
16 #include "fio.h"
17 #include "parse.h"
18 #include "smalloc.h"
19 #include "filehash.h"
20 #include "verify.h"
21 #include "profile.h"
22 #include "server.h"
23
24 #include "lib/getopt.h"
25
26 const char fio_version_string[] = FIO_VERSION;
27
28 #define FIO_RANDSEED            (0xb1899bedUL)
29
30 static char **ini_file;
31 static int max_jobs = FIO_MAX_JOBS;
32 static int dump_cmdline;
33 static int def_timeout;
34
35 static struct thread_data def_thread;
36 struct thread_data *threads = NULL;
37
38 int exitall_on_terminate = 0;
39 int terse_output = 0;
40 int eta_print;
41 unsigned long long mlock_size = 0;
42 FILE *f_out = NULL;
43 FILE *f_err = NULL;
44 char **job_sections = NULL;
45 int nr_job_sections = 0;
46 char *exec_profile = NULL;
47 int warnings_fatal = 0;
48 int terse_version = 3;
49 int is_backend = 0;
50 int nr_clients = 0;
51 int log_syslog = 0;
52
53 int write_bw_log = 0;
54 int read_only = 0;
55
56 static int write_lat_log;
57
58 static int prev_group_jobs;
59
60 unsigned long fio_debug = 0;
61 unsigned int fio_debug_jobno = -1;
62 unsigned int *fio_debug_jobp = NULL;
63
64 static char cmd_optstr[256];
65 static int did_arg;
66
67 const fio_fp64_t def_percentile_list[FIO_IO_U_LIST_MAX_LEN] = {
68         { .u.f  =  1.00 },
69         { .u.f  =  5.00 },
70         { .u.f  = 10.00 },
71         { .u.f  = 20.00 },
72         { .u.f  = 30.00 },
73         { .u.f  = 40.00 },
74         { .u.f  = 50.00 },
75         { .u.f  = 60.00 },
76         { .u.f  = 70.00 },
77         { .u.f  = 80.00 },
78         { .u.f  = 90.00 },
79         { .u.f  = 95.00 },
80         { .u.f  = 99.00 },
81         { .u.f  = 99.50 },
82         { .u.f  = 99.90 },
83         { .u.f  = 99.95 },
84         { .u.f  = 99.99 },
85 };
86
87 #define FIO_CLIENT_FLAG         (1 << 16)
88
89 /*
90  * Command line options. These will contain the above, plus a few
91  * extra that only pertain to fio itself and not jobs.
92  */
93 static struct option l_opts[FIO_NR_OPTIONS] = {
94         {
95                 .name           = (char *) "output",
96                 .has_arg        = required_argument,
97                 .val            = 'o' | FIO_CLIENT_FLAG,
98         },
99         {
100                 .name           = (char *) "timeout",
101                 .has_arg        = required_argument,
102                 .val            = 't' | FIO_CLIENT_FLAG,
103         },
104         {
105                 .name           = (char *) "latency-log",
106                 .has_arg        = required_argument,
107                 .val            = 'l' | FIO_CLIENT_FLAG,
108         },
109         {
110                 .name           = (char *) "bandwidth-log",
111                 .has_arg        = required_argument,
112                 .val            = 'b' | FIO_CLIENT_FLAG,
113         },
114         {
115                 .name           = (char *) "minimal",
116                 .has_arg        = optional_argument,
117                 .val            = 'm' | FIO_CLIENT_FLAG,
118         },
119         {
120                 .name           = (char *) "version",
121                 .has_arg        = no_argument,
122                 .val            = 'v' | FIO_CLIENT_FLAG,
123         },
124         {
125                 .name           = (char *) "help",
126                 .has_arg        = no_argument,
127                 .val            = 'h' | FIO_CLIENT_FLAG,
128         },
129         {
130                 .name           = (char *) "cmdhelp",
131                 .has_arg        = optional_argument,
132                 .val            = 'c' | FIO_CLIENT_FLAG,
133         },
134         {
135                 .name              = (char *) "enghelp",
136                 .has_arg        = optional_argument,
137                 .val                = 'i' | FIO_CLIENT_FLAG,
138         },
139         {
140                 .name           = (char *) "showcmd",
141                 .has_arg        = no_argument,
142                 .val            = 's' | FIO_CLIENT_FLAG,
143         },
144         {
145                 .name           = (char *) "readonly",
146                 .has_arg        = no_argument,
147                 .val            = 'r' | FIO_CLIENT_FLAG,
148         },
149         {
150                 .name           = (char *) "eta",
151                 .has_arg        = required_argument,
152                 .val            = 'e' | FIO_CLIENT_FLAG,
153         },
154         {
155                 .name           = (char *) "debug",
156                 .has_arg        = required_argument,
157                 .val            = 'd' | FIO_CLIENT_FLAG,
158         },
159         {
160                 .name           = (char *) "section",
161                 .has_arg        = required_argument,
162                 .val            = 'x' | FIO_CLIENT_FLAG,
163         },
164         {
165                 .name           = (char *) "alloc-size",
166                 .has_arg        = required_argument,
167                 .val            = 'a' | FIO_CLIENT_FLAG,
168         },
169         {
170                 .name           = (char *) "profile",
171                 .has_arg        = required_argument,
172                 .val            = 'p' | FIO_CLIENT_FLAG,
173         },
174         {
175                 .name           = (char *) "warnings-fatal",
176                 .has_arg        = no_argument,
177                 .val            = 'w' | FIO_CLIENT_FLAG,
178         },
179         {
180                 .name           = (char *) "max-jobs",
181                 .has_arg        = required_argument,
182                 .val            = 'j' | FIO_CLIENT_FLAG,
183         },
184         {
185                 .name           = (char *) "terse-version",
186                 .has_arg        = required_argument,
187                 .val            = 'V' | FIO_CLIENT_FLAG,
188         },
189         {
190                 .name           = (char *) "server",
191                 .has_arg        = optional_argument,
192                 .val            = 'S',
193         },
194         {       .name           = (char *) "daemonize",
195                 .has_arg        = required_argument,
196                 .val            = 'D',
197         },
198         {
199                 .name           = (char *) "client",
200                 .has_arg        = required_argument,
201                 .val            = 'C',
202         },
203         {
204                 .name           = NULL,
205         },
206 };
207
208 static void free_shm(void)
209 {
210         struct shmid_ds sbuf;
211
212         if (threads) {
213                 void *tp = threads;
214
215                 threads = NULL;
216                 file_hash_exit();
217                 flow_exit();
218                 fio_debug_jobp = NULL;
219                 shmdt(tp);
220                 shmctl(shm_id, IPC_RMID, &sbuf);
221         }
222
223         scleanup();
224 }
225
226 /*
227  * The thread area is shared between the main process and the job
228  * threads/processes. So setup a shared memory segment that will hold
229  * all the job info. We use the end of the region for keeping track of
230  * open files across jobs, for file sharing.
231  */
232 static int setup_thread_area(void)
233 {
234         void *hash;
235
236         if (threads)
237                 return 0;
238
239         /*
240          * 1024 is too much on some machines, scale max_jobs if
241          * we get a failure that looks like too large a shm segment
242          */
243         do {
244                 size_t size = max_jobs * sizeof(struct thread_data);
245
246                 size += file_hash_size;
247                 size += sizeof(unsigned int);
248
249                 shm_id = shmget(0, size, IPC_CREAT | 0600);
250                 if (shm_id != -1)
251                         break;
252                 if (errno != EINVAL && errno != ENOMEM) {
253                         perror("shmget");
254                         break;
255                 }
256
257                 max_jobs >>= 1;
258         } while (max_jobs);
259
260         if (shm_id == -1)
261                 return 1;
262
263         threads = shmat(shm_id, NULL, 0);
264         if (threads == (void *) -1) {
265                 perror("shmat");
266                 return 1;
267         }
268
269         memset(threads, 0, max_jobs * sizeof(struct thread_data));
270         hash = (void *) threads + max_jobs * sizeof(struct thread_data);
271         fio_debug_jobp = (void *) hash + file_hash_size;
272         *fio_debug_jobp = -1;
273         file_hash_init(hash);
274
275         flow_init();
276
277         return 0;
278 }
279
280 /*
281  * Return a free job structure.
282  */
283 static struct thread_data *get_new_job(int global, struct thread_data *parent,
284                                        int preserve_eo)
285 {
286         struct thread_data *td;
287
288         if (global)
289                 return &def_thread;
290         if (setup_thread_area()) {
291                 log_err("error: failed to setup shm segment\n");
292                 return NULL;
293         }
294         if (thread_number >= max_jobs) {
295                 log_err("error: maximum number of jobs (%d) reached.\n",
296                                 max_jobs);
297                 return NULL;
298         }
299
300         td = &threads[thread_number++];
301         *td = *parent;
302
303         td->io_ops = NULL;
304         if (!preserve_eo)
305                 td->eo = NULL;
306
307         td->o.uid = td->o.gid = -1U;
308
309         dup_files(td, parent);
310         fio_options_mem_dupe(td);
311
312         profile_add_hooks(td);
313
314         td->thread_number = thread_number;
315         return td;
316 }
317
318 static void put_job(struct thread_data *td)
319 {
320         if (td == &def_thread)
321                 return;
322
323         profile_td_exit(td);
324         flow_exit_job(td);
325
326         if (td->error)
327                 log_info("fio: %s\n", td->verror);
328
329         fio_options_free(td);
330         if (td->io_ops)
331                 free_ioengine(td);
332
333         memset(&threads[td->thread_number - 1], 0, sizeof(*td));
334         thread_number--;
335 }
336
337 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
338 {
339         unsigned int bs = td->o.min_bs[ddir];
340
341         assert(ddir_rw(ddir));
342
343         if (td->o.rate[ddir])
344                 td->rate_bps[ddir] = td->o.rate[ddir];
345         else
346                 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
347
348         if (!td->rate_bps[ddir]) {
349                 log_err("rate lower than supported\n");
350                 return -1;
351         }
352
353         td->rate_pending_usleep[ddir] = 0;
354         return 0;
355 }
356
357 static int setup_rate(struct thread_data *td)
358 {
359         int ret = 0;
360
361         if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
362                 ret = __setup_rate(td, DDIR_READ);
363         if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
364                 ret |= __setup_rate(td, DDIR_WRITE);
365         if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
366                 ret |= __setup_rate(td, DDIR_TRIM);
367
368         return ret;
369 }
370
371 static int fixed_block_size(struct thread_options *o)
372 {
373         return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
374                 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
375                 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
376                 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
377                 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
378 }
379
380 /*
381  * Lazy way of fixing up options that depend on each other. We could also
382  * define option callback handlers, but this is easier.
383  */
384 static int fixup_options(struct thread_data *td)
385 {
386         struct thread_options *o = &td->o;
387         int ret = 0;
388
389 #ifndef FIO_HAVE_PSHARED_MUTEX
390         if (!o->use_thread) {
391                 log_info("fio: this platform does not support process shared"
392                          " mutexes, forcing use of threads. Use the 'thread'"
393                          " option to get rid of this warning.\n");
394                 o->use_thread = 1;
395                 ret = warnings_fatal;
396         }
397 #endif
398
399         if (o->write_iolog_file && o->read_iolog_file) {
400                 log_err("fio: read iolog overrides write_iolog\n");
401                 free(o->write_iolog_file);
402                 o->write_iolog_file = NULL;
403                 ret = warnings_fatal;
404         }
405
406         /*
407          * only really works with 1 file
408          */
409         if (o->zone_size && o->open_files == 1)
410                 o->zone_size = 0;
411
412         /*
413          * If zone_range isn't specified, backward compatibility dictates it
414          * should be made equal to zone_size.
415          */
416         if (o->zone_size && !o->zone_range)
417                 o->zone_range = o->zone_size;
418
419         /*
420          * Reads can do overwrites, we always need to pre-create the file
421          */
422         if (td_read(td) || td_rw(td))
423                 o->overwrite = 1;
424
425         if (!o->min_bs[DDIR_READ])
426                 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
427         if (!o->max_bs[DDIR_READ])
428                 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
429         if (!o->min_bs[DDIR_WRITE])
430                 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
431         if (!o->max_bs[DDIR_WRITE])
432                 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
433         if (!o->min_bs[DDIR_TRIM])
434                 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
435         if (!o->max_bs[DDIR_TRIM])
436                 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
437
438
439         o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
440         o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
441
442         /*
443          * For random IO, allow blockalign offset other than min_bs.
444          */
445         if (!o->ba[DDIR_READ] || !td_random(td))
446                 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
447         if (!o->ba[DDIR_WRITE] || !td_random(td))
448                 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
449         if (!o->ba[DDIR_TRIM] || !td_random(td))
450                 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
451
452         if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
453             o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
454             o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
455             !o->norandommap) {
456                 log_err("fio: Any use of blockalign= turns off randommap\n");
457                 o->norandommap = 1;
458                 ret = warnings_fatal;
459         }
460
461         if (!o->file_size_high)
462                 o->file_size_high = o->file_size_low;
463
464         if (o->norandommap && o->verify != VERIFY_NONE
465             && !fixed_block_size(o))  {
466                 log_err("fio: norandommap given for variable block sizes, "
467                         "verify disabled\n");
468                 o->verify = VERIFY_NONE;
469                 ret = warnings_fatal;
470         }
471         if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
472                 log_err("fio: bs_unaligned may not work with raw io\n");
473
474         /*
475          * thinktime_spin must be less than thinktime
476          */
477         if (o->thinktime_spin > o->thinktime)
478                 o->thinktime_spin = o->thinktime;
479
480         /*
481          * The low water mark cannot be bigger than the iodepth
482          */
483         if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
484                 /*
485                  * syslet work around - if the workload is sequential,
486                  * we want to let the queue drain all the way down to
487                  * avoid seeking between async threads
488                  */
489                 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
490                         o->iodepth_low = 1;
491                 else
492                         o->iodepth_low = o->iodepth;
493         }
494
495         /*
496          * If batch number isn't set, default to the same as iodepth
497          */
498         if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
499                 o->iodepth_batch = o->iodepth;
500
501         if (o->nr_files > td->files_index)
502                 o->nr_files = td->files_index;
503
504         if (o->open_files > o->nr_files || !o->open_files)
505                 o->open_files = o->nr_files;
506
507         if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
508             (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
509             ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
510             (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
511                 log_err("fio: rate and rate_iops are mutually exclusive\n");
512                 ret = 1;
513         }
514         if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
515             (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
516             (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
517             (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
518             (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
519             (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
520                 log_err("fio: minimum rate exceeds rate\n");
521                 ret = 1;
522         }
523
524         if (!o->timeout && o->time_based) {
525                 log_err("fio: time_based requires a runtime/timeout setting\n");
526                 o->time_based = 0;
527                 ret = warnings_fatal;
528         }
529
530         if (o->fill_device && !o->size)
531                 o->size = -1ULL;
532
533         if (o->verify != VERIFY_NONE) {
534                 if (td_write(td) && o->do_verify && o->numjobs > 1) {
535                         log_info("Multiple writers may overwrite blocks that "
536                                 "belong to other jobs. This can cause "
537                                 "verification failures.\n");
538                         ret = warnings_fatal;
539                 }
540
541                 o->refill_buffers = 1;
542                 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
543                     !o->verify_interval)
544                         o->verify_interval = o->min_bs[DDIR_WRITE];
545         }
546
547         if (o->pre_read) {
548                 o->invalidate_cache = 0;
549                 if (td->io_ops->flags & FIO_PIPEIO) {
550                         log_info("fio: cannot pre-read files with an IO engine"
551                                  " that isn't seekable. Pre-read disabled.\n");
552                         ret = warnings_fatal;
553                 }
554         }
555
556 #ifndef FIO_HAVE_FDATASYNC
557         if (o->fdatasync_blocks) {
558                 log_info("fio: this platform does not support fdatasync()"
559                          " falling back to using fsync().  Use the 'fsync'"
560                          " option instead of 'fdatasync' to get rid of"
561                          " this warning\n");
562                 o->fsync_blocks = o->fdatasync_blocks;
563                 o->fdatasync_blocks = 0;
564                 ret = warnings_fatal;
565         }
566 #endif
567
568 #ifdef WIN32
569         /*
570          * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
571          * so fail if we're passed those flags
572          */
573         if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
574                 log_err("fio: Windows does not support direct or non-buffered io with"
575                                 " the synchronous ioengines. Use the 'windowsaio' ioengine"
576                                 " with 'direct=1' and 'iodepth=1' instead.\n");
577                 ret = 1;
578         }
579 #endif
580
581         /*
582          * For fully compressible data, just zero them at init time.
583          * It's faster than repeatedly filling it.
584          */
585         if (td->o.compress_percentage == 100) {
586                 td->o.zero_buffers = 1;
587                 td->o.compress_percentage = 0;
588         }
589
590         return ret;
591 }
592
593 /*
594  * This function leaks the buffer
595  */
596 static char *to_kmg(unsigned int val)
597 {
598         char *buf = malloc(32);
599         char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
600         char *p = post;
601
602         do {
603                 if (val & 1023)
604                         break;
605
606                 val >>= 10;
607                 p++;
608         } while (*p);
609
610         snprintf(buf, 31, "%u%c", val, *p);
611         return buf;
612 }
613
614 /* External engines are specified by "external:name.o") */
615 static const char *get_engine_name(const char *str)
616 {
617         char *p = strstr(str, ":");
618
619         if (!p)
620                 return str;
621
622         p++;
623         strip_blank_front(&p);
624         strip_blank_end(p);
625         return p;
626 }
627
628 static int exists_and_not_file(const char *filename)
629 {
630         struct stat sb;
631
632         if (lstat(filename, &sb) == -1)
633                 return 0;
634
635         /* \\.\ is the device namespace in Windows, where every file
636          * is a device node */
637         if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
638                 return 0;
639
640         return 1;
641 }
642
643 static void td_fill_rand_seeds_os(struct thread_data *td)
644 {
645         os_random_seed(td->rand_seeds[0], &td->bsrange_state);
646         os_random_seed(td->rand_seeds[1], &td->verify_state);
647         os_random_seed(td->rand_seeds[2], &td->rwmix_state);
648
649         if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
650                 os_random_seed(td->rand_seeds[3], &td->next_file_state);
651
652         os_random_seed(td->rand_seeds[5], &td->file_size_state);
653         os_random_seed(td->rand_seeds[6], &td->trim_state);
654
655         if (!td_random(td))
656                 return;
657
658         if (td->o.rand_repeatable)
659                 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
660
661         os_random_seed(td->rand_seeds[4], &td->random_state);
662 }
663
664 static void td_fill_rand_seeds_internal(struct thread_data *td)
665 {
666         init_rand_seed(&td->__bsrange_state, td->rand_seeds[0]);
667         init_rand_seed(&td->__verify_state, td->rand_seeds[1]);
668         init_rand_seed(&td->__rwmix_state, td->rand_seeds[2]);
669
670         if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
671                 init_rand_seed(&td->__next_file_state, td->rand_seeds[3]);
672
673         init_rand_seed(&td->__file_size_state, td->rand_seeds[5]);
674         init_rand_seed(&td->__trim_state, td->rand_seeds[6]);
675
676         if (!td_random(td))
677                 return;
678
679         if (td->o.rand_repeatable)
680                 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
681
682         init_rand_seed(&td->__random_state, td->rand_seeds[4]);
683 }
684
685 void td_fill_rand_seeds(struct thread_data *td)
686 {
687         if (td->o.use_os_rand)
688                 td_fill_rand_seeds_os(td);
689         else
690                 td_fill_rand_seeds_internal(td);
691
692         init_rand_seed(&td->buf_state, td->rand_seeds[7]);
693 }
694
695
696 /*
697  * Initializes the ioengine configured for a job, if it has not been done so
698  * already.
699  */
700 int ioengine_load(struct thread_data *td)
701 {
702         const char *engine;
703
704         /*
705          * Engine has already been loaded.
706          */
707         if (td->io_ops)
708                 return 0;
709
710         engine = get_engine_name(td->o.ioengine);
711         td->io_ops = load_ioengine(td, engine);
712         if (!td->io_ops) {
713                 log_err("fio: failed to load engine %s\n", engine);
714                 return 1;
715         }
716
717         if (td->io_ops->option_struct_size && td->io_ops->options) {
718                 /*
719                  * In cases where td->eo is set, clone it for a child thread.
720                  * This requires that the parent thread has the same ioengine,
721                  * but that requirement must be enforced by the code which
722                  * cloned the thread.
723                  */
724                 void *origeo = td->eo;
725                 /*
726                  * Otherwise use the default thread options.
727                  */
728                 if (!origeo && td != &def_thread && def_thread.eo &&
729                     def_thread.io_ops->options == td->io_ops->options)
730                         origeo = def_thread.eo;
731
732                 options_init(td->io_ops->options);
733                 td->eo = malloc(td->io_ops->option_struct_size);
734                 /*
735                  * Use the default thread as an option template if this uses the
736                  * same options structure and there are non-default options
737                  * used.
738                  */
739                 if (origeo) {
740                         memcpy(td->eo, origeo, td->io_ops->option_struct_size);
741                         options_mem_dupe(td->eo, td->io_ops->options);
742                 } else {
743                         memset(td->eo, 0, td->io_ops->option_struct_size);
744                         fill_default_options(td->eo, td->io_ops->options);
745                 }
746                 *(struct thread_data **)td->eo = td;
747         }
748
749         return 0;
750 }
751
752 /*
753  * Adds a job to the list of things todo. Sanitizes the various options
754  * to make sure we don't have conflicts, and initializes various
755  * members of td.
756  */
757 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
758 {
759         const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
760                                    "randread", "randwrite", "randrw",
761                                    "trim", NULL, NULL, NULL, "randtrim" };
762         unsigned int i;
763         char fname[PATH_MAX];
764         int numjobs, file_alloced;
765
766         /*
767          * the def_thread is just for options, it's not a real job
768          */
769         if (td == &def_thread)
770                 return 0;
771
772         /*
773          * if we are just dumping the output command line, don't add the job
774          */
775         if (dump_cmdline) {
776                 put_job(td);
777                 return 0;
778         }
779
780         if (profile_td_init(td))
781                 goto err;
782
783         if (ioengine_load(td))
784                 goto err;
785
786         if (td->o.use_thread)
787                 nr_thread++;
788         else
789                 nr_process++;
790
791         if (td->o.odirect)
792                 td->io_ops->flags |= FIO_RAWIO;
793
794         file_alloced = 0;
795         if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
796                 file_alloced = 1;
797
798                 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
799                         add_file(td, jobname);
800                 else {
801                         for (i = 0; i < td->o.nr_files; i++) {
802                                 sprintf(fname, "%s.%d.%d", jobname,
803                                                         td->thread_number, i);
804                                 add_file(td, fname);
805                         }
806                 }
807         }
808
809         if (fixup_options(td))
810                 goto err;
811
812         flow_init_job(td);
813
814         /*
815          * IO engines only need this for option callbacks, and the address may
816          * change in subprocesses.
817          */
818         if (td->eo)
819                 *(struct thread_data **)td->eo = NULL;
820
821         if (td->io_ops->flags & FIO_DISKLESSIO) {
822                 struct fio_file *f;
823
824                 for_each_file(td, f, i)
825                         f->real_file_size = -1ULL;
826         }
827
828         td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
829
830         td->ts.clat_percentiles = td->o.clat_percentiles;
831         if (td->o.overwrite_plist)
832                 memcpy(td->ts.percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
833         else
834                 memcpy(td->ts.percentile_list, def_percentile_list, sizeof(def_percentile_list));
835
836         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
837                 td->ts.clat_stat[i].min_val = ULONG_MAX;
838                 td->ts.slat_stat[i].min_val = ULONG_MAX;
839                 td->ts.lat_stat[i].min_val = ULONG_MAX;
840                 td->ts.bw_stat[i].min_val = ULONG_MAX;
841         }
842         td->ddir_seq_nr = td->o.ddir_seq_nr;
843
844         if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
845                 prev_group_jobs = 0;
846                 groupid++;
847         }
848
849         td->groupid = groupid;
850         prev_group_jobs++;
851
852         if (init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds))) {
853                 td_verror(td, errno, "init_random_state");
854                 goto err;
855         }
856
857         if (setup_rate(td))
858                 goto err;
859
860         if (td->o.write_lat_log) {
861                 setup_log(&td->lat_log, td->o.log_avg_msec);
862                 setup_log(&td->slat_log, td->o.log_avg_msec);
863                 setup_log(&td->clat_log, td->o.log_avg_msec);
864         }
865         if (td->o.write_bw_log)
866                 setup_log(&td->bw_log, td->o.log_avg_msec);
867         if (td->o.write_iops_log)
868                 setup_log(&td->iops_log, td->o.log_avg_msec);
869
870         if (!td->o.name)
871                 td->o.name = strdup(jobname);
872
873         if (!terse_output) {
874                 if (!job_add_num) {
875                         if (!strcmp(td->io_ops->name, "cpuio")) {
876                                 log_info("%s: ioengine=cpu, cpuload=%u,"
877                                          " cpucycle=%u\n", td->o.name,
878                                                         td->o.cpuload,
879                                                         td->o.cpucycle);
880                         } else {
881                                 char *c1, *c2, *c3, *c4, *c5, *c6;
882
883                                 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
884                                 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
885                                 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
886                                 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
887                                 c5 = to_kmg(td->o.min_bs[DDIR_TRIM]);
888                                 c6 = to_kmg(td->o.max_bs[DDIR_TRIM]);
889
890                                 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s/%s-%s,"
891                                          " ioengine=%s, iodepth=%u\n",
892                                                 td->o.name, td->groupid,
893                                                 ddir_str[td->o.td_ddir],
894                                                 c1, c2, c3, c4, c5, c6,
895                                                 td->io_ops->name,
896                                                 td->o.iodepth);
897
898                                 free(c1);
899                                 free(c2);
900                                 free(c3);
901                                 free(c4);
902                                 free(c5);
903                                 free(c6);
904                         }
905                 } else if (job_add_num == 1)
906                         log_info("...\n");
907         }
908
909         /*
910          * recurse add identical jobs, clear numjobs and stonewall options
911          * as they don't apply to sub-jobs
912          */
913         numjobs = td->o.numjobs;
914         while (--numjobs) {
915                 struct thread_data *td_new = get_new_job(0, td, 1);
916
917                 if (!td_new)
918                         goto err;
919
920                 td_new->o.numjobs = 1;
921                 td_new->o.stonewall = 0;
922                 td_new->o.new_group = 0;
923
924                 if (file_alloced) {
925                         td_new->o.filename = NULL;
926                         td_new->files_index = 0;
927                         td_new->files_size = 0;
928                         td_new->files = NULL;
929                 }
930
931                 job_add_num = numjobs - 1;
932
933                 if (add_job(td_new, jobname, job_add_num))
934                         goto err;
935         }
936
937         return 0;
938 err:
939         put_job(td);
940         return -1;
941 }
942
943 /*
944  * Parse as if 'o' was a command line
945  */
946 void add_job_opts(const char **o)
947 {
948         struct thread_data *td, *td_parent;
949         int i, in_global = 1;
950         char jobname[32];
951
952         i = 0;
953         td_parent = td = NULL;
954         while (o[i]) {
955                 if (!strncmp(o[i], "name", 4)) {
956                         in_global = 0;
957                         if (td)
958                                 add_job(td, jobname, 0);
959                         td = NULL;
960                         sprintf(jobname, "%s", o[i] + 5);
961                 }
962                 if (in_global && !td_parent)
963                         td_parent = get_new_job(1, &def_thread, 0);
964                 else if (!in_global && !td) {
965                         if (!td_parent)
966                                 td_parent = &def_thread;
967                         td = get_new_job(0, td_parent, 0);
968                 }
969                 if (in_global)
970                         fio_options_parse(td_parent, (char **) &o[i], 1);
971                 else
972                         fio_options_parse(td, (char **) &o[i], 1);
973                 i++;
974         }
975
976         if (td)
977                 add_job(td, jobname, 0);
978 }
979
980 static int skip_this_section(const char *name)
981 {
982         int i;
983
984         if (!nr_job_sections)
985                 return 0;
986         if (!strncmp(name, "global", 6))
987                 return 0;
988
989         for (i = 0; i < nr_job_sections; i++)
990                 if (!strcmp(job_sections[i], name))
991                         return 0;
992
993         return 1;
994 }
995
996 static int is_empty_or_comment(char *line)
997 {
998         unsigned int i;
999
1000         for (i = 0; i < strlen(line); i++) {
1001                 if (line[i] == ';')
1002                         return 1;
1003                 if (line[i] == '#')
1004                         return 1;
1005                 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1006                         return 0;
1007         }
1008
1009         return 1;
1010 }
1011
1012 /*
1013  * This is our [ini] type file parser.
1014  */
1015 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag)
1016 {
1017         unsigned int global;
1018         struct thread_data *td;
1019         char *string, *name;
1020         FILE *f;
1021         char *p;
1022         int ret = 0, stonewall;
1023         int first_sect = 1;
1024         int skip_fgets = 0;
1025         int inside_skip = 0;
1026         char **opts;
1027         int i, alloc_opts, num_opts;
1028
1029         if (is_buf)
1030                 f = NULL;
1031         else {
1032                 if (!strcmp(file, "-"))
1033                         f = stdin;
1034                 else
1035                         f = fopen(file, "r");
1036
1037                 if (!f) {
1038                         perror("fopen job file");
1039                         return 1;
1040                 }
1041         }
1042
1043         string = malloc(4096);
1044
1045         /*
1046          * it's really 256 + small bit, 280 should suffice
1047          */
1048         name = malloc(280);
1049         memset(name, 0, 280);
1050
1051         alloc_opts = 8;
1052         opts = malloc(sizeof(char *) * alloc_opts);
1053         num_opts = 0;
1054
1055         stonewall = stonewall_flag;
1056         do {
1057                 /*
1058                  * if skip_fgets is set, we already have loaded a line we
1059                  * haven't handled.
1060                  */
1061                 if (!skip_fgets) {
1062                         if (is_buf)
1063                                 p = strsep(&file, "\n");
1064                         else
1065                                 p = fgets(string, 4096, f);
1066                         if (!p)
1067                                 break;
1068                 }
1069
1070                 skip_fgets = 0;
1071                 strip_blank_front(&p);
1072                 strip_blank_end(p);
1073
1074                 if (is_empty_or_comment(p))
1075                         continue;
1076                 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1077                         if (inside_skip)
1078                                 continue;
1079                         log_err("fio: option <%s> outside of [] job section\n",
1080                                                                         p);
1081                         break;
1082                 }
1083
1084                 name[strlen(name) - 1] = '\0';
1085
1086                 if (skip_this_section(name)) {
1087                         inside_skip = 1;
1088                         continue;
1089                 } else
1090                         inside_skip = 0;
1091
1092                 global = !strncmp(name, "global", 6);
1093
1094                 if (dump_cmdline) {
1095                         if (first_sect)
1096                                 log_info("fio ");
1097                         if (!global)
1098                                 log_info("--name=%s ", name);
1099                         first_sect = 0;
1100                 }
1101
1102                 td = get_new_job(global, &def_thread, 0);
1103                 if (!td) {
1104                         ret = 1;
1105                         break;
1106                 }
1107
1108                 /*
1109                  * Seperate multiple job files by a stonewall
1110                  */
1111                 if (!global && stonewall) {
1112                         td->o.stonewall = stonewall;
1113                         stonewall = 0;
1114                 }
1115
1116                 num_opts = 0;
1117                 memset(opts, 0, alloc_opts * sizeof(char *));
1118
1119                 while (1) {
1120                         if (is_buf)
1121                                 p = strsep(&file, "\n");
1122                         else
1123                                 p = fgets(string, 4096, f);
1124                         if (!p)
1125                                 break;
1126
1127                         if (is_empty_or_comment(p))
1128                                 continue;
1129
1130                         strip_blank_front(&p);
1131
1132                         /*
1133                          * new section, break out and make sure we don't
1134                          * fgets() a new line at the top.
1135                          */
1136                         if (p[0] == '[') {
1137                                 skip_fgets = 1;
1138                                 break;
1139                         }
1140
1141                         strip_blank_end(p);
1142
1143                         if (num_opts == alloc_opts) {
1144                                 alloc_opts <<= 1;
1145                                 opts = realloc(opts,
1146                                                 alloc_opts * sizeof(char *));
1147                         }
1148
1149                         opts[num_opts] = strdup(p);
1150                         num_opts++;
1151                 }
1152
1153                 ret = fio_options_parse(td, opts, num_opts);
1154                 if (!ret) {
1155                         if (dump_cmdline)
1156                                 for (i = 0; i < num_opts; i++)
1157                                         log_info("--%s ", opts[i]);
1158
1159                         ret = add_job(td, name, 0);
1160                 } else {
1161                         log_err("fio: job %s dropped\n", name);
1162                         put_job(td);
1163                 }
1164
1165                 for (i = 0; i < num_opts; i++)
1166                         free(opts[i]);
1167                 num_opts = 0;
1168         } while (!ret);
1169
1170         if (dump_cmdline)
1171                 log_info("\n");
1172
1173         i = 0;
1174         while (i < nr_job_sections) {
1175                 free(job_sections[i]);
1176                 i++;
1177         }
1178
1179         for (i = 0; i < num_opts; i++)
1180                 free(opts[i]);
1181
1182         free(string);
1183         free(name);
1184         free(opts);
1185         if (!is_buf && f != stdin)
1186                 fclose(f);
1187         return ret;
1188 }
1189
1190 static int fill_def_thread(void)
1191 {
1192         memset(&def_thread, 0, sizeof(def_thread));
1193
1194         fio_getaffinity(getpid(), &def_thread.o.cpumask);
1195         def_thread.o.timeout = def_timeout;
1196
1197         /*
1198          * fill default options
1199          */
1200         fio_fill_default_options(&def_thread);
1201         return 0;
1202 }
1203
1204 static void usage(const char *name)
1205 {
1206         printf("%s\n", fio_version_string);
1207         printf("%s [options] [job options] <job file(s)>\n", name);
1208         printf("  --debug=options\tEnable debug logging. May be one/more of:\n"
1209                 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
1210                 "\t\t\tdiskutil,job,mutex,profile,time,net\n");
1211         printf("  --output\t\tWrite output to file\n");
1212         printf("  --runtime\t\tRuntime in seconds\n");
1213         printf("  --latency-log\t\tGenerate per-job latency logs\n");
1214         printf("  --bandwidth-log\tGenerate per-job bandwidth logs\n");
1215         printf("  --minimal\t\tMinimal (terse) output\n");
1216         printf("  --version\t\tPrint version info and exit\n");
1217         printf("  --terse-version=x\tSet terse version output format to 'x'\n");
1218         printf("  --help\t\tPrint this page\n");
1219         printf("  --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
1220                 " them\n");
1221         printf("  --enghelp=engine\tPrint ioengine help, or list"
1222                 " available ioengines\n");
1223         printf("  --enghelp=engine,cmd\tPrint help for an ioengine"
1224                 " cmd\n");
1225         printf("  --showcmd\t\tTurn a job file into command line options\n");
1226         printf("  --eta=when\t\tWhen ETA estimate should be printed\n");
1227         printf("            \t\tMay be \"always\", \"never\" or \"auto\"\n");
1228         printf("  --readonly\t\tTurn on safety read-only checks, preventing"
1229                 " writes\n");
1230         printf("  --section=name\tOnly run specified section in job file\n");
1231         printf("  --alloc-size=kb\tSet smalloc pool to this size in kb"
1232                 " (def 1024)\n");
1233         printf("  --warnings-fatal\tFio parser warnings are fatal\n");
1234         printf("  --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1235         printf("  --server=args\t\tStart a backend fio server\n");
1236         printf("  --daemonize=pidfile\tBackground fio server, write pid to file\n");
1237         printf("  --client=hostname\tTalk to remote backend fio server at hostname\n");
1238         printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1239         printf("\n                 Jens Axboe <jaxboe@fusionio.com>\n");
1240 }
1241
1242 #ifdef FIO_INC_DEBUG
1243 struct debug_level debug_levels[] = {
1244         { .name = "process",    .shift = FD_PROCESS, },
1245         { .name = "file",       .shift = FD_FILE, },
1246         { .name = "io",         .shift = FD_IO, },
1247         { .name = "mem",        .shift = FD_MEM, },
1248         { .name = "blktrace",   .shift = FD_BLKTRACE },
1249         { .name = "verify",     .shift = FD_VERIFY },
1250         { .name = "random",     .shift = FD_RANDOM },
1251         { .name = "parse",      .shift = FD_PARSE },
1252         { .name = "diskutil",   .shift = FD_DISKUTIL },
1253         { .name = "job",        .shift = FD_JOB },
1254         { .name = "mutex",      .shift = FD_MUTEX },
1255         { .name = "profile",    .shift = FD_PROFILE },
1256         { .name = "time",       .shift = FD_TIME },
1257         { .name = "net",        .shift = FD_NET },
1258         { .name = NULL, },
1259 };
1260
1261 static int set_debug(const char *string)
1262 {
1263         struct debug_level *dl;
1264         char *p = (char *) string;
1265         char *opt;
1266         int i;
1267
1268         if (!strcmp(string, "?") || !strcmp(string, "help")) {
1269                 log_info("fio: dumping debug options:");
1270                 for (i = 0; debug_levels[i].name; i++) {
1271                         dl = &debug_levels[i];
1272                         log_info("%s,", dl->name);
1273                 }
1274                 log_info("all\n");
1275                 return 1;
1276         }
1277
1278         while ((opt = strsep(&p, ",")) != NULL) {
1279                 int found = 0;
1280
1281                 if (!strncmp(opt, "all", 3)) {
1282                         log_info("fio: set all debug options\n");
1283                         fio_debug = ~0UL;
1284                         continue;
1285                 }
1286
1287                 for (i = 0; debug_levels[i].name; i++) {
1288                         dl = &debug_levels[i];
1289                         found = !strncmp(opt, dl->name, strlen(dl->name));
1290                         if (!found)
1291                                 continue;
1292
1293                         if (dl->shift == FD_JOB) {
1294                                 opt = strchr(opt, ':');
1295                                 if (!opt) {
1296                                         log_err("fio: missing job number\n");
1297                                         break;
1298                                 }
1299                                 opt++;
1300                                 fio_debug_jobno = atoi(opt);
1301                                 log_info("fio: set debug jobno %d\n",
1302                                                         fio_debug_jobno);
1303                         } else {
1304                                 log_info("fio: set debug option %s\n", opt);
1305                                 fio_debug |= (1UL << dl->shift);
1306                         }
1307                         break;
1308                 }
1309
1310                 if (!found)
1311                         log_err("fio: debug mask %s not found\n", opt);
1312         }
1313         return 0;
1314 }
1315 #else
1316 static int set_debug(const char *string)
1317 {
1318         log_err("fio: debug tracing not included in build\n");
1319         return 1;
1320 }
1321 #endif
1322
1323 static void fio_options_fill_optstring(void)
1324 {
1325         char *ostr = cmd_optstr;
1326         int i, c;
1327
1328         c = i = 0;
1329         while (l_opts[i].name) {
1330                 ostr[c++] = l_opts[i].val;
1331                 if (l_opts[i].has_arg == required_argument)
1332                         ostr[c++] = ':';
1333                 else if (l_opts[i].has_arg == optional_argument) {
1334                         ostr[c++] = ':';
1335                         ostr[c++] = ':';
1336                 }
1337                 i++;
1338         }
1339         ostr[c] = '\0';
1340 }
1341
1342 static int client_flag_set(char c)
1343 {
1344         int i;
1345
1346         i = 0;
1347         while (l_opts[i].name) {
1348                 int val = l_opts[i].val;
1349
1350                 if (c == (val & 0xff))
1351                         return (val & FIO_CLIENT_FLAG);
1352
1353                 i++;
1354         }
1355
1356         return 0;
1357 }
1358
1359 void parse_cmd_client(void *client, char *opt)
1360 {
1361         fio_client_add_cmd_option(client, opt);
1362 }
1363
1364 int parse_cmd_line(int argc, char *argv[])
1365 {
1366         struct thread_data *td = NULL;
1367         int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
1368         char *ostr = cmd_optstr;
1369         void *pid_file = NULL;
1370         void *cur_client = NULL;
1371         int backend = 0;
1372
1373         /*
1374          * Reset optind handling, since we may call this multiple times
1375          * for the backend.
1376          */
1377         optind = 1;
1378
1379         while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
1380                 did_arg = 1;
1381
1382                 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
1383                         parse_cmd_client(cur_client, argv[optind - 1]);
1384                         c &= ~FIO_CLIENT_FLAG;
1385                 }
1386
1387                 switch (c) {
1388                 case 'a':
1389                         smalloc_pool_size = atoi(optarg);
1390                         break;
1391                 case 't':
1392                         def_timeout = atoi(optarg);
1393                         break;
1394                 case 'l':
1395                         write_lat_log = 1;
1396                         break;
1397                 case 'b':
1398                         write_bw_log = 1;
1399                         break;
1400                 case 'o':
1401                         f_out = fopen(optarg, "w+");
1402                         if (!f_out) {
1403                                 perror("fopen output");
1404                                 exit(1);
1405                         }
1406                         f_err = f_out;
1407                         break;
1408                 case 'm':
1409                         terse_output = 1;
1410                         break;
1411                 case 'h':
1412                         if (!cur_client) {
1413                                 usage(argv[0]);
1414                                 do_exit++;
1415                         }
1416                         break;
1417                 case 'c':
1418                         if (!cur_client) {
1419                                 fio_show_option_help(optarg);
1420                                 do_exit++;
1421                         }
1422                         break;
1423                 case 'i':
1424                         if (!cur_client) {
1425                                 fio_show_ioengine_help(optarg);
1426                                 do_exit++;
1427                         }
1428                         break;
1429                 case 's':
1430                         dump_cmdline = 1;
1431                         break;
1432                 case 'r':
1433                         read_only = 1;
1434                         break;
1435                 case 'v':
1436                         if (!cur_client) {
1437                                 log_info("%s\n", fio_version_string);
1438                                 do_exit++;
1439                         }
1440                         break;
1441                 case 'V':
1442                         terse_version = atoi(optarg);
1443                         if (!(terse_version == 2 || terse_version == 3) ||
1444                              (terse_version == 4)) {
1445                                 log_err("fio: bad terse version format\n");
1446                                 exit_val = 1;
1447                                 do_exit++;
1448                         }
1449                         break;
1450                 case 'e':
1451                         if (!strcmp("always", optarg))
1452                                 eta_print = FIO_ETA_ALWAYS;
1453                         else if (!strcmp("never", optarg))
1454                                 eta_print = FIO_ETA_NEVER;
1455                         break;
1456                 case 'd':
1457                         if (set_debug(optarg))
1458                                 do_exit++;
1459                         break;
1460                 case 'x': {
1461                         size_t new_size;
1462
1463                         if (!strcmp(optarg, "global")) {
1464                                 log_err("fio: can't use global as only "
1465                                         "section\n");
1466                                 do_exit++;
1467                                 exit_val = 1;
1468                                 break;
1469                         }
1470                         new_size = (nr_job_sections + 1) * sizeof(char *);
1471                         job_sections = realloc(job_sections, new_size);
1472                         job_sections[nr_job_sections] = strdup(optarg);
1473                         nr_job_sections++;
1474                         break;
1475                         }
1476                 case 'p':
1477                         exec_profile = strdup(optarg);
1478                         break;
1479                 case FIO_GETOPT_JOB: {
1480                         const char *opt = l_opts[lidx].name;
1481                         char *val = optarg;
1482
1483                         if (!strncmp(opt, "name", 4) && td) {
1484                                 ret = add_job(td, td->o.name ?: "fio", 0);
1485                                 if (ret)
1486                                         return 0;
1487                                 td = NULL;
1488                         }
1489                         if (!td) {
1490                                 int is_section = !strncmp(opt, "name", 4);
1491                                 int global = 0;
1492
1493                                 if (!is_section || !strncmp(val, "global", 6))
1494                                         global = 1;
1495
1496                                 if (is_section && skip_this_section(val))
1497                                         continue;
1498
1499                                 td = get_new_job(global, &def_thread, 1);
1500                                 if (!td || ioengine_load(td))
1501                                         return 0;
1502                                 fio_options_set_ioengine_opts(l_opts, td);
1503                         }
1504
1505                         ret = fio_cmd_option_parse(td, opt, val);
1506
1507                         if (!ret && !strcmp(opt, "ioengine")) {
1508                                 free_ioengine(td);
1509                                 if (ioengine_load(td))
1510                                         return 0;
1511                                 fio_options_set_ioengine_opts(l_opts, td);
1512                         }
1513                         break;
1514                 }
1515                 case FIO_GETOPT_IOENGINE: {
1516                         const char *opt = l_opts[lidx].name;
1517                         char *val = optarg;
1518                         opt = l_opts[lidx].name;
1519                         val = optarg;
1520                         ret = fio_cmd_ioengine_option_parse(td, opt, val);
1521                         break;
1522                 }
1523                 case 'w':
1524                         warnings_fatal = 1;
1525                         break;
1526                 case 'j':
1527                         max_jobs = atoi(optarg);
1528                         if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1529                                 log_err("fio: invalid max jobs: %d\n", max_jobs);
1530                                 do_exit++;
1531                                 exit_val = 1;
1532                         }
1533                         break;
1534                 case 'S':
1535                         if (nr_clients) {
1536                                 log_err("fio: can't be both client and server\n");
1537                                 do_exit++;
1538                                 exit_val = 1;
1539                                 break;
1540                         }
1541                         if (optarg)
1542                                 fio_server_set_arg(optarg);
1543                         is_backend = 1;
1544                         backend = 1;
1545                         break;
1546                 case 'D':
1547                         pid_file = strdup(optarg);
1548                         break;
1549                 case 'C':
1550                         if (is_backend) {
1551                                 log_err("fio: can't be both client and server\n");
1552                                 do_exit++;
1553                                 exit_val = 1;
1554                                 break;
1555                         }
1556                         if (fio_client_add(optarg, &cur_client)) {
1557                                 log_err("fio: failed adding client %s\n", optarg);
1558                                 do_exit++;
1559                                 exit_val = 1;
1560                                 break;
1561                         }
1562                         /*
1563                          * If the next argument exists and isn't an option,
1564                          * assume it's a job file for this client only.
1565                          */
1566                         while (optind < argc) {
1567                                 if (!strncmp(argv[optind], "--", 2) ||
1568                                     !strncmp(argv[optind], "-", 1))
1569                                         break;
1570
1571                                 fio_client_add_ini_file(cur_client, argv[optind]);
1572                                 optind++;
1573                         }
1574                         break;
1575                 default:
1576                         do_exit++;
1577                         exit_val = 1;
1578                         break;
1579                 }
1580                 if (do_exit)
1581                         break;
1582         }
1583
1584         if (do_exit) {
1585                 if (exit_val && !(is_backend || nr_clients))
1586                         exit(exit_val);
1587         }
1588
1589         if (nr_clients && fio_clients_connect()) {
1590                 do_exit++;
1591                 exit_val = 1;
1592                 return -1;
1593         }
1594
1595         if (is_backend && backend)
1596                 return fio_start_server(pid_file);
1597
1598         if (td) {
1599                 if (!ret)
1600                         ret = add_job(td, td->o.name ?: "fio", 0);
1601         }
1602
1603         while (!ret && optind < argc) {
1604                 ini_idx++;
1605                 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1606                 ini_file[ini_idx - 1] = strdup(argv[optind]);
1607                 optind++;
1608         }
1609
1610         return ini_idx;
1611 }
1612
1613 int parse_options(int argc, char *argv[])
1614 {
1615         int job_files, i;
1616
1617         f_out = stdout;
1618         f_err = stderr;
1619
1620         fio_options_fill_optstring();
1621         fio_options_dup_and_init(l_opts);
1622
1623         atexit(free_shm);
1624
1625         if (fill_def_thread())
1626                 return 1;
1627
1628         job_files = parse_cmd_line(argc, argv);
1629
1630         if (job_files > 0) {
1631                 for (i = 0; i < job_files; i++) {
1632                         if (fill_def_thread())
1633                                 return 1;
1634                         if (nr_clients) {
1635                                 if (fio_clients_send_ini(ini_file[i]))
1636                                         return 1;
1637                                 free(ini_file[i]);
1638                         } else if (!is_backend) {
1639                                 if (parse_jobs_ini(ini_file[i], 0, i))
1640                                         return 1;
1641                                 free(ini_file[i]);
1642                         }
1643                 }
1644         } else if (nr_clients) {
1645                 if (fill_def_thread())
1646                         return 1;
1647                 if (fio_clients_send_ini(NULL))
1648                         return 1;
1649         }
1650
1651         free(ini_file);
1652         fio_options_free(&def_thread);
1653
1654         if (!thread_number) {
1655                 if (dump_cmdline)
1656                         return 0;
1657                 if (exec_profile)
1658                         return 0;
1659                 if (is_backend || nr_clients)
1660                         return 0;
1661                 if (did_arg)
1662                         return 0;
1663
1664                 log_err("No jobs(s) defined\n\n");
1665
1666                 if (!did_arg) {
1667                         usage(argv[0]);
1668                         return 1;
1669                 }
1670
1671                 return 0;
1672         }
1673
1674         if (def_thread.o.gtod_offload) {
1675                 fio_gtod_init();
1676                 fio_gtod_offload = 1;
1677                 fio_gtod_cpu = def_thread.o.gtod_cpu;
1678         }
1679
1680         if (!terse_output)
1681                 log_info("%s\n", fio_version_string);
1682
1683         return 0;
1684 }