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