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