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