Merge branch 'master' of ssh://git.kernel.dk/data/git/fio
[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
29 #include "lib/getopt.h"
30 #include "lib/strcasestr.h"
31
32 #include "crc/test.h"
33
34 const char fio_version_string[] = FIO_VERSION;
35
36 #define FIO_RANDSEED            (0xb1899bedUL)
37
38 static char **ini_file;
39 static int max_jobs = FIO_MAX_JOBS;
40 static int dump_cmdline;
41 static int def_timeout;
42 static int parse_only;
43
44 static struct thread_data def_thread;
45 struct thread_data *threads = NULL;
46 static char **job_sections;
47 static int nr_job_sections;
48
49 int exitall_on_terminate = 0;
50 int output_format = FIO_OUTPUT_NORMAL;
51 int append_terse_output = 0;
52 int eta_print = FIO_ETA_AUTO;
53 int eta_new_line = 0;
54 FILE *f_out = NULL;
55 FILE *f_err = NULL;
56 char *exec_profile = NULL;
57 int warnings_fatal = 0;
58 int terse_version = 3;
59 int is_backend = 0;
60 int nr_clients = 0;
61 int log_syslog = 0;
62
63 int write_bw_log = 0;
64 int read_only = 0;
65 int status_interval = 0;
66
67 static int write_lat_log;
68
69 static int prev_group_jobs;
70
71 unsigned long fio_debug = 0;
72 unsigned int fio_debug_jobno = -1;
73 unsigned int *fio_debug_jobp = NULL;
74
75 static char cmd_optstr[256];
76 static int did_arg;
77
78 #define FIO_CLIENT_FLAG         (1 << 16)
79
80 /*
81  * Command line options. These will contain the above, plus a few
82  * extra that only pertain to fio itself and not jobs.
83  */
84 static struct option l_opts[FIO_NR_OPTIONS] = {
85         {
86                 .name           = (char *) "output",
87                 .has_arg        = required_argument,
88                 .val            = 'o' | FIO_CLIENT_FLAG,
89         },
90         {
91                 .name           = (char *) "timeout",
92                 .has_arg        = required_argument,
93                 .val            = 't' | FIO_CLIENT_FLAG,
94         },
95         {
96                 .name           = (char *) "latency-log",
97                 .has_arg        = required_argument,
98                 .val            = 'l' | FIO_CLIENT_FLAG,
99         },
100         {
101                 .name           = (char *) "bandwidth-log",
102                 .has_arg        = required_argument,
103                 .val            = 'b' | FIO_CLIENT_FLAG,
104         },
105         {
106                 .name           = (char *) "minimal",
107                 .has_arg        = no_argument,
108                 .val            = 'm' | FIO_CLIENT_FLAG,
109         },
110         {
111                 .name           = (char *) "output-format",
112                 .has_arg        = optional_argument,
113                 .val            = 'F' | FIO_CLIENT_FLAG,
114         },
115         {
116                 .name           = (char *) "append-terse",
117                 .has_arg        = optional_argument,
118                 .val            = 'f',
119         },
120         {
121                 .name           = (char *) "version",
122                 .has_arg        = no_argument,
123                 .val            = 'v' | FIO_CLIENT_FLAG,
124         },
125         {
126                 .name           = (char *) "help",
127                 .has_arg        = no_argument,
128                 .val            = 'h' | FIO_CLIENT_FLAG,
129         },
130         {
131                 .name           = (char *) "cmdhelp",
132                 .has_arg        = optional_argument,
133                 .val            = 'c' | FIO_CLIENT_FLAG,
134         },
135         {
136                 .name           = (char *) "enghelp",
137                 .has_arg        = optional_argument,
138                 .val            = 'i' | FIO_CLIENT_FLAG,
139         },
140         {
141                 .name           = (char *) "showcmd",
142                 .has_arg        = no_argument,
143                 .val            = 's' | FIO_CLIENT_FLAG,
144         },
145         {
146                 .name           = (char *) "readonly",
147                 .has_arg        = no_argument,
148                 .val            = 'r' | FIO_CLIENT_FLAG,
149         },
150         {
151                 .name           = (char *) "eta",
152                 .has_arg        = required_argument,
153                 .val            = 'e' | FIO_CLIENT_FLAG,
154         },
155         {
156                 .name           = (char *) "eta-newline",
157                 .has_arg        = required_argument,
158                 .val            = 'E' | FIO_CLIENT_FLAG,
159         },
160         {
161                 .name           = (char *) "debug",
162                 .has_arg        = required_argument,
163                 .val            = 'd' | FIO_CLIENT_FLAG,
164         },
165         {
166                 .name           = (char *) "parse-only",
167                 .has_arg        = no_argument,
168                 .val            = 'P' | FIO_CLIENT_FLAG,
169         },
170         {
171                 .name           = (char *) "section",
172                 .has_arg        = required_argument,
173                 .val            = 'x' | FIO_CLIENT_FLAG,
174         },
175         {
176                 .name           = (char *) "alloc-size",
177                 .has_arg        = required_argument,
178                 .val            = 'a' | FIO_CLIENT_FLAG,
179         },
180         {
181                 .name           = (char *) "profile",
182                 .has_arg        = required_argument,
183                 .val            = 'p' | FIO_CLIENT_FLAG,
184         },
185         {
186                 .name           = (char *) "warnings-fatal",
187                 .has_arg        = no_argument,
188                 .val            = 'w' | FIO_CLIENT_FLAG,
189         },
190         {
191                 .name           = (char *) "max-jobs",
192                 .has_arg        = required_argument,
193                 .val            = 'j' | FIO_CLIENT_FLAG,
194         },
195         {
196                 .name           = (char *) "terse-version",
197                 .has_arg        = required_argument,
198                 .val            = 'V' | FIO_CLIENT_FLAG,
199         },
200         {
201                 .name           = (char *) "server",
202                 .has_arg        = optional_argument,
203                 .val            = 'S',
204         },
205         {       .name           = (char *) "daemonize",
206                 .has_arg        = required_argument,
207                 .val            = 'D',
208         },
209         {
210                 .name           = (char *) "client",
211                 .has_arg        = required_argument,
212                 .val            = 'C',
213         },
214         {
215                 .name           = (char *) "cpuclock-test",
216                 .has_arg        = no_argument,
217                 .val            = 'T',
218         },
219         {
220                 .name           = (char *) "crctest",
221                 .has_arg        = optional_argument,
222                 .val            = 'G',
223         },
224         {
225                 .name           = (char *) "idle-prof",
226                 .has_arg        = required_argument,
227                 .val            = 'I',
228         },
229         {
230                 .name           = (char *) "status-interval",
231                 .has_arg        = required_argument,
232                 .val            = 'L',
233         },
234         {
235                 .name           = NULL,
236         },
237 };
238
239 void free_threads_shm(void)
240 {
241         struct shmid_ds sbuf;
242
243         if (threads) {
244                 void *tp = threads;
245
246                 threads = NULL;
247                 shmdt(tp);
248                 shmctl(shm_id, IPC_RMID, &sbuf);
249                 shm_id = -1;
250         }
251 }
252
253 static void free_shm(void)
254 {
255         if (threads) {
256                 file_hash_exit();
257                 flow_exit();
258                 fio_debug_jobp = NULL;
259                 free_threads_shm();
260         }
261
262         options_free(fio_options, &def_thread);
263         fio_filelock_exit();
264         scleanup();
265 }
266
267 /*
268  * The thread area is shared between the main process and the job
269  * threads/processes. So setup a shared memory segment that will hold
270  * all the job info. We use the end of the region for keeping track of
271  * open files across jobs, for file sharing.
272  */
273 static int setup_thread_area(void)
274 {
275         void *hash;
276
277         if (threads)
278                 return 0;
279
280         /*
281          * 1024 is too much on some machines, scale max_jobs if
282          * we get a failure that looks like too large a shm segment
283          */
284         do {
285                 size_t size = max_jobs * sizeof(struct thread_data);
286
287                 size += file_hash_size;
288                 size += sizeof(unsigned int);
289
290                 shm_id = shmget(0, size, IPC_CREAT | 0600);
291                 if (shm_id != -1)
292                         break;
293                 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
294                         perror("shmget");
295                         break;
296                 }
297
298                 max_jobs >>= 1;
299         } while (max_jobs);
300
301         if (shm_id == -1)
302                 return 1;
303
304         threads = shmat(shm_id, NULL, 0);
305         if (threads == (void *) -1) {
306                 perror("shmat");
307                 return 1;
308         }
309
310         memset(threads, 0, max_jobs * sizeof(struct thread_data));
311         hash = (void *) threads + max_jobs * sizeof(struct thread_data);
312         fio_debug_jobp = (void *) hash + file_hash_size;
313         *fio_debug_jobp = -1;
314         file_hash_init(hash);
315
316         flow_init();
317
318         return 0;
319 }
320
321 /*
322  * Return a free job structure.
323  */
324 static struct thread_data *get_new_job(int global, struct thread_data *parent,
325                                        int preserve_eo)
326 {
327         struct thread_data *td;
328
329         if (global)
330                 return &def_thread;
331         if (setup_thread_area()) {
332                 log_err("error: failed to setup shm segment\n");
333                 return NULL;
334         }
335         if (thread_number >= max_jobs) {
336                 log_err("error: maximum number of jobs (%d) reached.\n",
337                                 max_jobs);
338                 return NULL;
339         }
340
341         td = &threads[thread_number++];
342         *td = *parent;
343
344         td->io_ops = NULL;
345         if (!preserve_eo)
346                 td->eo = NULL;
347
348         td->o.uid = td->o.gid = -1U;
349
350         dup_files(td, parent);
351         fio_options_mem_dupe(td);
352
353         profile_add_hooks(td);
354
355         td->thread_number = thread_number;
356
357         if (!parent || !parent->o.group_reporting)
358                 stat_number++;
359
360         return td;
361 }
362
363 static void put_job(struct thread_data *td)
364 {
365         if (td == &def_thread)
366                 return;
367
368         profile_td_exit(td);
369         flow_exit_job(td);
370
371         if (td->error)
372                 log_info("fio: %s\n", td->verror);
373
374         fio_options_free(td);
375         if (td->io_ops)
376                 free_ioengine(td);
377
378         memset(&threads[td->thread_number - 1], 0, sizeof(*td));
379         thread_number--;
380 }
381
382 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
383 {
384         unsigned int bs = td->o.min_bs[ddir];
385
386         assert(ddir_rw(ddir));
387
388         if (td->o.rate[ddir])
389                 td->rate_bps[ddir] = td->o.rate[ddir];
390         else
391                 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
392
393         if (!td->rate_bps[ddir]) {
394                 log_err("rate lower than supported\n");
395                 return -1;
396         }
397
398         td->rate_pending_usleep[ddir] = 0;
399         return 0;
400 }
401
402 static int setup_rate(struct thread_data *td)
403 {
404         int ret = 0;
405
406         if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
407                 ret = __setup_rate(td, DDIR_READ);
408         if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
409                 ret |= __setup_rate(td, DDIR_WRITE);
410         if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
411                 ret |= __setup_rate(td, DDIR_TRIM);
412
413         return ret;
414 }
415
416 static int fixed_block_size(struct thread_options *o)
417 {
418         return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
419                 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
420                 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
421                 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
422                 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
423 }
424
425
426 static unsigned long long get_rand_start_delay(struct thread_data *td)
427 {
428         unsigned long long delayrange;
429         unsigned long r;
430
431         delayrange = td->o.start_delay_high - td->o.start_delay;
432
433         if (td->o.use_os_rand) {
434                 r = os_random_long(&td->delay_state);
435                 delayrange = (unsigned long long) ((double) delayrange * (r / (OS_RAND_MAX + 1.0)));
436         } else {
437                 r = __rand(&td->__delay_state);
438                 delayrange = (unsigned long long) ((double) delayrange * (r / (FRAND_MAX + 1.0)));
439         }
440
441         delayrange += td->o.start_delay;
442         return delayrange;
443 }
444
445 /*
446  * Lazy way of fixing up options that depend on each other. We could also
447  * define option callback handlers, but this is easier.
448  */
449 static int fixup_options(struct thread_data *td)
450 {
451         struct thread_options *o = &td->o;
452         int ret = 0;
453
454 #ifndef FIO_HAVE_PSHARED_MUTEX
455         if (!o->use_thread) {
456                 log_info("fio: this platform does not support process shared"
457                          " mutexes, forcing use of threads. Use the 'thread'"
458                          " option to get rid of this warning.\n");
459                 o->use_thread = 1;
460                 ret = warnings_fatal;
461         }
462 #endif
463
464         if (o->write_iolog_file && o->read_iolog_file) {
465                 log_err("fio: read iolog overrides write_iolog\n");
466                 free(o->write_iolog_file);
467                 o->write_iolog_file = NULL;
468                 ret = warnings_fatal;
469         }
470
471         /*
472          * only really works with 1 file
473          */
474         if (o->zone_size && o->open_files > 1)
475                 o->zone_size = 0;
476
477         /*
478          * If zone_range isn't specified, backward compatibility dictates it
479          * should be made equal to zone_size.
480          */
481         if (o->zone_size && !o->zone_range)
482                 o->zone_range = o->zone_size;
483
484         /*
485          * Reads can do overwrites, we always need to pre-create the file
486          */
487         if (td_read(td) || td_rw(td))
488                 o->overwrite = 1;
489
490         if (!o->min_bs[DDIR_READ])
491                 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
492         if (!o->max_bs[DDIR_READ])
493                 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
494         if (!o->min_bs[DDIR_WRITE])
495                 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
496         if (!o->max_bs[DDIR_WRITE])
497                 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
498         if (!o->min_bs[DDIR_TRIM])
499                 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
500         if (!o->max_bs[DDIR_TRIM])
501                 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
502
503
504         o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
505         o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
506
507         /*
508          * For random IO, allow blockalign offset other than min_bs.
509          */
510         if (!o->ba[DDIR_READ] || !td_random(td))
511                 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
512         if (!o->ba[DDIR_WRITE] || !td_random(td))
513                 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
514         if (!o->ba[DDIR_TRIM] || !td_random(td))
515                 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
516
517         if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
518             o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
519             o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
520             !o->norandommap) {
521                 log_err("fio: Any use of blockalign= turns off randommap\n");
522                 o->norandommap = 1;
523                 ret = warnings_fatal;
524         }
525
526         if (!o->file_size_high)
527                 o->file_size_high = o->file_size_low;
528
529         if (o->start_delay_high)
530                 o->start_delay = get_rand_start_delay(td);
531
532         if (o->norandommap && o->verify != VERIFY_NONE
533             && !fixed_block_size(o))  {
534                 log_err("fio: norandommap given for variable block sizes, "
535                         "verify disabled\n");
536                 o->verify = VERIFY_NONE;
537                 ret = warnings_fatal;
538         }
539         if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
540                 log_err("fio: bs_unaligned may not work with raw io\n");
541
542         /*
543          * thinktime_spin must be less than thinktime
544          */
545         if (o->thinktime_spin > o->thinktime)
546                 o->thinktime_spin = o->thinktime;
547
548         /*
549          * The low water mark cannot be bigger than the iodepth
550          */
551         if (o->iodepth_low > o->iodepth || !o->iodepth_low)
552                 o->iodepth_low = o->iodepth;
553
554         /*
555          * If batch number isn't set, default to the same as iodepth
556          */
557         if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
558                 o->iodepth_batch = o->iodepth;
559
560         if (o->nr_files > td->files_index)
561                 o->nr_files = td->files_index;
562
563         if (o->open_files > o->nr_files || !o->open_files)
564                 o->open_files = o->nr_files;
565
566         if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
567             (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
568             ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
569             (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
570                 log_err("fio: rate and rate_iops are mutually exclusive\n");
571                 ret = 1;
572         }
573         if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
574             (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
575             (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
576             (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
577             (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
578             (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
579                 log_err("fio: minimum rate exceeds rate\n");
580                 ret = 1;
581         }
582
583         if (!o->timeout && o->time_based) {
584                 log_err("fio: time_based requires a runtime/timeout setting\n");
585                 o->time_based = 0;
586                 ret = warnings_fatal;
587         }
588
589         if (o->fill_device && !o->size)
590                 o->size = -1ULL;
591
592         if (o->verify != VERIFY_NONE) {
593                 if (td_write(td) && o->do_verify && o->numjobs > 1) {
594                         log_info("Multiple writers may overwrite blocks that "
595                                 "belong to other jobs. This can cause "
596                                 "verification failures.\n");
597                         ret = warnings_fatal;
598                 }
599
600                 o->refill_buffers = 1;
601                 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
602                     !o->verify_interval)
603                         o->verify_interval = o->min_bs[DDIR_WRITE];
604         }
605
606         if (o->pre_read) {
607                 o->invalidate_cache = 0;
608                 if (td->io_ops->flags & FIO_PIPEIO) {
609                         log_info("fio: cannot pre-read files with an IO engine"
610                                  " that isn't seekable. Pre-read disabled.\n");
611                         ret = warnings_fatal;
612                 }
613         }
614
615         if (!o->unit_base) {
616                 if (td->io_ops->flags & FIO_BIT_BASED)
617                         o->unit_base = 1;
618                 else
619                         o->unit_base = 8;
620         }
621
622 #ifndef CONFIG_FDATASYNC
623         if (o->fdatasync_blocks) {
624                 log_info("fio: this platform does not support fdatasync()"
625                          " falling back to using fsync().  Use the 'fsync'"
626                          " option instead of 'fdatasync' to get rid of"
627                          " this warning\n");
628                 o->fsync_blocks = o->fdatasync_blocks;
629                 o->fdatasync_blocks = 0;
630                 ret = warnings_fatal;
631         }
632 #endif
633
634 #ifdef WIN32
635         /*
636          * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
637          * so fail if we're passed those flags
638          */
639         if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
640                 log_err("fio: Windows does not support direct or non-buffered io with"
641                                 " the synchronous ioengines. Use the 'windowsaio' ioengine"
642                                 " with 'direct=1' and 'iodepth=1' instead.\n");
643                 ret = 1;
644         }
645 #endif
646
647         /*
648          * For fully compressible data, just zero them at init time.
649          * It's faster than repeatedly filling it.
650          */
651         if (td->o.compress_percentage == 100) {
652                 td->o.zero_buffers = 1;
653                 td->o.compress_percentage = 0;
654         }
655
656         /*
657          * Using a non-uniform random distribution excludes usage of
658          * a random map
659          */
660         if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
661                 td->o.norandommap = 1;
662
663         /*
664          * If size is set but less than the min block size, complain
665          */
666         if (o->size && o->size < td_min_bs(td)) {
667                 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
668                 ret = 1;
669         }
670
671         /*
672          * O_ATOMIC implies O_DIRECT
673          */
674         if (td->o.oatomic)
675                 td->o.odirect = 1;
676
677         /*
678          * If randseed is set, that overrides randrepeat
679          */
680         if (td->o.rand_seed)
681                 td->o.rand_repeatable = 0;
682
683         if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
684                 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
685                 ret = 1;
686         }
687
688         return ret;
689 }
690
691 /*
692  * This function leaks the buffer
693  */
694 char *fio_uint_to_kmg(unsigned int val)
695 {
696         char *buf = malloc(32);
697         char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
698         char *p = post;
699
700         do {
701                 if (val & 1023)
702                         break;
703
704                 val >>= 10;
705                 p++;
706         } while (*p);
707
708         snprintf(buf, 32, "%u%c", val, *p);
709         return buf;
710 }
711
712 /* External engines are specified by "external:name.o") */
713 static const char *get_engine_name(const char *str)
714 {
715         char *p = strstr(str, ":");
716
717         if (!p)
718                 return str;
719
720         p++;
721         strip_blank_front(&p);
722         strip_blank_end(p);
723         return p;
724 }
725
726 static int exists_and_not_file(const char *filename)
727 {
728         struct stat sb;
729
730         if (lstat(filename, &sb) == -1)
731                 return 0;
732
733         /* \\.\ is the device namespace in Windows, where every file
734          * is a device node */
735         if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
736                 return 0;
737
738         return 1;
739 }
740
741 static void td_fill_rand_seeds_os(struct thread_data *td)
742 {
743         os_random_seed(td->rand_seeds[FIO_RAND_BS_OFF], &td->bsrange_state);
744         os_random_seed(td->rand_seeds[FIO_RAND_VER_OFF], &td->verify_state);
745         os_random_seed(td->rand_seeds[FIO_RAND_MIX_OFF], &td->rwmix_state);
746
747         if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
748                 os_random_seed(td->rand_seeds[FIO_RAND_FILE_OFF], &td->next_file_state);
749
750         os_random_seed(td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], &td->file_size_state);
751         os_random_seed(td->rand_seeds[FIO_RAND_TRIM_OFF], &td->trim_state);
752         os_random_seed(td->rand_seeds[FIO_RAND_START_DELAY], &td->delay_state);
753
754         if (!td_random(td))
755                 return;
756
757         if (td->o.rand_repeatable)
758                 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
759
760         os_random_seed(td->rand_seeds[FIO_RAND_BLOCK_OFF], &td->random_state);
761
762         os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], &td->seq_rand_state[DDIR_READ]);
763         os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF], &td->seq_rand_state[DDIR_WRITE]);
764         os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF], &td->seq_rand_state[DDIR_TRIM]);
765 }
766
767 static void td_fill_rand_seeds_internal(struct thread_data *td)
768 {
769         init_rand_seed(&td->__bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
770         init_rand_seed(&td->__verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
771         init_rand_seed(&td->__rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
772
773         if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
774                 init_rand_seed(&td->__next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
775
776         init_rand_seed(&td->__file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
777         init_rand_seed(&td->__trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
778         init_rand_seed(&td->__delay_state, td->rand_seeds[FIO_RAND_START_DELAY]);
779
780         if (!td_random(td))
781                 return;
782
783         if (td->o.rand_repeatable)
784                 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
785
786         init_rand_seed(&td->__random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
787         init_rand_seed(&td->__seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF]);
788         init_rand_seed(&td->__seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF]);
789         init_rand_seed(&td->__seq_rand_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF]);
790 }
791
792 void td_fill_rand_seeds(struct thread_data *td)
793 {
794         if (td->o.allrand_repeatable) {
795                 for (int i = 0; i < FIO_RAND_NR_OFFS; i++)
796                         td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
797                                 + i;
798         }
799
800         if (td->o.use_os_rand)
801                 td_fill_rand_seeds_os(td);
802         else
803                 td_fill_rand_seeds_internal(td);
804
805         init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
806 }
807
808 /*
809  * Initializes the ioengine configured for a job, if it has not been done so
810  * already.
811  */
812 int ioengine_load(struct thread_data *td)
813 {
814         const char *engine;
815
816         /*
817          * Engine has already been loaded.
818          */
819         if (td->io_ops)
820                 return 0;
821         if (!td->o.ioengine) {
822                 log_err("fio: internal fault, no IO engine specified\n");
823                 return 1;
824         }
825
826         engine = get_engine_name(td->o.ioengine);
827         td->io_ops = load_ioengine(td, engine);
828         if (!td->io_ops) {
829                 log_err("fio: failed to load engine %s\n", engine);
830                 return 1;
831         }
832
833         if (td->io_ops->option_struct_size && td->io_ops->options) {
834                 /*
835                  * In cases where td->eo is set, clone it for a child thread.
836                  * This requires that the parent thread has the same ioengine,
837                  * but that requirement must be enforced by the code which
838                  * cloned the thread.
839                  */
840                 void *origeo = td->eo;
841                 /*
842                  * Otherwise use the default thread options.
843                  */
844                 if (!origeo && td != &def_thread && def_thread.eo &&
845                     def_thread.io_ops->options == td->io_ops->options)
846                         origeo = def_thread.eo;
847
848                 options_init(td->io_ops->options);
849                 td->eo = malloc(td->io_ops->option_struct_size);
850                 /*
851                  * Use the default thread as an option template if this uses the
852                  * same options structure and there are non-default options
853                  * used.
854                  */
855                 if (origeo) {
856                         memcpy(td->eo, origeo, td->io_ops->option_struct_size);
857                         options_mem_dupe(td->eo, td->io_ops->options);
858                 } else {
859                         memset(td->eo, 0, td->io_ops->option_struct_size);
860                         fill_default_options(td->eo, td->io_ops->options);
861                 }
862                 *(struct thread_data **)td->eo = td;
863         }
864
865         return 0;
866 }
867
868 static void init_flags(struct thread_data *td)
869 {
870         struct thread_options *o = &td->o;
871
872         if (o->verify_backlog)
873                 td->flags |= TD_F_VER_BACKLOG;
874         if (o->trim_backlog)
875                 td->flags |= TD_F_TRIM_BACKLOG;
876         if (o->read_iolog_file)
877                 td->flags |= TD_F_READ_IOLOG;
878         if (o->refill_buffers)
879                 td->flags |= TD_F_REFILL_BUFFERS;
880         if (o->scramble_buffers)
881                 td->flags |= TD_F_SCRAMBLE_BUFFERS;
882         if (o->verify != VERIFY_NONE)
883                 td->flags |= TD_F_VER_NONE;
884 }
885
886 static int setup_random_seeds(struct thread_data *td)
887 {
888         unsigned long seed;
889         unsigned int i;
890
891         if (!td->o.rand_repeatable && !td->o.rand_seed)
892                 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
893
894         if (!td->o.rand_seed)
895                 seed = 0x89;
896         else
897                 seed = td->o.rand_seed;
898
899         for (i = 0; i < 4; i++)
900                 seed *= 0x9e370001UL;
901
902         for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
903                 td->rand_seeds[i] = seed;
904                 seed *= 0x9e370001UL;
905         }
906
907         td_fill_rand_seeds(td);
908         return 0;
909 }
910
911 enum {
912         FPRE_NONE = 0,
913         FPRE_JOBNAME,
914         FPRE_JOBNUM,
915         FPRE_FILENUM
916 };
917
918 static struct fpre_keyword {
919         const char *keyword;
920         size_t strlen;
921         int key;
922 } fpre_keywords[] = {
923         { .keyword = "$jobname",        .key = FPRE_JOBNAME, },
924         { .keyword = "$jobnum",         .key = FPRE_JOBNUM, },
925         { .keyword = "$filenum",        .key = FPRE_FILENUM, },
926         { .keyword = NULL, },
927         };
928
929 static char *make_filename(char *buf, struct thread_options *o,
930                            const char *jobname, int jobnum, int filenum)
931 {
932         struct fpre_keyword *f;
933         char copy[PATH_MAX];
934
935         if (!o->filename_format || !strlen(o->filename_format)) {
936                 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
937                 return NULL;
938         }
939
940         for (f = &fpre_keywords[0]; f->keyword; f++)
941                 f->strlen = strlen(f->keyword);
942
943         strcpy(buf, o->filename_format);
944         memset(copy, 0, sizeof(copy));
945         for (f = &fpre_keywords[0]; f->keyword; f++) {
946                 do {
947                         size_t pre_len, post_start = 0;
948                         char *str, *dst = copy;
949
950                         str = strcasestr(buf, f->keyword);
951                         if (!str)
952                                 break;
953
954                         pre_len = str - buf;
955                         if (strlen(str) != f->strlen)
956                                 post_start = pre_len + f->strlen;
957
958                         if (pre_len) {
959                                 strncpy(dst, buf, pre_len);
960                                 dst += pre_len;
961                         }
962
963                         switch (f->key) {
964                         case FPRE_JOBNAME:
965                                 dst += sprintf(dst, "%s", jobname);
966                                 break;
967                         case FPRE_JOBNUM:
968                                 dst += sprintf(dst, "%d", jobnum);
969                                 break;
970                         case FPRE_FILENUM:
971                                 dst += sprintf(dst, "%d", filenum);
972                                 break;
973                         default:
974                                 assert(0);
975                                 break;
976                         }
977
978                         if (post_start)
979                                 strcpy(dst, buf + post_start);
980
981                         strcpy(buf, copy);
982                 } while (1);
983         }
984
985         return buf;
986 }
987
988 int parse_dryrun(void)
989 {
990         return dump_cmdline || parse_only;
991 }
992
993 /*
994  * Adds a job to the list of things todo. Sanitizes the various options
995  * to make sure we don't have conflicts, and initializes various
996  * members of td.
997  */
998 static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
999                    int recursed, int client_type)
1000 {
1001         unsigned int i;
1002         char fname[PATH_MAX];
1003         int numjobs, file_alloced;
1004         struct thread_options *o = &td->o;
1005
1006         /*
1007          * the def_thread is just for options, it's not a real job
1008          */
1009         if (td == &def_thread)
1010                 return 0;
1011
1012         init_flags(td);
1013
1014         /*
1015          * if we are just dumping the output command line, don't add the job
1016          */
1017         if (parse_dryrun()) {
1018                 put_job(td);
1019                 return 0;
1020         }
1021
1022         td->client_type = client_type;
1023
1024         if (profile_td_init(td))
1025                 goto err;
1026
1027         if (ioengine_load(td))
1028                 goto err;
1029
1030         if (o->odirect)
1031                 td->io_ops->flags |= FIO_RAWIO;
1032
1033         file_alloced = 0;
1034         if (!o->filename && !td->files_index && !o->read_iolog_file) {
1035                 file_alloced = 1;
1036
1037                 if (o->nr_files == 1 && exists_and_not_file(jobname))
1038                         add_file(td, jobname, job_add_num, 0);
1039                 else {
1040                         for (i = 0; i < o->nr_files; i++)
1041                                 add_file(td, make_filename(fname, o, jobname, job_add_num, i), job_add_num, 0);
1042                 }
1043         }
1044
1045         if (fixup_options(td))
1046                 goto err;
1047
1048         flow_init_job(td);
1049
1050         /*
1051          * IO engines only need this for option callbacks, and the address may
1052          * change in subprocesses.
1053          */
1054         if (td->eo)
1055                 *(struct thread_data **)td->eo = NULL;
1056
1057         if (td->io_ops->flags & FIO_DISKLESSIO) {
1058                 struct fio_file *f;
1059
1060                 for_each_file(td, f, i)
1061                         f->real_file_size = -1ULL;
1062         }
1063
1064         td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1065
1066         td->ts.clat_percentiles = o->clat_percentiles;
1067         td->ts.percentile_precision = o->percentile_precision;
1068         memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1069
1070         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1071                 td->ts.clat_stat[i].min_val = ULONG_MAX;
1072                 td->ts.slat_stat[i].min_val = ULONG_MAX;
1073                 td->ts.lat_stat[i].min_val = ULONG_MAX;
1074                 td->ts.bw_stat[i].min_val = ULONG_MAX;
1075         }
1076         td->ddir_seq_nr = o->ddir_seq_nr;
1077
1078         if ((o->stonewall || o->new_group) && prev_group_jobs) {
1079                 prev_group_jobs = 0;
1080                 groupid++;
1081         }
1082
1083         td->groupid = groupid;
1084         prev_group_jobs++;
1085
1086         if (setup_random_seeds(td)) {
1087                 td_verror(td, errno, "init_random_state");
1088                 goto err;
1089         }
1090
1091         if (setup_rate(td))
1092                 goto err;
1093
1094         if (o->lat_log_file || write_lat_log) {
1095                 setup_log(&td->lat_log, o->log_avg_msec, IO_LOG_TYPE_LAT);
1096                 setup_log(&td->slat_log, o->log_avg_msec, IO_LOG_TYPE_SLAT);
1097                 setup_log(&td->clat_log, o->log_avg_msec, IO_LOG_TYPE_CLAT);
1098         }
1099         if (o->bw_log_file || write_bw_log)
1100                 setup_log(&td->bw_log, o->log_avg_msec, IO_LOG_TYPE_BW);
1101         if (o->iops_log_file)
1102                 setup_log(&td->iops_log, o->log_avg_msec, IO_LOG_TYPE_IOPS);
1103
1104         if (!o->name)
1105                 o->name = strdup(jobname);
1106
1107         if (output_format == FIO_OUTPUT_NORMAL) {
1108                 if (!job_add_num) {
1109                         if (is_backend && !recursed)
1110                                 fio_server_send_add_job(td);
1111
1112                         if (!(td->io_ops->flags & FIO_NOIO)) {
1113                                 char *c1, *c2, *c3, *c4;
1114                                 char *c5 = NULL, *c6 = NULL;
1115
1116                                 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1117                                 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1118                                 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1119                                 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
1120
1121                                 if (!o->bs_is_seq_rand) {
1122                                         c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1123                                         c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1124                                 }
1125
1126                                 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1127                                                         td->groupid,
1128                                                         ddir_str(o->td_ddir));
1129
1130                                 if (o->bs_is_seq_rand)
1131                                         log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1132                                                         c1, c2, c3, c4);
1133                                 else
1134                                         log_info("bs=%s-%s/%s-%s/%s-%s, ",
1135                                                         c1, c2, c3, c4, c5, c6);
1136
1137                                 log_info("ioengine=%s, iodepth=%u\n",
1138                                                 td->io_ops->name, o->iodepth);
1139
1140                                 free(c1);
1141                                 free(c2);
1142                                 free(c3);
1143                                 free(c4);
1144                                 free(c5);
1145                                 free(c6);
1146                         }
1147                 } else if (job_add_num == 1)
1148                         log_info("...\n");
1149         }
1150
1151         /*
1152          * recurse add identical jobs, clear numjobs and stonewall options
1153          * as they don't apply to sub-jobs
1154          */
1155         numjobs = o->numjobs;
1156         while (--numjobs) {
1157                 struct thread_data *td_new = get_new_job(0, td, 1);
1158
1159                 if (!td_new)
1160                         goto err;
1161
1162                 td_new->o.numjobs = 1;
1163                 td_new->o.stonewall = 0;
1164                 td_new->o.new_group = 0;
1165
1166                 if (file_alloced) {
1167                         if (td_new->files) {
1168                                 struct fio_file *f;
1169                                 for_each_file(td_new, f, i) {
1170                                         if (f->file_name)
1171                                                 sfree(f->file_name);
1172                                         sfree(f);
1173                                 }
1174                                 free(td_new->files);
1175                                 td_new->files = NULL;
1176                         }
1177                         td_new->files_index = 0;
1178                         td_new->files_size = 0;
1179                         if (td_new->o.filename) {
1180                                 free(td_new->o.filename);
1181                                 td_new->o.filename = NULL;
1182                         }
1183                 }
1184
1185                 if (add_job(td_new, jobname, numjobs, 1, client_type))
1186                         goto err;
1187         }
1188
1189         return 0;
1190 err:
1191         put_job(td);
1192         return -1;
1193 }
1194
1195 /*
1196  * Parse as if 'o' was a command line
1197  */
1198 void add_job_opts(const char **o, int client_type)
1199 {
1200         struct thread_data *td, *td_parent;
1201         int i, in_global = 1;
1202         char jobname[32];
1203
1204         i = 0;
1205         td_parent = td = NULL;
1206         while (o[i]) {
1207                 if (!strncmp(o[i], "name", 4)) {
1208                         in_global = 0;
1209                         if (td)
1210                                 add_job(td, jobname, 0, 0, client_type);
1211                         td = NULL;
1212                         sprintf(jobname, "%s", o[i] + 5);
1213                 }
1214                 if (in_global && !td_parent)
1215                         td_parent = get_new_job(1, &def_thread, 0);
1216                 else if (!in_global && !td) {
1217                         if (!td_parent)
1218                                 td_parent = &def_thread;
1219                         td = get_new_job(0, td_parent, 0);
1220                 }
1221                 if (in_global)
1222                         fio_options_parse(td_parent, (char **) &o[i], 1, 0);
1223                 else
1224                         fio_options_parse(td, (char **) &o[i], 1, 0);
1225                 i++;
1226         }
1227
1228         if (td)
1229                 add_job(td, jobname, 0, 0, client_type);
1230 }
1231
1232 static int skip_this_section(const char *name)
1233 {
1234         int i;
1235
1236         if (!nr_job_sections)
1237                 return 0;
1238         if (!strncmp(name, "global", 6))
1239                 return 0;
1240
1241         for (i = 0; i < nr_job_sections; i++)
1242                 if (!strcmp(job_sections[i], name))
1243                         return 0;
1244
1245         return 1;
1246 }
1247
1248 static int is_empty_or_comment(char *line)
1249 {
1250         unsigned int i;
1251
1252         for (i = 0; i < strlen(line); i++) {
1253                 if (line[i] == ';')
1254                         return 1;
1255                 if (line[i] == '#')
1256                         return 1;
1257                 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1258                         return 0;
1259         }
1260
1261         return 1;
1262 }
1263
1264 /*
1265  * This is our [ini] type file parser.
1266  */
1267 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
1268 {
1269         unsigned int global;
1270         struct thread_data *td;
1271         char *string, *name;
1272         FILE *f;
1273         char *p;
1274         int ret = 0, stonewall;
1275         int first_sect = 1;
1276         int skip_fgets = 0;
1277         int inside_skip = 0;
1278         char **opts;
1279         int i, alloc_opts, num_opts;
1280
1281         if (is_buf)
1282                 f = NULL;
1283         else {
1284                 if (!strcmp(file, "-"))
1285                         f = stdin;
1286                 else
1287                         f = fopen(file, "r");
1288
1289                 if (!f) {
1290                         perror("fopen job file");
1291                         return 1;
1292                 }
1293         }
1294
1295         string = malloc(4096);
1296
1297         /*
1298          * it's really 256 + small bit, 280 should suffice
1299          */
1300         name = malloc(280);
1301         memset(name, 0, 280);
1302
1303         alloc_opts = 8;
1304         opts = malloc(sizeof(char *) * alloc_opts);
1305         num_opts = 0;
1306
1307         stonewall = stonewall_flag;
1308         do {
1309                 /*
1310                  * if skip_fgets is set, we already have loaded a line we
1311                  * haven't handled.
1312                  */
1313                 if (!skip_fgets) {
1314                         if (is_buf)
1315                                 p = strsep(&file, "\n");
1316                         else
1317                                 p = fgets(string, 4096, f);
1318                         if (!p)
1319                                 break;
1320                 }
1321
1322                 skip_fgets = 0;
1323                 strip_blank_front(&p);
1324                 strip_blank_end(p);
1325
1326                 if (is_empty_or_comment(p))
1327                         continue;
1328                 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1329                         if (inside_skip)
1330                                 continue;
1331                         log_err("fio: option <%s> outside of [] job section\n",
1332                                                                         p);
1333                         break;
1334                 }
1335
1336                 name[strlen(name) - 1] = '\0';
1337
1338                 if (skip_this_section(name)) {
1339                         inside_skip = 1;
1340                         continue;
1341                 } else
1342                         inside_skip = 0;
1343
1344                 global = !strncmp(name, "global", 6);
1345
1346                 if (dump_cmdline) {
1347                         if (first_sect)
1348                                 log_info("fio ");
1349                         if (!global)
1350                                 log_info("--name=%s ", name);
1351                         first_sect = 0;
1352                 }
1353
1354                 td = get_new_job(global, &def_thread, 0);
1355                 if (!td) {
1356                         ret = 1;
1357                         break;
1358                 }
1359
1360                 /*
1361                  * Separate multiple job files by a stonewall
1362                  */
1363                 if (!global && stonewall) {
1364                         td->o.stonewall = stonewall;
1365                         stonewall = 0;
1366                 }
1367
1368                 num_opts = 0;
1369                 memset(opts, 0, alloc_opts * sizeof(char *));
1370
1371                 while (1) {
1372                         if (is_buf)
1373                                 p = strsep(&file, "\n");
1374                         else
1375                                 p = fgets(string, 4096, f);
1376                         if (!p)
1377                                 break;
1378
1379                         if (is_empty_or_comment(p))
1380                                 continue;
1381
1382                         strip_blank_front(&p);
1383
1384                         /*
1385                          * new section, break out and make sure we don't
1386                          * fgets() a new line at the top.
1387                          */
1388                         if (p[0] == '[') {
1389                                 skip_fgets = 1;
1390                                 break;
1391                         }
1392
1393                         strip_blank_end(p);
1394
1395                         if (num_opts == alloc_opts) {
1396                                 alloc_opts <<= 1;
1397                                 opts = realloc(opts,
1398                                                 alloc_opts * sizeof(char *));
1399                         }
1400
1401                         opts[num_opts] = strdup(p);
1402                         num_opts++;
1403                 }
1404
1405                 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1406                 if (!ret)
1407                         ret = add_job(td, name, 0, 0, type);
1408                 else {
1409                         log_err("fio: job %s dropped\n", name);
1410                         put_job(td);
1411                 }
1412
1413                 for (i = 0; i < num_opts; i++)
1414                         free(opts[i]);
1415                 num_opts = 0;
1416         } while (!ret);
1417
1418         if (dump_cmdline)
1419                 log_info("\n");
1420
1421         i = 0;
1422         while (i < nr_job_sections) {
1423                 free(job_sections[i]);
1424                 i++;
1425         }
1426
1427         for (i = 0; i < num_opts; i++)
1428                 free(opts[i]);
1429
1430         free(string);
1431         free(name);
1432         free(opts);
1433         if (!is_buf && f != stdin)
1434                 fclose(f);
1435         return ret;
1436 }
1437
1438 static int fill_def_thread(void)
1439 {
1440         memset(&def_thread, 0, sizeof(def_thread));
1441
1442         fio_getaffinity(getpid(), &def_thread.o.cpumask);
1443         def_thread.o.timeout = def_timeout;
1444         def_thread.o.error_dump = 1;
1445         /*
1446          * fill default options
1447          */
1448         fio_fill_default_options(&def_thread);
1449         return 0;
1450 }
1451
1452 static void usage(const char *name)
1453 {
1454         printf("%s\n", fio_version_string);
1455         printf("%s [options] [job options] <job file(s)>\n", name);
1456         printf("  --debug=options\tEnable debug logging. May be one/more of:\n"
1457                 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
1458                 "\t\t\tdiskutil,job,mutex,profile,time,net,rate\n");
1459         printf("  --parse-only\t\tParse options only, don't start any IO\n");
1460         printf("  --output\t\tWrite output to file\n");
1461         printf("  --runtime\t\tRuntime in seconds\n");
1462         printf("  --latency-log\t\tGenerate per-job latency logs\n");
1463         printf("  --bandwidth-log\tGenerate per-job bandwidth logs\n");
1464         printf("  --minimal\t\tMinimal (terse) output\n");
1465         printf("  --output-format=x\tOutput format (terse,json,normal)\n");
1466         printf("  --terse-version=x\tSet terse version output format to 'x'\n");
1467         printf("  --version\t\tPrint version info and exit\n");
1468         printf("  --help\t\tPrint this page\n");
1469         printf("  --cpuclock-test\tPerform test/validation of CPU clock\n");
1470         printf("  --crctest\t\tTest speed of checksum functions\n");
1471         printf("  --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
1472                 " them\n");
1473         printf("  --enghelp=engine\tPrint ioengine help, or list"
1474                 " available ioengines\n");
1475         printf("  --enghelp=engine,cmd\tPrint help for an ioengine"
1476                 " cmd\n");
1477         printf("  --showcmd\t\tTurn a job file into command line options\n");
1478         printf("  --eta=when\t\tWhen ETA estimate should be printed\n");
1479         printf("            \t\tMay be \"always\", \"never\" or \"auto\"\n");
1480         printf("  --eta-newline=time\tForce a new line for every 'time'");
1481         printf(" period passed\n");
1482         printf("  --status-interval=t\tForce full status dump every");
1483         printf(" 't' period passed\n");
1484         printf("  --readonly\t\tTurn on safety read-only checks, preventing"
1485                 " writes\n");
1486         printf("  --section=name\tOnly run specified section in job file\n");
1487         printf("  --alloc-size=kb\tSet smalloc pool to this size in kb"
1488                 " (def 1024)\n");
1489         printf("  --warnings-fatal\tFio parser warnings are fatal\n");
1490         printf("  --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1491         printf("  --server=args\t\tStart a backend fio server\n");
1492         printf("  --daemonize=pidfile\tBackground fio server, write pid to file\n");
1493         printf("  --client=hostname\tTalk to remote backend fio server at hostname\n");
1494         printf("  --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1495                 "\t\t\t(option=system,percpu) or run unit work\n"
1496                 "\t\t\tcalibration only (option=calibrate)\n");
1497         printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1498         printf("\n                   Jens Axboe <jaxboe@fusionio.com>");
1499         printf("\n                   Jens Axboe <axboe@fb.com>\n");
1500 }
1501
1502 #ifdef FIO_INC_DEBUG
1503 struct debug_level debug_levels[] = {
1504         { .name = "process",
1505           .help = "Process creation/exit logging",
1506           .shift = FD_PROCESS,
1507         },
1508         { .name = "file",
1509           .help = "File related action logging",
1510           .shift = FD_FILE,
1511         },
1512         { .name = "io",
1513           .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1514           .shift = FD_IO,
1515         },
1516         { .name = "mem",
1517           .help = "Memory allocation/freeing logging",
1518           .shift = FD_MEM,
1519         },
1520         { .name = "blktrace",
1521           .help = "blktrace action logging",
1522           .shift = FD_BLKTRACE,
1523         },
1524         { .name = "verify",
1525           .help = "IO verification action logging",
1526           .shift = FD_VERIFY,
1527         },
1528         { .name = "random",
1529           .help = "Random generation logging",
1530           .shift = FD_RANDOM,
1531         },
1532         { .name = "parse",
1533           .help = "Parser logging",
1534           .shift = FD_PARSE,
1535         },
1536         { .name = "diskutil",
1537           .help = "Disk utility logging actions",
1538           .shift = FD_DISKUTIL,
1539         },
1540         { .name = "job",
1541           .help = "Logging related to creating/destroying jobs",
1542           .shift = FD_JOB,
1543         },
1544         { .name = "mutex",
1545           .help = "Mutex logging",
1546           .shift = FD_MUTEX
1547         },
1548         { .name = "profile",
1549           .help = "Logging related to profiles",
1550           .shift = FD_PROFILE,
1551         },
1552         { .name = "time",
1553           .help = "Logging related to time keeping functions",
1554           .shift = FD_TIME,
1555         },
1556         { .name = "net",
1557           .help = "Network logging",
1558           .shift = FD_NET,
1559         },
1560         { .name = "rate",
1561           .help = "Rate logging",
1562           .shift = FD_RATE,
1563         },
1564         { .name = NULL, },
1565 };
1566
1567 static int set_debug(const char *string)
1568 {
1569         struct debug_level *dl;
1570         char *p = (char *) string;
1571         char *opt;
1572         int i;
1573
1574         if (!strcmp(string, "?") || !strcmp(string, "help")) {
1575                 log_info("fio: dumping debug options:");
1576                 for (i = 0; debug_levels[i].name; i++) {
1577                         dl = &debug_levels[i];
1578                         log_info("%s,", dl->name);
1579                 }
1580                 log_info("all\n");
1581                 return 1;
1582         }
1583
1584         while ((opt = strsep(&p, ",")) != NULL) {
1585                 int found = 0;
1586
1587                 if (!strncmp(opt, "all", 3)) {
1588                         log_info("fio: set all debug options\n");
1589                         fio_debug = ~0UL;
1590                         continue;
1591                 }
1592
1593                 for (i = 0; debug_levels[i].name; i++) {
1594                         dl = &debug_levels[i];
1595                         found = !strncmp(opt, dl->name, strlen(dl->name));
1596                         if (!found)
1597                                 continue;
1598
1599                         if (dl->shift == FD_JOB) {
1600                                 opt = strchr(opt, ':');
1601                                 if (!opt) {
1602                                         log_err("fio: missing job number\n");
1603                                         break;
1604                                 }
1605                                 opt++;
1606                                 fio_debug_jobno = atoi(opt);
1607                                 log_info("fio: set debug jobno %d\n",
1608                                                         fio_debug_jobno);
1609                         } else {
1610                                 log_info("fio: set debug option %s\n", opt);
1611                                 fio_debug |= (1UL << dl->shift);
1612                         }
1613                         break;
1614                 }
1615
1616                 if (!found)
1617                         log_err("fio: debug mask %s not found\n", opt);
1618         }
1619         return 0;
1620 }
1621 #else
1622 static int set_debug(const char *string)
1623 {
1624         log_err("fio: debug tracing not included in build\n");
1625         return 1;
1626 }
1627 #endif
1628
1629 static void fio_options_fill_optstring(void)
1630 {
1631         char *ostr = cmd_optstr;
1632         int i, c;
1633
1634         c = i = 0;
1635         while (l_opts[i].name) {
1636                 ostr[c++] = l_opts[i].val;
1637                 if (l_opts[i].has_arg == required_argument)
1638                         ostr[c++] = ':';
1639                 else if (l_opts[i].has_arg == optional_argument) {
1640                         ostr[c++] = ':';
1641                         ostr[c++] = ':';
1642                 }
1643                 i++;
1644         }
1645         ostr[c] = '\0';
1646 }
1647
1648 static int client_flag_set(char c)
1649 {
1650         int i;
1651
1652         i = 0;
1653         while (l_opts[i].name) {
1654                 int val = l_opts[i].val;
1655
1656                 if (c == (val & 0xff))
1657                         return (val & FIO_CLIENT_FLAG);
1658
1659                 i++;
1660         }
1661
1662         return 0;
1663 }
1664
1665 static void parse_cmd_client(void *client, char *opt)
1666 {
1667         fio_client_add_cmd_option(client, opt);
1668 }
1669
1670 int parse_cmd_line(int argc, char *argv[], int client_type)
1671 {
1672         struct thread_data *td = NULL;
1673         int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
1674         char *ostr = cmd_optstr;
1675         void *pid_file = NULL;
1676         void *cur_client = NULL;
1677         int backend = 0;
1678
1679         /*
1680          * Reset optind handling, since we may call this multiple times
1681          * for the backend.
1682          */
1683         optind = 1;
1684
1685         while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
1686                 did_arg = 1;
1687
1688                 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
1689                         parse_cmd_client(cur_client, argv[optind - 1]);
1690                         c &= ~FIO_CLIENT_FLAG;
1691                 }
1692
1693                 switch (c) {
1694                 case 'a':
1695                         smalloc_pool_size = atoi(optarg);
1696                         break;
1697                 case 't':
1698                         def_timeout = atoi(optarg);
1699                         break;
1700                 case 'l':
1701                         write_lat_log = 1;
1702                         break;
1703                 case 'b':
1704                         write_bw_log = 1;
1705                         break;
1706                 case 'o':
1707                         f_out = fopen(optarg, "w+");
1708                         if (!f_out) {
1709                                 perror("fopen output");
1710                                 exit(1);
1711                         }
1712                         f_err = f_out;
1713                         break;
1714                 case 'm':
1715                         output_format = FIO_OUTPUT_TERSE;
1716                         break;
1717                 case 'F':
1718                         if (!optarg) {
1719                                 log_err("fio: missing --output-format argument\n");
1720                                 exit_val = 1;
1721                                 do_exit++;
1722                                 break;
1723                         }
1724                         if (!strcmp(optarg, "minimal") ||
1725                             !strcmp(optarg, "terse") ||
1726                             !strcmp(optarg, "csv"))
1727                                 output_format = FIO_OUTPUT_TERSE;
1728                         else if (!strcmp(optarg, "json"))
1729                                 output_format = FIO_OUTPUT_JSON;
1730                         else
1731                                 output_format = FIO_OUTPUT_NORMAL;
1732                         break;
1733                 case 'f':
1734                         append_terse_output = 1;
1735                         break;
1736                 case 'h':
1737                         if (!cur_client) {
1738                                 usage(argv[0]);
1739                                 do_exit++;
1740                         }
1741                         break;
1742                 case 'c':
1743                         if (!cur_client) {
1744                                 fio_show_option_help(optarg);
1745                                 do_exit++;
1746                         }
1747                         break;
1748                 case 'i':
1749                         if (!cur_client) {
1750                                 fio_show_ioengine_help(optarg);
1751                                 do_exit++;
1752                         }
1753                         break;
1754                 case 's':
1755                         dump_cmdline = 1;
1756                         break;
1757                 case 'r':
1758                         read_only = 1;
1759                         break;
1760                 case 'v':
1761                         if (!cur_client) {
1762                                 log_info("%s\n", fio_version_string);
1763                                 do_exit++;
1764                         }
1765                         break;
1766                 case 'V':
1767                         terse_version = atoi(optarg);
1768                         if (!(terse_version == 2 || terse_version == 3 ||
1769                              terse_version == 4)) {
1770                                 log_err("fio: bad terse version format\n");
1771                                 exit_val = 1;
1772                                 do_exit++;
1773                         }
1774                         break;
1775                 case 'e':
1776                         if (!strcmp("always", optarg))
1777                                 eta_print = FIO_ETA_ALWAYS;
1778                         else if (!strcmp("never", optarg))
1779                                 eta_print = FIO_ETA_NEVER;
1780                         break;
1781                 case 'E': {
1782                         long long t = 0;
1783
1784                         if (str_to_decimal(optarg, &t, 0, NULL, 1)) {
1785                                 log_err("fio: failed parsing eta time %s\n", optarg);
1786                                 exit_val = 1;
1787                                 do_exit++;
1788                         }
1789                         eta_new_line = t;
1790                         break;
1791                         }
1792                 case 'd':
1793                         if (set_debug(optarg))
1794                                 do_exit++;
1795                         break;
1796                 case 'P':
1797                         parse_only = 1;
1798                         break;
1799                 case 'x': {
1800                         size_t new_size;
1801
1802                         if (!strcmp(optarg, "global")) {
1803                                 log_err("fio: can't use global as only "
1804                                         "section\n");
1805                                 do_exit++;
1806                                 exit_val = 1;
1807                                 break;
1808                         }
1809                         new_size = (nr_job_sections + 1) * sizeof(char *);
1810                         job_sections = realloc(job_sections, new_size);
1811                         job_sections[nr_job_sections] = strdup(optarg);
1812                         nr_job_sections++;
1813                         break;
1814                         }
1815                 case 'p':
1816                         exec_profile = strdup(optarg);
1817                         break;
1818                 case FIO_GETOPT_JOB: {
1819                         const char *opt = l_opts[lidx].name;
1820                         char *val = optarg;
1821
1822                         if (!strncmp(opt, "name", 4) && td) {
1823                                 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
1824                                 if (ret)
1825                                         return 0;
1826                                 td = NULL;
1827                         }
1828                         if (!td) {
1829                                 int is_section = !strncmp(opt, "name", 4);
1830                                 int global = 0;
1831
1832                                 if (!is_section || !strncmp(val, "global", 6))
1833                                         global = 1;
1834
1835                                 if (is_section && skip_this_section(val))
1836                                         continue;
1837
1838                                 td = get_new_job(global, &def_thread, 1);
1839                                 if (!td || ioengine_load(td))
1840                                         return 0;
1841                                 fio_options_set_ioengine_opts(l_opts, td);
1842                         }
1843
1844                         if ((!val || !strlen(val)) &&
1845                             l_opts[lidx].has_arg == required_argument) {
1846                                 log_err("fio: option %s requires an argument\n", opt);
1847                                 ret = 1;
1848                         } else
1849                                 ret = fio_cmd_option_parse(td, opt, val);
1850
1851                         if (ret) {
1852                                 if (td) {
1853                                         put_job(td);
1854                                         td = NULL;
1855                                 }
1856                                 do_exit++;
1857                         }
1858
1859                         if (!ret && !strcmp(opt, "ioengine")) {
1860                                 free_ioengine(td);
1861                                 if (ioengine_load(td))
1862                                         return 0;
1863                                 fio_options_set_ioengine_opts(l_opts, td);
1864                         }
1865                         break;
1866                 }
1867                 case FIO_GETOPT_IOENGINE: {
1868                         const char *opt = l_opts[lidx].name;
1869                         char *val = optarg;
1870                         ret = fio_cmd_ioengine_option_parse(td, opt, val);
1871                         break;
1872                 }
1873                 case 'w':
1874                         warnings_fatal = 1;
1875                         break;
1876                 case 'j':
1877                         max_jobs = atoi(optarg);
1878                         if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1879                                 log_err("fio: invalid max jobs: %d\n", max_jobs);
1880                                 do_exit++;
1881                                 exit_val = 1;
1882                         }
1883                         break;
1884                 case 'S':
1885                         if (nr_clients) {
1886                                 log_err("fio: can't be both client and server\n");
1887                                 do_exit++;
1888                                 exit_val = 1;
1889                                 break;
1890                         }
1891                         if (optarg)
1892                                 fio_server_set_arg(optarg);
1893                         is_backend = 1;
1894                         backend = 1;
1895                         break;
1896                 case 'D':
1897                         pid_file = strdup(optarg);
1898                         break;
1899                 case 'I':
1900                         if ((ret = fio_idle_prof_parse_opt(optarg))) {
1901                                 /* exit on error and calibration only */
1902                                 do_exit++;
1903                                 if (ret == -1) 
1904                                         exit_val = 1;
1905                         }
1906                         break;
1907                 case 'C':
1908                         if (is_backend) {
1909                                 log_err("fio: can't be both client and server\n");
1910                                 do_exit++;
1911                                 exit_val = 1;
1912                                 break;
1913                         }
1914                         if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
1915                                 log_err("fio: failed adding client %s\n", optarg);
1916                                 do_exit++;
1917                                 exit_val = 1;
1918                                 break;
1919                         }
1920                         /*
1921                          * If the next argument exists and isn't an option,
1922                          * assume it's a job file for this client only.
1923                          */
1924                         while (optind < argc) {
1925                                 if (!strncmp(argv[optind], "--", 2) ||
1926                                     !strncmp(argv[optind], "-", 1))
1927                                         break;
1928
1929                                 fio_client_add_ini_file(cur_client, argv[optind]);
1930                                 optind++;
1931                         }
1932                         break;
1933                 case 'T':
1934                         do_exit++;
1935                         exit_val = fio_monotonic_clocktest();
1936                         break;
1937                 case 'G':
1938                         do_exit++;
1939                         exit_val = fio_crctest(optarg);
1940                         break;
1941                 case 'L': {
1942                         long long val;
1943
1944                         if (check_str_time(optarg, &val, 0)) {
1945                                 log_err("fio: failed parsing time %s\n", optarg);
1946                                 do_exit++;
1947                                 exit_val = 1;
1948                                 break;
1949                         }
1950                         status_interval = val * 1000;
1951                         break;
1952                         }
1953                 case '?':
1954                         log_err("%s: unrecognized option '%s'\n", argv[0],
1955                                                         argv[optind - 1]);
1956                 default:
1957                         do_exit++;
1958                         exit_val = 1;
1959                         break;
1960                 }
1961                 if (do_exit)
1962                         break;
1963         }
1964
1965         if (do_exit && !(is_backend || nr_clients))
1966                 exit(exit_val);
1967
1968         if (nr_clients && fio_clients_connect())
1969                 exit(1);
1970
1971         if (is_backend && backend)
1972                 return fio_start_server(pid_file);
1973
1974         if (td) {
1975                 if (!ret)
1976                         ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
1977         }
1978
1979         while (!ret && optind < argc) {
1980                 ini_idx++;
1981                 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1982                 ini_file[ini_idx - 1] = strdup(argv[optind]);
1983                 optind++;
1984         }
1985
1986         return ini_idx;
1987 }
1988
1989 int fio_init_options(void)
1990 {
1991         f_out = stdout;
1992         f_err = stderr;
1993
1994         fio_options_fill_optstring();
1995         fio_options_dup_and_init(l_opts);
1996
1997         atexit(free_shm);
1998
1999         if (fill_def_thread())
2000                 return 1;
2001
2002         return 0;
2003 }
2004
2005 extern int fio_check_options(struct thread_options *);
2006
2007 int parse_options(int argc, char *argv[])
2008 {
2009         const int type = FIO_CLIENT_TYPE_CLI;
2010         int job_files, i;
2011
2012         if (fio_init_options())
2013                 return 1;
2014         if (fio_test_cconv(&def_thread.o))
2015                 log_err("fio: failed internal cconv test\n");
2016
2017         job_files = parse_cmd_line(argc, argv, type);
2018
2019         if (job_files > 0) {
2020                 for (i = 0; i < job_files; i++) {
2021                         if (fill_def_thread())
2022                                 return 1;
2023                         if (nr_clients) {
2024                                 if (fio_clients_send_ini(ini_file[i]))
2025                                         return 1;
2026                                 free(ini_file[i]);
2027                         } else if (!is_backend) {
2028                                 if (parse_jobs_ini(ini_file[i], 0, i, type))
2029                                         return 1;
2030                                 free(ini_file[i]);
2031                         }
2032                 }
2033         } else if (nr_clients) {
2034                 if (fill_def_thread())
2035                         return 1;
2036                 if (fio_clients_send_ini(NULL))
2037                         return 1;
2038         }
2039
2040         free(ini_file);
2041         fio_options_free(&def_thread);
2042         filesetup_mem_free();
2043
2044         if (!thread_number) {
2045                 if (parse_dryrun())
2046                         return 0;
2047                 if (exec_profile)
2048                         return 0;
2049                 if (is_backend || nr_clients)
2050                         return 0;
2051                 if (did_arg)
2052                         return 0;
2053
2054                 log_err("No jobs(s) defined\n\n");
2055
2056                 if (!did_arg) {
2057                         usage(argv[0]);
2058                         return 1;
2059                 }
2060
2061                 return 0;
2062         }
2063
2064         if (def_thread.o.gtod_offload) {
2065                 fio_gtod_init();
2066                 fio_gtod_offload = 1;
2067                 fio_gtod_cpu = def_thread.o.gtod_cpu;
2068         }
2069
2070         if (output_format == FIO_OUTPUT_NORMAL)
2071                 log_info("%s\n", fio_version_string);
2072
2073         return 0;
2074 }
2075
2076 void options_default_fill(struct thread_options *o)
2077 {
2078         memcpy(o, &def_thread.o, sizeof(*o));
2079 }