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