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