Split helper thread debug logging away from steadystate debug logging
[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/types.h>
13 #include <sys/stat.h>
14
15 #include "fio.h"
16 #ifndef FIO_NO_HAVE_SHM_H
17 #include <sys/shm.h>
18 #endif
19
20 #include "parse.h"
21 #include "smalloc.h"
22 #include "filehash.h"
23 #include "verify.h"
24 #include "profile.h"
25 #include "server.h"
26 #include "idletime.h"
27 #include "filelock.h"
28 #include "steadystate.h"
29
30 #include "oslib/getopt.h"
31 #include "oslib/strcasestr.h"
32
33 #include "crc/test.h"
34
35 const char fio_version_string[] = FIO_VERSION;
36
37 #define FIO_RANDSEED            (0xb1899bedUL)
38
39 static char **ini_file;
40 static int max_jobs = FIO_MAX_JOBS;
41 static int dump_cmdline;
42 static long long def_timeout;
43 static int parse_only;
44
45 static struct thread_data def_thread;
46 struct thread_data *threads = NULL;
47 static char **job_sections;
48 static int nr_job_sections;
49
50 int exitall_on_terminate = 0;
51 int exitall_on_terminate_error = 0;
52 int output_format = FIO_OUTPUT_NORMAL;
53 int eta_print = FIO_ETA_AUTO;
54 int eta_new_line = 0;
55 FILE *f_out = NULL;
56 FILE *f_err = NULL;
57 char *exec_profile = NULL;
58 int warnings_fatal = 0;
59 int terse_version = 3;
60 int is_backend = 0;
61 int nr_clients = 0;
62 int log_syslog = 0;
63
64 int write_bw_log = 0;
65 int read_only = 0;
66 int status_interval = 0;
67
68 char *trigger_file = NULL;
69 long long trigger_timeout = 0;
70 char *trigger_cmd = NULL;
71 char *trigger_remote_cmd = NULL;
72
73 char *aux_path = NULL;
74
75 static int prev_group_jobs;
76
77 unsigned long fio_debug = 0;
78 unsigned int fio_debug_jobno = -1;
79 unsigned int *fio_debug_jobp = NULL;
80
81 static char cmd_optstr[256];
82 static int did_arg;
83
84 bool steadystate = false;
85
86 #define FIO_CLIENT_FLAG         (1 << 16)
87
88 /*
89  * Command line options. These will contain the above, plus a few
90  * extra that only pertain to fio itself and not jobs.
91  */
92 static struct option l_opts[FIO_NR_OPTIONS] = {
93         {
94                 .name           = (char *) "output",
95                 .has_arg        = required_argument,
96                 .val            = 'o' | FIO_CLIENT_FLAG,
97         },
98         {
99                 .name           = (char *) "timeout",
100                 .has_arg        = required_argument,
101                 .val            = 't' | FIO_CLIENT_FLAG,
102         },
103         {
104                 .name           = (char *) "latency-log",
105                 .has_arg        = required_argument,
106                 .val            = 'l' | FIO_CLIENT_FLAG,
107         },
108         {
109                 .name           = (char *) "bandwidth-log",
110                 .has_arg        = required_argument,
111                 .val            = 'b' | FIO_CLIENT_FLAG,
112         },
113         {
114                 .name           = (char *) "minimal",
115                 .has_arg        = no_argument,
116                 .val            = 'm' | FIO_CLIENT_FLAG,
117         },
118         {
119                 .name           = (char *) "output-format",
120                 .has_arg        = required_argument,
121                 .val            = 'F' | FIO_CLIENT_FLAG,
122         },
123         {
124                 .name           = (char *) "append-terse",
125                 .has_arg        = optional_argument,
126                 .val            = 'f',
127         },
128         {
129                 .name           = (char *) "version",
130                 .has_arg        = no_argument,
131                 .val            = 'v' | FIO_CLIENT_FLAG,
132         },
133         {
134                 .name           = (char *) "help",
135                 .has_arg        = no_argument,
136                 .val            = 'h' | FIO_CLIENT_FLAG,
137         },
138         {
139                 .name           = (char *) "cmdhelp",
140                 .has_arg        = optional_argument,
141                 .val            = 'c' | FIO_CLIENT_FLAG,
142         },
143         {
144                 .name           = (char *) "enghelp",
145                 .has_arg        = optional_argument,
146                 .val            = 'i' | FIO_CLIENT_FLAG,
147         },
148         {
149                 .name           = (char *) "showcmd",
150                 .has_arg        = no_argument,
151                 .val            = 's' | FIO_CLIENT_FLAG,
152         },
153         {
154                 .name           = (char *) "readonly",
155                 .has_arg        = no_argument,
156                 .val            = 'r' | FIO_CLIENT_FLAG,
157         },
158         {
159                 .name           = (char *) "eta",
160                 .has_arg        = required_argument,
161                 .val            = 'e' | FIO_CLIENT_FLAG,
162         },
163         {
164                 .name           = (char *) "eta-newline",
165                 .has_arg        = required_argument,
166                 .val            = 'E' | FIO_CLIENT_FLAG,
167         },
168         {
169                 .name           = (char *) "debug",
170                 .has_arg        = required_argument,
171                 .val            = 'd' | FIO_CLIENT_FLAG,
172         },
173         {
174                 .name           = (char *) "parse-only",
175                 .has_arg        = no_argument,
176                 .val            = 'P' | FIO_CLIENT_FLAG,
177         },
178         {
179                 .name           = (char *) "section",
180                 .has_arg        = required_argument,
181                 .val            = 'x' | FIO_CLIENT_FLAG,
182         },
183 #ifdef CONFIG_ZLIB
184         {
185                 .name           = (char *) "inflate-log",
186                 .has_arg        = required_argument,
187                 .val            = 'X' | FIO_CLIENT_FLAG,
188         },
189 #endif
190         {
191                 .name           = (char *) "alloc-size",
192                 .has_arg        = required_argument,
193                 .val            = 'a' | FIO_CLIENT_FLAG,
194         },
195         {
196                 .name           = (char *) "profile",
197                 .has_arg        = required_argument,
198                 .val            = 'p' | FIO_CLIENT_FLAG,
199         },
200         {
201                 .name           = (char *) "warnings-fatal",
202                 .has_arg        = no_argument,
203                 .val            = 'w' | FIO_CLIENT_FLAG,
204         },
205         {
206                 .name           = (char *) "max-jobs",
207                 .has_arg        = required_argument,
208                 .val            = 'j' | FIO_CLIENT_FLAG,
209         },
210         {
211                 .name           = (char *) "terse-version",
212                 .has_arg        = required_argument,
213                 .val            = 'V' | FIO_CLIENT_FLAG,
214         },
215         {
216                 .name           = (char *) "server",
217                 .has_arg        = optional_argument,
218                 .val            = 'S',
219         },
220         {       .name           = (char *) "daemonize",
221                 .has_arg        = required_argument,
222                 .val            = 'D',
223         },
224         {
225                 .name           = (char *) "client",
226                 .has_arg        = required_argument,
227                 .val            = 'C',
228         },
229         {
230                 .name           = (char *) "remote-config",
231                 .has_arg        = required_argument,
232                 .val            = 'R',
233         },
234         {
235                 .name           = (char *) "cpuclock-test",
236                 .has_arg        = no_argument,
237                 .val            = 'T',
238         },
239         {
240                 .name           = (char *) "crctest",
241                 .has_arg        = optional_argument,
242                 .val            = 'G',
243         },
244         {
245                 .name           = (char *) "idle-prof",
246                 .has_arg        = required_argument,
247                 .val            = 'I',
248         },
249         {
250                 .name           = (char *) "status-interval",
251                 .has_arg        = required_argument,
252                 .val            = 'L',
253         },
254         {
255                 .name           = (char *) "trigger-file",
256                 .has_arg        = required_argument,
257                 .val            = 'W',
258         },
259         {
260                 .name           = (char *) "trigger-timeout",
261                 .has_arg        = required_argument,
262                 .val            = 'B',
263         },
264         {
265                 .name           = (char *) "trigger",
266                 .has_arg        = required_argument,
267                 .val            = 'H',
268         },
269         {
270                 .name           = (char *) "trigger-remote",
271                 .has_arg        = required_argument,
272                 .val            = 'J',
273         },
274         {
275                 .name           = (char *) "aux-path",
276                 .has_arg        = required_argument,
277                 .val            = 'K',
278         },
279         {
280                 .name           = NULL,
281         },
282 };
283
284 void free_threads_shm(void)
285 {
286         if (threads) {
287                 void *tp = threads;
288 #ifndef CONFIG_NO_SHM
289                 struct shmid_ds sbuf;
290
291                 threads = NULL;
292                 shmdt(tp);
293                 shmctl(shm_id, IPC_RMID, &sbuf);
294                 shm_id = -1;
295 #else
296                 threads = NULL;
297                 free(tp);
298 #endif
299         }
300 }
301
302 static void free_shm(void)
303 {
304         if (threads) {
305                 file_hash_exit();
306                 flow_exit();
307                 fio_debug_jobp = NULL;
308                 free_threads_shm();
309         }
310
311         free(trigger_file);
312         free(trigger_cmd);
313         free(trigger_remote_cmd);
314         trigger_file = trigger_cmd = trigger_remote_cmd = NULL;
315
316         options_free(fio_options, &def_thread);
317         fio_filelock_exit();
318         scleanup();
319 }
320
321 /*
322  * The thread area is shared between the main process and the job
323  * threads/processes. So setup a shared memory segment that will hold
324  * all the job info. We use the end of the region for keeping track of
325  * open files across jobs, for file sharing.
326  */
327 static int setup_thread_area(void)
328 {
329         void *hash;
330
331         if (threads)
332                 return 0;
333
334         /*
335          * 1024 is too much on some machines, scale max_jobs if
336          * we get a failure that looks like too large a shm segment
337          */
338         do {
339                 size_t size = max_jobs * sizeof(struct thread_data);
340
341                 size += file_hash_size;
342                 size += sizeof(unsigned int);
343
344 #ifndef CONFIG_NO_SHM
345                 shm_id = shmget(0, size, IPC_CREAT | 0600);
346                 if (shm_id != -1)
347                         break;
348                 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
349                         perror("shmget");
350                         break;
351                 }
352 #else
353                 threads = malloc(size);
354                 if (threads)
355                         break;
356 #endif
357
358                 max_jobs >>= 1;
359         } while (max_jobs);
360
361 #ifndef CONFIG_NO_SHM
362         if (shm_id == -1)
363                 return 1;
364
365         threads = shmat(shm_id, NULL, 0);
366         if (threads == (void *) -1) {
367                 perror("shmat");
368                 return 1;
369         }
370 #endif
371
372         memset(threads, 0, max_jobs * sizeof(struct thread_data));
373         hash = (void *) threads + max_jobs * sizeof(struct thread_data);
374         fio_debug_jobp = (void *) hash + file_hash_size;
375         *fio_debug_jobp = -1;
376         file_hash_init(hash);
377
378         flow_init();
379
380         return 0;
381 }
382
383 static void set_cmd_options(struct thread_data *td)
384 {
385         struct thread_options *o = &td->o;
386
387         if (!o->timeout)
388                 o->timeout = def_timeout;
389 }
390
391 static void dump_print_option(struct print_option *p)
392 {
393         const char *delim;
394
395         if (!strcmp("description", p->name))
396                 delim = "\"";
397         else
398                 delim = "";
399
400         log_info("--%s%s", p->name, p->value ? "" : " ");
401         if (p->value)
402                 log_info("=%s%s%s ", delim, p->value, delim);
403 }
404
405 static void dump_opt_list(struct thread_data *td)
406 {
407         struct flist_head *entry;
408         struct print_option *p;
409
410         if (flist_empty(&td->opt_list))
411                 return;
412
413         flist_for_each(entry, &td->opt_list) {
414                 p = flist_entry(entry, struct print_option, list);
415                 dump_print_option(p);
416         }
417 }
418
419 static void fio_dump_options_free(struct thread_data *td)
420 {
421         while (!flist_empty(&td->opt_list)) {
422                 struct print_option *p;
423
424                 p = flist_first_entry(&td->opt_list, struct print_option, list);
425                 flist_del_init(&p->list);
426                 free(p->name);
427                 free(p->value);
428                 free(p);
429         }
430 }
431
432 static void copy_opt_list(struct thread_data *dst, struct thread_data *src)
433 {
434         struct flist_head *entry;
435
436         if (flist_empty(&src->opt_list))
437                 return;
438
439         flist_for_each(entry, &src->opt_list) {
440                 struct print_option *srcp, *dstp;
441
442                 srcp = flist_entry(entry, struct print_option, list);
443                 dstp = malloc(sizeof(*dstp));
444                 dstp->name = strdup(srcp->name);
445                 if (srcp->value)
446                         dstp->value = strdup(srcp->value);
447                 else
448                         dstp->value = NULL;
449                 flist_add_tail(&dstp->list, &dst->opt_list);
450         }
451 }
452
453 /*
454  * Return a free job structure.
455  */
456 static struct thread_data *get_new_job(int global, struct thread_data *parent,
457                                        int preserve_eo, const char *jobname)
458 {
459         struct thread_data *td;
460
461         if (global) {
462                 set_cmd_options(&def_thread);
463                 return &def_thread;
464         }
465         if (setup_thread_area()) {
466                 log_err("error: failed to setup shm segment\n");
467                 return NULL;
468         }
469         if (thread_number >= max_jobs) {
470                 log_err("error: maximum number of jobs (%d) reached.\n",
471                                 max_jobs);
472                 return NULL;
473         }
474
475         td = &threads[thread_number++];
476         *td = *parent;
477
478         INIT_FLIST_HEAD(&td->opt_list);
479         if (parent != &def_thread)
480                 copy_opt_list(td, parent);
481
482         td->io_ops = NULL;
483         if (!preserve_eo)
484                 td->eo = NULL;
485
486         td->o.uid = td->o.gid = -1U;
487
488         dup_files(td, parent);
489         fio_options_mem_dupe(td);
490
491         profile_add_hooks(td);
492
493         td->thread_number = thread_number;
494         td->subjob_number = 0;
495
496         if (jobname)
497                 td->o.name = strdup(jobname);
498
499         if (!parent->o.group_reporting || parent == &def_thread)
500                 stat_number++;
501
502         set_cmd_options(td);
503         return td;
504 }
505
506 static void put_job(struct thread_data *td)
507 {
508         if (td == &def_thread)
509                 return;
510
511         profile_td_exit(td);
512         flow_exit_job(td);
513
514         if (td->error)
515                 log_info("fio: %s\n", td->verror);
516
517         fio_options_free(td);
518         fio_dump_options_free(td);
519         if (td->io_ops)
520                 free_ioengine(td);
521
522         if (td->o.name)
523                 free(td->o.name);
524
525         memset(&threads[td->thread_number - 1], 0, sizeof(*td));
526         thread_number--;
527 }
528
529 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
530 {
531         unsigned int bs = td->o.min_bs[ddir];
532
533         assert(ddir_rw(ddir));
534
535         if (td->o.rate[ddir])
536                 td->rate_bps[ddir] = td->o.rate[ddir];
537         else
538                 td->rate_bps[ddir] = (uint64_t) td->o.rate_iops[ddir] * bs;
539
540         if (!td->rate_bps[ddir]) {
541                 log_err("rate lower than supported\n");
542                 return -1;
543         }
544
545         td->rate_next_io_time[ddir] = 0;
546         td->rate_io_issue_bytes[ddir] = 0;
547         td->last_usec = 0;
548         return 0;
549 }
550
551 static int setup_rate(struct thread_data *td)
552 {
553         int ret = 0;
554
555         if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
556                 ret = __setup_rate(td, DDIR_READ);
557         if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
558                 ret |= __setup_rate(td, DDIR_WRITE);
559         if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
560                 ret |= __setup_rate(td, DDIR_TRIM);
561
562         return ret;
563 }
564
565 static int fixed_block_size(struct thread_options *o)
566 {
567         return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
568                 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
569                 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
570                 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
571                 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
572 }
573
574
575 static unsigned long long get_rand_start_delay(struct thread_data *td)
576 {
577         unsigned long long delayrange;
578         uint64_t frand_max;
579         unsigned long r;
580
581         delayrange = td->o.start_delay_high - td->o.start_delay;
582
583         frand_max = rand_max(&td->delay_state);
584         r = __rand(&td->delay_state);
585         delayrange = (unsigned long long) ((double) delayrange * (r / (frand_max + 1.0)));
586
587         delayrange += td->o.start_delay;
588         return delayrange;
589 }
590
591 /*
592  * Lazy way of fixing up options that depend on each other. We could also
593  * define option callback handlers, but this is easier.
594  */
595 static int fixup_options(struct thread_data *td)
596 {
597         struct thread_options *o = &td->o;
598         int ret = 0;
599
600 #ifndef FIO_HAVE_PSHARED_MUTEX
601         if (!o->use_thread) {
602                 log_info("fio: this platform does not support process shared"
603                          " mutexes, forcing use of threads. Use the 'thread'"
604                          " option to get rid of this warning.\n");
605                 o->use_thread = 1;
606                 ret = warnings_fatal;
607         }
608 #endif
609
610         if (o->write_iolog_file && o->read_iolog_file) {
611                 log_err("fio: read iolog overrides write_iolog\n");
612                 free(o->write_iolog_file);
613                 o->write_iolog_file = NULL;
614                 ret = warnings_fatal;
615         }
616
617         /*
618          * only really works with 1 file
619          */
620         if (o->zone_size && o->open_files > 1)
621                 o->zone_size = 0;
622
623         /*
624          * If zone_range isn't specified, backward compatibility dictates it
625          * should be made equal to zone_size.
626          */
627         if (o->zone_size && !o->zone_range)
628                 o->zone_range = o->zone_size;
629
630         /*
631          * Reads can do overwrites, we always need to pre-create the file
632          */
633         if (td_read(td) || td_rw(td))
634                 o->overwrite = 1;
635
636         if (!o->min_bs[DDIR_READ])
637                 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
638         if (!o->max_bs[DDIR_READ])
639                 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
640         if (!o->min_bs[DDIR_WRITE])
641                 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
642         if (!o->max_bs[DDIR_WRITE])
643                 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
644         if (!o->min_bs[DDIR_TRIM])
645                 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
646         if (!o->max_bs[DDIR_TRIM])
647                 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
648
649         o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
650         o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
651
652         /*
653          * For random IO, allow blockalign offset other than min_bs.
654          */
655         if (!o->ba[DDIR_READ] || !td_random(td))
656                 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
657         if (!o->ba[DDIR_WRITE] || !td_random(td))
658                 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
659         if (!o->ba[DDIR_TRIM] || !td_random(td))
660                 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
661
662         if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
663             o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
664             o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
665             !o->norandommap) {
666                 log_err("fio: Any use of blockalign= turns off randommap\n");
667                 o->norandommap = 1;
668                 ret = warnings_fatal;
669         }
670
671         if (!o->file_size_high)
672                 o->file_size_high = o->file_size_low;
673
674         if (o->start_delay_high)
675                 o->start_delay = get_rand_start_delay(td);
676
677         if (o->norandommap && o->verify != VERIFY_NONE
678             && !fixed_block_size(o))  {
679                 log_err("fio: norandommap given for variable block sizes, "
680                         "verify limited\n");
681                 ret = warnings_fatal;
682         }
683         if (o->bs_unaligned && (o->odirect || td_ioengine_flagged(td, FIO_RAWIO)))
684                 log_err("fio: bs_unaligned may not work with raw io\n");
685
686         /*
687          * thinktime_spin must be less than thinktime
688          */
689         if (o->thinktime_spin > o->thinktime)
690                 o->thinktime_spin = o->thinktime;
691
692         /*
693          * The low water mark cannot be bigger than the iodepth
694          */
695         if (o->iodepth_low > o->iodepth || !o->iodepth_low)
696                 o->iodepth_low = o->iodepth;
697
698         /*
699          * If batch number isn't set, default to the same as iodepth
700          */
701         if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
702                 o->iodepth_batch = o->iodepth;
703
704         /*
705          * If max batch complete number isn't set or set incorrectly,
706          * default to the same as iodepth_batch_complete_min
707          */
708         if (o->iodepth_batch_complete_min > o->iodepth_batch_complete_max)
709                 o->iodepth_batch_complete_max = o->iodepth_batch_complete_min;
710
711         if (o->nr_files > td->files_index)
712                 o->nr_files = td->files_index;
713
714         if (o->open_files > o->nr_files || !o->open_files)
715                 o->open_files = o->nr_files;
716
717         if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
718             (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
719             ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
720             (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
721                 log_err("fio: rate and rate_iops are mutually exclusive\n");
722                 ret = 1;
723         }
724         if ((o->rate[DDIR_READ] && (o->rate[DDIR_READ] < o->ratemin[DDIR_READ])) ||
725             (o->rate[DDIR_WRITE] && (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE])) ||
726             (o->rate[DDIR_TRIM] && (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM])) ||
727             (o->rate_iops[DDIR_READ] && (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ])) ||
728             (o->rate_iops[DDIR_WRITE] && (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE])) ||
729             (o->rate_iops[DDIR_TRIM] && (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM]))) {
730                 log_err("fio: minimum rate exceeds rate\n");
731                 ret = 1;
732         }
733
734         if (!o->timeout && o->time_based) {
735                 log_err("fio: time_based requires a runtime/timeout setting\n");
736                 o->time_based = 0;
737                 ret = warnings_fatal;
738         }
739
740         if (o->fill_device && !o->size)
741                 o->size = -1ULL;
742
743         if (o->verify != VERIFY_NONE) {
744                 if (td_write(td) && o->do_verify && o->numjobs > 1) {
745                         log_info("Multiple writers may overwrite blocks that "
746                                 "belong to other jobs. This can cause "
747                                 "verification failures.\n");
748                         ret = warnings_fatal;
749                 }
750
751                 if (!fio_option_is_set(o, refill_buffers))
752                         o->refill_buffers = 1;
753
754                 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
755                     !o->verify_interval)
756                         o->verify_interval = o->min_bs[DDIR_WRITE];
757
758                 /*
759                  * Verify interval must be smaller or equal to the
760                  * write size.
761                  */
762                 if (o->verify_interval > o->min_bs[DDIR_WRITE])
763                         o->verify_interval = o->min_bs[DDIR_WRITE];
764                 else if (td_read(td) && o->verify_interval > o->min_bs[DDIR_READ])
765                         o->verify_interval = o->min_bs[DDIR_READ];
766         }
767
768         if (o->pre_read) {
769                 o->invalidate_cache = 0;
770                 if (td_ioengine_flagged(td, FIO_PIPEIO)) {
771                         log_info("fio: cannot pre-read files with an IO engine"
772                                  " that isn't seekable. Pre-read disabled.\n");
773                         ret = warnings_fatal;
774                 }
775         }
776
777         if (!o->unit_base) {
778                 if (td_ioengine_flagged(td, FIO_BIT_BASED))
779                         o->unit_base = 1;
780                 else
781                         o->unit_base = 8;
782         }
783
784 #ifndef CONFIG_FDATASYNC
785         if (o->fdatasync_blocks) {
786                 log_info("fio: this platform does not support fdatasync()"
787                          " falling back to using fsync().  Use the 'fsync'"
788                          " option instead of 'fdatasync' to get rid of"
789                          " this warning\n");
790                 o->fsync_blocks = o->fdatasync_blocks;
791                 o->fdatasync_blocks = 0;
792                 ret = warnings_fatal;
793         }
794 #endif
795
796 #ifdef WIN32
797         /*
798          * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
799          * so fail if we're passed those flags
800          */
801         if (td_ioengine_flagged(td, FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
802                 log_err("fio: Windows does not support direct or non-buffered io with"
803                                 " the synchronous ioengines. Use the 'windowsaio' ioengine"
804                                 " with 'direct=1' and 'iodepth=1' instead.\n");
805                 ret = 1;
806         }
807 #endif
808
809         /*
810          * For fully compressible data, just zero them at init time.
811          * It's faster than repeatedly filling it. For non-zero
812          * compression, we should have refill_buffers set. Set it, unless
813          * the job file already changed it.
814          */
815         if (o->compress_percentage) {
816                 if (o->compress_percentage == 100) {
817                         o->zero_buffers = 1;
818                         o->compress_percentage = 0;
819                 } else if (!fio_option_is_set(o, refill_buffers))
820                         o->refill_buffers = 1;
821         }
822
823         /*
824          * Using a non-uniform random distribution excludes usage of
825          * a random map
826          */
827         if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
828                 td->o.norandommap = 1;
829
830         /*
831          * If size is set but less than the min block size, complain
832          */
833         if (o->size && o->size < td_min_bs(td)) {
834                 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
835                 ret = 1;
836         }
837
838         /*
839          * O_ATOMIC implies O_DIRECT
840          */
841         if (td->o.oatomic)
842                 td->o.odirect = 1;
843
844         /*
845          * If randseed is set, that overrides randrepeat
846          */
847         if (fio_option_is_set(&td->o, rand_seed))
848                 td->o.rand_repeatable = 0;
849
850         if (td_ioengine_flagged(td, FIO_NOEXTEND) && td->o.file_append) {
851                 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
852                 ret = 1;
853         }
854
855         if (fio_option_is_set(o, gtod_cpu)) {
856                 fio_gtod_init();
857                 fio_gtod_set_cpu(o->gtod_cpu);
858                 fio_gtod_offload = 1;
859         }
860
861         td->loops = o->loops;
862         if (!td->loops)
863                 td->loops = 1;
864
865         if (td->o.block_error_hist && td->o.nr_files != 1) {
866                 log_err("fio: block error histogram only available "
867                         "with a single file per job, but %d files "
868                         "provided\n", td->o.nr_files);
869                 ret = 1;
870         }
871
872         return ret;
873 }
874
875 /*
876  * This function leaks the buffer
877  */
878 char *fio_uint_to_kmg(unsigned int val)
879 {
880         char *buf = malloc(32);
881         char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
882         char *p = post;
883
884         do {
885                 if (val & 1023)
886                         break;
887
888                 val >>= 10;
889                 p++;
890         } while (*p);
891
892         snprintf(buf, 32, "%u%c", val, *p);
893         return buf;
894 }
895
896 /* External engines are specified by "external:name.o") */
897 static const char *get_engine_name(const char *str)
898 {
899         char *p = strstr(str, ":");
900
901         if (!p)
902                 return str;
903
904         p++;
905         strip_blank_front(&p);
906         strip_blank_end(p);
907         return p;
908 }
909
910 static int exists_and_not_regfile(const char *filename)
911 {
912         struct stat sb;
913
914         if (lstat(filename, &sb) == -1)
915                 return 0;
916
917 #ifndef WIN32 /* NOT Windows */
918         if (S_ISREG(sb.st_mode))
919                 return 0;
920 #else
921         /* \\.\ is the device namespace in Windows, where every file
922          * is a device node */
923         if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
924                 return 0;
925 #endif
926
927         return 1;
928 }
929
930 static void init_rand_file_service(struct thread_data *td)
931 {
932         unsigned long nranges = td->o.nr_files << FIO_FSERVICE_SHIFT;
933         const unsigned int seed = td->rand_seeds[FIO_RAND_FILE_OFF];
934
935         if (td->o.file_service_type == FIO_FSERVICE_ZIPF) {
936                 zipf_init(&td->next_file_zipf, nranges, td->zipf_theta, seed);
937                 zipf_disable_hash(&td->next_file_zipf);
938         } else if (td->o.file_service_type == FIO_FSERVICE_PARETO) {
939                 pareto_init(&td->next_file_zipf, nranges, td->pareto_h, seed);
940                 zipf_disable_hash(&td->next_file_zipf);
941         } else if (td->o.file_service_type == FIO_FSERVICE_GAUSS) {
942                 gauss_init(&td->next_file_gauss, nranges, td->gauss_dev, seed);
943                 gauss_disable_hash(&td->next_file_gauss);
944         }
945 }
946
947 void td_fill_verify_state_seed(struct thread_data *td)
948 {
949         bool use64;
950
951         if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
952                 use64 = 1;
953         else
954                 use64 = 0;
955
956         init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF],
957                 use64);
958 }
959
960 static void td_fill_rand_seeds_internal(struct thread_data *td, bool use64)
961 {
962         int i;
963
964         init_rand_seed(&td->bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF], use64);
965         td_fill_verify_state_seed(td);
966         init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF], false);
967
968         if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
969                 init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], use64);
970         else if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
971                 init_rand_file_service(td);
972
973         init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], use64);
974         init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF], use64);
975         init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY], use64);
976         init_rand_seed(&td->poisson_state, td->rand_seeds[FIO_RAND_POISSON_OFF], 0);
977         init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], false);
978         init_rand_seed(&td->zone_state, td->rand_seeds[FIO_RAND_ZONE_OFF], false);
979
980         if (!td_random(td))
981                 return;
982
983         if (td->o.rand_repeatable)
984                 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
985
986         init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], use64);
987
988         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
989                 struct frand_state *s = &td->seq_rand_state[i];
990
991                 init_rand_seed(s, td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], false);
992         }
993 }
994
995 void td_fill_rand_seeds(struct thread_data *td)
996 {
997         bool use64;
998
999         if (td->o.allrand_repeatable) {
1000                 unsigned int i;
1001
1002                 for (i = 0; i < FIO_RAND_NR_OFFS; i++)
1003                         td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
1004                                 + i;
1005         }
1006
1007         if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
1008                 use64 = 1;
1009         else
1010                 use64 = 0;
1011
1012         td_fill_rand_seeds_internal(td, use64);
1013
1014         init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], use64);
1015         frand_copy(&td->buf_state_prev, &td->buf_state);
1016 }
1017
1018 /*
1019  * Initializes the ioengine configured for a job, if it has not been done so
1020  * already.
1021  */
1022 int ioengine_load(struct thread_data *td)
1023 {
1024         const char *engine;
1025
1026         /*
1027          * Engine has already been loaded.
1028          */
1029         if (td->io_ops)
1030                 return 0;
1031         if (!td->o.ioengine) {
1032                 log_err("fio: internal fault, no IO engine specified\n");
1033                 return 1;
1034         }
1035
1036         engine = get_engine_name(td->o.ioengine);
1037         td->io_ops = load_ioengine(td, engine);
1038         if (!td->io_ops) {
1039                 log_err("fio: failed to load engine %s\n", engine);
1040                 return 1;
1041         }
1042
1043         if (td->io_ops->option_struct_size && td->io_ops->options) {
1044                 /*
1045                  * In cases where td->eo is set, clone it for a child thread.
1046                  * This requires that the parent thread has the same ioengine,
1047                  * but that requirement must be enforced by the code which
1048                  * cloned the thread.
1049                  */
1050                 void *origeo = td->eo;
1051                 /*
1052                  * Otherwise use the default thread options.
1053                  */
1054                 if (!origeo && td != &def_thread && def_thread.eo &&
1055                     def_thread.io_ops->options == td->io_ops->options)
1056                         origeo = def_thread.eo;
1057
1058                 options_init(td->io_ops->options);
1059                 td->eo = malloc(td->io_ops->option_struct_size);
1060                 /*
1061                  * Use the default thread as an option template if this uses the
1062                  * same options structure and there are non-default options
1063                  * used.
1064                  */
1065                 if (origeo) {
1066                         memcpy(td->eo, origeo, td->io_ops->option_struct_size);
1067                         options_mem_dupe(td->eo, td->io_ops->options);
1068                 } else {
1069                         memset(td->eo, 0, td->io_ops->option_struct_size);
1070                         fill_default_options(td->eo, td->io_ops->options);
1071                 }
1072                 *(struct thread_data **)td->eo = td;
1073         }
1074
1075         if (td->o.odirect)
1076                 td->io_ops->flags |= FIO_RAWIO;
1077
1078         td_set_ioengine_flags(td);
1079         return 0;
1080 }
1081
1082 static void init_flags(struct thread_data *td)
1083 {
1084         struct thread_options *o = &td->o;
1085
1086         if (o->verify_backlog)
1087                 td->flags |= TD_F_VER_BACKLOG;
1088         if (o->trim_backlog)
1089                 td->flags |= TD_F_TRIM_BACKLOG;
1090         if (o->read_iolog_file)
1091                 td->flags |= TD_F_READ_IOLOG;
1092         if (o->refill_buffers)
1093                 td->flags |= TD_F_REFILL_BUFFERS;
1094         /*
1095          * Always scramble buffers if asked to
1096          */
1097         if (o->scramble_buffers && fio_option_is_set(o, scramble_buffers))
1098                 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1099         /*
1100          * But also scramble buffers, unless we were explicitly asked
1101          * to zero them.
1102          */
1103         if (o->scramble_buffers && !(o->zero_buffers &&
1104             fio_option_is_set(o, zero_buffers)))
1105                 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1106         if (o->verify != VERIFY_NONE)
1107                 td->flags |= TD_F_VER_NONE;
1108
1109         if (o->verify_async || o->io_submit_mode == IO_MODE_OFFLOAD)
1110                 td->flags |= TD_F_NEED_LOCK;
1111 }
1112
1113 static int setup_random_seeds(struct thread_data *td)
1114 {
1115         unsigned long seed;
1116         unsigned int i;
1117
1118         if (!td->o.rand_repeatable && !fio_option_is_set(&td->o, rand_seed))
1119                 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
1120
1121         seed = td->o.rand_seed;
1122         for (i = 0; i < 4; i++)
1123                 seed *= 0x9e370001UL;
1124
1125         for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
1126                 td->rand_seeds[i] = seed * td->thread_number + i;
1127                 seed *= 0x9e370001UL;
1128         }
1129
1130         td_fill_rand_seeds(td);
1131         return 0;
1132 }
1133
1134 enum {
1135         FPRE_NONE = 0,
1136         FPRE_JOBNAME,
1137         FPRE_JOBNUM,
1138         FPRE_FILENUM
1139 };
1140
1141 static struct fpre_keyword {
1142         const char *keyword;
1143         size_t strlen;
1144         int key;
1145 } fpre_keywords[] = {
1146         { .keyword = "$jobname",        .key = FPRE_JOBNAME, },
1147         { .keyword = "$jobnum",         .key = FPRE_JOBNUM, },
1148         { .keyword = "$filenum",        .key = FPRE_FILENUM, },
1149         { .keyword = NULL, },
1150         };
1151
1152 static char *make_filename(char *buf, size_t buf_size,struct thread_options *o,
1153                            const char *jobname, int jobnum, int filenum)
1154 {
1155         struct fpre_keyword *f;
1156         char copy[PATH_MAX];
1157         size_t dst_left = PATH_MAX - 1;
1158
1159         if (!o->filename_format || !strlen(o->filename_format)) {
1160                 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
1161                 return NULL;
1162         }
1163
1164         for (f = &fpre_keywords[0]; f->keyword; f++)
1165                 f->strlen = strlen(f->keyword);
1166
1167         buf[buf_size - 1] = '\0';
1168         strncpy(buf, o->filename_format, buf_size - 1);
1169
1170         memset(copy, 0, sizeof(copy));
1171         for (f = &fpre_keywords[0]; f->keyword; f++) {
1172                 do {
1173                         size_t pre_len, post_start = 0;
1174                         char *str, *dst = copy;
1175
1176                         str = strcasestr(buf, f->keyword);
1177                         if (!str)
1178                                 break;
1179
1180                         pre_len = str - buf;
1181                         if (strlen(str) != f->strlen)
1182                                 post_start = pre_len + f->strlen;
1183
1184                         if (pre_len) {
1185                                 strncpy(dst, buf, pre_len);
1186                                 dst += pre_len;
1187                                 dst_left -= pre_len;
1188                         }
1189
1190                         switch (f->key) {
1191                         case FPRE_JOBNAME: {
1192                                 int ret;
1193
1194                                 ret = snprintf(dst, dst_left, "%s", jobname);
1195                                 if (ret < 0)
1196                                         break;
1197                                 else if (ret > dst_left) {
1198                                         log_err("fio: truncated filename\n");
1199                                         dst += dst_left;
1200                                         dst_left = 0;
1201                                 } else {
1202                                         dst += ret;
1203                                         dst_left -= ret;
1204                                 }
1205                                 break;
1206                                 }
1207                         case FPRE_JOBNUM: {
1208                                 int ret;
1209
1210                                 ret = snprintf(dst, dst_left, "%d", jobnum);
1211                                 if (ret < 0)
1212                                         break;
1213                                 else if (ret > dst_left) {
1214                                         log_err("fio: truncated filename\n");
1215                                         dst += dst_left;
1216                                         dst_left = 0;
1217                                 } else {
1218                                         dst += ret;
1219                                         dst_left -= ret;
1220                                 }
1221                                 break;
1222                                 }
1223                         case FPRE_FILENUM: {
1224                                 int ret;
1225
1226                                 ret = snprintf(dst, dst_left, "%d", filenum);
1227                                 if (ret < 0)
1228                                         break;
1229                                 else if (ret > dst_left) {
1230                                         log_err("fio: truncated filename\n");
1231                                         dst += dst_left;
1232                                         dst_left = 0;
1233                                 } else {
1234                                         dst += ret;
1235                                         dst_left -= ret;
1236                                 }
1237                                 break;
1238                                 }
1239                         default:
1240                                 assert(0);
1241                                 break;
1242                         }
1243
1244                         if (post_start)
1245                                 strncpy(dst, buf + post_start, dst_left);
1246
1247                         strncpy(buf, copy, buf_size - 1);
1248                 } while (1);
1249         }
1250
1251         return buf;
1252 }
1253
1254 int parse_dryrun(void)
1255 {
1256         return dump_cmdline || parse_only;
1257 }
1258
1259 static void gen_log_name(char *name, size_t size, const char *logtype,
1260                          const char *logname, unsigned int num,
1261                          const char *suf, int per_job)
1262 {
1263         if (per_job)
1264                 snprintf(name, size, "%s_%s.%d.%s", logname, logtype, num, suf);
1265         else
1266                 snprintf(name, size, "%s_%s.%s", logname, logtype, suf);
1267 }
1268
1269 static int check_waitees(char *waitee)
1270 {
1271         struct thread_data *td;
1272         int i, ret = 0;
1273
1274         for_each_td(td, i) {
1275                 if (td->subjob_number)
1276                         continue;
1277
1278                 ret += !strcmp(td->o.name, waitee);
1279         }
1280
1281         return ret;
1282 }
1283
1284 static bool wait_for_ok(const char *jobname, struct thread_options *o)
1285 {
1286         int nw;
1287
1288         if (!o->wait_for)
1289                 return true;
1290
1291         if (!strcmp(jobname, o->wait_for)) {
1292                 log_err("%s: a job cannot wait for itself (wait_for=%s).\n",
1293                                 jobname, o->wait_for);
1294                 return false;
1295         }
1296
1297         if (!(nw = check_waitees(o->wait_for))) {
1298                 log_err("%s: waitee job %s unknown.\n", jobname, o->wait_for);
1299                 return false;
1300         }
1301
1302         if (nw > 1) {
1303                 log_err("%s: multiple waitees %s found,\n"
1304                         "please avoid duplicates when using wait_for option.\n",
1305                                 jobname, o->wait_for);
1306                 return false;
1307         }
1308
1309         return true;
1310 }
1311
1312 /*
1313  * Adds a job to the list of things todo. Sanitizes the various options
1314  * to make sure we don't have conflicts, and initializes various
1315  * members of td.
1316  */
1317 static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
1318                    int recursed, int client_type)
1319 {
1320         unsigned int i;
1321         char fname[PATH_MAX];
1322         int numjobs, file_alloced;
1323         struct thread_options *o = &td->o;
1324         char logname[PATH_MAX + 32];
1325
1326         /*
1327          * the def_thread is just for options, it's not a real job
1328          */
1329         if (td == &def_thread)
1330                 return 0;
1331
1332         init_flags(td);
1333
1334         /*
1335          * if we are just dumping the output command line, don't add the job
1336          */
1337         if (parse_dryrun()) {
1338                 put_job(td);
1339                 return 0;
1340         }
1341
1342         td->client_type = client_type;
1343
1344         if (profile_td_init(td))
1345                 goto err;
1346
1347         if (ioengine_load(td))
1348                 goto err;
1349
1350         file_alloced = 0;
1351         if (!o->filename && !td->files_index && !o->read_iolog_file) {
1352                 file_alloced = 1;
1353
1354                 if (o->nr_files == 1 && exists_and_not_regfile(jobname))
1355                         add_file(td, jobname, job_add_num, 0);
1356                 else {
1357                         for (i = 0; i < o->nr_files; i++)
1358                                 add_file(td, make_filename(fname, sizeof(fname), o, jobname, job_add_num, i), job_add_num, 0);
1359                 }
1360         }
1361
1362         if (fixup_options(td))
1363                 goto err;
1364
1365         /*
1366          * Belongs to fixup_options, but o->name is not necessarily set as yet
1367          */
1368         if (!wait_for_ok(jobname, o))
1369                 goto err;
1370
1371         flow_init_job(td);
1372
1373         /*
1374          * IO engines only need this for option callbacks, and the address may
1375          * change in subprocesses.
1376          */
1377         if (td->eo)
1378                 *(struct thread_data **)td->eo = NULL;
1379
1380         if (td_ioengine_flagged(td, FIO_DISKLESSIO)) {
1381                 struct fio_file *f;
1382
1383                 for_each_file(td, f, i)
1384                         f->real_file_size = -1ULL;
1385         }
1386
1387         td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1388
1389         td->ts.clat_percentiles = o->clat_percentiles;
1390         td->ts.percentile_precision = o->percentile_precision;
1391         memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1392
1393         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1394                 td->ts.clat_stat[i].min_val = ULONG_MAX;
1395                 td->ts.slat_stat[i].min_val = ULONG_MAX;
1396                 td->ts.lat_stat[i].min_val = ULONG_MAX;
1397                 td->ts.bw_stat[i].min_val = ULONG_MAX;
1398         }
1399         td->ddir_seq_nr = o->ddir_seq_nr;
1400
1401         if ((o->stonewall || o->new_group) && prev_group_jobs) {
1402                 prev_group_jobs = 0;
1403                 groupid++;
1404                 if (groupid == INT_MAX) {
1405                         log_err("fio: too many groups defined\n");
1406                         goto err;
1407                 }
1408         }
1409
1410         td->groupid = groupid;
1411         prev_group_jobs++;
1412
1413         if (setup_random_seeds(td)) {
1414                 td_verror(td, errno, "init_random_state");
1415                 goto err;
1416         }
1417
1418         if (setup_rate(td))
1419                 goto err;
1420
1421         if (o->lat_log_file) {
1422                 struct log_params p = {
1423                         .td = td,
1424                         .avg_msec = o->log_avg_msec,
1425                         .hist_msec = o->log_hist_msec,
1426                         .hist_coarseness = o->log_hist_coarseness,
1427                         .log_type = IO_LOG_TYPE_LAT,
1428                         .log_offset = o->log_offset,
1429                         .log_gz = o->log_gz,
1430                         .log_gz_store = o->log_gz_store,
1431                 };
1432                 const char *suf;
1433
1434                 if (p.log_gz_store)
1435                         suf = "log.fz";
1436                 else
1437                         suf = "log";
1438
1439                 gen_log_name(logname, sizeof(logname), "lat", o->lat_log_file,
1440                                 td->thread_number, suf, o->per_job_logs);
1441                 setup_log(&td->lat_log, &p, logname);
1442
1443                 gen_log_name(logname, sizeof(logname), "slat", o->lat_log_file,
1444                                 td->thread_number, suf, o->per_job_logs);
1445                 setup_log(&td->slat_log, &p, logname);
1446
1447                 gen_log_name(logname, sizeof(logname), "clat", o->lat_log_file,
1448                                 td->thread_number, suf, o->per_job_logs);
1449                 setup_log(&td->clat_log, &p, logname);
1450         }
1451
1452         if (o->hist_log_file) {
1453                 struct log_params p = {
1454                         .td = td,
1455                         .avg_msec = o->log_avg_msec,
1456                         .hist_msec = o->log_hist_msec,
1457                         .hist_coarseness = o->log_hist_coarseness,
1458                         .log_type = IO_LOG_TYPE_HIST,
1459                         .log_offset = o->log_offset,
1460                         .log_gz = o->log_gz,
1461                         .log_gz_store = o->log_gz_store,
1462                 };
1463                 const char *suf;
1464
1465                 if (p.log_gz_store)
1466                         suf = "log.fz";
1467                 else
1468                         suf = "log";
1469
1470                 gen_log_name(logname, sizeof(logname), "clat_hist", o->hist_log_file,
1471                                 td->thread_number, suf, o->per_job_logs);
1472                 setup_log(&td->clat_hist_log, &p, logname);
1473         }
1474
1475         if (o->bw_log_file) {
1476                 struct log_params p = {
1477                         .td = td,
1478                         .avg_msec = o->log_avg_msec,
1479                         .hist_msec = o->log_hist_msec,
1480                         .hist_coarseness = o->log_hist_coarseness,
1481                         .log_type = IO_LOG_TYPE_BW,
1482                         .log_offset = o->log_offset,
1483                         .log_gz = o->log_gz,
1484                         .log_gz_store = o->log_gz_store,
1485                 };
1486                 const char *suf;
1487
1488                 if (fio_option_is_set(o, bw_avg_time))
1489                         p.avg_msec = min(o->log_avg_msec, o->bw_avg_time);
1490                 else
1491                         o->bw_avg_time = p.avg_msec;
1492         
1493                 p.hist_msec = o->log_hist_msec;
1494                 p.hist_coarseness = o->log_hist_coarseness;
1495
1496                 if (p.log_gz_store)
1497                         suf = "log.fz";
1498                 else
1499                         suf = "log";
1500
1501                 gen_log_name(logname, sizeof(logname), "bw", o->bw_log_file,
1502                                 td->thread_number, suf, o->per_job_logs);
1503                 setup_log(&td->bw_log, &p, logname);
1504         }
1505         if (o->iops_log_file) {
1506                 struct log_params p = {
1507                         .td = td,
1508                         .avg_msec = o->log_avg_msec,
1509                         .hist_msec = o->log_hist_msec,
1510                         .hist_coarseness = o->log_hist_coarseness,
1511                         .log_type = IO_LOG_TYPE_IOPS,
1512                         .log_offset = o->log_offset,
1513                         .log_gz = o->log_gz,
1514                         .log_gz_store = o->log_gz_store,
1515                 };
1516                 const char *suf;
1517
1518                 if (fio_option_is_set(o, iops_avg_time))
1519                         p.avg_msec = min(o->log_avg_msec, o->iops_avg_time);
1520                 else
1521                         o->iops_avg_time = p.avg_msec;
1522         
1523                 p.hist_msec = o->log_hist_msec;
1524                 p.hist_coarseness = o->log_hist_coarseness;
1525
1526                 if (p.log_gz_store)
1527                         suf = "log.fz";
1528                 else
1529                         suf = "log";
1530
1531                 gen_log_name(logname, sizeof(logname), "iops", o->iops_log_file,
1532                                 td->thread_number, suf, o->per_job_logs);
1533                 setup_log(&td->iops_log, &p, logname);
1534         }
1535
1536         if (!o->name)
1537                 o->name = strdup(jobname);
1538
1539         if (output_format & FIO_OUTPUT_NORMAL) {
1540                 if (!job_add_num) {
1541                         if (is_backend && !recursed)
1542                                 fio_server_send_add_job(td);
1543
1544                         if (!td_ioengine_flagged(td, FIO_NOIO)) {
1545                                 char *c1, *c2, *c3, *c4;
1546                                 char *c5 = NULL, *c6 = NULL;
1547
1548                                 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1549                                 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1550                                 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1551                                 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
1552
1553                                 if (!o->bs_is_seq_rand) {
1554                                         c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1555                                         c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1556                                 }
1557
1558                                 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1559                                                         td->groupid,
1560                                                         ddir_str(o->td_ddir));
1561
1562                                 if (o->bs_is_seq_rand)
1563                                         log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1564                                                         c1, c2, c3, c4);
1565                                 else
1566                                         log_info("bs=%s-%s/%s-%s/%s-%s, ",
1567                                                         c1, c2, c3, c4, c5, c6);
1568
1569                                 log_info("ioengine=%s, iodepth=%u\n",
1570                                                 td->io_ops->name, o->iodepth);
1571
1572                                 free(c1);
1573                                 free(c2);
1574                                 free(c3);
1575                                 free(c4);
1576                                 free(c5);
1577                                 free(c6);
1578                         }
1579                 } else if (job_add_num == 1)
1580                         log_info("...\n");
1581         }
1582
1583         if (o->ss_dur) {
1584                 steadystate = true;
1585                 o->ss_dur /= 1000000L;
1586
1587                 /* put all steady state info in one place */
1588                 td->ss.dur = o->ss_dur;
1589                 td->ss.limit = o->ss_limit.u.f; 
1590                 td->ss.ramp_time = o->ss_ramp_time;
1591                 td->ss.pct = o->ss_pct;
1592
1593                 if (o->ss == FIO_STEADYSTATE_IOPS_SLOPE || o->ss == FIO_STEADYSTATE_BW_SLOPE) {
1594                         td->ss.check_slope = true;
1595                         td->ss.evaluate = &steadystate_slope;
1596                 } else {
1597                         td->ss.check_slope = false;
1598                         td->ss.evaluate = &steadystate_deviation;
1599                 }
1600
1601                 if (o->ss == FIO_STEADYSTATE_IOPS || o->ss == FIO_STEADYSTATE_IOPS_SLOPE)
1602                         td->ss.check_iops = true;
1603                 else
1604                         td->ss.check_iops = false;
1605
1606                 td->ss.bw_data = NULL;
1607                 td->ss.iops_data = NULL;
1608                 td->ss.ramp_time_over = (td->ss.ramp_time == 0);
1609                 td->ss.attained = 0;
1610                 td->ss.last_in_group = 0;
1611                 td->ss.head = 0;
1612                 td->ss.tail = 0;
1613                 td->ss.sum_x = o->ss_dur * (o->ss_dur - 1) / 2;
1614                 td->ss.sum_x_sq = (o->ss_dur - 1) * (o->ss_dur) * (2*o->ss_dur - 1) / 6;
1615                 td->ss.prev_bytes = 0;
1616                 td->ss.prev_iops = 0;
1617                 td->ss.sum_y = 0;
1618                 td->ss.oldest_y = 0;
1619                 td->ss.criterion = 0.0;
1620                 td->ss.slope = 0.0;
1621                 td->ss.deviation = 0.0;
1622                 td->ts.ss = &td->ss;
1623         }
1624         else
1625                 td->ts.ss = NULL;
1626
1627         /*
1628          * recurse add identical jobs, clear numjobs and stonewall options
1629          * as they don't apply to sub-jobs
1630          */
1631         numjobs = o->numjobs;
1632         while (--numjobs) {
1633                 struct thread_data *td_new = get_new_job(0, td, 1, jobname);
1634
1635                 if (!td_new)
1636                         goto err;
1637
1638                 td_new->o.numjobs = 1;
1639                 td_new->o.stonewall = 0;
1640                 td_new->o.new_group = 0;
1641                 td_new->subjob_number = numjobs;
1642                 td_new->o.ss_dur = o->ss_dur * 1000000l;
1643                 td_new->o.ss_limit = o->ss_limit;
1644
1645                 if (file_alloced) {
1646                         if (td_new->files) {
1647                                 struct fio_file *f;
1648                                 for_each_file(td_new, f, i) {
1649                                         if (f->file_name)
1650                                                 sfree(f->file_name);
1651                                         sfree(f);
1652                                 }
1653                                 free(td_new->files);
1654                                 td_new->files = NULL;
1655                         }
1656                         td_new->files_index = 0;
1657                         td_new->files_size = 0;
1658                         if (td_new->o.filename) {
1659                                 free(td_new->o.filename);
1660                                 td_new->o.filename = NULL;
1661                         }
1662                 }
1663
1664                 if (add_job(td_new, jobname, numjobs, 1, client_type))
1665                         goto err;
1666         }
1667
1668         return 0;
1669 err:
1670         put_job(td);
1671         return -1;
1672 }
1673
1674 /*
1675  * Parse as if 'o' was a command line
1676  */
1677 void add_job_opts(const char **o, int client_type)
1678 {
1679         struct thread_data *td, *td_parent;
1680         int i, in_global = 1;
1681         char jobname[32];
1682
1683         i = 0;
1684         td_parent = td = NULL;
1685         while (o[i]) {
1686                 if (!strncmp(o[i], "name", 4)) {
1687                         in_global = 0;
1688                         if (td)
1689                                 add_job(td, jobname, 0, 0, client_type);
1690                         td = NULL;
1691                         sprintf(jobname, "%s", o[i] + 5);
1692                 }
1693                 if (in_global && !td_parent)
1694                         td_parent = get_new_job(1, &def_thread, 0, jobname);
1695                 else if (!in_global && !td) {
1696                         if (!td_parent)
1697                                 td_parent = &def_thread;
1698                         td = get_new_job(0, td_parent, 0, jobname);
1699                 }
1700                 if (in_global)
1701                         fio_options_parse(td_parent, (char **) &o[i], 1);
1702                 else
1703                         fio_options_parse(td, (char **) &o[i], 1);
1704                 i++;
1705         }
1706
1707         if (td)
1708                 add_job(td, jobname, 0, 0, client_type);
1709 }
1710
1711 static int skip_this_section(const char *name)
1712 {
1713         int i;
1714
1715         if (!nr_job_sections)
1716                 return 0;
1717         if (!strncmp(name, "global", 6))
1718                 return 0;
1719
1720         for (i = 0; i < nr_job_sections; i++)
1721                 if (!strcmp(job_sections[i], name))
1722                         return 0;
1723
1724         return 1;
1725 }
1726
1727 static int is_empty_or_comment(char *line)
1728 {
1729         unsigned int i;
1730
1731         for (i = 0; i < strlen(line); i++) {
1732                 if (line[i] == ';')
1733                         return 1;
1734                 if (line[i] == '#')
1735                         return 1;
1736                 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1737                         return 0;
1738         }
1739
1740         return 1;
1741 }
1742
1743 /*
1744  * This is our [ini] type file parser.
1745  */
1746 int __parse_jobs_ini(struct thread_data *td,
1747                 char *file, int is_buf, int stonewall_flag, int type,
1748                 int nested, char *name, char ***popts, int *aopts, int *nopts)
1749 {
1750         unsigned int global = 0;
1751         char *string;
1752         FILE *f;
1753         char *p;
1754         int ret = 0, stonewall;
1755         int first_sect = 1;
1756         int skip_fgets = 0;
1757         int inside_skip = 0;
1758         char **opts;
1759         int i, alloc_opts, num_opts;
1760
1761         dprint(FD_PARSE, "Parsing ini file %s\n", file);
1762         assert(td || !nested);
1763
1764         if (is_buf)
1765                 f = NULL;
1766         else {
1767                 if (!strcmp(file, "-"))
1768                         f = stdin;
1769                 else
1770                         f = fopen(file, "r");
1771
1772                 if (!f) {
1773                         int __err = errno;
1774
1775                         log_err("fio: unable to open '%s' job file\n", file);
1776                         if (td)
1777                                 td_verror(td, __err, "job file open");
1778                         return 1;
1779                 }
1780         }
1781
1782         string = malloc(4096);
1783
1784         /*
1785          * it's really 256 + small bit, 280 should suffice
1786          */
1787         if (!nested) {
1788                 name = malloc(280);
1789                 memset(name, 0, 280);
1790         }
1791
1792         opts = NULL;
1793         if (nested && popts) {
1794                 opts = *popts;
1795                 alloc_opts = *aopts;
1796                 num_opts = *nopts;
1797         }
1798
1799         if (!opts) {
1800                 alloc_opts = 8;
1801                 opts = malloc(sizeof(char *) * alloc_opts);
1802                 num_opts = 0;
1803         }
1804
1805         stonewall = stonewall_flag;
1806         do {
1807                 /*
1808                  * if skip_fgets is set, we already have loaded a line we
1809                  * haven't handled.
1810                  */
1811                 if (!skip_fgets) {
1812                         if (is_buf)
1813                                 p = strsep(&file, "\n");
1814                         else
1815                                 p = fgets(string, 4096, f);
1816                         if (!p)
1817                                 break;
1818                 }
1819
1820                 skip_fgets = 0;
1821                 strip_blank_front(&p);
1822                 strip_blank_end(p);
1823
1824                 dprint(FD_PARSE, "%s\n", p);
1825                 if (is_empty_or_comment(p))
1826                         continue;
1827
1828                 if (!nested) {
1829                         if (sscanf(p, "[%255[^\n]]", name) != 1) {
1830                                 if (inside_skip)
1831                                         continue;
1832
1833                                 log_err("fio: option <%s> outside of "
1834                                         "[] job section\n", p);
1835                                 ret = 1;
1836                                 break;
1837                         }
1838
1839                         name[strlen(name) - 1] = '\0';
1840
1841                         if (skip_this_section(name)) {
1842                                 inside_skip = 1;
1843                                 continue;
1844                         } else
1845                                 inside_skip = 0;
1846
1847                         dprint(FD_PARSE, "Parsing section [%s]\n", name);
1848
1849                         global = !strncmp(name, "global", 6);
1850
1851                         if (dump_cmdline) {
1852                                 if (first_sect)
1853                                         log_info("fio ");
1854                                 if (!global)
1855                                         log_info("--name=%s ", name);
1856                                 first_sect = 0;
1857                         }
1858
1859                         td = get_new_job(global, &def_thread, 0, name);
1860                         if (!td) {
1861                                 ret = 1;
1862                                 break;
1863                         }
1864
1865                         /*
1866                          * Separate multiple job files by a stonewall
1867                          */
1868                         if (!global && stonewall) {
1869                                 td->o.stonewall = stonewall;
1870                                 stonewall = 0;
1871                         }
1872
1873                         num_opts = 0;
1874                         memset(opts, 0, alloc_opts * sizeof(char *));
1875                 }
1876                 else
1877                         skip_fgets = 1;
1878
1879                 while (1) {
1880                         if (!skip_fgets) {
1881                                 if (is_buf)
1882                                         p = strsep(&file, "\n");
1883                                 else
1884                                         p = fgets(string, 4096, f);
1885                                 if (!p)
1886                                         break;
1887                                 dprint(FD_PARSE, "%s", p);
1888                         }
1889                         else
1890                                 skip_fgets = 0;
1891
1892                         if (is_empty_or_comment(p))
1893                                 continue;
1894
1895                         strip_blank_front(&p);
1896
1897                         /*
1898                          * new section, break out and make sure we don't
1899                          * fgets() a new line at the top.
1900                          */
1901                         if (p[0] == '[') {
1902                                 if (nested) {
1903                                         log_err("No new sections in included files\n");
1904                                         return 1;
1905                                 }
1906
1907                                 skip_fgets = 1;
1908                                 break;
1909                         }
1910
1911                         strip_blank_end(p);
1912
1913                         if (!strncmp(p, "include", strlen("include"))) {
1914                                 char *filename = p + strlen("include") + 1,
1915                                         *ts, *full_fn = NULL;
1916
1917                                 /*
1918                                  * Allow for the include filename
1919                                  * specification to be relative.
1920                                  */
1921                                 if (access(filename, F_OK) &&
1922                                     (ts = strrchr(file, '/'))) {
1923                                         int len = ts - file +
1924                                                 strlen(filename) + 2;
1925
1926                                         if (!(full_fn = calloc(1, len))) {
1927                                                 ret = ENOMEM;
1928                                                 break;
1929                                         }
1930
1931                                         strncpy(full_fn,
1932                                                 file, (ts - file) + 1);
1933                                         strncpy(full_fn + (ts - file) + 1,
1934                                                 filename, strlen(filename));
1935                                         full_fn[len - 1] = 0;
1936                                         filename = full_fn;
1937                                 }
1938
1939                                 ret = __parse_jobs_ini(td, filename, is_buf,
1940                                                        stonewall_flag, type, 1,
1941                                                        name, &opts,
1942                                                        &alloc_opts, &num_opts);
1943
1944                                 if (ret) {
1945                                         log_err("Error %d while parsing "
1946                                                 "include file %s\n",
1947                                                 ret, filename);
1948                                 }
1949
1950                                 if (full_fn)
1951                                         free(full_fn);
1952
1953                                 if (ret)
1954                                         break;
1955
1956                                 continue;
1957                         }
1958
1959                         if (num_opts == alloc_opts) {
1960                                 alloc_opts <<= 1;
1961                                 opts = realloc(opts,
1962                                                 alloc_opts * sizeof(char *));
1963                         }
1964
1965                         opts[num_opts] = strdup(p);
1966                         num_opts++;
1967                 }
1968
1969                 if (nested) {
1970                         *popts = opts;
1971                         *aopts = alloc_opts;
1972                         *nopts = num_opts;
1973                         goto out;
1974                 }
1975
1976                 ret = fio_options_parse(td, opts, num_opts);
1977                 if (!ret) {
1978                         if (dump_cmdline)
1979                                 dump_opt_list(td);
1980
1981                         ret = add_job(td, name, 0, 0, type);
1982                 } else {
1983                         log_err("fio: job %s dropped\n", name);
1984                         put_job(td);
1985                 }
1986
1987                 for (i = 0; i < num_opts; i++)
1988                         free(opts[i]);
1989                 num_opts = 0;
1990         } while (!ret);
1991
1992         if (dump_cmdline)
1993                 log_info("\n");
1994
1995         i = 0;
1996         while (i < nr_job_sections) {
1997                 free(job_sections[i]);
1998                 i++;
1999         }
2000
2001         free(opts);
2002 out:
2003         free(string);
2004         if (!nested)
2005                 free(name);
2006         if (!is_buf && f != stdin)
2007                 fclose(f);
2008         return ret;
2009 }
2010
2011 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
2012 {
2013         return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
2014                         0, NULL, NULL, NULL, NULL);
2015 }
2016
2017 static int fill_def_thread(void)
2018 {
2019         memset(&def_thread, 0, sizeof(def_thread));
2020         INIT_FLIST_HEAD(&def_thread.opt_list);
2021
2022         fio_getaffinity(getpid(), &def_thread.o.cpumask);
2023         def_thread.o.error_dump = 1;
2024
2025         /*
2026          * fill default options
2027          */
2028         fio_fill_default_options(&def_thread);
2029         return 0;
2030 }
2031
2032 static void show_debug_categories(void)
2033 {
2034 #ifdef FIO_INC_DEBUG
2035         struct debug_level *dl = &debug_levels[0];
2036         int curlen, first = 1;
2037
2038         curlen = 0;
2039         while (dl->name) {
2040                 int has_next = (dl + 1)->name != NULL;
2041
2042                 if (first || curlen + strlen(dl->name) >= 80) {
2043                         if (!first) {
2044                                 printf("\n");
2045                                 curlen = 0;
2046                         }
2047                         curlen += printf("\t\t\t%s", dl->name);
2048                         curlen += 3 * (8 - 1);
2049                         if (has_next)
2050                                 curlen += printf(",");
2051                 } else {
2052                         curlen += printf("%s", dl->name);
2053                         if (has_next)
2054                                 curlen += printf(",");
2055                 }
2056                 dl++;
2057                 first = 0;
2058         }
2059         printf("\n");
2060 #endif
2061 }
2062
2063 static void usage(const char *name)
2064 {
2065         printf("%s\n", fio_version_string);
2066         printf("%s [options] [job options] <job file(s)>\n", name);
2067         printf("  --debug=options\tEnable debug logging. May be one/more of:\n");
2068         show_debug_categories();
2069         printf("  --parse-only\t\tParse options only, don't start any IO\n");
2070         printf("  --output\t\tWrite output to file\n");
2071         printf("  --runtime\t\tRuntime in seconds\n");
2072         printf("  --bandwidth-log\tGenerate per-job bandwidth logs\n");
2073         printf("  --minimal\t\tMinimal (terse) output\n");
2074         printf("  --output-format=x\tOutput format (terse,json,json+,normal)\n");
2075         printf("  --terse-version=x\tSet terse version output format to 'x'\n");
2076         printf("  --version\t\tPrint version info and exit\n");
2077         printf("  --help\t\tPrint this page\n");
2078         printf("  --cpuclock-test\tPerform test/validation of CPU clock\n");
2079         printf("  --crctest\t\tTest speed of checksum functions\n");
2080         printf("  --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
2081                 " them\n");
2082         printf("  --enghelp=engine\tPrint ioengine help, or list"
2083                 " available ioengines\n");
2084         printf("  --enghelp=engine,cmd\tPrint help for an ioengine"
2085                 " cmd\n");
2086         printf("  --showcmd\t\tTurn a job file into command line options\n");
2087         printf("  --eta=when\t\tWhen ETA estimate should be printed\n");
2088         printf("            \t\tMay be \"always\", \"never\" or \"auto\"\n");
2089         printf("  --eta-newline=time\tForce a new line for every 'time'");
2090         printf(" period passed\n");
2091         printf("  --status-interval=t\tForce full status dump every");
2092         printf(" 't' period passed\n");
2093         printf("  --readonly\t\tTurn on safety read-only checks, preventing"
2094                 " writes\n");
2095         printf("  --section=name\tOnly run specified section in job file\n");
2096         printf("  --alloc-size=kb\tSet smalloc pool to this size in kb"
2097                 " (def 1024)\n");
2098         printf("  --warnings-fatal\tFio parser warnings are fatal\n");
2099         printf("  --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
2100         printf("  --server=args\t\tStart a backend fio server\n");
2101         printf("  --daemonize=pidfile\tBackground fio server, write pid to file\n");
2102         printf("  --client=hostname\tTalk to remote backend fio server at hostname\n");
2103         printf("  --remote-config=file\tTell fio server to load this local job file\n");
2104         printf("  --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
2105                 "\t\t\t(option=system,percpu) or run unit work\n"
2106                 "\t\t\tcalibration only (option=calibrate)\n");
2107 #ifdef CONFIG_ZLIB
2108         printf("  --inflate-log=log\tInflate and output compressed log\n");
2109 #endif
2110         printf("  --trigger-file=file\tExecute trigger cmd when file exists\n");
2111         printf("  --trigger-timeout=t\tExecute trigger af this time\n");
2112         printf("  --trigger=cmd\t\tSet this command as local trigger\n");
2113         printf("  --trigger-remote=cmd\tSet this command as remote trigger\n");
2114         printf("  --aux-path=path\tUse this path for fio state generated files\n");
2115         printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
2116         printf("\n                   Jens Axboe <jaxboe@fusionio.com>");
2117         printf("\n                   Jens Axboe <axboe@fb.com>\n");
2118 }
2119
2120 #ifdef FIO_INC_DEBUG
2121 struct debug_level debug_levels[] = {
2122         { .name = "process",
2123           .help = "Process creation/exit logging",
2124           .shift = FD_PROCESS,
2125         },
2126         { .name = "file",
2127           .help = "File related action logging",
2128           .shift = FD_FILE,
2129         },
2130         { .name = "io",
2131           .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
2132           .shift = FD_IO,
2133         },
2134         { .name = "mem",
2135           .help = "Memory allocation/freeing logging",
2136           .shift = FD_MEM,
2137         },
2138         { .name = "blktrace",
2139           .help = "blktrace action logging",
2140           .shift = FD_BLKTRACE,
2141         },
2142         { .name = "verify",
2143           .help = "IO verification action logging",
2144           .shift = FD_VERIFY,
2145         },
2146         { .name = "random",
2147           .help = "Random generation logging",
2148           .shift = FD_RANDOM,
2149         },
2150         { .name = "parse",
2151           .help = "Parser logging",
2152           .shift = FD_PARSE,
2153         },
2154         { .name = "diskutil",
2155           .help = "Disk utility logging actions",
2156           .shift = FD_DISKUTIL,
2157         },
2158         { .name = "job",
2159           .help = "Logging related to creating/destroying jobs",
2160           .shift = FD_JOB,
2161         },
2162         { .name = "mutex",
2163           .help = "Mutex logging",
2164           .shift = FD_MUTEX
2165         },
2166         { .name = "profile",
2167           .help = "Logging related to profiles",
2168           .shift = FD_PROFILE,
2169         },
2170         { .name = "time",
2171           .help = "Logging related to time keeping functions",
2172           .shift = FD_TIME,
2173         },
2174         { .name = "net",
2175           .help = "Network logging",
2176           .shift = FD_NET,
2177         },
2178         { .name = "rate",
2179           .help = "Rate logging",
2180           .shift = FD_RATE,
2181         },
2182         { .name = "compress",
2183           .help = "Log compression logging",
2184           .shift = FD_COMPRESS,
2185         },
2186         { .name = "steadystate",
2187           .help = "Steady state detection logging",
2188           .shift = FD_STEADYSTATE,
2189         },
2190         { .name = "helperthread",
2191           .help = "Helper thread logging",
2192           .shift = FD_HELPERTHREAD,
2193         },
2194         { .name = NULL, },
2195 };
2196
2197 static int set_debug(const char *string)
2198 {
2199         struct debug_level *dl;
2200         char *p = (char *) string;
2201         char *opt;
2202         int i;
2203
2204         if (!string)
2205                 return 0;
2206
2207         if (!strcmp(string, "?") || !strcmp(string, "help")) {
2208                 log_info("fio: dumping debug options:");
2209                 for (i = 0; debug_levels[i].name; i++) {
2210                         dl = &debug_levels[i];
2211                         log_info("%s,", dl->name);
2212                 }
2213                 log_info("all\n");
2214                 return 1;
2215         }
2216
2217         while ((opt = strsep(&p, ",")) != NULL) {
2218                 int found = 0;
2219
2220                 if (!strncmp(opt, "all", 3)) {
2221                         log_info("fio: set all debug options\n");
2222                         fio_debug = ~0UL;
2223                         continue;
2224                 }
2225
2226                 for (i = 0; debug_levels[i].name; i++) {
2227                         dl = &debug_levels[i];
2228                         found = !strncmp(opt, dl->name, strlen(dl->name));
2229                         if (!found)
2230                                 continue;
2231
2232                         if (dl->shift == FD_JOB) {
2233                                 opt = strchr(opt, ':');
2234                                 if (!opt) {
2235                                         log_err("fio: missing job number\n");
2236                                         break;
2237                                 }
2238                                 opt++;
2239                                 fio_debug_jobno = atoi(opt);
2240                                 log_info("fio: set debug jobno %d\n",
2241                                                         fio_debug_jobno);
2242                         } else {
2243                                 log_info("fio: set debug option %s\n", opt);
2244                                 fio_debug |= (1UL << dl->shift);
2245                         }
2246                         break;
2247                 }
2248
2249                 if (!found)
2250                         log_err("fio: debug mask %s not found\n", opt);
2251         }
2252         return 0;
2253 }
2254 #else
2255 static int set_debug(const char *string)
2256 {
2257         log_err("fio: debug tracing not included in build\n");
2258         return 1;
2259 }
2260 #endif
2261
2262 static void fio_options_fill_optstring(void)
2263 {
2264         char *ostr = cmd_optstr;
2265         int i, c;
2266
2267         c = i = 0;
2268         while (l_opts[i].name) {
2269                 ostr[c++] = l_opts[i].val;
2270                 if (l_opts[i].has_arg == required_argument)
2271                         ostr[c++] = ':';
2272                 else if (l_opts[i].has_arg == optional_argument) {
2273                         ostr[c++] = ':';
2274                         ostr[c++] = ':';
2275                 }
2276                 i++;
2277         }
2278         ostr[c] = '\0';
2279 }
2280
2281 static int client_flag_set(char c)
2282 {
2283         int i;
2284
2285         i = 0;
2286         while (l_opts[i].name) {
2287                 int val = l_opts[i].val;
2288
2289                 if (c == (val & 0xff))
2290                         return (val & FIO_CLIENT_FLAG);
2291
2292                 i++;
2293         }
2294
2295         return 0;
2296 }
2297
2298 static void parse_cmd_client(void *client, char *opt)
2299 {
2300         fio_client_add_cmd_option(client, opt);
2301 }
2302
2303 static void show_closest_option(const char *name)
2304 {
2305         int best_option, best_distance;
2306         int i, distance;
2307
2308         while (*name == '-')
2309                 name++;
2310
2311         best_option = -1;
2312         best_distance = INT_MAX;
2313         i = 0;
2314         while (l_opts[i].name) {
2315                 distance = string_distance(name, l_opts[i].name);
2316                 if (distance < best_distance) {
2317                         best_distance = distance;
2318                         best_option = i;
2319                 }
2320                 i++;
2321         }
2322
2323         if (best_option != -1 && string_distance_ok(name, best_distance))
2324                 log_err("Did you mean %s?\n", l_opts[best_option].name);
2325 }
2326
2327 static int parse_output_format(const char *optarg)
2328 {
2329         char *p, *orig, *opt;
2330         int ret = 0;
2331
2332         p = orig = strdup(optarg);
2333
2334         output_format = 0;
2335
2336         while ((opt = strsep(&p, ",")) != NULL) {
2337                 if (!strcmp(opt, "minimal") ||
2338                     !strcmp(opt, "terse") ||
2339                     !strcmp(opt, "csv"))
2340                         output_format |= FIO_OUTPUT_TERSE;
2341                 else if (!strcmp(opt, "json"))
2342                         output_format |= FIO_OUTPUT_JSON;
2343                 else if (!strcmp(opt, "json+"))
2344                         output_format |= (FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS);
2345                 else if (!strcmp(opt, "normal"))
2346                         output_format |= FIO_OUTPUT_NORMAL;
2347                 else {
2348                         log_err("fio: invalid output format %s\n", opt);
2349                         ret = 1;
2350                         break;
2351                 }
2352         }
2353
2354         free(orig);
2355         return ret;
2356 }
2357
2358 int parse_cmd_line(int argc, char *argv[], int client_type)
2359 {
2360         struct thread_data *td = NULL;
2361         int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
2362         char *ostr = cmd_optstr;
2363         char *pid_file = NULL;
2364         void *cur_client = NULL;
2365         int backend = 0;
2366
2367         /*
2368          * Reset optind handling, since we may call this multiple times
2369          * for the backend.
2370          */
2371         optind = 1;
2372
2373         while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
2374                 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
2375                         parse_cmd_client(cur_client, argv[optind - 1]);
2376                         c &= ~FIO_CLIENT_FLAG;
2377                 }
2378
2379                 switch (c) {
2380                 case 'a':
2381                         smalloc_pool_size = atoi(optarg);
2382                         break;
2383                 case 't':
2384                         if (check_str_time(optarg, &def_timeout, 1)) {
2385                                 log_err("fio: failed parsing time %s\n", optarg);
2386                                 do_exit++;
2387                                 exit_val = 1;
2388                         }
2389                         break;
2390                 case 'l':
2391                         log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
2392                         do_exit++;
2393                         exit_val = 1;
2394                         break;
2395                 case 'b':
2396                         write_bw_log = 1;
2397                         break;
2398                 case 'o':
2399                         if (f_out && f_out != stdout)
2400                                 fclose(f_out);
2401
2402                         f_out = fopen(optarg, "w+");
2403                         if (!f_out) {
2404                                 perror("fopen output");
2405                                 exit(1);
2406                         }
2407                         f_err = f_out;
2408                         break;
2409                 case 'm':
2410                         output_format = FIO_OUTPUT_TERSE;
2411                         break;
2412                 case 'F':
2413                         if (parse_output_format(optarg)) {
2414                                 log_err("fio: failed parsing output-format\n");
2415                                 exit_val = 1;
2416                                 do_exit++;
2417                                 break;
2418                         }
2419                         break;
2420                 case 'f':
2421                         output_format |= FIO_OUTPUT_TERSE;
2422                         break;
2423                 case 'h':
2424                         did_arg = 1;
2425                         if (!cur_client) {
2426                                 usage(argv[0]);
2427                                 do_exit++;
2428                         }
2429                         break;
2430                 case 'c':
2431                         did_arg = 1;
2432                         if (!cur_client) {
2433                                 fio_show_option_help(optarg);
2434                                 do_exit++;
2435                         }
2436                         break;
2437                 case 'i':
2438                         did_arg = 1;
2439                         if (!cur_client) {
2440                                 fio_show_ioengine_help(optarg);
2441                                 do_exit++;
2442                         }
2443                         break;
2444                 case 's':
2445                         did_arg = 1;
2446                         dump_cmdline = 1;
2447                         break;
2448                 case 'r':
2449                         read_only = 1;
2450                         break;
2451                 case 'v':
2452                         did_arg = 1;
2453                         if (!cur_client) {
2454                                 log_info("%s\n", fio_version_string);
2455                                 do_exit++;
2456                         }
2457                         break;
2458                 case 'V':
2459                         terse_version = atoi(optarg);
2460                         if (!(terse_version == 2 || terse_version == 3 ||
2461                              terse_version == 4)) {
2462                                 log_err("fio: bad terse version format\n");
2463                                 exit_val = 1;
2464                                 do_exit++;
2465                         }
2466                         break;
2467                 case 'e':
2468                         if (!strcmp("always", optarg))
2469                                 eta_print = FIO_ETA_ALWAYS;
2470                         else if (!strcmp("never", optarg))
2471                                 eta_print = FIO_ETA_NEVER;
2472                         break;
2473                 case 'E': {
2474                         long long t = 0;
2475
2476                         if (check_str_time(optarg, &t, 1)) {
2477                                 log_err("fio: failed parsing eta time %s\n", optarg);
2478                                 exit_val = 1;
2479                                 do_exit++;
2480                         }
2481                         eta_new_line = t / 1000;
2482                         break;
2483                         }
2484                 case 'd':
2485                         if (set_debug(optarg))
2486                                 do_exit++;
2487                         break;
2488                 case 'P':
2489                         did_arg = 1;
2490                         parse_only = 1;
2491                         break;
2492                 case 'x': {
2493                         size_t new_size;
2494
2495                         if (!strcmp(optarg, "global")) {
2496                                 log_err("fio: can't use global as only "
2497                                         "section\n");
2498                                 do_exit++;
2499                                 exit_val = 1;
2500                                 break;
2501                         }
2502                         new_size = (nr_job_sections + 1) * sizeof(char *);
2503                         job_sections = realloc(job_sections, new_size);
2504                         job_sections[nr_job_sections] = strdup(optarg);
2505                         nr_job_sections++;
2506                         break;
2507                         }
2508 #ifdef CONFIG_ZLIB
2509                 case 'X':
2510                         exit_val = iolog_file_inflate(optarg);
2511                         did_arg++;
2512                         do_exit++;
2513                         break;
2514 #endif
2515                 case 'p':
2516                         did_arg = 1;
2517                         if (exec_profile)
2518                                 free(exec_profile);
2519                         exec_profile = strdup(optarg);
2520                         break;
2521                 case FIO_GETOPT_JOB: {
2522                         const char *opt = l_opts[lidx].name;
2523                         char *val = optarg;
2524
2525                         if (!strncmp(opt, "name", 4) && td) {
2526                                 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2527                                 if (ret)
2528                                         goto out_free;
2529                                 td = NULL;
2530                                 did_arg = 1;
2531                         }
2532                         if (!td) {
2533                                 int is_section = !strncmp(opt, "name", 4);
2534                                 int global = 0;
2535
2536                                 if (!is_section || !strncmp(val, "global", 6))
2537                                         global = 1;
2538
2539                                 if (is_section && skip_this_section(val))
2540                                         continue;
2541
2542                                 td = get_new_job(global, &def_thread, 1, NULL);
2543                                 if (!td || ioengine_load(td)) {
2544                                         if (td) {
2545                                                 put_job(td);
2546                                                 td = NULL;
2547                                         }
2548                                         do_exit++;
2549                                         exit_val = 1;
2550                                         break;
2551                                 }
2552                                 fio_options_set_ioengine_opts(l_opts, td);
2553                         }
2554
2555                         if ((!val || !strlen(val)) &&
2556                             l_opts[lidx].has_arg == required_argument) {
2557                                 log_err("fio: option %s requires an argument\n", opt);
2558                                 ret = 1;
2559                         } else
2560                                 ret = fio_cmd_option_parse(td, opt, val);
2561
2562                         if (ret) {
2563                                 if (td) {
2564                                         put_job(td);
2565                                         td = NULL;
2566                                 }
2567                                 do_exit++;
2568                                 exit_val = 1;
2569                         }
2570
2571                         if (!ret && !strcmp(opt, "ioengine")) {
2572                                 free_ioengine(td);
2573                                 if (ioengine_load(td)) {
2574                                         put_job(td);
2575                                         td = NULL;
2576                                         do_exit++;
2577                                         exit_val = 1;
2578                                         break;
2579                                 }
2580                                 fio_options_set_ioengine_opts(l_opts, td);
2581                         }
2582                         break;
2583                 }
2584                 case FIO_GETOPT_IOENGINE: {
2585                         const char *opt = l_opts[lidx].name;
2586                         char *val = optarg;
2587
2588                         if (!td)
2589                                 break;
2590
2591                         ret = fio_cmd_ioengine_option_parse(td, opt, val);
2592                         break;
2593                 }
2594                 case 'w':
2595                         warnings_fatal = 1;
2596                         break;
2597                 case 'j':
2598                         max_jobs = atoi(optarg);
2599                         if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2600                                 log_err("fio: invalid max jobs: %d\n", max_jobs);
2601                                 do_exit++;
2602                                 exit_val = 1;
2603                         }
2604                         break;
2605                 case 'S':
2606                         did_arg = 1;
2607 #ifndef CONFIG_NO_SHM
2608                         if (nr_clients) {
2609                                 log_err("fio: can't be both client and server\n");
2610                                 do_exit++;
2611                                 exit_val = 1;
2612                                 break;
2613                         }
2614                         if (optarg)
2615                                 fio_server_set_arg(optarg);
2616                         is_backend = 1;
2617                         backend = 1;
2618 #else
2619                         log_err("fio: client/server requires SHM support\n");
2620                         do_exit++;
2621                         exit_val = 1;
2622 #endif
2623                         break;
2624                 case 'D':
2625                         if (pid_file)
2626                                 free(pid_file);
2627                         pid_file = strdup(optarg);
2628                         break;
2629                 case 'I':
2630                         if ((ret = fio_idle_prof_parse_opt(optarg))) {
2631                                 /* exit on error and calibration only */
2632                                 did_arg = 1;
2633                                 do_exit++;
2634                                 if (ret == -1)
2635                                         exit_val = 1;
2636                         }
2637                         break;
2638                 case 'C':
2639                         did_arg = 1;
2640                         if (is_backend) {
2641                                 log_err("fio: can't be both client and server\n");
2642                                 do_exit++;
2643                                 exit_val = 1;
2644                                 break;
2645                         }
2646                         /* if --client parameter contains a pathname */
2647                         if (0 == access(optarg, R_OK)) {
2648                                 /* file contains a list of host addrs or names */
2649                                 char hostaddr[PATH_MAX] = {0};
2650                                 char formatstr[8];
2651                                 FILE * hostf = fopen(optarg, "r");
2652                                 if (!hostf) {
2653                                         log_err("fio: could not open client list file %s for read\n", optarg);
2654                                         do_exit++;
2655                                         exit_val = 1;
2656                                         break;
2657                                 }
2658                                 sprintf(formatstr, "%%%ds", PATH_MAX - 1);
2659                                 /*
2660                                  * read at most PATH_MAX-1 chars from each
2661                                  * record in this file
2662                                  */
2663                                 while (fscanf(hostf, formatstr, hostaddr) == 1) {
2664                                         /* expect EVERY host in file to be valid */
2665                                         if (fio_client_add(&fio_client_ops, hostaddr, &cur_client)) {
2666                                                 log_err("fio: failed adding client %s from file %s\n", hostaddr, optarg);
2667                                                 do_exit++;
2668                                                 exit_val = 1;
2669                                                 break;
2670                                         }
2671                                 }
2672                                 fclose(hostf);
2673                                 break; /* no possibility of job file for "this client only" */
2674                         }
2675                         if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
2676                                 log_err("fio: failed adding client %s\n", optarg);
2677                                 do_exit++;
2678                                 exit_val = 1;
2679                                 break;
2680                         }
2681                         /*
2682                          * If the next argument exists and isn't an option,
2683                          * assume it's a job file for this client only.
2684                          */
2685                         while (optind < argc) {
2686                                 if (!strncmp(argv[optind], "--", 2) ||
2687                                     !strncmp(argv[optind], "-", 1))
2688                                         break;
2689
2690                                 if (fio_client_add_ini_file(cur_client, argv[optind], false))
2691                                         break;
2692                                 optind++;
2693                         }
2694                         break;
2695                 case 'R':
2696                         did_arg = 1;
2697                         if (fio_client_add_ini_file(cur_client, optarg, true)) {
2698                                 do_exit++;
2699                                 exit_val = 1;
2700                         }
2701                         break;
2702                 case 'T':
2703                         did_arg = 1;
2704                         do_exit++;
2705                         exit_val = fio_monotonic_clocktest(1);
2706                         break;
2707                 case 'G':
2708                         did_arg = 1;
2709                         do_exit++;
2710                         exit_val = fio_crctest(optarg);
2711                         break;
2712                 case 'L': {
2713                         long long val;
2714
2715                         if (check_str_time(optarg, &val, 1)) {
2716                                 log_err("fio: failed parsing time %s\n", optarg);
2717                                 do_exit++;
2718                                 exit_val = 1;
2719                                 break;
2720                         }
2721                         status_interval = val / 1000;
2722                         break;
2723                         }
2724                 case 'W':
2725                         if (trigger_file)
2726                                 free(trigger_file);
2727                         trigger_file = strdup(optarg);
2728                         break;
2729                 case 'H':
2730                         if (trigger_cmd)
2731                                 free(trigger_cmd);
2732                         trigger_cmd = strdup(optarg);
2733                         break;
2734                 case 'J':
2735                         if (trigger_remote_cmd)
2736                                 free(trigger_remote_cmd);
2737                         trigger_remote_cmd = strdup(optarg);
2738                         break;
2739                 case 'K':
2740                         if (aux_path)
2741                                 free(aux_path);
2742                         aux_path = strdup(optarg);
2743                         break;
2744                 case 'B':
2745                         if (check_str_time(optarg, &trigger_timeout, 1)) {
2746                                 log_err("fio: failed parsing time %s\n", optarg);
2747                                 do_exit++;
2748                                 exit_val = 1;
2749                         }
2750                         trigger_timeout /= 1000000;
2751                         break;
2752                 case '?':
2753                         log_err("%s: unrecognized option '%s'\n", argv[0],
2754                                                         argv[optind - 1]);
2755                         show_closest_option(argv[optind - 1]);
2756                 default:
2757                         do_exit++;
2758                         exit_val = 1;
2759                         break;
2760                 }
2761                 if (do_exit)
2762                         break;
2763         }
2764
2765         if (do_exit && !(is_backend || nr_clients))
2766                 exit(exit_val);
2767
2768         if (nr_clients && fio_clients_connect())
2769                 exit(1);
2770
2771         if (is_backend && backend)
2772                 return fio_start_server(pid_file);
2773         else if (pid_file)
2774                 free(pid_file);
2775
2776         if (td) {
2777                 if (!ret) {
2778                         ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2779                         if (ret)
2780                                 did_arg = 1;
2781                 }
2782         }
2783
2784         while (!ret && optind < argc) {
2785                 ini_idx++;
2786                 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2787                 ini_file[ini_idx - 1] = strdup(argv[optind]);
2788                 optind++;
2789         }
2790
2791 out_free:
2792         if (pid_file)
2793                 free(pid_file);
2794
2795         return ini_idx;
2796 }
2797
2798 int fio_init_options(void)
2799 {
2800         f_out = stdout;
2801         f_err = stderr;
2802
2803         fio_options_fill_optstring();
2804         fio_options_dup_and_init(l_opts);
2805
2806         atexit(free_shm);
2807
2808         if (fill_def_thread())
2809                 return 1;
2810
2811         return 0;
2812 }
2813
2814 extern int fio_check_options(struct thread_options *);
2815
2816 int parse_options(int argc, char *argv[])
2817 {
2818         const int type = FIO_CLIENT_TYPE_CLI;
2819         int job_files, i;
2820
2821         if (fio_init_options())
2822                 return 1;
2823         if (fio_test_cconv(&def_thread.o))
2824                 log_err("fio: failed internal cconv test\n");
2825
2826         job_files = parse_cmd_line(argc, argv, type);
2827
2828         if (job_files > 0) {
2829                 for (i = 0; i < job_files; i++) {
2830                         if (i && fill_def_thread())
2831                                 return 1;
2832                         if (nr_clients) {
2833                                 if (fio_clients_send_ini(ini_file[i]))
2834                                         return 1;
2835                                 free(ini_file[i]);
2836                         } else if (!is_backend) {
2837                                 if (parse_jobs_ini(ini_file[i], 0, i, type))
2838                                         return 1;
2839                                 free(ini_file[i]);
2840                         }
2841                 }
2842         } else if (nr_clients) {
2843                 if (fill_def_thread())
2844                         return 1;
2845                 if (fio_clients_send_ini(NULL))
2846                         return 1;
2847         }
2848
2849         free(ini_file);
2850         fio_options_free(&def_thread);
2851         filesetup_mem_free();
2852
2853         if (!thread_number) {
2854                 if (parse_dryrun())
2855                         return 0;
2856                 if (exec_profile)
2857                         return 0;
2858                 if (is_backend || nr_clients)
2859                         return 0;
2860                 if (did_arg)
2861                         return 0;
2862
2863                 log_err("No jobs(s) defined\n\n");
2864
2865                 if (!did_arg) {
2866                         usage(argv[0]);
2867                         return 1;
2868                 }
2869
2870                 return 0;
2871         }
2872
2873         if (output_format & FIO_OUTPUT_NORMAL)
2874                 log_info("%s\n", fio_version_string);
2875
2876         return 0;
2877 }
2878
2879 void options_default_fill(struct thread_options *o)
2880 {
2881         memcpy(o, &def_thread.o, sizeof(*o));
2882 }
2883
2884 struct thread_data *get_global_options(void)
2885 {
2886         return &def_thread;
2887 }