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